The following test implements a simple test case:
open home page
search for keyword
get the results count
verify that the results count is positive
expand the search (broaden it)
get the expanded results count
verify that the new count is greater than the previous one
@Test
public void goingFromKeywordSearchToBroadenSearchDisplaysMoreResultsTest() {
open(HOME_PAGE_URL);
Assert.assertEquals(getUrl(), HOME_PAGE_URL);
search(KEYWORD);
Assert.assertTrue(getUrl().contains(RESULTS_PAGE_URL));
int keywordSearchCount = getResultsCount();
Assert.assertTrue(keywordSearchCount > 0);
changeSearchTypeToBroaden();
Assert.assertTrue(getUrl().contains("searchType=bkw"));
int broadenSearchCount = getResultsCount();
Assert.assertTrue(broadenSearchCount > keywordSearchCount);
}
After broadening the search, the results page displays more results.
A similar test case verifies that less results are displayed after the search is shortened:
@Test
public void goingFromBroadenSearchToKeywordSearchDisplaysLessResultsTest() {
open(HOME_PAGE_URL);
Assert.assertEquals(getUrl(), HOME_PAGE_URL);
search(KEYWORD);
Assert.assertTrue(getUrl().contains(RESULTS_PAGE_URL));
int keywordSearchCount = getResultsCount();
Assert.assertTrue(keywordSearchCount > 0);
changeSearchTypeToBroaden();
Assert.assertTrue(getUrl().contains("searchType=bkw"));
int broadenSearchCount = getResultsCount();
Assert.assertTrue(broadenSearchCount > keywordSearchCount);
changeSearchTypeToKeyword();
Assert.assertTrue(getUrl().contains("searchType=keyword"));
int newKeywordSearchCount = getResultsCount();
Assert.assertTrue(newKeywordSearchCount < broadenSearchCount);
}
Both test cases have a lot of code in common.
This code is used in both of them:
open(HOME_PAGE_URL);
Assert.assertEquals(getUrl(), HOME_PAGE_URL);
search(KEYWORD);
Assert.assertTrue(getUrl().contains(RESULTS_PAGE_URL));
int keywordSearchCount = getResultsCount();
Assert.assertTrue(keywordSearchCount > 0);
changeSearchTypeToBroaden();
Assert.assertTrue(getUrl().contains("searchType=bkw"));
int broadenSearchCount = getResultsCount();
Assert.assertTrue(broadenSearchCount > keywordSearchCount);
Since the first test already verifies that the search broadening works as expected, it is a waste of time to repeat this verification in the second test.
Still, this code seems to be unavoidable since the second test needs to go from a broaden search to a keyword search.
How can we avoid repeating the code in the second test?
It helps to check the url template of each results page.
The results page displayed after searching has as url
https://vpl.bibliocommons.com/v2/search?query=java&searchType=smart
The results page displayed after broadening the search has a similar url:
https://vpl.bibliocommons.com/v2/search?query=java&searchType=bkw
The results page displayed after shortening the search has as url:
https://vpl.bibliocommons.com/v2/search?query=java&searchType=keyword
All urls have the same domain, same path and same parameters (query and searchType).
Knowing this, we can prepare the url of the broaden search results page and go straight to it in the second test:
@Test
public void goingFromBroadenSearchToKeywordSearchDisplaysLessResultsTest() {
String broadenSearchUrl = getResultsPageUrl(KEYWORD, SEARCH_TYPE);
open(broadenSearchUrl);
Assert.assertEquals(getUrl(), broadenSearchUrl);
int broadenSearchCount = getResultsCount();
Assert.assertTrue(broadenSearchCount > 0);
changeSearchTypeToKeyword();
Assert.assertTrue(getUrl().contains("searchType=keyword"));
int keywordSearchCount = getResultsCount();
Assert.assertTrue(keywordSearchCount < broadenSearchCount);
}
The results page url is generated in this method:
private String getResultsPageUrl(String keyword, String searchType) {
String url =
"https://vpl.bibliocommons.com/v2/search?query=%s&searchType=%s";
return String.format(url, keyword, searchType);
}
The new test is significantly shorter than the original one.
It does not re-test that
searching returns results
the initial results count is positive
broadening the search works
the broaden search results count is greater than the initial one
Since all these are already checked by the first test, the second test loads the broaden search page directly and starts from it.
This leads to a significant time saving.
The time saving is also happening for any other tests that work with the broaden search results page.
This idea cannot unfortunately be applied all the time.
It works well for
public websites that do not require logging in
pages that have a simple and predictable url
pages that can be opened directly