Powerful techniques to handle exception in Selenium with Java

Category: Selenium WebDriver 19 0

Many people utter how to handle the exception in Selenium! But one thing I would like to clear very straightforward that Selenium itself never handles Exception, Rather, It is the programming language which you use that gives you an environment to handle the exception in Selenium. Hope you got the point. Therefore, you must be good in the programming language which you have chosen to work with Selenium. In this tutorial, I am going to tell you exception handling in Selenium by using Java programming. Whatever, I will discuss here that will be the base to whichever programming language you will use for your Automation testing project with Selenium.

Before we proceed to our tutorial on exception handling, I’d highly recommend you to know the 20 types of major exceptions in Selenium. Once you go through these exceptions then you will be pretty clear about the scenarios where tests are likely to fail because of Exceptions. One thing, you should always keep in your mind that Selenium is nothing to do with Exception; however, it is the programming language which you are using will only deal with the exceptions. So, be prepared with your programming language, like Java.

Now I assume that you have gone thoroughly with the chapter on different types of Exceptions in Selenium, so today we will take forward the topic and will skip the already discussed one.

Hence, let’s begin with the advantage of handling the exception in Selenium.

Advantage to handle exception in Selenium

In general, a program will fail to continue its execution when the user gives input to divide any number by zero. Because in real time such arithmetic operation is not feasible, hence, Java will throw an Arithmetic Exception at runtime, thus, the program likely to terminate. Just to avoid the abnormal termination of the program we use exception handling. Hence, this is the major advantage of using an exception handler.

Suppose there are 100 statements and the normal flow of the execution gets terminated after 50th statement due to some abnormal behavior which we didn’t consider while writing the code. So in this case program stops its execution at the middle point. Now think, if we inject exception handler at the 50th position then Java will send the log of exception to console and further execution of the program will be continued normally.

What are the techniques to handle an exception in Selenium using Java programming language?

We mainly extend the core techniques cum methods of base programming language to handle exceptions in Automation testing. So here are the techniques listed below which we use to handle the exception in Selenium using Java programming:

  • Try – Catch
  • Try with multiple catches
  • Nested try – catch
  • Finally block
  • Throw keyword
  • Throws Keyword

Recommended Tutorials:

A guide to making dynamic XPath

A guide to making dynamic CSSSelector

Uses of Robot Class in Selenium

Verify title of any webpage

Essential list of Core Java topic for Selenium

handle exception in Selenium

Let’s start with Try – Catch first.

Use of “Try – Catch” block to handle exception in Selenium

Till now we have ideas about the types of exception that may come during Selenium test automation exception. So once we have figured out the kind of exception which probably may come then we pass the object of that exception as an argument within catch statement. Moreover, the main business test logic will within try statement. Here is the example:

 

Now the question you may ask, what happens if we don’t handle the exception?

The immediate cause is program will be terminated at that moment itself, which we have discussed many times. Therefore, let me tell you the working procedure of JVM if we do not handle the exception.  In addition to program termination, JVM prints out stake trace as well as exception description in the console.

Use of “Try – Multiple Catch” blocks to handle exception in Selenium

Here you will learn the sequence in which exceptions are handled. This technique will be applicable where there are chances of getting different kinds of exception while your program starts the execution. A set of rules are defined when you use multiple catch block with a single try statement. Those rules are as follows:

Rule#1: At a time, only one exception will occur so the only one catch statement will come into action.

Rule#2: You must follow the order to pass the object of the exception as an argument in the catch statement. Thus, you must give most specific exception to the most general one. For example, NoSuchElementException to Exception.

 

 

handle exception in Selenium with java

Use of “Nested Try –Catch” blocks to handle exception in Selenium

It is same as Nested If – Else Statement. In this case, a part of try statement will have one set of business test logic, whereas, another set of try statement will have some modular business test logic. Likewise, we handle multiple exceptions by using nested try-catch blocks in Selenium using Java.

Use of “finally” block to handle exception in Selenium

If you are using finally block then it doesn’t matter whether your program handles exception properly or not, but statement written within “finally” block will be always executed. This is the beauty of OOPS programming language.

So in which scenario, you can use finally block. Hence, you can use finally block where there is a requirement of closing connection, closing files, cleaning up of the session and so on.

One thing you should keep in your mind, you include catch block in your program or not, but when you include finally block then the entire exception handler will be valid, even without catch statement.

 

Use of “throw” keyword to handle exception in Selenium

When you have programmed any custom exception then you can use throw keyword. It explicitly throws the exception and it includes both checked and unchecked exception in it.

Use of “throws” keyword to handle exception in Selenium

This is highly useful to handle checked exception. A programmer can propagate the checked exception for the specific method.

 

Related Articles

Add Comment