One of the most important things when writing Java code is using clear and meaningful names for variables.
Let’s use this test as an example:
@Test
public void searchTest() {
driver.get(url);
Assert.assertEquals(driver.getCurrentUrl(), url);
WebElement eleSearch = driver.findElement(search_By);
eleSearch.sendKeys(keyword);
WebElement eleButton = driver.findElement(searchBtn_By);
eleButton.click();
Assert.assertEquals(driver.getCurrentUrl(), resultsUrl);
List<WebElement> listOfResultElements = driver.findElements(result_By);
int numberOfResultElements = listOfResultElements.size();
Assert.assertTrue(numberOfResultElements > 0);
WebElement infoOnResultsPage = driver.findElement(pageInfo_By);
String infoValue = infoOnResultsPage.getText();
Assert.assertTrue(infoValue.contains(" 1 to 10"));
WebElement booksOnlyElement = driver.findElement(booksOnly_By);
booksOnlyElement.click();
List<WebElement> listOfElements = driver.findElements(result_By);
int numberOfElements = listOfElements.size();
Assert.assertTrue(numberOfElements < numberOfResultElements);
}I am not sure of how you see this but for me it is difficult to understand.
It is difficult to understand mostly because of the variable names.
Let’s look at a few of them:
eleSearch - what does this mean?
eleButton - what does it mean?
listOfResultElements - it is obvious that this is a list because the variables uses the List<WebElement> type; it is also obvious that the list has elements; why using “list” and “element” then in the variable name?
resultsUrl - results do not have a url, only pages have urls
numberOfResultElements - this variable is clearly a number since it is declared as int
infoOnResultsPage - it is easy to understand that we are on results page at this point, why use the page name then in the variable name? also, info? what info?
infoValue - it is easy to see that this is a value, a string value is a value
listOfElements - the type is List<WebElement> and the name of the variable is the same as the type
Bad variable names like these are so common.
I see often variables with names as follows:
String eleOwner;
Boolean validateNames;
boolean allElementsDisplayed;
WebElement toggleIConOnEachOpenReport;
List<Boolean> listOfBoolean = new ArrayList<>();
WebElement date;
List<String> arrayList = new ArrayList<String>();
boolean flag;
WebElement eleLoginWithUsername;
String[] arr;
Set<String> owner = new HashSet<>();
WebElement element;
When using this type of names for variables, the code can only be very difficult to understand.
The name of a variable should be
descriptive
unambiguous
meaningful
without prefixes or type information.
Let’s see the same script with better names:
@Test
public void lessResultsAreDisplayedAfterFilteringByBooksTest() {
driver.get(URL);
Assert.assertEquals(driver.getCurrentUrl(), URL);
WebElement searchBox = driver.findElement(SEARCH_BOX_ID);
searchBox.sendKeys(keyword);
WebElement searchButton = driver.findElement(SEARCH_BUTTON_ID);
searchButton.click();
Assert.assertEquals(driver.getCurrentUrl(), RESULTS_PAGE_URL);
List<WebElement> results = driver.findElements(RESULT_BY);
int resultsCount = results.size();
Assert.assertTrue(resultsCount > 0);
WebElement pagingInfoLabel = driver.findElement(PAGE_INFO_ID);
String pagingInfo = pagingInfoLabel.getText();
Assert.assertTrue(pagingInfo.contains(" 1 to 10"));
WebElement booksOnlyFilter = driver.findElement(BOOKS_ONLY_ID);
booksOnlyFilter.click();
results = driver.findElements(RESULT_BY);
int filteredResultsCount = results.size();
Assert.assertTrue(resultsCount > filteredResultsCount);
}