Start automating your tests 10X Faster in Simple English with Testsigma
Try for freeOrganizations are moving toward test automation, which reduces software delivery time. The automation helps to reduce manual effort, especially when you need to re-run the regression multiple times on a tight schedule. Additionally, an automation framework can be added to the CI/CD pipeline, which can serve as a quality gate before pushing the development code to different stages.
Though test automation seems easy, it has many challenges, such as maintaining a stable environment and designing and maintaining robust automation frameworks. Exception handling and error reporting are key areas to consider while designing the automation framework. Every automation tool and programming language has its own way of exception handling. Let’s discuss the exceptions and exception handling in the most popular test automation tool, Selenium.
Table Of Contents
- 1 Errors and Exceptions in Selenium
- 2 List of Exceptions in Selenium
- 3 Handling Exception in Selenium
- 4 Different ways to handle Exception in Selenium
- 5 Custom Exception in Selenium
- 6 Exception Handling Best Practices
- 7 Summary
Errors and Exceptions in Selenium
Exceptions are problems in the program that can cause the program to work abnormally. Exceptions occur during compilation or during the run time. The errors are the events that cause the program to halt or stop.
The exceptions can be broadly classified into two types Checked exceptions and Unchecked exceptions.
Checked Exceptions: These type of exceptions occurs while writing the code. These exceptions need to be analyzed and fixed before compilation else the compiler will throw the exception. Most of the advanced IDEs highlight compilation errors while writing the code.
Unchecked Exceptions: The errors or exceptions which occur during the runtime are called Unchecked exceptions. The Unchecked exceptions require careful analysis and debugging as it is thrown during the execution. Though IDEs provide debugging features, it doesn’t highlight such issues while writing the code.
List of Exceptions in Selenium
Selenium has some predefined exception lists which help to analyze and debug the Selenium tests in an easy way. Let’s understand some of the common Selenium exceptions in detail:
- WebDriverException: The WebDriverException occurs when it tries to perform an action when the webDriver connection is in a closed state.
- TimeOutException: TimeOutException occurs when a command takes more time than the specified or default wait time.
- ElementNotVisibleException: Selenium tries to find the element based on the values provided such as XPath, CSS, ID, etc. If the element state is hidden, then the ElementNotVisibleException is thrown.
- NoSuchWindowException: The NoSuchWindowException occurs when the target window to be switched doesn’t exist.
- NoSuchElementException: The exception occurs when Selenium cannot find the element. The NoSuchElementException and ElementNotVisibleException are very similar. The NoSuchElementException is thrown when the element doesn’t exist at all on the webpage. The ElementNotVisibleException is thrown when an element exists, but it is hidden so the webDriver cannot find and interact with it.
- ElementNotInteractableException: This exception is thrown when an element is present in the DOM Tree but impossible to interact with them. There may be various reasons such as an element might be overlapped with another element or performing interactions before it is ready to take an event etc.
- ConnectionClosedException: This exception occurs when the connection to the Selenium driver is closed.
- ElementClickInterceptedException: The command could not be completed as the element receiving the events is concealing the element which was requested clicked.
- ElementNotSelectableException: Thrown when element exists in the DOM but is unavailable for selection, so the webDriver cannot interact with the element.
- ErrorHandler.UnknownServerException: If a server returns an error without any stack trace, then this exception can be used as a placeholder
- ErrorInResponseException: This exception typically originated from the server side.
- ImeActivationFailedException: The IME stands for Input Method Engine, Typically used when inputting Chinese/Japanese or multi-byte characters. So the ImeActivationFailedException happens when IME Engine cannot be activated.
- ImeNotAvailableException: Similar to the above exception; it occurs when the IME support is not available.
- InsecureCertificateException: The webpage uses the TLS certificate for authenticity. When your webpage has an expired or invalid TLS certificate and you are trying to navigate, then the InsecureCertificateException is thrown.
- InvalidArgumentException: Thrown when a specified argument type does not belong to any exception type.
- InvalidCookieDomainException: If you are trying to add the cookie for an invalid URL or domain, then this exception is thrown
- InvalidCoordinatesException: This exception is self-explanatory and happens when you specify the invalid coordinates to perform a specific operation.
- InvalidSessionIdException: If the session isn’t active or the session is terminated, then Selenium considers the session id invalid and throws the InvalidSessionIdException.
- InvalidElementStateException: When Selenium cannot complete the command on a specified element, then the InvalidElementStateException is thrown
- JavascriptException: Typically thrown if the JavaScript code provided by user cannot be executed or if supplied script is wrong.
- InvalidSwitchToTargetException: Selenium allows you to switch to different tabs, windows, or frames. If you specify wrong arguments, then Selenium throws this exception.
- JsonException: The JsonException occurs when any action/operation return values are not able to parse by the Selenium client.
- MoveTargetOutOfBoundsException: Occurs when the element that Selenium webDriver is trying to access is not within the viewport. To suppress this exception, one can try scrolling or other actions to bring the element to viewport.
- NoAlertPresentException: If you have written the code to handle the alert however during the execution, if it can’t find the alert to switch then this exception will be thrown.
- NoSuchAttributeException: If you are trying to access the element with an attribute, however, due to various reasons, that attribute is not available, this exception is raised.
- NoSuchContextException: Typically thrown while performing the mobile device testing.
- NoSuchCookieException: If Selenium cannot match the cookie in the user-specified path then this exception is thrown.
- NoSuchFrameException: When Selenium cannot find the target iFrame, then it throws the NoSuchFrameException
- RemoteDriverServerException: When the server doesn’t respond thanks to the matter that the capabilities described aren’t proper
- UnsupportedCommandException: This exception occurs when the remote webDriver sends invalid commands.
- StaleElementReferenceException: Occurs if you are trying to access the DOM element which is detached from DOM tree.
- SessionNotFoundException: If you perform the action when there is no active webDriver session or if it is terminated just now, then this exception happens.
- SessionNotCreatedException: When webDriver cannot create a new session then it throws the SessionNotCreatedException.
Handling Exception in Selenium
Exceptions are quite common in Selenium; if it is not handled properly, then the automation tests can give you false failures or wrong results.
If an exception is not handled as expected, your subsequent tests might also fail. The Exception in Selenium makes it difficult to distinguish between the application problem and the Selenium problem; hence, the interpretation of automation results will become complicated.
Selenium or underlying programming languages provides a different way to handle the exceptions gracefully and provide the custom messages stack traces as and when such exceptions occur.
Why Exception Handling is Important
- Easy to identify the error with custom messages and stack traces
- Makes the program run smoothly even after the exception is thrown.
- Helps to identify the root cause of the problem.
- Helps in meaningful error reporting.
- Easy to identify the error types.
Common Reason for Exceptions
The Exception may occur due to multiple reasons. Below are some common reasons:
- Invalid user input/passing arguments
- Memory issues
- Device failure
- Network related problems
- Error in the code
- Problem with the file or file format while opening the file
Exception Categories
At the top level, the exception can be categorized as Built-in exceptions and User-defined exceptions.
Built-in Exceptions: These are defined by the programming languages you use.
User-defined exceptions: They are also called custom exceptions, where you can define your own type of exception using the exception libraries.
Different ways to handle Exception in Selenium
Using the Throws to handle Exceptions in Selenium
The throws keyword can be used to specify the type of exception that is being thrown from a method.
The difference between throw and throws keyword in Selenium Exception handling:
The throw keyword is used inside the block of code to throw the single exception. The throw keyword can be useful for throwing exceptions based on certain conditions within a code block and for throwing custom exceptions. The throws keyword is used in the method signature to declare an exception that might be thrown by the function while the execution of the code.
Example:
public static void someMethod() throws Exception {
//Your code that throw exception
}
Optionally you can specify multiple exceptions in throws
Example:
public static void someMethod() throws Exception1, Exception2 {
//Your code that throw exception
}
Handling Selenium exceptions using the Try/Catch Block
The try/catch block mechanism is the most used exception-handling mechanism.The try keyword is specified to a block where the exception will happen. In short, the try block contains the code which throws some exceptions. The try block must follow the catch block or finally block or both.The catch block must be associated with the try block. This block is used for handling the exception which is thrown by the preceding try block.
Example
try{
driver.switchTo().alert().accept();
}
catch (NoAlertPresentException e){
//Write code to handle exception
}
Handling Selenium Exception using Multiple catch blocks
The Programming language allows us to have multiple catch blocks. This will help us when you expect more than one type of exception.
Example
try{
//Some code
}
catch (ExceptionType1 e){
//Write code to handle the exception
}
catch (ExceptionType2 e){
//Write code to handle the exception
}
Handling Selenium Exception using Nested Try-Catch blocks
A try-catch block can have another try-catch block within them. That is called a nested try-catch block.
Example
try{
//Some code
try{
// some code
}
catch (ExceptionType1 e){
//Write code to handle exception
}
}
catch (ExceptionType1 e){
//Write code to handle exception
}
catch (ExceptionType2 e){
//Write code to handle the exception
}
Exception handling with Finally block
The finally keyword is used to assign the block, which contains the code that needs to be executed irrespective of whether the exception is thrown. You can use finally block along with the catch block. Typically, the finally block is used for freeing up the resources.
Example:
try{
//Some code
}
catch(Exception e){
}
finally{
//Code
}
Custom Exception in Selenium
Custom Exception provides flexibility to add the attributes that are not part of standard built-in exceptions. You can store the custom information, which helps to interpret the error messages quickly. However, in-depth understanding of the programming language is required before creating the custom exception. Poorly designed custom exception may degrade the readability.
The Programming languages we use, such as Java, cover most of the common exceptions. However, you might need to define your own type of exception. Every programing language has its own way of defining the custom exception. Below is the Java example of defining the custom exception.
class InvalidAgeException extends Exception
{
public InvalidAgeException (String str)
{
super(str);
}
}
You can use the above exception using the below line of code
public static void someMethod()
{
try
{
throw new InvalidAgeException("Invalid age");
}
catch (InvalidAgeException ex)
{
System.out.println(" Exception Caught");
}
}
Some of the important Exception handling functions available in Java:
- getMessage(): The get method returns the details about the exception that occurred
- getCause(): Returns the cause of the exception
- printStackTrace(): Prints the stack trace
- StackTraceElement [] getStackTrace(): Returns an array containing each element on the stack trace
Exception Handling Best Practices
- Handle common conditions without throwing exceptions
- Design classes or methods so that exceptions can be avoided
- Always predict the exception and have a try/catch/finally block inside your code
- Write meaningful error messages
- Create a custom exception as and when required to make it more readable
- Catch the exception with the exact exception type; for example rather than using Exception use the concrete exception type ArithmethicException
- Use the proper naming conventions when defining the custom exceptions
- Differentiate between the test case errors (Assertion errors) and coding errors
- Use the final block to clean up the resources
Summary
The exception handling mechanism has both advantages and disadvantages. The proper way of handling exceptions provides smooth functioning of your automation code. However, if it is used without understanding, the exception handling may mask the actual issues.
The exception handling and stack traces are also helpful in custom reporting, providing details about the exception along with the reason. However, building custom reporting libraries is complicated and takes more time. Moreover, custom reporting requires a deep understanding of the programming language and test automation tool that is used. If you are a beginner or intermediate, then building such libraries is challenging. One of the easiest solutions is using a test automation tool like Testsigma.
Testsigma doesn’t require much coding knowledge as it is a codeless automation tool. Available as a SaaS product, you can just signup and start using it. It has various incomparable features such as automatic test case generation, test report generation after each run, the ability to share the result without using any third-party tools, etc.