If a test case fails, how to close the browser and move to the next one?



  • 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.
    }
    


  • 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();
    }
    


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2