This is an example of a bad page class:
public class HomePage {
public WebDriver driver;
public static final String HOME_PAGE_URL = "https://www.vpl.ca/";
public static final By SEARCH_BOX_ID = By.id("edit-search");
public static final By SEARCH_BUTTON_ID = By.id("edit-submit");
public HomePage(WebDriver driver) {
this.driver = driver;
}
public void open() {
driver.get(HOME_PAGE_URL);
}
public void checkPageIsDisplayed() {
Assert.assertTrue(driver.getCurrentUrl().contains(HOME_PAGE_URL));
}
public void searchBy(String keyword, boolean keyboardEnabled) {
typeKeyword(keyword);
if (keyboardEnabled)
pressEnterInSearchBox(keyword);
else
clickSearchButton();
}
public void pressEnterInSearchBox(String keyword) {
WebElement searchBox = driver.findElement(SEARCH_BOX_ID);
searchBox.sendKeys(Keys.ENTER);
}
public void typeKeyword(String keyword) {
WebElement searchBox = driver.findElement(SEARCH_BOX_ID);
searchBox.sendKeys(keyword);
}
public void clickSearchButton() {
WebElement searchButton = driver.findElement(SEARCH_BUTTON_ID);
searchButton.click();
}
}
The page class is bad for the following reasons:
the class exposes its own internals by having all its variables (driver, url, locators) declared as public
public WebDriver driver;
public static final String HOME_PAGE_URL = "https://www.vpl.ca/";
public static final By SEARCH_BOX_ID = By.id("edit-search");
public static final By SEARCH_BUTTON_ID = By.id("edit-submit");
the methods of the class are making assertions:
public void checkPageIsDisplayed() {
Assert.assertTrue(driver.getCurrentUrl().contains(HOME_PAGE_URL));
}
page methods are not returning other page objects; since after executing a search, the results page is displayed in the browser, searchBy() should return a ResultsPage object:
public void searchBy(String keyword, boolean keyboardEnabled)
all page methods are public
the searchBy() method implements two types of searches, one by clicking the search button, the other by pressing ENTER after typing the search keyword:
public void searchBy(String keyword, boolean keyboardEnabled) {
typeKeyword(keyword);
if (keyboardEnabled)
pressEnterInSearchBox(keyword);
else
clickSearchButton();
}
the page methods are not using any synchronization
See next post for a good page class.