The following script does not make the site navigation explicit:
@Test
public void canFilterByBooksTest() {
HomePage homePage = new HomePage(driver);
homePage.open();
verify(homePage.isDisplayed(), "home page is not displayed!");
homePage.searchBy(KEYWORD);
ResultsPage resultsPage = new ResultsPage(driver);
verify(resultsPage.isDisplayed(), "results page is not displayed!");
verify(resultsPage.getResultsFound().contains("1 to 10"),
"results found label does not include 1 to 10!");
assertFalse(resultsPage.getBookFilterState(), "checked book filter!");
resultsPage.checkBookFilter();
assertTrue(resultsPage.getBookFilterState(), "unchecked book filter!");
resultsPage.unCheckBookFilter();
assertFalse(resultsPage.getBookFilterState(), "checked book filter!");
}
What is the problem here?
Let’s trace the script steps:
open home page
verify that home page is displayed
search for keyword in the home page
check that results page is displayed
But how did we get to the results page?
Nothing explains how we got from the home page to results page.
You know that this happened as a result of searching.
But the code should show it as well.
To make this navigation explicit, we need to change the searchBy() method a little bit:
public ResultsPage searchBy(String keyword) {
typeKeyword(keyword);
clickSearchButton();
return new ResultsPage(driver);
}
The searchBy() method now returns a ResultsPage object.
The test shows now the navigation from home page to results page:
@Test
public void canFilterByBooksTest() {
HomePage homePage = new HomePage(driver);
homePage.open();
verify(homePage.isDisplayed(), "home page is not displayed!");
ResultsPage resultsPage = homePage.searchBy(KEYWORD);
verify(resultsPage.isDisplayed(), "results page is not displayed!");
verify(resultsPage.getResultsFound().contains("1 to 10"),
"results found label does not include 1 to 10!");
assertFalse(resultsPage.getBookFilterState(), "checked book filter!");
resultsPage.checkBookFilter();
assertTrue(resultsPage.getBookFilterState(), "unchecked book filter!");
resultsPage.unCheckBookFilter();
assertFalse(resultsPage.getBookFilterState(), "checked book filter!");
}
This line shows clearly that the result of executing the searchBy() method is getting a results page:
ResultsPage resultsPage = homePage.searchBy(KEYWORD);
There are 2 other improvements in the test:
there is no need of creating the results page object separately
the test becomes shorter by 1 line