This page class corrects all issues of the class from the previous post:
public class HomePage {
private WebDriver driver;
private static final String HOME_PAGE_URL = "https://www.vpl.ca/";
private static final By SEARCH_BOX_ID = By.id("edit-search");
private static final By SEARCH_BUTTON_ID = By.id("edit-submit");
private static final int TIMEOUT = 30;
public HomePage(WebDriver driver) {
this.driver = driver;
}
public void open() {
driver.get(HOME_PAGE_URL);
}
public boolean isDisplayed() {
wait(TIMEOUT).until(ExpectedConditions.urlContains(HOME_PAGE_URL));
return true;
}
public ResultsPage searchBy(String keyword) {
typeKeyword(keyword);
clickSearchButton();
return new ResultsPage(driver);
}
public ResultsPage searchByKeyboard(String keyword) {
typeKeyword(keyword);
pressEnter(keyword);
return new ResultsPage(driver);
}
private void pressEnter(String keyword) {
WebElement searchBox = findClickableElement(SEARCH_BOX_ID);
searchBox.sendKeys(Keys.ENTER);
}
private void typeKeyword(String keyword) {
WebElement searchBox = findClickableElement(SEARCH_BOX_ID);
searchBox.sendKeys(keyword);
}
private void clickSearchButton() {
WebElement searchButton = findClickableElement(SEARCH_BUTTON_ID);
searchButton.click();
}
private WebElement findClickableElement(By by) {
wait(TIMEOUT).until(ExpectedConditions.elementToBeClickable(by));
WebElement element = driver.findElement(by);
return element;
}
private WebDriverWait wait(int seconds) {
return new WebDriverWait(driver, Duration.ofSeconds(seconds));
}
}
the class does not expose its internal structure as all its variables are declared as private
only the methods used in tests are public, all other methods being private
it does not use any assertions; instead, all its methods return values that will be asserted in the test method
the searchBy() method is broken down in 2 smaller methods, one for searching by clicking the search button, the other for searching by pressing the ENTER key
both search methods return page objects
the page methods use sychronisation; you can see this in the isDisplayed() method and in all private methods