Have a look at this page class:
public class ResultsPage {
private WebDriver driver;
private static final String RESULTS_PAGE_URL =
"https://vpl.bibliocommons.com";
private static final By RESULTS_FOUND_XPATH =
By.xpath("(//span[@data-key = 'pagination-text'])[1]");
private static final By PAGE_2_XPATH =
By.xpath("//li[contains(@class, 'page-number')]/a[@data-page = '2']");
private static final By PAGE_3_XPATH =
By.xpath("//li[contains(@class, 'page-number')]/a[@data-page = '3']");
private static final By PAGE_4_XPATH =
By.xpath("//li[contains(@class, 'page-number')]/a[@data-page = '4']");
private String pageXpath =
"//li[contains(@class, 'page-number')]/a[@data-page = '%s']";
public ResultsPage(WebDriver driver) {
this.driver = driver;
}
private void scrollToBottom() {
((JavascriptExecutor) driver).executeScript(
"window.scrollTo(0, document.body.scrollHeight)");
}
private String getUrl() {
return driver.getCurrentUrl();
}
private By getPageXpath(int n) {
String locator = String.format(pageXpath, n);
return By.xpath(locator);
}
public String getResultsFound() {
WebElement label = driver.findElement(RESULTS_FOUND_XPATH);
return label.getText();
}
public void goToPage2() {
WebElement page = driver.findElement(PAGE_2_XPATH);
page.click();
}
public void goToPage3() {
WebElement page = driver.findElement(PAGE_3_XPATH);
page.click();
}
public void goToPage(int n) {
scrollToBottom();
By xpath = getPageXpath(n);
WebElement page = driver.findElement(xpath);
page.click();
}
public void goToPage4() {
WebElement page = driver.findElement(PAGE_4_XPATH);
page.click();
}
public boolean isDisplayed() {
return getUrl().startsWith(RESULTS_PAGE_URL);
}
}
The class has several public and private methods, created in a random order:
private void scrollToBottom()
private String getUrl()
private By getPageXpath()
public String getResultsFound()
public void goToPage2()
public void goToPage3()
public void goToPage4()
public void goToPage()
public boolean isDisplayed()
Having public methods mixed with private methods like this leads to wasted time.
Because methods depend on other methods as follows:
goToPage() depends on scrollToBottom() and getPageXpath()
isDisplayed() depends on getUrl()
This means that, while reading the code of goToPage(), if you want to see how scrollToBottom() works, you will need to find it first by scrolling up. After reading the code of scrollToBottom(), you have to scroll down to get back to goToPage(). Next, if you are curious about getPageXpath(), again scroll up, read the method and scroll down.
All scrolling up and down is a waste of time.
You may say that this is fine because the class in small. But imagine now that the class has 25 methods instead of 9, each method with 5 lines of code. The larger the class, the bigger the waste of time due to scrolling.
Fine, Alex, maybe you have a point.
How do we fix the method order?
First, we have the public methods. If a public method depends on other private methods, the private methods should follow it.
So, public method 1, followed by all private methods that it uses. Then, public method 2, followed by all private methods that it uses. You get it ….
public class ResultsPage {
private WebDriver driver;
private static final String RESULTS_PAGE_URL =
"https://vpl.bibliocommons.com";
private static final By RESULTS_FOUND_XPATH =
By.xpath("(//span[@data-key = 'pagination-text'])[1]");
private static final By PAGE_2_XPATH =
By.xpath("//li[contains(@class, 'page-number')]/a[@data-page = '2']");
private static final By PAGE_3_XPATH =
By.xpath("//li[contains(@class, 'page-number')]/a[@data-page = '3']");
private static final By PAGE_4_XPATH =
By.xpath("//li[contains(@class, 'page-number')]/a[@data-page = '4']");
private String pageXpath =
"//li[contains(@class, 'page-number')]/a[@data-page = '%s']";
public ResultsPage(WebDriver driver) {
this.driver = driver;
}
public boolean isDisplayed() {
return getUrl().startsWith(RESULTS_PAGE_URL);
}
private String getUrl() {
return driver.getCurrentUrl();
}
public String getResultsFound() {
WebElement label = driver.findElement(RESULTS_FOUND_XPATH);
return label.getText();
}
public void goToPage2() {
WebElement page = driver.findElement(PAGE_2_XPATH);
page.click();
}
public void goToPage3() {
WebElement page = driver.findElement(PAGE_3_XPATH);
page.click();
}
public void goToPage4() {
WebElement page = driver.findElement(PAGE_4_XPATH);
page.click();
}
public void goToPage(int n) {
scrollToBottom();
By xpath = getPageXpath(n);
WebElement page = driver.findElement(xpath);
page.click();
}
private void scrollToBottom() {
((JavascriptExecutor) driver).executeScript(
"window.scrollTo(0, document.body.scrollHeight)");
}
private By getPageXpath(int n) {
String locator = String.format(pageXpath, n);
return By.xpath(locator);
}
}
isDisplayed() is followed now by getUrl().
goToPage() is followed by scrollToBottom() and getPageXpath().
Why is this better?
Because, while reading the code of goToPage(), if you are curious how scrollToBottom() and getPageXpath() work, they are right there. You either do not have to do any scrolling or do just a little.
One more thing.
Methods that seem to be related should be grouped together.
I am referring here to
goToPage2()
goToPage3()
goToPage4()
One more thing.
If you find this site useful, please share it on social media. Thank you.
And
if you buy a yearly-subscription to this site, you will get not only all published posts (public, for free subscribers, for paid subscribers) but also
a package of 3 ebooks on Selenium and Java ($35 value)
2 free hours of talking in Zoom about testing/test automation/interviewing/finding a job