Exception handling in automation framework:

Brief:

If you dream about a mature automation framework, then this should be your first step. There are millions of article floating around the web related to exception handling, exception class etc etc. Here, I want automation developers to understand the best practices, pros and cons of exception handling in automation framework perspective only.

We have to understand that developing  a java app is different from developing a java automation framework. Think about it!

What is expected out of a automation framework:

It very straight forward, our automation framework should fail wherever there is a problem like, web element is not found, or assert fail, time out exception and other java exceptions.

We even need the stack trace(exception log) to locate the area of failure and analyse the root cause. We don’t want to suppress it.

And lastly automation framework should execute the next test irrespective of what happened for the previous test.

Below you can see that all the above points can be achieved without exception handling.

Let’s understand, what happens if we don’t do exceptional handling:

  • Test will fail abruptly,
  • it will print the stack trace in the console and
  • further steps are not executed. Note: However in case of batch run(test suite execution in case of TestNg and runner file execution of jUnit) the next test will be executed irrespective of whatsoever happened to the previous test.
  • We cant execute any special steps when exception occurs. For example: writing some log, capturing screenshot, adjusting the driver default wait time etc.

To summarize (before we jump into exceptional handling), in normal scenario we are not able to execute any customized code snippet on occurrence of exception.

We will see below how different scenario can be handled when exception occurs.

Exception Handling scenarios in automation framework:

  • Scenario-1: Validating absent of one element!

In this scenario, selenium will throw “NOSUCHELEMENTFOUND” exception. One have to catch this exception and do necessary steps.

Code snippet below:

//initialize the web element
//classic way of finding elements
WebElement someCheckBox = driver.findElement(By.Xpath("<<xpath>>"));
//OR 
//Page Object Model(POM) and pageFactory style of finding elements
@FindBy (how = How.XPATH, using="<<xpath>>")
private WebElement someCheckBox;
//defining method to verify element is not present
//returns true if element is not present and vice versa
public Boolean validateElementIsNotPresent(WebElement x){
try{
x.isDisplayed();// this line will throw exception as this element is not present
//take screenshot if required
return false;
}
catch(NOSUCHELEMENTFOUND noSuchElementExp){
return true;
}}
  • Scenario-2: Adjusting driver timeout as per requirement:

This scenario is an important one, driver should not use the default wait time everywhere as this can’t be ideal. To illustrate more, I would like to consider scenario-1 code snippet. Inside the try block, we are performing  an action[x.isDisplayed()] on an element which is not present and we know that! Now, every time by default selenium will wait for some 60 seconds and try to find the element again and again(it will look like the test went to hung state), and then the exception is thrown.

To avoid this we can reduce the default driver wait time (selenium will throw TIMEOUT exception)

code snippet below:

//rewriting Scenario-1
//initialize the web element
//classic way of finding elements
WebElement someCheckBox = driver.findElement(By.Xpath("<<xpath>>"));
//OR 
//Page Object Model(POM) and pageFactory style of finding elements
@FindBy (how = How.XPATH, using="<<xpath>>")
private WebElement someCheckBox;
//defining method to verify element is not present
//returns true if element is not present and vice versa
public Boolean validateElementIsNotPresent(WebElement x){
try{
//default time is decreased
driver.manage().timeouts().implicitlyWait(500, TimeUnit.MILLISECONDS);// this line may throw timeout exp
x.isDisplayed();// this line will throw exception as this element is not present
//take screenshot if required
return false;
}
catch(NOSUCHELEMENTFOUND noSuchElementExp){return true;}
catch(TimeoutException timeOutExp){
timeOutExp.printStackTrace();
Assert.fail("write something relevant");}
finally{
//increase the time after the operation is performed.
driver.manage().timeouts().implicitlyWait(20000, TimeUnit.MILLISECONDS);
}}
  • Scenario-3: capturing screenshot when ever test failed

It’s the simplest one, make sure all your methods are covered with try-catch block. Then every catch block is calling the common method of capturing the current screenshot of the Application Under Test (AUT).

  • Scenario-4: AssertError, want to capture screenshot of the AUT, whenever assert is failing. Otherwise handling this error separately is not necessary, as Asserts are suppose to fail with stack trace.
try{
Assert.assertEquals(actual,Expected);
}
catch(AssertError assertExp){
//call the common method for screen capture
Assert.fail("<<some relevant text>>");
}

Conclusion:

Apart from above scenarios there can be many such scenarios which can be easily implemented using exception handling. As part of java best practice include exception handling almost every where possible (most common ArrayIndexOutOfBoundsExceptionDivideByZeroException etc.) as per your buffer.

“Exception” is parent class for all java exception, do not use it until you require it. Otherwise it may catch all the exception and failed test case will be shown as passed. Be careful! 😛

Refer to all the selenium exception available and implement as required.

Hope you like this blog! Please do like, comment, follow and let me know if there are any other scenario you come across which relates to exception handling. There is always scope to learn.


 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s