From 08414cb4f366ef7aa67ea75e35f34d2cbb7645f5 Mon Sep 17 00:00:00 2001 From: Michael Berry Date: Wed, 22 Jan 2014 16:02:34 -0500 Subject: [PATCH 1/3] Fixed "next" page logic and button detection --- Testing/Testing/Class1.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Testing/Testing/Class1.cs b/Testing/Testing/Class1.cs index 1950700..c9e9b5f 100644 --- a/Testing/Testing/Class1.cs +++ b/Testing/Testing/Class1.cs @@ -38,8 +38,8 @@ public void simplesearch() //create method to perform a google search //When driver.FindElement(By.Name("q")).Clear(); //locate search box element and clear it of any text -// driver.FindElement(By.Name("q")).SendKeys("abcasdfewawef"); //locate search box element and insert a string (1 page sample) - driver.FindElement(By.Name("q")).SendKeys("dominion"); //locate search box element and insert a string (multi page sample) + driver.FindElement(By.Name("q")).SendKeys("abcasdfewawef"); //locate search box element and insert a string (1 page sample) +// driver.FindElement(By.Name("q")).SendKeys("dominion"); //locate search box element and insert a string (multi page sample) driver.FindElement(By.Name("btnG")).Click(); //locate search button element and click it @@ -47,7 +47,7 @@ public void simplesearch() //create method to perform a google search System.Threading.Thread.Sleep(5000); //delay for 5000ms to visually verify page results; also to allow for page to load Assert.IsTrue(ClassElementDisplayed("g", driver), "CLASS results not found"); //Test to verify results were found - nextpage = driver.FindElement(By.Id("pnnext")).Displayed; + nextpage = IdElementDisplayed( "pnnext", driver);//driver.FindElement(By.Id("pnnext")).Displayed; if (nextpage == true) //are more results found? { @@ -71,7 +71,7 @@ public void simplesearch() //create method to perform a google search Console.Write(pagenumber); //print to console Console.WriteLine(" has been displayed"); //print to console } - Assert.IsTrue(ClassElementDisplayed("pn", driver), "ID results not found"); //test to verify 'next' button found + Assert.IsTrue(ClassElementDisplayed("g", driver), "Search results not found"); //test to verify 'next' button found } From a31f5a678a1c38eaad507c177d4ea564bb3e75b0 Mon Sep 17 00:00:00 2001 From: Michael Berry Date: Wed, 22 Jan 2014 20:30:41 -0500 Subject: [PATCH 2/3] Added "Results Title" display for reference. --- Testing/Testing/Class1.cs | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/Testing/Testing/Class1.cs b/Testing/Testing/Class1.cs index c9e9b5f..0b7f7af 100644 --- a/Testing/Testing/Class1.cs +++ b/Testing/Testing/Class1.cs @@ -38,8 +38,8 @@ public void simplesearch() //create method to perform a google search //When driver.FindElement(By.Name("q")).Clear(); //locate search box element and clear it of any text - driver.FindElement(By.Name("q")).SendKeys("abcasdfewawef"); //locate search box element and insert a string (1 page sample) -// driver.FindElement(By.Name("q")).SendKeys("dominion"); //locate search box element and insert a string (multi page sample) +// driver.FindElement(By.Name("q")).SendKeys("abcasdfewawef"); //locate search box element and insert a string (1 page sample) + driver.FindElement(By.Name("q")).SendKeys("dominion"); //locate search box element and insert a string (multi page sample) driver.FindElement(By.Name("btnG")).Click(); //locate search button element and click it @@ -53,11 +53,15 @@ public void simplesearch() //create method to perform a google search { do//if more results are found { - - Console.Write("Page "); //print to console - Console.Write(pagenumber); //print current page count to console - Console.WriteLine(" has been displayed"); //print to console - + Console.WriteLine( "\nPage " + pagenumber + " has been displayed" ); //print current page count to console starting with a new line. + + IReadOnlyCollection resultTitles = driver.FindElements( By.ClassName( "g" ) ); + int resultIndex = 1; + foreach( IWebElement result in resultTitles ) + { + Console.WriteLine( resultIndex + ": " + GetResultTitle( result, driver ) ); + resultIndex++; + } GoNext(driver); //go to the next page pages--; //decrease number of pages to search through @@ -67,9 +71,7 @@ public void simplesearch() //create method to perform a google search } else if (nextpage == false) //if no results found { - Console.Write("Page "); //print to console - Console.Write(pagenumber); //print to console - Console.WriteLine(" has been displayed"); //print to console + Console.WriteLine( "\nPage " + pagenumber + " has been displayed" ); //print current page count to console starting with a new line. } Assert.IsTrue(ClassElementDisplayed("g", driver), "Search results not found"); //test to verify 'next' button found @@ -109,8 +111,13 @@ public void simplesearch() //create method to perform a google search public void GoNext(IWebDriver driver) //method to tell Google to go to the next page of search elements { driver.FindElement(By.Id("pnnext")).Click(); //click the next button - System.Threading.Thread.Sleep(5000); //delay for 5000ms to visually verify page results; also to allow for page to load + System.Threading.Thread.Sleep(2500); //delay for 5000ms to visually verify page results; also to allow for page to load } + + public string GetResultTitle(IWebElement currentResult, IWebDriver driver) + { + return currentResult.FindElement( By.TagName( "a" ) ).Text; + } } } From 33fe98712eacf5878f8f0cda17895cc8d3f7bb25 Mon Sep 17 00:00:00 2001 From: Michael Berry Date: Wed, 22 Jan 2014 20:40:01 -0500 Subject: [PATCH 3/3] Turned hard-coded tests into test cases --- Testing/Testing/Class1.cs | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/Testing/Testing/Class1.cs b/Testing/Testing/Class1.cs index 0b7f7af..e08ec15 100644 --- a/Testing/Testing/Class1.cs +++ b/Testing/Testing/Class1.cs @@ -24,10 +24,10 @@ public void destroy() //method to close the browser driver.Dispose(); //kill the firefox browser } - [Test] //mark following method for testing - public void simplesearch() //create method to perform a google search + [TestCase( "abcasdfewawef" )] //locate search box element and insert a string (1 page sample) + [TestCase( "dominion" )] //locate search box element and insert a string (multi page sample) + public void simplesearch(string searchQuery) //create method to perform a google search { - string samplestring = "sample"; int pages = 3; //max number of pages to search int pagenumber = 1; //track number of pages searched bool nextpage = false; //to simplify displayed results @@ -38,8 +38,7 @@ public void simplesearch() //create method to perform a google search //When driver.FindElement(By.Name("q")).Clear(); //locate search box element and clear it of any text -// driver.FindElement(By.Name("q")).SendKeys("abcasdfewawef"); //locate search box element and insert a string (1 page sample) - driver.FindElement(By.Name("q")).SendKeys("dominion"); //locate search box element and insert a string (multi page sample) + driver.FindElement( By.Name( "q" ) ).SendKeys( searchQuery ); //locate search box element and insert a string driver.FindElement(By.Name("btnG")).Click(); //locate search button element and click it @@ -54,7 +53,8 @@ public void simplesearch() //create method to perform a google search do//if more results are found { Console.WriteLine( "\nPage " + pagenumber + " has been displayed" ); //print current page count to console starting with a new line. - + + //Display the Page Titles IReadOnlyCollection resultTitles = driver.FindElements( By.ClassName( "g" ) ); int resultIndex = 1; foreach( IWebElement result in resultTitles ) @@ -72,9 +72,17 @@ public void simplesearch() //create method to perform a google search else if (nextpage == false) //if no results found { Console.WriteLine( "\nPage " + pagenumber + " has been displayed" ); //print current page count to console starting with a new line. + + //Display the Page Titles + IReadOnlyCollection resultTitles = driver.FindElements( By.ClassName( "g" ) ); + int resultIndex = 1; + foreach( IWebElement result in resultTitles ) + { + Console.WriteLine( resultIndex + ": " + GetResultTitle( result, driver ) ); + resultIndex++; + } } - Assert.IsTrue(ClassElementDisplayed("g", driver), "Search results not found"); //test to verify 'next' button found - + Assert.IsTrue(ClassElementDisplayed("g", driver), "Search results not found"); //test to verify one result card is found } public bool ClassElementDisplayed(string classname, IWebDriver driver) //method to check the class element we searched for