top of page
rajeemozhi

Exception Handling in Selenium Java

Introduction

When we work with Selenium Java, our test scripts may fail due to 2 different situations.

Error : Errors happen due to a lack of system resources. It cannot be solved

programmatically. If there is an infinite loop, the script will not stop. After some

time, when the memory is fully occupied by the system, it throws an error

message. Ex: StackOverflowError, OutOfMemoryError.

Exception : Exceptions are abnormal behaviors to which our test scripts end abruptly.

It can be solved programmatically.


Types of Exception


There are 2 types of Exceptions.

  1. Checked Exception or Compile Time Exception - These exceptions are checked by the compiler during compilation time. If the exception is not handled by the tester, it throws an error message.

Example : FileNotFoundException, IOException


2. Unchecked Exception or Run Time Exception - These exceptions happen during the execution of scripts to

check for logical errors.


Example : NoSuchElementException, TimeOutException


Before Exception Handling

When there is an exception, the test script will be failed abruptly and statements after the exception line will not be executed. In this example, after the exception, the End statement will be not printed.


Exception Handling

The process of handling exceptions is called Exception Handling. The script will be executed till the end without stopping abruptly when there is an exception using exception handling. Exceptions can be handled using try..catch..finally block.


Syntax:

try {

Statements;

} catch (Exception e) {

catch block statements;

}

finally {

finally block statements;

}


Keywords used in Exception Handling

  • try

  • catch

  • finally

  • throw

  • throws


1. try block

Statements that have a chance of throwing an exception are added in the try block. When there is no exception in a try block, it will move to the next line. When there is an exception in the try block, the remaining lines will not be executed.


2. catch block

We have to handle the exception with the help of a catch block if any. The catch block will be executed if there is an exception in the try block only. The try block must follow a catch block. After catching the exception also, the remaining lines in the catch block will be executed.


· When the name of the exception is known, it can be specified directly in the catch block. Ex: ArithmeticException.


· Otherwise, the parent class Exception can be specified if we don’t know the exact exception name. Parent class Exception should be used at the end.


· Multiple catch blocks are allowed in Java.


3. finally block

The finally block is executed whether there is an exception occurs or not. It appears after the catch block, or after the try block without the catch block. It will not appear in between the try block and catch block.


Output:


4. throw keyword

Users can define and throw their own exceptions using the throw keyword to voluntarily stop the execution with some text easily understandable to them.


Output:

5. throws keyword


throws keyword is used to throw the compile time exception at the method level. It is usually followed by a class and used with the method signature. When we use throws, wherever the function is called, we have to add the declaration.


Common Exceptions in Selenium Java

Some of the commonly encountered exceptions in Selenium Java are


· NoSuchElementException

  • It occurs when the web element is not found in DOM

  • Exception Handling

· NoSuchWindowException

  • It occurs when the driver tries to switch to a window, but it is not available

  • Exception Handling

· NoSuchFrameException

  • It occurs when the driver tries to switch to a frame, but it is not available

  • Exception Handling

· NoSuchAlertException

  • It occurs when the driver tries to switch to an alert, but it is not available

  • Exception Handling

· NoSuchAttributeException

  • It occurs when the driver tries to get the value of the attribute, but it is not available in DOM

  • Exception Handling


· StaleElementReferenceException

  • It occurs when the driver tries to perform an action on the web element which is no longer present on a page

  • Exception Handling

· ElementNotVisibleException

  • It occurs when the driver tries to find an element, but it is not visible on the page

  • Exception Handling

· ElementNotSelectableException

  • It occurs when the web element is present on the web page, but it is not able to select

  • Exception Handling

Methods used for displaying Exception

The below methods are used for displaying Exception.

  • printStackTrace() - This method prints stack trace to System.err. It also prints the exception name and its description.

  • getMessage() -This method displays the description of the Exception.

  • toString() - This method returns a text message contains Exception name and its description.


Points to remember

1. try block only is not allowed

2. catch block only is not allowed

3. try block can be followed by either catch block or finally block or catch / finally block

4. try block can be followed by multiple catch blocks

5. finally block is allowed after a try block when there is no catch block or after a catch block

6. finally block cannot be placed in between try block and catch block

7. No other statements are allowed in between try block, catch block or finally block

8. There should be only one finally block

9. catch block is executed when there is an exception

10. All the Exceptions are Java classes

11. Make sure that the parent class Exception should be at the end if there are multiple catch blocks


Conclusion

Exception handling is a good practice in test scripts which should not be ignored for better performance. Either we can use pre-defined exceptions or throw our own exceptions depending on the requirement. In this blog, we covered different types of Selenium Java exceptions and the keywords used in exception handling.

67 views

Recent Posts

See All
bottom of page