jeudi 16 avril 2020

Problem with writing a selenium c# test for google video results based on words from some phrase

So I need to check if by typing in "Coronavirus COVID-19 Global Cases by Johns Hopkins CSSE" in the Google Search, there will be at least one video result (in the Videos tab) which contains at least 80% of the words from the phrase (the words can be in any order and lower- or uppercase). I have tried doing it by using for loops. The test contains this:

        driver.Navigate().GoToUrl(URL);

        IWebElement element = driver.FindElement(By.Name("q"));

        element.SendKeys("Coronavirus COVID-19 Global Cases by Johns Hopkins CSSE");

        element.SendKeys(Keys.Enter);

        IWebElement videos = driver.FindElement(By.CssSelector("#hdtb-msb-vis > div:nth-child(5) > a"));
        videos.SendKeys(Keys.Enter);

        int resultscount = 0;
        int wordscount = 0;

        String phrase = "Coronavirus COVID-19 Global Cases by Johns Hopkins CSSE";
        String[] list = phrase.Split(' ');


        for (int i = 1; i < 11; i++)
        {
            String results = driver.FindElement(By.XPath("//*[@id=\"rso\"]/div["+i+"]/div/div[1]/a[1]/h3")).Text;

            for (int j = 0; j < list.Length; j++)
            {

                if (results.Contains(list[j]))
                {
                    wordscount++;
                    if (wordscount > (0.8*list.Length))
                    {
                        resultscount++;
                    }
                }

            }
        }
        Assert.IsTrue(resultscount > 0);

    }

The test is always positivie since it takes the count of every word from every header and not just only one at the time. I am really confused now and I wish someone could help me with this task.

Aucun commentaire:

Enregistrer un commentaire