'What Exception to throw when a web element is found in Selenium Automation [closed]
Currently, I am working on a "Selenium Wrapper Library", where a set of methods are created in order to perform automation on a particular Web Portal. These methods includes debug logging and other logic, and are called within test classes.
I've just created a new method to check whether a table cell (tag name td
), is present inside a table row (tag name tr
). Below is the created method:
public void checkEntryIsNotInTable(String elementTableName, By byElementTable, String checkVisibleText1) {
// Get All Rows from table, using tagname <tr>
List<WebElement> tableRows = getElements(elementTableName, byElementTable);
Debug.log.info("\tTotal of [" + tableRows.size() + "] Rows found in table [" + elementTableName + "]" );
int rowCount = 0;
boolean isNotFound = false;
FINDCELL:
for(WebElement row : tableRows) {
// Get All Cells from table, using tagname <td>
List<WebElement> rowCells = row.findElements(By.tagName("td"));
if(rowCells == null) {
Debug.log.error("\tNo Elements found with TagName [td] in Element [" + byElementTable + "]");
} else {
Debug.log.info("\tRow [" + rowCount++ + "] contains [" + rowCells.size() + "] Cells");
}
// Check Visible Text in Cell, and if found, break from for loop.
for(WebElement cell : rowCells) {
waitFor(2.0);
if(!cell.getText().contains(checkVisibleText1)){
Debug.log.info("\t[" + checkVisibleText1 + "] not found in any Cell.");
isNotFound = true;
break FINDCELL;
}
}
}
// What kind of exception should I throw here!!
if(isNotFound) {
Debug.log.error("\t[" + checkVisibleText1 + "] found in one Cell.");
throw new NoSuchElementException(checkVisibleText1);
}
}
At this stage, when checkVisibleText1
is found, I need to throw a new exception. However, for this method, the exception NoSuchElementException
does not reflect a proper exception, since the element is found - and it should not be found.
What exception would be appropriate to throw here?
Solution 1:[1]
You should check the available exceptions and:
If you have the appropriate exception that matches your error then use it, else create/use a custom exception or create/use a general exception with a custom message depending on the method.
Always try to output an exception with an message that will be helpful to you.
In this case, assuming checkVisibleText1 is the text you are looking for, using NoSuchElementException with the text as the parameter is not OK.
You should have an exception like ElementDoesNotContainsTextException with a message saying "\t Element [" + elementTableName+ "] does not contain text [" + checkVisibleText1 + "]."
Try to use an appropriate exception when possible or use a general exception with the appropriate message.
My opinion is that you have too much noise/logs in this method, too much logs and too complex for the functionality that it has. The methods should have their exceptions you you shouldn't need so many logs.
Some solutions: 1. Get the text from the table and check if the text is there 2. Create a selector based on that text with xpath and check for that selector 3. Check for the text in a specific selector
Create reusable methods like: theElementContainsText, theElementDoesNotContainsText
Reuse as much as possible for example for above method(s) you could use other methods that: find element, get element from text
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | lauda |