Providing good names to variables is extremely important.
Let’s see how this test script does it:
@Test
public void searchTestRefactored() throws InterruptedException {
driver.get(HOME_PAGE_URL);
String homeurl = driver.getCurrentUrl();
Assert.assertEquals(homeurl, HOME_PAGE_URL);
WebElement search_Box = driver.findElement(SEARCH_BOX_ID);
search_Box.sendKeys(KEYWORD);
WebElement SearchButton = driver.findElement(SEARCH_BUTTON_ID);
SearchButton.click();
String resultsurl = driver.getCurrentUrl();
boolean correctUrl = resultsurl.startsWith(RESULTS_PAGE_URL);
Assert.assertTrue(correctUrl);
boolean keywordIncluded = resultsurl.contains("query=" + KEYWORD);
Assert.assertTrue(keywordIncluded);
boolean searchTypeInUrl = resultsurl.contains("searchType=smart");
Assert.assertTrue(searchTypeInUrl);
WebElement label = driver.findElement(RESULTS_FOUND_XPATH);
String found = label.getText();
boolean verifyFoundValue = found.contains("1 to 10");
Assert.assertTrue(verifyFoundValue);
int firstindex = found.indexOf("of") + 3;
int secondindex = found.indexOf(" results");
int value = Integer.parseInt(found.substring(firstindex, secondindex));
Assert.assertTrue(value > 0);
WebElement searchBroaden = driver.findElement(BROADEN_SEARCH_XPATH);
searchBroaden.click();
String broadenurl = driver.getCurrentUrl();
Assert.assertTrue(broadenurl.contains("searchType=bkw"));
Thread.sleep(5000);
WebElement label2 = driver.findElement(RESULTS_FOUND_XPATH);
String found2 = label2.getText();
int indexForOf = found2.indexOf("of") + 3;
int indexForResults = found2.indexOf(" results");
int info = Integer.parseInt(found2.substring(indexForOf, indexForResults));
Assert.assertTrue(info > 0);
Assert.assertTrue(info > value);
}
The test script has multiple issues related to variable names.
Not only most of the variables have wrong names but some do not follow the naming convention for variables.
Let’s take them one by one.
homeurl
String homeurl = driver.getCurrentUrl();
homeurl does not follow the camel case notation where the first letter of each word should be upper case with the rest of the letters being lowercase. This does not apply to the first word that should be all lower case. So, homeurl should be homeUrl.
But homeUrl is also not a good name. We are using a home page which has a url so a better name is homePageUrl.
search_Box
WebElement search_Box = driver.findElement(SEARCH_BOX_ID);
The variable name has only one issue and this is about including _ in it. searchBox is a good name since it refers to the search box element.
SearchButton
WebElement SearchButton = driver.findElement(SEARCH_BUTTON_ID);
SearchButton needs to start with s instead of S. Other than that, searchButton is ok.
resultsurl
String resultsurl = driver.getCurrentUrl();
Similar with homeurl, it should be changed to resultsPageUrl.
CorrectUrl
boolean correctUrl = resultsurl.startsWith(RESULTS_PAGE_URL);
All boolean variables should have names like a predicate. In this case, correctUrl should be replaced with isUrlCorrect.
keywordIncluded
boolean keywordIncluded = resultsurl.contains("query=" + KEYWORD);
Another boolean variable. keywordIncluded should be replaced with isKeywordIncluded.
searchTypeInUrl
boolean searchTypeInUrl = resultsurl.contains("searchType=smart");
Change the variable name to isSearchTypeIncluded.
label
WebElement label = driver.findElement(RESULTS_FOUND_XPATH);
label? Which label? Aaaa, the resultsFoundLabel.
found
String found = label.getText();
found? A variable name should start with a noun and found is a verb. Change found to resultsFoundText.
verifyFoundText
boolean verifyFoundValue = found.contains("1 to 10");
Since this is a boolean variable, let’s change it to isPageCountIncluded.
firstindex
int firstindex = found.indexOf("of") + 3;
This variable can have a very simple name such as i1 without losing much clarity of the code.
secondindex
int secondindex = found.indexOf(" results");
Let’s use i2 instead.
value
int value = Integer.parseInt(found.substring(firstindex, secondindex));
Value is too generic and it does not tell us much about its “value”.
Since the value of this variable is about the total result count extracted from the text of the results found label, let’s name it totalCount.
broadenurl
String broadenurl = driver.getCurrentUrl();
Same as before, broadenurl should be resultsPageUrl. After clicking the broaden search link, we still get a results page with more results.
label2
WebElement label2 = driver.findElement(RESULTS_FOUND_XPATH);
label2 should be resultsFoundLabel.
found2
String found2 = label2.getText();
found2 can be renamed to resultsFoundText.
indexForOf
int indexForOf = found2.indexOf("of") + 3;
Too complex name, change to j1.
indexForResults
int indexForResults = found2.indexOf(" results");
j2 is better.
info
int info = Integer.parseInt(found2.substring(indexForOf, indexForResults));
info :) Maybe newTotalCount.
The updated test script is below:
@Test
public void searchTestRefactored() throws InterruptedException {
driver.get(HOME_PAGE_URL);
String homePageUrl = driver.getCurrentUrl();
Assert.assertEquals(homePageUrl, 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();
boolean isUrlCorrect = resultsPageUrl.startsWith(RESULTS_PAGE_URL);
Assert.assertTrue(isUrlCorrect);
boolean isKeywordIncluded = resultsPageUrl.contains("query=" + KEYWORD);
Assert.assertTrue(isKeywordIncluded);
boolean isSearchTypeIncluded = resultsPageurl.contains("searchType=smart");
Assert.assertTrue(isSearchTypeIncluded);
WebElement resultsFoundLabel = driver.findElement(RESULTS_FOUND_XPATH);
String resultsFoundText = resultsFoundLabel.getText();
boolean isPageCountIncluded = resultsFoundText.contains("1 to 10");
Assert.assertTrue(isPageCountIncluded);
int i1 = resultsFoundText.indexOf("of") + 3;
int i2 = resultsFoundText.indexOf(" results");
int totalCount = Integer.parseInt(resultsFoundText.substring(i1, i2));
Assert.assertTrue(totalCount > 0);
WebElement searchBroaden = driver.findElement(BROADEN_SEARCH_XPATH);
searchBroaden.click();
String broadenPageUrl = driver.getCurrentUrl();
Assert.assertTrue(broadenPageUrl.contains("searchType=bkw"));
Thread.sleep(5000);
resultsFoundLabel = driver.findElement(RESULTS_FOUND_XPATH);
resultsFoundText = resultsFoundLabel.getText();
int j1 = resultsFoundText.indexOf("of") + 3;
int j2 = resultsFoundText.indexOf(" results");
int newTotalCount = Integer.parseInt(resultsFoundText.substring(j1, j2));
Assert.assertTrue(newTotalCount > 0);
Assert.assertTrue(newTotalCount > totalCount);
}
Good variable names are very important for having code that is easy to understand.
Good variable names follow a few rules:
they explain clearly what the variable value is all about
they follow the camel case notation
they do not include special characters like _
if the variable is boolean, its name is like a predicate (isDisplayed, isEnabled)
a variable name starts with a noun