Day 4 - Page Object Model

One thing is certain, this new job is full of surprises.

I read the code of a few page object classes yesterday and had a big surprise.

Some classes have almost 1000 lines of code and approximately 200 methods.

How can this be, you may ask.

The project implements the page object model incorrectly.

To understand what I mean, lets say that we have a page object class that corresponds to a results page.

It looks as follows:

public class ResultsPage {

 WebDriver driver;
 Properties locators;
 
 public ResultsPage(WebDriver driver) {
   this.driver = driver;
   this.properties = Util.getResultsPageLocators();
 }
 
 public void clickPage2() {
   String page2Xpath = locators.get("page2");
   By locator = By.xpath(page2Xpath);
   WebElement page2 = driver.findElement(locator);
   page2.click();
 }
 
 public void clickPage1() {
   String page1Xpath = locators.get("page1");
   By locator = By.xpath(page1Xpath); 
   WebElement page1 = driver.findElement(locator);
   page1.click();
 }
 
 public String resultCount() {
   String resultCountXpath = locators.get("resultCount");
   By locator = By.xpath(resultCountXpath);
   WebElement resultCount = driver.findElement(locator); 
   return resultCount.getText();
 }
 
 public String resultPerPage() {
   String resultPerPageXpath = locators.get("resultPerPage");
   By locator = By.xpath(resultPerPageXpath);
   WebElement resultPerPage = driver.findElement(locator);
   return resultPerPage.getText();
 }
 
 public String onlineOnlyCount() {
   String onlineOnlyCountXpath = locators.get("onlineOnlyCount");
   By locator = By.xpath(onlineOnlyCountXpath);
   WebElement onlineOnlyCount = driver.findElement(locator);
   return onlineOnlyCount.getText();
 }
 
 public void clickOnlineOnlyLink() {
   String onlineOnlyXpath = locators.get("onlineOnly");
   By locator = By.xpath(onlineOnlyXpath);
   WebElement onlineOnly = driver.findElement(locator);
   onlineOnly.click();
 }
 
 public boolean isOnlineOnlyLinkDisplayed() {
   String onlineOnlyXpath = locators.get("onlineOnly");
   By locator = By.xpath(onlineOnlyXpath);
   WebElement onlineOnly = driver.findElement(locator);
   return onlineOnly.isDisplayed();
 }
 
 public boolean isPage2Displayed() {
   String page2Xpath = locators.get("page2"); 
   By locator = By.xpath(page2Xpath);
   WebElement page2 = driver.findElement(locator);
   return page2.isDisplayed();
 }
 
 public boolean isPage5Displayed() {
   String page5Xpath = locators.get("page5");
   By locator = By.xpath(page5Xpath);
   WebElement page5 = driver.findElement(locator);
   return page5.isDisplayed();
 }

}


Why is this wrong?

The class should have methods that implement application logic specific to the page.

Instead, it has methods that do simple actions on specific HTML elements.

Since there are many elements in a web page, for each of them, there are methods for clicking, checking if displayed or getting the value.

This type a page object is about interacting with HTML instead of interacting with the application.

No wonder now that the page class has so many lines of code and methods.

There are many problems with this class but lets see how the tests look first.

@Test
public void canSelectOnlineOnlyProducts() {

  HomePage homePage = new HomePage(driver);
  homePage.open();
  homePage.typeSearchKeyword("java");
  homePage.clickSearchButton();
   
  ResultsPage resultsPage = new ResultsPage(driver);
  assertTrue(resultPage.isOnlineOnlyLinkDisplayed() == true);
   
  int onlineOnlyResultCount = Integer.parseInt(
                              resultsPage.onlineOnlyCount());
  assertTrue(onlineOnlyResultCount > 0);
   
  resultsPage.clickOnlineOnlyLink();
   
  int resultCount = Integer.parseInt(
                    resultsPage.resultCount());
  assertTrue(onlineOnlyResultCount == resultCount);

}


More about the tests and the page object model tomorrow.

0 Comment to "Day 4 - Page Object Model"

Post a comment