I see sometimes test scripts created as follows:
@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!");
Utilities.reloadPage(driver);
Utilities.scrollToBottom(driver);
Thread.sleep(2000);
resultsPage.goToPage(2);
Assert.assertTrue(driver.getCurrentUrl().contains("page=2"));
Utilities.reloadPage(driver);
Utilities.scrollToBottom(driver);
Thread.sleep(2000);
resultsPage.goToPage(3);
Assert.assertTrue(driver.getCurrentUrl().contains("page=3"));
}
I don’t know how this is for you, but for me, this is hard to understand.
Why is this difficult to understand?
The test script works at too many layers (page objects, utility class, driver object, java framework):
page methods - homePage methods, resultsPage methods
driver methods - getCurrentUrl()
utility methods - reloadPage(), scrollToBottom()
Java methods - Thread.sleep()
This is just too much.
The test script should work at 1 layer only.
It should only use page methods and nothing else.
Do you need to use a static wait (you shouldn’t)? Use it in a page method.
Do you need to use a utility method (you shouldn’t)? Use it in a page method.
Do you need to use a driver method? Use it in a page method.
Ok, so how should the test look like then?
It could look like this:
@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.goToPage(2);
Assert.assertTrue(resultsPage.getUrl().contains("page=2"));
resultsPage.scrollToBottom();
resultsPage.goToPage(3);
Assert.assertTrue(resultsPage.getUrl().contains("page=3"));
}
How did we change the test script?
added a getUrl() method to the ResultsPage class
removed the Utilities.reloadPage() from the test script as this is a bad practice (see specific post on this)
moved the Utilities.scrollToBottom() method to the ResultsPage class
added synchronization to the goToPage() method instead of using static waits in the test script
This is better because the test script only works with home page and results page objects. Keeping the test script on one layer increases the readability and maintenance a lot.