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();
Assert.assertTrue(paginationInfo.length() > 0, "pagination info is empty!");
Assert.assertTrue(paginationInfo.contains("1 to 10"),
"incorrect pagination info - " + paginationInfo);
int totalResultCount = resultsPage.getTotalResultCount();
Assert.assertTrue(totalResultCount > 0, "totalResultCount is 0!");
resultsPage.goToPage(2);
int pageNumber = resultsPage.getPageNumber();
Assert.assertTrue(pageNumber == 2,
String.format("incorrect page number - actual %d, expected %d",
pageNumber,
2));
List<String> resultTitles = resultsPage.getAllResultTitles();
Assert.assertTrue(resultTitles.size() > 0, "result titles list is empty!");
}
A few assertions can be written in a better way.
Can you guess which ones?
Assert.assertTrue(paginationInfo.length() > 0, "pagination info is empty!");
can be written in a better way.
Instead of relying on the length() method for checking if pagination info is empty, we can use the isEmpty() method:
Assert.assertFalse(paginationInfo.isEmpty(), "pagination info is empty!");
The new assertion is simpler and more clear.
Assert.assertTrue(pageNumber == 2,
String.format("incorrect page number - actual %d, expected %d",
pageNumber,
2));
We can do better here as well.
Instead of assertTrue, how about using assertEquals?
Assert.assertEquals(pageNumber, 2,
String.format("incorrect page number - actual %d, expected %d",
pageNumber,
2));
Isn’t this simpler?
Finally,
Assert.assertTrue(resultTitles.size() > 0, "result titles list is empty!");
can be improved by using the isEmpty() method instead of comparing the list size with 0:
Assert.assertFalse(resultTitles.isEmpty(), "result titles list is empty!");
Putting everything together, we get a simpler 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();
Assert.assertFalse(paginationInfo.isEmpty(), "pagination info is empty!");
Assert.assertTrue(paginationInfo.contains("1 to 10"),
"incorrect pagination info - " + paginationInfo);
int totalResultCount = resultsPage.getTotalResultCount();
Assert.assertTrue(totalResultCount > 0, "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!");
}
Always check if you are using the best assertion.
Do not use assertTrue for comparing 2 values. Instead, use assertEquals.
Do not use the length() string method for checking if the string value is empty. Use isEmpty() instead.
Do not use the size() list method for checking if the list is empty. Use isEmpty() instead.
There are 2 more assertions in our test that can be improved further:
Assert.assertTrue(paginationInfo.contains("1 to 10"),
"incorrect pagination info - " + paginationInfo);
Assert.assertTrue(totalResultCount > 0, "totalResultCount is 0!");
We will see how to do this in the next post.