The following page class uses a lot of duplicated locators for different page elements:
public class ResultsPage {
private WebDriver driver;
private static final String RESULTS_PAGE_URL =
"https://vpl.bibliocommons.com";
@FindBy(xpath = "(//span[@data-key = 'pagination-text'])[1]")
private WebElement resultsFoundLabel;
@FindBy(xpath = "//li[contains(@class, 'page-number')]/a[@data-page = '2']")
private WebElement page2;
@FindBy(xpath = "//li[contains(@class, 'page-number')]/a[@data-page = '3']")
private WebElement page3;
@FindBy(xpath = "//li[contains(@class, 'page-number')]/a[@data-page = '4']")
private WebElement page4;
public ResultsPage(WebDriver driver) {
PageFactory.initElements(driver, this);
this.driver = driver;
}
public boolean isDisplayed() {
return getUrl().startsWith(RESULTS_PAGE_URL);
}
private String getUrl() {
return driver.getCurrentUrl();
}
public String getResultsFound() {
return resultsFoundLabel.getText();
}
public void goToPage2() throws InterruptedException {
scrollToBottom();
page2.click();
}
public void goToPage3() throws InterruptedException {
scrollToBottom();
page3.click();
}
public void goToPage4() throws InterruptedException {
scrollToBottom();
page4.click();
}
private void scrollToBottom() throws InterruptedException {
((JavascriptExecutor) driver).executeScript(
"window.scrollTo(0, document.body.scrollHeight)");
Thread.sleep(2000);
}
}
There is a lot of duplicated code in this page class:
page2, page3 and page4 elements have duplicated locators
goToPage2(), goToPage3(), goToPage4() methods have duplicated code
The solution for both the locator and methods duplication is obviously using a dynamic locator.
Unfortunately, this is impossible with Page Factory.
Keep reading with a 7-day free trial
Subscribe to Selenium For Beginners to keep reading this post and get 7 days of free access to the full post archives.