Have a look at this test:
@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.isDisplayed(),
"results page is not displayed!");
String paginationInfo = resultsPage.getPaginationInfo();
assertContains("1 to 10",
paginationInfo,
"incorrect pagination info - " + paginationInfo);
int totalResultCount = resultsPage.getTotalResultCount();
assertPositive(totalResultCount, "totalResultCount is 0!");
resultsPage.goToPage(2);
int pageNumber = resultsPage.getPageNumber();
Assert.assertEquals(pageNumber, 2,
String.format("incorrect page number - actual %d, expected %d",
pageNumber,
2));
List<String> resultTitles = resultsPage.getAllResultTitles();
Assert.assertFalse(resultTitles.isEmpty(), "result titles list is empty!");
String firstTitle = resultTitles.get(0);
Assert.assertFalse(firstTitle.isEmpty(), "first title is empty!");
}
The last part of the test is most interesting:
List<String> resultTitles = resultsPage.getAllResultTitles();
Assert.assertFalse(resultTitles.isEmpty(), "result titles list is empty!");
String firstTitle = resultTitles.get(0);
Assert.assertFalse(firstTitle.isEmpty(), "first title is empty!");
It gets all result titles from the page and checks that this list is not empty. Then, it gets the first title from the list and it checks that its value is also not empty.
Assume now that you have other tests that do the same assertions for other result titles.
Obviously, you do not want to duplicate these 4 lines of code every time a result title needs to be verified.
How do we solve it?
There is no assertion that we can use here, not even a custom one.
Because we start with a list of titles, then check that it is not empty, get one of its values and check it.
Too many things to use in a custom assertion.
In these cases, we can use a verification method. It is similar to a custom assertion but it can have code that interacts with the site in addition to assertions:
private void assertResultTitleIsNotEmpty(List<String> titles, int i) {
if (i < 0 || i > titles.size() - 1)
throw new IllegalArgumentException(i + " is invalid!");
Assert.assertFalse(titles.isEmpty(), "titles list is empty!");
String title = titles.get(i);
Assert.assertFalse(title.isEmpty(), "title is empty!");
}
The method gets a list of titles as a parameter and the index of a title.
It validates first the index parameter.
Then it checks that the list is not empty, gets the value from the list and checks that it is not empty as well.
Using the verification method, the test becomes simpler:
@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!");
String paginationInfo = resultsPage.getPaginationInfo();
Assert.assertFalse(paginationInfo.isEmpty(), "pagination info is empty!");
assertContains("1 to 10", paginationInfo,
"incorrect pagination info - " + paginationInfo);
int totalResultCount = resultsPage.getTotalResultCount();
assertPositive(totalResultCount, "totalResultCount is 0!");
resultsPage.goToPage(2);
int pageNumber = resultsPage.getPageNumber();
Assert.assertEquals(pageNumber, 2,
String.format("incorrect page number - actual %d, expected %d",
pageNumber,
2));
List<String> resultTitles = resultsPage.getAllResultTitles();
assertResultTitleIsNotEmpty(resultTitles, 0);
}