The following test does not use any messages for its assertions:
@Test
public void canNavigateToOtherPagesTest() {
HomePage homePage = new HomePage(driver);
homePage.open();
Assert.assertTrue(homePage.isDisplayed());
ResultsPage resultsPage = homePage.searchBy(KEYWORD);
Assert.assertTrue(resultsPage.isDisplayed());
Assert.assertTrue(resultsPage.getResultsFound().contains("1 to 10"));
resultsPage.goToPage(2);
Assert.assertEquals(resultsPage.getPageNumber(), 2);
}
This is a problem because, if any assertion fails, it will be hard to understand the context of the assertion without the assertion using any message.
If this assertion fails,
Assert.assertTrue(resultsPage.isDisplayed());
the only information we will get is that true is not equal to false.
If this assertion fails,
Assert.assertEquals(resultsPage.getPageNumber(), 2);
the only information we get is that 2 is not equal to 1, for example.
We need to add messages to our assertions so, in case that an assertion fails, the message will provide us more information about what went wrong.
The updated test uses messages for all assertions:
@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!");
Assert.assertTrue(resultsPage.getResultsFound().contains("1 to 10"),
"incorrect paging info!");
resultsPage.goToPage(2);
Assert.assertEquals(resultsPage.getPageNumber(), 2,
"incorrect page number!");
}
This is a step in the correct direction.
For
Assert.assertTrue(homePage.isDisplayed(), "home page is not displayed!");
we get now a user-friendly message if the assertion fails.
But the error messages are still not good enough for
Assert.assertTrue(resultsPage.getResultsFound().contains("1 to 10"),
"incorrect paging info!");
and for
Assert.assertEquals(resultsPage.getPageNumber(), 2,
"incorrect page number!");
For the first assertion, it would be nice if we can add the actual pagination info in the error message. For the second assertion, we should add the actual page number in the message.
Which leads us to the correct version of the 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.getResultsFound();
Assert.assertTrue(paginationInfo.contains("1 to 10"),
"incorrect pagination info - " + paginationInfo);
resultsPage.goToPage(2);
int pageNumber = resultsPage.getPageNumber();
Assert.assertEquals(pageNumber, 2,
String.format("incorrect page number - actual %d, expected %d",
pageNumber,
2));
}
We include now the pagination info actual value in the message of the assertion related to the pagination info. We also include the page number actual value in the message of the assertion related to the page number.
Always add assertion messages.
Also, make the assertion messages as specific as possible by adding to them the actual and expected values that are being asserted.