Let’s start with this test method:
@Test
public void filteringResultsShowsLessResultsTest() {
//open site
driver.get(HOME_PAGE_URL);
//check that home page is displayed
Assert.assertTrue(driver.getCurrentUrl().equalsIgnoreCase(HOME_PAGE_URL));
//search for keyword
driver.findElement(searchBoxBy).sendKeys(KEYWORD);
driver.findElement(searchButtonBy).click();
//check that results page is displayed
Assert.assertTrue(driver.getCurrentUrl().contains(RESULTS_PAGE_URL));
//get initial results count
int initialCountValue = driver.findElement(pageInfoBy).getText();
int initialCount = Integer.parseInt(initialCountValue);
//check that initial results count is positive
Assert.assertTrue(initialCount > 0);
/*
select filter;
if ebook filter is displayed
click ebook filter
else
click book filter
*/
if (driver.findElement(eBookFilterBy).isDisplayed())
driver.findElement(eBookFilterBy).click();
else
driver.findElement(bookFilterBy).click();
//get filtered results count
int filteredCountValue = driver.findElement(pageInfoBy).getText();
int filteredCount = Integer.parseInt(filteredCountValue);
//check that filtered results count is less than initial results count
Assert.assertTrue(filteredCount < initialCount);
}
This is a simple test that
opens the site
searches for a keyword
checks that the results page is displayed after the search
checks that the number of results is positive
selects a filter
checks that the filtered number of results is less that before
What is the issue with it?
The issue is the if statement.
A test method should not include an if statement.
Let’s see why.
First, let’s consider the reason for using the if statement. Some searches return results that are ebooks, other do not. The if statement deals with this situation so that we can use both keywords that search for ebooks but also keywords that search only for books.
Assume that the test fails because the number of results is not reduced after selecting the filter. Does this mean that the filtering for books does not work? Or the filtering for ebooks? There is no way of telling this.
There are 2 user flows involved in this test method:
one for the if clause where the filtering is done by clicking the ebooks filter
the second for the else clause where the filtering is done by clicking the books filter
This means that the test method does not automate one test case but two since it includes 2 completely different user flows.
So, we should remove the if statement.
How do we do this?
We split the test method in 2:
one that uses a keyword that returns ebooks
another that uses a keyword that returns only books
This is the first test method.
@Test
public void filteringByEbookShowsLessResultsTest() {
//open site
driver.get(HOME_PAGE_URL);
//check that home page is displayed
Assert.assertTrue(driver.getCurrentUrl().equalsIgnoreCase(HOME_PAGE_URL));
//search for keyword
driver.findElement(searchBoxBy).sendKeys(EBOOKS_KEYWORD);
driver.findElement(searchButtonBy).click();
//check that results page is displayed
Assert.assertTrue(driver.getCurrentUrl().contains(RESULTS_PAGE_URL));
//get initial results count
int initialCountValue = driver.findElement(pageInfoBy).getText();
int initialCount = Integer.parseInt(initialCountValue);
//check that initial results count is positive
Assert.assertTrue(initialCount > 0);
//select ebook filter
driver.findElement(eBookFilterBy).click();
//get filtered results count
int filteredCountValue = driver.findElement(pageInfoBy).getText();
int filteredCount = Integer.parseInt(filteredCountValue);
//check that filtered results count is less than initial results count
Assert.assertTrue(filteredCount < initialCount);
}
And the second one:
@Test
public void filteringByBooksShowsLessResultsTest() {
//open site
driver.get(HOME_PAGE_URL);
//check that home page is displayed
Assert.assertTrue(driver.getCurrentUrl().equalsIgnoreCase(HOME_PAGE_URL));
//search for keyword
driver.findElement(searchBoxBy).sendKeys(BOOKS_ONLY_KEYWORD);
driver.findElement(searchButtonBy).click();
//check that results page is displayed
Assert.assertTrue(driver.getCurrentUrl().contains(RESULTS_PAGE_URL));
//get initial results count
int initialCountValue = driver.findElement(pageInfoBy).getText();
int initialCount = Integer.parseInt(initialCountValue);
//check that initial results count is positive
Assert.assertTrue(initialCount > 0);
// select filter
driver.findElement(bookFilterBy).click();
//get filtered results count
int filteredCountValue = driver.findElement(pageInfoBy).getText();
int filteredCount = Integer.parseInt(filteredCountValue);
//check that filtered results count is less than initial results count
Assert.assertTrue(filteredCount < initialCount);
}
In this case, if a test fails, we know exactly where to look into the problem, without looking at the code first.
Please avoid if statements in your test methods.
Also, please avoid implementing more than 1 test case in your automated tests.