Use StringUtils library for what String class cannot do
or do not implement your own String operations
Let’s have a look at this test:
@Test
public void searchTest() {
driver.get(HOME_PAGE_URL);
String homePageUrl = driver.getCurrentUrl();
Verify.verify(homePageUrl.equalsIgnoreCase(HOME_PAGE_URL));
WebElement searchBox = driver.findElement(SEARCH_BOX_ID);
searchBox.sendKeys(KEYWORD);
WebElement searchButton = driver.findElement(SEARCH_BUTTON_ID);
searchButton.click();
String resultsPageUrl = driver.getCurrentUrl();
Verify.verify(resultsPageUrl.startsWith(RESULTS_PAGE_URL),
"results page url does not start with " + RESULTS_PAGE_URL);
Verify.verify(resultsPageUrl.contains("query=" + KEYWORD),
"results page url does not contain query=" + KEYWORD);
Verify.verify(resultsPageUrl.contains("searchType=smart"),
"results page url does not contain searchType=smart");
WebElement resultsFoundLabel = driver.findElement(RESULTS_FOUND_XPATH);
String foundText = resultsFoundLabel.getText();
Verify.verify(foundText.contains("1 to 10"),
"results page does not include 1 to 10");
int i1 = foundText.indexOf("of") + 3;
int i2 = foundText.indexOf(" results");
String totalCountText = foundText.substring(i1, i2);
int totalCount = Integer.parseInt(totalCountText);
Verify.verify(totalCount > 0, totalCount + " is not > 0");
WebElement searchBroaden = driver.findElement(BROADEN_SEARCH_XPATH);
searchBroaden.click();
resultsPageUrl = driver.getCurrentUrl();
Verify.verify(resultsPageUrl.contains("searchType=bkw"),
"results page url doesnt include searchType=bkw");
resultsFoundLabel = driver.findElement(RESULTS_FOUND_XPATH);
foundText = resultsFoundLabel.getText();
int j1 = foundText.indexOf("of") + 3;
int j2 = foundText.indexOf(" results");
totalCountText = foundText.substring(j1, j2);
int newTotalCount = Integer.parseInt(totalCountText);
Assert.assertTrue(newTotalCount > 0, newTotalCount + " is not > 0");
Assert.assertTrue(newTotalCount > totalCount,
newTotalCount + " is not > than " + totalCount);
}
There is some code duplication in the test script about getting the total count:
int i1 = foundText.indexOf("of") + 3;
int i2 = foundText.indexOf(" results");
String totalCountText = foundText.substring(i1, i2);
int totalCount = Integer.parseInt(totalCountText);
This code is used first for getting the total count from the results page, right after executing the search.
It is used again after broadening the search for getting the new total count:
int j1 = foundText.indexOf("of") + 3;
int j2 = foundText.indexOf(" results");
totalCountText = foundText.substring(j1, j2);
int newTotalCount = Integer.parseInt(totalCountText);
There are 2 issues with having this code:
the code is duplicated
this code is implemented by the user
There is no need to implement such String operation by ourselves. It would be great if the String class had a method that allows extracting a part from a string between 2 keywords. Unfortunately, this method does not exist.
In such a case, instead of implementing this by ourselves, we should use the StringUtils class from the org.apache.commons.lang3 package. The class is part of the commons lang 3 library on this url.
StringUtils has lots of useful methods that complement very well what is available by default in the String class. These are a few examples:
substringBetween()
substringBefore()
substringAfter()
deleteWhitespace()
containsIgnoreCase()
compareIgnoreCase()
countMatches()
Read more about it here.
We can improve our test script with the substringBetween() method:
@Test
public void searchTestUsingCommonsLang3() {
driver.get(HOME_PAGE_URL);
String homePageUrl = driver.getCurrentUrl();
Verify.verify(homePageUrl.equalsIgnoreCase(HOME_PAGE_URL));
WebElement searchBox = driver.findElement(SEARCH_BOX_ID);
searchBox.sendKeys(KEYWORD);
WebElement searchButton = driver.findElement(SEARCH_BUTTON_ID);
searchButton.click();
String resultsPageUrl = driver.getCurrentUrl();
Verify.verify(resultsPageUrl.startsWith(RESULTS_PAGE_URL),
"results page url does not start with " + RESULTS_PAGE_URL);
Verify.verify(resultsPageUrl.contains("query=" + KEYWORD),
"results page url does not contain query=" + KEYWORD);
Verify.verify(resultsPageUrl.contains("searchType=smart"),
"results page url does not contain searchType=smart");
WebElement resultsFoundLabel = driver.findElement(RESULTS_FOUND_XPATH);
String foundText = resultsFoundLabel.getText();
Verify.verify(foundText.contains("1 to 10"),
"results page doesnt include 1 to 10");
String countText = StringUtils.substringBetween(foundText,"of "," results");
int totalCount = Integer.parseInt(countText);
Verify.verify(totalCount > 0, "totalCount is not > 0");
WebElement searchBroaden = driver.findElement(BROADEN_SEARCH_XPATH);
searchBroaden.click();
resultsPageUrl = driver.getCurrentUrl();
Verify.verify(resultsPageUrl.contains("searchType=bkw"),
"results page url doesnt include searchType=bkw");
resultsFoundLabel = driver.findElement(RESULTS_FOUND_XPATH);
foundText = resultsFoundLabel.getText();
countText = StringUtils.substringBetween(foundText, "of ", " results");
int newTotalCount = Integer.parseInt(totalCountText);
Assert.assertTrue(newTotalCount > 0,
newTotalCount + " is not > 0");
Assert.assertTrue(newTotalCount > totalCount,
newTotalCount + " is not > than " + totalCount);
}
The test script code is shorter when using the substringBetween() method. Also, it does not have the duplication about getting the countText value.
Do not implement your own String operations.
Use StringUtils instead.