Always work only with the page object for the current page
We begin with a simple test case:
OPEN the home page of the site
verify that home page is displayed
SEARCH for a keyword; results page is displayed next
verify that results page is displayed
in results page, SAVE the information for the 1st result
in results page, SELECT the 1st result; details page is displayed next
verify that details page is displayed
in details page,
verify that results title is the same with the result title from results page
verify that result description is the same with the result description from results page
verify that result author is the same with the result author from results page
A possible implementation of this test case is below:
@Test
public void resultHasSameInfoOnResultsAndDetailsPages() {
HomePage homePage = new HomePage(driver);
homePage.open();
assertTrue(homePage.isDisplayed(),
"home page is not displayed!");
ResultsPage resultsPage = homePage.search(KEYWORD);
assertTrue(resultsPage.isDisplayed(),
"results page is not displayed!");
resultsPage.saveResultInfo(1);
DetailsPage detailsPage = resultsPage.selectResult(1);
assertTrue(detailsPage.isDisplayed(),
"details page is not displayed!");
assertEquals(detailsPage.getResultTitle(), resultsPage.getResultTitle());
assertEquals(detailsPage.getResultDescription(),
resultsPage.getResultDescription());
assertEquals(detailsPage.getResultAuthor(), resultsPage.getResultAuthor());
}
The lines of code to pay attention to are
assertEquals(detailsPage.getResultTitle(), resultsPage.getResultTitle());
assertEquals(detailsPage.getResultDescription(),
resultsPage.getResultDescription());
assertEquals(detailsPage.getResultAuthor(), resultsPage.getResultAuthor());
The 1st assertion compares the result title from details page with the result title from results page. The 2nd assertion does the same comparison for the result description from details and results pages. The 3rd assertion deals with the result author.
So what’s the issue with this?
The problem is that in the same line of code we use the results page object and the details page object.
When making this assertion, the current page is details page. Results page is no longer available.
If results page is no longer available, then the results page object should no longer be used, right?
When we test this manually, while on the details page, we no longer have access to results page. Why then the Selenium test uses both details page object and results page object in the same line of code?
So that is the first problem.
The second problem is that, in order for the result info to be saved in the result page object, we need to execute the method saveResultInfo(). Every time we need the result info, we have to execute saveResultInfo() first. Which is a bad practice since it means that methods of Results Page have to be used in a specific order.
What is the solution then?
See below.
@Test
public void resultHasSameInfoOnResultsAndDetailsPages() {
HomePage homePage = new HomePage(driver);
homePage.open();
assertTrue(homePage.isDisplayed(),
"home page is not displayed!");
ResultsPage resultsPage = homePage.search(KEYWORD);
assertTrue(resultsPage.isDisplayed(),
"results page is not displayed!");
String resultTitle = resultsPage.getResultTitle(1);
String resultAuthor = resultsPage.getResultAuthor(1);
String resultDescription = resultsPage.getResultDescription(1);
DetailsPage detailsPage = resultsPage.selectResult(1);
assertTrue(detailsPage.isDisplayed(),
"details page is not displayed!");
assertEquals(detailsPage.getTitle(), resultTitle);
assertEquals(detailsPage.getDescription(), resultDescription);
assertEquals(detailsPage.getAuthor(), resultAuthor);
}
While on results page, the test saves the result title, author and description in variables that are used later in assertions.
The test uses only the page object for the current page.