Let’s start with this test:
@Test
public void canFilterByBooksAndComicBooksTest() {
//search for keyword
driver.get(HOME_PAGE_URL);
Verify.verify(driver.getCurrentUrl().equalsIgnoreCase(HOME_PAGE_URL));
WebElement searchBox = driver.findElement(SEARCH_BOX_ID);
searchBox.sendKeys(KEYWORD);
WebElement searchButton = driver.findElement(SEARCH_BUTTON_ID);
searchButton.click();
Verify.verify(driver.getCurrentUrl().startsWith(RESULTS_PAGE_URL),
"results page url does not start with " + RESULTS_PAGE_URL);
WebElement resultsFoundLabel = driver.findElement(RESULTS_FOUND_XPATH);
Verify.verify(resultsFoundLabel.getText().contains("1 to 10"),
"results found label does not include 1 to 10!");
//filter results by books
WebElement bookFilter = driver.findElement(BOOK_FILTER_XPATH);
Assert.assertEquals(bookFilter.getAttribute("aria-checked"), "false");
bookFilter.click();
bookFilter = driver.findElement(BOOK_FILTER_XPATH);
Assert.assertEquals(bookFilter.getAttribute("aria-checked"), "true");
bookFilter.click();
bookFilter = driver.findElement(BOOK_FILTER_XPATH);
Assert.assertEquals(bookFilter.getAttribute("aria-checked"), "false");
//filter results by comic books
WebElement comicBookFilter = driver.findElement(COMIC_BOOK_FILTER_XPATH);
Assert.assertEquals(comicBookFilter.getAttribute("aria-checked"), "false");
comicBookFilter.click();
comicBookFilter = driver.findElement(COMIC_BOOK_FILTER_XPATH);
Assert.assertEquals(comicBookFilter.getAttribute("aria-checked"), "true");
comicBookFilter.click();
comicBookFilter = driver.findElement(COMIC_BOOK_FILTER_XPATH);
Assert.assertEquals(comicBookFilter.getAttribute("aria-checked"), "false");
}
The test script is pretty long. There are 3 parts to it:
searching for a keyword
filtering results by books
filtering results by comic books
The filtering part has also 3 parts:
verify that the filter checkbox is unchecked by default
click the filter checkbox
verify that the filter checkbox becomes checked
click the filter checkbox again
verify that the filter checkbox becomes unchecked
The filtering by books and comic books are obviously identical.
There are a few things that are not good with this test script.
There is obviously the duplication about filtering by books and filtering by comic books.
The test script does too much. You can see this right away in the test script name: canFilterByBooksAndComicBooksTest(). It verifies that it is possible to filter by books and also to filter by comic books.
If the test script fails, it is hard to understand quickly what went wrong. Was it the part about filtering by books or the part about filtering by comic books?
If the test fails due to an exception, verification or assertion, let’s say this happens during filtering by books, the code about filtering by comic books is not executed at all.
How do we simplify the test?
We split the test script in 2, the first one focusing on the filtering by books and the second on filtering by comic books.
@Test
public void canFilterByBooksTest() {
driver.get(HOME_PAGE_URL);
Verify.verify(driver.getCurrentUrl().equalsIgnoreCase(HOME_PAGE_URL));
WebElement searchBox = driver.findElement(SEARCH_BOX_ID);
searchBox.sendKeys(KEYWORD);
WebElement searchButton = driver.findElement(SEARCH_BUTTON_ID);
searchButton.click();
Verify.verify(driver.getCurrentUrl().startsWith(RESULTS_PAGE_URL),
"results page url does not start with " + RESULTS_PAGE_URL);
WebElement resultsFoundLabel = driver.findElement(RESULTS_FOUND_XPATH);
Verify.verify(resultsFoundLabel.getText().contains("1 to 10"),
"results found label does not include 1 to 10!");
WebElement bookFilter = driver.findElement(BOOK_FILTER_XPATH);
Assert.assertEquals(bookFilter.getAttribute("aria-checked"), "false");
bookFilter.click();
bookFilter = driver.findElement(BOOK_FILTER_XPATH);
Assert.assertEquals(bookFilter.getAttribute("aria-checked"), "true");
bookFilter.click();
bookFilter = driver.findElement(BOOK_FILTER_XPATH);
Assert.assertEquals(bookFilter.getAttribute("aria-checked"), "false");
}
@Test
public void canFilterByComicBooksTest() {
driver.get(HOME_PAGE_URL);
Verify.verify(driver.getCurrentUrl().equalsIgnoreCase(HOME_PAGE_URL));
WebElement searchBox = driver.findElement(SEARCH_BOX_ID);
searchBox.sendKeys(KEYWORD);
WebElement searchButton = driver.findElement(SEARCH_BUTTON_ID);
searchButton.click();
Verify.verify(driver.getCurrentUrl().startsWith(RESULTS_PAGE_URL),
"results page url does not start with " + RESULTS_PAGE_URL);
WebElement resultsFoundLabel = driver.findElement(RESULTS_FOUND_XPATH);
Verify.verify(resultsFoundLabel.getText().contains("1 to 10"),
"results found label does not include 1 to 10!");
WebElement comicBookFilter = driver.findElement(COMIC_BOOK_FILTER_XPATH);
Assert.assertEquals(comicBookFilter.getAttribute("aria-checked"), "false");
comicBookFilter.click();
comicBookFilter = driver.findElement(COMIC_BOOK_FILTER_XPATH);
Assert.assertEquals(comicBookFilter.getAttribute("aria-checked"), "true");
comicBookFilter.click();
comicBookFilter = driver.findElement(COMIC_BOOK_FILTER_XPATH);
Assert.assertEquals(comicBookFilter.getAttribute("aria-checked"), "false");
}
Having 2 shorter test scripts is much better than having a long one.
Each test script is shorter.
Each test script is much more focused (working on 1 thing only).
If one of the test script fails, the other one still executes since both scripts are independent.
If any of the test scripts fails, it is very clear what failed. It is either something related to filtering by books or something about filtering by comic books.