As usual, we start with a test script:
@Test
public void canNavigateToOtherPagesTest() {
HomePage homePage = new HomePage(driver);
homePage.open();
Verify.verify(homePage.isDisplayed(), "home page is not displayed!");
ResultsPage resultsPage = homePage.searchBy(KEYWORD);
Verify.verify(resultsPage.isDisplayed(), "results page is not displayed!");
Verify.verify(resultsPage.getResultsFound().contains("1 to 10"),
"results found label does not include 1 to 10!");
resultsPage.scrollToBottomAndGoToPage2();
Assert.assertTrue(resultsPage.getUrl().contains("page=2"));
resultsPage.scrollToBottomAndGoToPage3();
Assert.assertTrue(resultsPage.getUrl().contains("page=3"));
}
The test script should be easy to understand as it is similar to tests we covered in previous posts.
There are 2 lines in it that need to be fixed ASAP.
Can you spot them by yourself?
Yes, it is these 2:
resultsPage.scrollToBottomAndGoToPage2();
resultsPage.scrollToBottomAndGoToPage3();
Why are they wrong?
It is because the scrollToBottomAndGoToPage2() method does too many things. Its name gives it away. It scrolls to the bottom of the page and then it goes to page 2.
Everything in Java should have 1 purpose:
Test classes should have 1 purpose.
Test scripts should have 1 purpose.
Page classes should have 1 purpose
Page methods should have 1 purpose
In the case of page methods, having 1 purpose means doing 1 thing.
Since this method does 2 things, we need to split it in 2 methods:
one that does the scrolling to the bottom of the page
one that goes to the next page
We can update the test script with 2 methods then:
@Test
public void canNavigateToOtherPagesTest() {
HomePage homePage = new HomePage(driver);
homePage.open();
Verify.verify(homePage.isDisplayed(), "home page is not displayed!");
ResultsPage resultsPage = homePage.searchBy(KEYWORD);
Verify.verify(resultsPage.isDisplayed(), "results page is not displayed!");
Verify.verify(resultsPage.getResultsFound().contains("1 to 10"),
"results found label does not include 1 to 10!");
resultsPage.scrollToBottom();
resultsPage.goToPage2();
Assert.assertTrue(resultsPage.getUrl().contains("page=2"));
resultsPage.scrollToBottom();
resultsPage.goToPage3();
Assert.assertTrue(resultsPage.getUrl().contains("page=3"));
}
In general, any method that does too much needs to be broken down in multiple, smaller methods.
Why should we do this?
A big method that does multiple things is harder to maintain and understand. If we change it in one place, it is possible that there will be impacts to other places in the same method. Having small methods is easier since each method is independent. Changing it should not have any impact on any other methods.
In this case, the name of the method gave it away and it showed clearly that the method does 2 things instead of 1. In other cases, the method name would not show this problem so the only way to understand if the method is correct or not is to read its code.
There is a second easy measure for the method doing too much.
If the method is longer than 5 - 10 lines of code, the chances are high that the method should be broken down.