RE: How to make test cases run even if an exception occurs. Exceution of cases should not stop.

Hi,

My selenium scripts are getting stopped if an exception occurred and the remaining set of cases are not executing in that suite file .

I want the suite to execute the remaining cases also even if an exception occured.

Thanks in advance.

mallik9292 Train Asked on November 10, 2014 in Selenium WebDriver.
3 Answers

Hi Malik,

Below is the same test case, avoid the code but try to understand the try catch logic:

package testCases;
public class Framework_002 {
    public WebDriver driver;
    private String sTestCaseName;
    private int iTestCaseRow;
    
  @BeforeMethod
  public void beforeMethod() throws Exception {
          DOMConfigurator.configure("log4j.xml");
          sTestCaseName = this.toString();
          sTestCaseName = Utils.getTestCaseName(this.toString());
        Log.startTestCase(sTestCaseName);
        ExcelUtils.setExcelFile(Constant.Path_TestData + Constant.File_TestData,"Sheet1");
        iTestCaseRow = ExcelUtils.getRowContains(sTestCaseName,Constant.Col_TestCaseName);
        driver = Utils.OpenBrowser(iTestCaseRow);
        new BaseClass(driver);  
        }
  
  @Test
  public void f() throws Exception {
      try{
        SignIn_Action.Execute(iTestCaseRow);
        ProductSelect_Action.productType(iTestCaseRow);
        ProductSelect_Action.productNumber(iTestCaseRow);
        ProductListing_Page.PopUpAddToCart.btn_GoToCart().click();
        CheckOut_Action.Execute();
        PaymentDetails_Action.execute(iTestCaseRow);
        Confirmation_Action.Execute();
        Verification_Action.Execute();
        ExcelUtils.setCellData("Pass", iTestCaseRow, Constant.Col_Result);
      }catch (Exception e){
          ExcelUtils.setCellData("Fail", iTestCaseRow, Constant.Col_Result);
          Utils.takeScreenshot(driver, sTestCaseName);
          Log.error(e.getMessage());
      }        
  }
        
        
  @AfterMethod
  public void afterMethod() {
        Log.endTestCase(sTestCaseName);
        driver.close();
          }
}

So, in case any exception occured in my main test, the try catch block will catch the exception, make the logicall end of the test case and move on to the next test case. The only problem in the above code is that in the TestNG, it will display you as TEst Pass, but in the Excel result file it will say Test Fail. but again you can handle that with inserting one more logic or with listeners. Above is just for illustration purpose to give you an idea. Please do visit the chapter of Exception handling on TOOLSQA.com

Regards,
Lakshay Sharma

Professor Answered on November 10, 2014.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.