What is an Exception

An Exception is an event, which occurs during the execution of a program, that disrupts the normal flow of the program’s instructions or in simple words, any issue which makes your test case stop in between the execution is an Exception.

Difference between Error and Exception

An Error “indicates serious problems that a reasonable application should not try to catch.”

An Exception “indicates conditions that a reasonable application might want to catch.”

Whenever an error occurs while executing a statement, creates an exception object and then the normal flow of the program halts and it tries to find someone that can handle the raised exception. The exception object contains a lot of debugging information such as method hierarchy, line number where the exception occurred, type of exception etc. When the exception occurs in a method, the process of creating the exception object and handing it over to runtime environment is called “throwing the exception”.

What is Exception Handling

Exception handling refers to the anticipation, detection, and resolution of programming application, and communications errors. It is the block of code that processes the exception object and helps us by giving us a chance to act on it.

Why Exception Handling is Important

1) Think of a situation where you have got an exception and you want to print some custom message in your logs, so that it can be understandable by the whole team.

2) There can be some situations where you want to just eat up the exception and want your test to carry on with rest of the execution.

3) In case you want to perform some series of steps on occurring of an certain exception for e.g. if you got an exception because a product is out of stock, that product is no longer displayed on the page and you want you to go with another product available on the page.

4) In case you want to handle some certain kind of exception in Selenium like ElementNotSelectableException, ElementNotVisibleException, NoSuchElementException etc. exceptions.

Different Exceptions in Selenium

There is a complete list of Exceptions mentioned on the Selenium Doc which you may or may not encounter in course of your testing.

Most common Exceptions:

NoSuchElementException: FindBy method can’t find the element.

StaleElementReferenceException: This tells that element is no longer appearing on the DOM page.

TimeoutException: This tells that the execution is failed because the command did not complete in enough time.

ElementNotVisibleException: Thrown to indicate that although an element is present on the DOM, it is not visible, and so is not able to be interacted with

ElementNotSelectableException: Thrown to indicate that may be the element is disabled, and so is not able to select.

You can access the complete list of exception here, but if you are a beginner that I would suggest you to move forward as of now. It can be over dose.

How to Handle Exception

Try/Catch: A method catches an exception using a combination of the try and catch keywords.Try is the start of the block and Catch is at the end of try block to handle the exceptions. A try/catch block is placed around the code that might generate an exception. Code within a try/catch block is referred to as protected code, and the syntax for using try/catch looks like the following:

1

2

3

4

5

try{

// Some code

}catch(Exceptione){

// Code for Handling the exception

}

Multiple Catch blocks: A try block can be followed by multiple catch blocks. Like I said earlier, that there are multiple exceptions and you can expect more than one type of exception on a single code block and if you like to handle each type of exception separately with a separate block of code. The syntax for multiple catch blocks looks like the following:

1

2

3

4

5

6

7

try{

//Some code

}catch(ExceptionType1 e1){

//Code for Handling the Exception 1

}catch(ExceptionType2 e2){

//Code for Handling the Exception 2

}

There is no restriction on the number of the catch blocks, you can use more than two. You may be wondering that how does it work. It is pretty simple, if an exception occurs in the protected code, the exception is thrown to the first catch block in the list. If the exception thrown matches the ExceptionType1, it gets caught there and it executes the code which is under the same exception block. If not, the exception passes down to the second catch statement and goes on like this.

Note:In case the exception does not match with any exception type and falls through all catches, the current method stops execution and exception is thrown. That is why it is advisable to include default exception as well in the end, so in case if the exception falls through, it can be handled by the default one.

Throw: Sometimes we want to generate exception explicitly in our code, for example in Selenium Automation Framework most of the time we print self-written logs, once we catch an exception and then we need to throw that exception back to the system so that the test case can be terminated. Throw keyword is used to throw exception to the run time to handle it.

Throws: When we are throwing any exception in a method and not handling it, then we need to use throws keyword in method signature to let caller program know the exceptions that might be thrown by the method.

1

2

3

4

5

6

7

8

9

10

// Method Signature\

publicstaticvoidanyFunction()throwsException{

try{

// write your code here

}catch(Exceptione){

// Do whatever you wish to do here

// Now throw the exception back to the system

throw(e);

}

}

Multiple Exceptions: Multiple exceptions can be handled in the throws clause.

1

2

3

4

5

6

7

8

publicstaticvoidanyFunction()throws ExceptionType1,ExceptionType2{

try{

//Some code

}catch(ExceptionType1 e1){

//Code for Handling the Exception 1

}catch(ExceptionType2 e2){

//Code for Handling the Exception 2

}

Finally: The finally keyword is used to create a block of code that follows a try block. A finally block of code always executes, whether or not an exception has occurred.

1

2

3

4

5

6

7

8

9

10

11

try{

//Protected code

}catch(ExceptionType1 e1){

//Catch block

}catch(ExceptionType2 e2){

//Catch block

}catch(ExceptionType3 e3){

//Catch block

}finally{

//The finally block always executes.

}

Exception Handling in Framework

Your Selenium test should be able to fail, but not because of exceptions that are thrown. If your test is failing from exceptions then quite likely you have no exception handling. By doing this, you don’t have the opportunity to cleanup the WebDriver object at the end of the test.

The tests should be failing under your terms only for example, you should never be getting exceptions like NullPointerException but if you are getting such as ElementNotFoundException, then also it is good idea to catch the exception, stop the further execution and end your test in a Logical way.

As I stated above that when ever we face any exception, it is better to print logs of the error, so that it can be investigated. Throwing error depends on framework to framework because if you catch the exception and throw the exception back to the main program, it is necessary to have an try catch block in the main program as well, so that test case can be closed in a proper manner. If you do not handle the exception, the test case will stop right there and it will not execute the other test cases of batch run.

Exception Handling in this Framework

As I said it completely depends on you how you want to handle the exception, so it is not necessary to follow this approach. You can do it on your own way. I create a ‘public static boolean bResult;‘ variable in my main driver script and I set its value to ‘true’ at starting of each test case and each test step. Means we have two loops in the main driver script, outer loop is for test cases and inner loop is for test steps. So on start of every new test case and test step, I set this variable value to ‘true‘. The idea behind it to change the value from ‘true‘ to ‘false‘ at the time of any exception. The exception will be caught with the try catch block and the statement to change the value from ‘true‘ to ‘false‘ will reside in that block. For example:

1

2

3

4

5

6

7

8

9

publicstaticvoidopenBrowser(Stringobject){

try{

Log.info("Opening Browser");

driver=newFirefoxDriver();

}catch(Exceptione){

Log.info("Not able to open Browser --- "+e.getMessage());

DriverScript.bResult=false;

}

}

It means the bResult value will set to ‘false‘ only in case of an exception, other wise it will always remain ‘true‘. So now we just need to match the value of bResult in the main driver script. If it is true, execute the next step and if it is false, stop the test step and test case and move forward to next test case.

The ActionKeyword class and ExcelUtils class will look like this, once the exception handling is set up in the framework: