As usual, we start with a simple test method:
@Test
public void canNavigateToOtherPagesTest() {
HomePage homePage = new HomePage(driver);
homePage.open();
Assert.assertTrue(homePage.isDisplayed(), "home page is not displayed!");
ResultsPage resultsPage = homePage.searchBy(KEYWORD);
Assert.assertTrue(resultsPage.verifyResultsFound("1 to 10"),
"results found label does not include 1 to 10!");
resultsPage.goToPage(2);
Assert.assertTrue(resultsPage.verifyUrl("page=2"));
resultsPage.goToPage(3);
Assert.assertTrue(resultsPage.verifyUrl("page=3"));
resultsPage.goToPage(4);
Assert.assertTrue(resultsPage.verifyUrl("page=4"));
}
There are 2 interesting things in this test method:
verifyResultsFound()
verifyUrl()
They are both implemented in the ResultsPage class:
public class ResultsPage {
public boolean verifyUrl(String text) {
String url = driver.getCurrentUrl();
return url.contains(text);
}
public boolean verifyResultsFound(String expectedResultsFound) {
WebElement label = driver.findElement(RESULTS_FOUND_XPATH);
String text = label.getText();
return text.contains(expectedResultsFound);
}
}
I removed all other methods from the page class as they are not relevant for this post.
verifyUrl() gets the current url (the results page url), it compares with its parameter and returns the result of the comparison. Simple method, isn’t it?
verifyResultsFound() gets the value of the results found label, it compares it with its parameter and returns the result of the comparison.
The test method becomes much more clear after reading the code of these methods.
But both methods are wrong.
verifyUrl() does too many things:
gets the current url
compares it with its parameter
returns the result of the comparison
Its name is too abstract. It is clear that the url is verified but how does this happen?
Similarly, verifyResultsFound() does too many things:
gets the label value
compares it with its parameter
returns the result of the comparison
Its name is also too abstract.
The test method is consequently unclear. The assertions related to verifyUrl() and verifyResultsFound() are not really telling much about what they do.
The solution is rather simple.
Change the verifyUrl() method so that it just gets the current url and returns it. Let the test method assert if the url is correct or not.
Change the verifyResultsFound() method so that it just gets the label value and returns it. Let the test method assert this value.
public class ResultsPage {
public String getUrl() {
return driver.getCurrentUrl();
}
public String getResultsFound() {
WebElement label = driver.findElement(RESULTS_FOUND_XPATH);
return label.getText();
}
}
Both methods do less now and have clear names.
The test method becomes more clear as well:
@Test
public void canNavigateToOtherPagesTest2() {
HomePage homePage = new HomePage(driver);
homePage.open();
Assert.assertTrue(homePage.isDisplayed(), "home page is not displayed!");
ResultsPage resultsPage = homePage.searchBy(KEYWORD);
Assert.assertTrue(resultsPage.isDisplayed(),
"results page is not displayed!");
Assert.assertTrue(resultsPage.getResultsFound().contains("1 to 10"),
"results found label does not include 1 to 10!");
resultsPage.goToPage(2);
Assert.assertTrue(resultsPage.getUrl().contains("page=2"));
resultsPage.goToPage(3);
Assert.assertTrue(resultsPage.getUrl().contains("page=3"));
resultsPage.goToPage(4);
Assert.assertTrue(resultsPage.getUrl().contains("page=4"));
}
It is clear now what the assertions do in the test method.
They can be simplified further by using a custom assertion method:
@Test
public void canNavigateToOtherPagesTest3() {
HomePage homePage = new HomePage(driver);
homePage.open();
Assert.assertTrue(homePage.isDisplayed(), "home page is not displayed!");
ResultsPage resultsPage = homePage.searchBy(KEYWORD);
Assert.assertTrue(resultsPage.isDisplayed(),
"results page is not displayed!");
assertContains(resultsPage.getResultsFound(), "1 to 10");
resultsPage.goToPage(2);
assertContains(resultsPage.getUrl(), "page=2");
resultsPage.goToPage(3);
assertContains(resultsPage.getUrl(), "page=3");
resultsPage.goToPage(4);
assertContains(resultsPage.getUrl(), "page=4");
}
private void assertContains(String text, String keyword) {
Assert.assertTrue(text.contains(keyword));
}
In summary, the page methods should just return data.
The data should be evaluated (asserted) in the test method only.
Also,
If you find this site useful, please share it on social media and with your friends. Thank you.
One more thing.
If you buy a yearly-subscription to this site, you will get not only all published posts 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