0

Please see a simplified code below

public class testClass   
{
    static WebDriver driver;

    @Test
    public void Checking()
    {
        driver = new FirefoxDriver();
        driver.get("http://xyz.com");
        try 
        {
            Assert.assertEquals("Hello", "Not Hello");
        }
        catch(Exception e)
        {
           System.out.println("Fail" + e.getMessage());
        }
        System.out.println("just to check");
    }
}

While debugging the code, once the Assertion fails in try block it ask for 'Source' to attach for testNG. see the screenshot below:

Attach the source and then I added the source with the help of an eclipse plugin. Now when I start debugging and if the Assertion in try block fails then controll passed to the attached source (invoker.class) and does not return to the catch block ever.

Please suggest what shall I do in this case.

3 Answers 3

0

In case if you really want to catch your assertion failures, you can try and use following:

catch (AssertionError e) {
      //Your code to execute when an assertion fails.
}

For detail explanation you can refer the following answer:
https://stackoverflow.com/a/29672257/3406534

1

Assertion :

  • Assert is implemented to verify expected and actual result.

  • If Assert failed, It will terminate the script and it will not execute further code/test or any other block

Try & Catch blocks :

  • Try and Catch blocks are used mainly to take action if errors & exception which might occur during execution

  • Just provision to handle those error and exceptions in scripts/codes

Back to your Question:

  • In your case, assert failed. Now it will terminate program to be executed further

  • So its not going to execute catch block at assert is neither error nor exception

0

You can try below structure in your @Test part:

try {
    assertEquals("Leave a Reply ", driver.findElement(By.id("reply-title")).getText());
} catch (Error e) {
    verificationErrors.append(e.toString());
}
Thread.sleep(6000); 

Your Answer

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

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