Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium WebDriver and Junit Class design

I am fairly new to Selenium WebDriver and JUnit, I am testing a web app and was wondering is my class design correct in Junit for testing the UI?
As I have seen instances of where people have 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);
}

}

like image 819
user1875703 Avatar asked Feb 17 '23 08:02

user1875703


1 Answers

Your Java class OverviewPage suggests to me that you are wanting to use the PageObject model.

If you want to follow Google's example (https://code.google.com/p/selenium/wiki/PageObjects), you could put all the fields and methods pertaining to a particular page in the PageObject rather than the TestClass.

For example, in your TestClass, instantiate the PageObject:

OverviewPage page = new OverViewPage(driver);

and throughout your TestCalss, replace things like driver.get("http://www.google.com"); with driver.get(page.URL);

Basically what it boils down to is - you shouldn't have anything in quotes in your TestClass. The benefit of this pattern is when you have multiple tests referring to the same field in the PageObject, then when you need to update that field - you can do so easily in one place, rather than refactoring multiple lines of duplicate code throughout your tests.

Also, any given test needn't have more than two lines - a method call and an assertion.

So using your test searchResults() as an example, you could move the following lines into a method within 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 becomes:

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

That's my interpretation. Hope it helps!

like image 190
Dingredient Avatar answered Feb 18 '23 22:02

Dingredient