There is a great kind of relationship between normal execution flow and Exception as Developer and Tester. It’s a very famous saying that Exception exists everywhere, so it exists in programming too. We know some of the Expected Exceptions in a particular logic like Arithmetic Exception occurs when we divide an integer by zero. Hence, when there are chances of getting some expected exceptions then we can handle it with exception handling technique either by try-catch or by throwing a custom exception.
We already have some of the detailed articles on Exceptions and Exception handling. Click on the link below to go through the tutorials on Exceptions and its handling:
- Some of the major exceptions in Selenium WebDriver
- Powerful techniques to handle Exceptions in Selenium with Java
These tutorials are the fundamental tutorials, which will introduce you to some of the major exceptions which generally occur while developing automation scripts in Selenium with Java. Also, they teach you the techniques to handle such unexpected and expected exceptions.
Here are some of the recommended TestNG tutorials for you:
- How to use @DataProvider in TestNG?
- How to create test dependencies in Selenium using TestNG?
- What are different annotations in TestNG?
If you have gone through above tutorials on Exception handling, you must have noticed that Java inbuilt techniques used to handle exceptions in Selenium. Today we will learn one of the most important features of the TestNG which helps in auto handling expected exceptions in our selenium automation project.
How to handle expected exceptions in Selenium using TestNG?
Let’s look at the general program in which we are storing the value in String array at the index which already has the NULL value or simply index does not exist. So here is the program below:
package Test; import javax.swing.plaf.synth.SynthSpinnerUI; import org.testng.annotations.Test; public class ExceptionSample { @Test public void testExceptionSample() { String[] str = new String[2]; str[0] = "Avinash"; str[1] = "Mishra"; str[2] = "Inviul"; int size = str.length; System.out.println(size); } }
After running the above program, we get ArrayIndexOutOfBoundsException. We can say that this is one of the expected exceptions when we are storing data after the index ends in an array. Here is the console output.
Once we have identified the expected exception then we can handle it properly. We can handle the above scenario with try and catch. Let’s look at the program which handled exception using try and catches block.
Exception handling using Try & Catch
package Test; import javax.swing.plaf.synth.SynthSpinnerUI; import org.testng.annotations.Test; public class ExceptionSample { @Test public void testExceptionSample() { try{ String[] str = new String[2]; str[0] = "Avinash"; str[1] = "Mishra"; str[2] = "Inviul"; int size = str.length; System.out.println(size); } catch(Exception e){ e.printStackTrace(); } } }
In this scenario, the test will be passed but catch will handle the exception.
Expected exceptions handling with TestNG
This is the today’s agenda. Now we will see how we can handle the exception using TestNG inbuilt feature. Let’s look at the declaration part:
@Test(expectedExceptions=Exception_Name.class)
Below is the sample program which uses the above statement and handles ArrayIndexOutOfBoundsException.
package Test; import javax.swing.plaf.synth.SynthSpinnerUI; import org.testng.annotations.Test; public class ExceptionSample { @Test(expectedExceptions=ArrayIndexOutOfBoundsException.class) public void testExceptionSample() { String[] str = new String[2]; str[0] = "Avinash"; str[1] = "Mishra"; str[2] = "Inviul"; int size = str.length; System.out.println(size); } }
We have seen exception handling with TestNG and Try catch as well. You can post your queries below and don’t miss to join our group for more updates.
Nothing understand, what is it?
It’s about software testing.
I have one question here, in which cases we need to handle the xpected exceptions? Exceptions are something which should not display in any case rgt.. If it displays then we should know the exact reason and work to rectify that rgt.. Pls help me with a scenario where it can be used.
Exceptions are checked and unchecked. If we know the exceptions prior to runtime then we can use them in Expected exceptions.
i dont want to print my exception statment in reporter html .
evn thgh my testcase is paased,exceptions are printing /displayd in default output testngreport .