Is there a way to handle exceptions in testNG?

0 votes
Suppose, I get error in a particular test case, if this happens the remaining test cases are not even checked. Is there a way to handle exception like try catch block in testNG?
Jan 3 in Selenium by Nitesh
• 2,950 points
365 views

2 answers to this question.

0 votes

Yes, like exception handling in java here also in testNG there is a method by which you can handle exceptions

package testNG;
import org.testng.annotations.Test;
public class Test {
    @Test
    public void testException() {
        System.out.println("Check the following");
        int i = 1 / 0;
    }
}

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="testNG">
<test name="Test">
<classes>
<class name="TestNGException" />
</classes>
</test>
</suite>
Output:

[TestNG] Running:

Check the following

===============================================
Default suite
Total tests run: 1, Failures: 1, Skips: 0
===============================================

You can see that there is a failed test case. Hope this helps 

answered Jan 3 by Nabarupa
0 votes
I don't think this should be a problem. If the test case fails, then the report will say this particular test case failed and others passed/ failed accordingly. It won't prevent others from executing. I'd advise you to check your issue once again.
answered Jan 4 by Vardhan
• 12,870 points

Related Questions In Selenium

0 votes
1 answer
0 votes
1 answer

Is there a way to pass on the options/ flags to Selenium if i'm scripting in Python?

This is the usage: from selenium import webdriver from ...READ MORE

answered Jun 8, 2018 in Selenium by king_kenny
• 3,650 points
352 views
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

Finding WebDriver element with Class Name in java

The better way to handle this element ...READ MORE

answered Apr 10, 2018 in Selenium by nsv999
• 5,430 points
1,198 views
0 votes
1 answer
0 votes
1 answer

Geo-location microphone camera pop up

To Allow or Block the notification, access using Selenium and you have to ...READ MORE

answered May 11, 2018 in Selenium by Samarpit
• 5,510 points
1,283 views
0 votes
1 answer

How to use such xpath to find web elements

It's much simpler to use: #MyDiv123 > div.super or ...READ MORE

answered Jun 14, 2018 in Selenium by Samarpit
• 5,510 points
1,041 views
0 votes
1 answer
0 votes
1 answer