Design by Selenium WebDriver and Junit Class

I am new to Selenium WebDriver and JUnit, I am testing a web application and wondering if the design in my class is correctly designed in Junit to test the user interface?
As I saw examples of where people used composition.

Any advice would be greatly appreciated.

Java class

public class OverviewPage { private WebDriver driver; private String URL = "http://www.google.com"; public String searchQuery = "BBC"; OverviewPage(WebDriver driver){ this.driver = driver; driver.get(URL); if(!"Login".equals(driver.getTitle())){ throw new IllegalStateException("Wrong site"); } } By searchBox = By.id("siteSearchField"); By submitSearch = By.cssSelector("button.btn.searchIco"); public OverviewPage searchBox(String findADealer){ driver.findElement(searchBox).sendKeys(findADealer); return this; } public OverviewPage searchBoxDisplayed(){ driver.findElement(searchBox); return this; } public FindADealerPage searchResults(){ driver.findElement(searchBox).sendKeys(searchQuery); driver.findElement(submitSearch).click(); String search = driver.getPageSource(); boolean searchResults = search.contains(searchQuery); return new FindADealerPage(driver); } 

}

Junit

  public class OverviewPageTest { private WebDriver driver; public String searchQuery = "find a dealer"; By searchBox = By.id("siteSearchField"); By submitSearch = By.cssSelector("button.btn.searchIco"); @Before public void setUp(){ driver = new HtmlUnitDriver(); driver.get("http://www.google.com"); } @After public void tearDown(){ driver.quit(); } @Test public void checkTitle(){ Assert.assertEquals("product edit", driver.getTitle()); } @Test public void checkSearchBoxExists(){ boolean searchBoxes = driver.findElement(searchBox).isDisplayed(); Assert.assertTrue(searchBoxes); } @Test public void searchResults(){ driver.findElement(searchBox).sendKeys(searchQuery); driver.findElement(submitSearch).click(); String search = driver.getPageSource(); boolean searchResults = search.contains(searchQuery); Assert.assertTrue(searchResults); } 

}

+4
source share
1 answer

Your Java OverviewPage class tells me that you want to use the PageObject model.

If you want to follow the example of Google ( https://code.google.com/p/selenium/wiki/PageObjects ), you can put all the fields and methods related to a particular page in PageObject, not TestClass.

For example, in your TestClass, instantiate the PageObject:

 OverviewPage page = new OverViewPage(driver); 

and in all your test packages, replace things like driver.get("http://www.google.com"); on driver.get(page.URL);

Basically what it comes down to is you shouldn't have anything in quotation marks in your TestClass. The advantage of this template is that when you have several tests related to the same field in PageObject, then when you need to update this field, you can do it easily in one place, rather than refactoring several lines of duplicated code during your tests.

In addition, for any given test, there should not be more than two lines - a method call and an assertion.

So, using the test search example (), you can move the following lines to a method inside the page object:

 driver.findElement(searchBox).sendKeys(searchQuery); driver.findElement(submitSearch).click(); String search = driver.getPageSource(); boolean searchResults = search.contains(searchQuery); return searchResults; // added this one... 

And your test will look like this:

 @Test public void searchResults(){ boolean searchResults = page.searchResults(); Assert.assertTrue(searchResults); } 

This is my interpretation. Hope this helps!

+8
source

Source: https://habr.com/ru/post/1484158/


All Articles