1

How can i stop the execution of a test case if an exception happen and caught and move to next one. I am using TestNG, Eclipse, Selenium. In my xml, i have:

<?xml version="1.0" encoding="UTF-8"?>
<suite name = "Regression Suite" >       
<listeners>
 <listener class-name="Utility.Listener"></listener> 
 </listeners>
<test name = "Regression">
<classes>
<class name = "Scripts.TestNG_Temp"/>  -->
<class name = "Scripts.CreateAppointment"/>
</classes>
</test>
</suite> 


@Test
public void main(){
try{
driver.findElment(by.name("textbox)).click();
}cuaght(ElementNotFoundException e){
//here i want to stop the execution of my test case, close the browser & move to next one.
}

5 Answers 5

1

If the element not being found is an expected failure in the event of a bug. One thing you can do is throw an assertion error which imitates the natural way to fail a test.

try {
    driver.findElment(by.name("textbox")).click();
} catch(ElementNotFoundException e) {
    throw new AssertionError("A clear description of the failure", e);
}

Passing in your original exception as the second argument preserves the original cause of failure, which will be reported in your testng results.

java.lang.AssertionError: A clear description of the failure
at ...
Caused by: org.openqa.selenium.ElementNotFoundException: I couldn't find an element!

As the other answers mention, make sure you have code in place to clean up your test environment and close your webdriver connection.

2
  • Hi Julian, I have one issue, my tests are getting continues failing once "WebdriverException" issue occur in same driver instance. how should i handle this scenario.Thanks in advance May 16, 2018 at 3:12
  • Hi @Kv.senthilkumar, I'm not sure I understand your problem. You should post a new question so more people see it, it will help you get an answer. If you want, share a link with me and I'll give an answer if I can
    – Julian
    May 16, 2018 at 5:28
1

As I understand you want execution to stop if error occurs, in that case you don't want to inclose your code in try and catch block. Remove try and catch block and add @AfterMethod in your testng class. Once your program errors out it would move move to AfterMethod block or after successful completion of your test.

@AfterMethod 
public void cleanup(){
driver.quit();
}
1

As far as I understand your want something like if you have 5 test case and if got exception in 2nd test method then it closes the browser and moves to the 3rd test method. First, need to understand how TestNG work. Whenever any test case got an exception then TestNG marked its status as Fail and move to next test case.

  1. Now your 1st problem solved that it should move to next test case.
  2. The close browser creates the @AfterMethod with below code:

CODE:

@AfterMethod
public void appendFinalHTMLReport(ITestResult result){
    if(result.getStatus() == ITestResult.FAILURE)
    {
       driver.close();
    }
 }

Description of above code: ITestResult is the last method final result. So we are checking if fail then try to close the browser. The difference between in:

  1. driver.close : Close the focused browser window.
  2. driver.quit : Close all browser windows and end the session safely.

So, if you used driver.quit() then your driver object needs to be initialized again. That's why using driver.close() - to close your current browser.

I hope this will help you out.

0

You should design your test cases to be independent, because modularity decreases complexity: If tests are dependent, you would always need to run all test cases if the last one failed, and needed to check all test cases whether they might have caused the error/failure.

With independent tests, you can run all of them, e.g. during a nightly build, even if some of them fail. Or execute a specific one in isolation. And you need not worry about the ordering of tests. That is why almost all test runners execute all tests and make no guarantee about their order.

This applies especially for unit tests. For a few integration tests and acceptance tests some dependency might be acceptable. In that case, use TestNG's dependency feature.

0

Use the keyword throw inside the catch block to stop the script from further execution, upon any failure/exception.

try
{
//code to test
}
catch (Exception e)
{
throw new AssertionError("A clear description of the failure", e);
}
1
  • 1
    Could you please update your answer to explain how it adds to the answers already given, especially the accepted answer. Repeating the basic code given in the accepted answer without explaining anything is not terribly helpful.
    – Kate Paulk
    Dec 6, 2017 at 13:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.