How to continue execution when Assertion is failed

Ripon Al Wasim picture Ripon Al Wasim · Mar 23, 2011 · Viewed 56.9k times · Source

I am using Selenium RC using Java with eclipse and TestNG framework. I have the following code snippet:

assertTrue(selenium.isTextPresent("Please enter Email ID"));
assertTrue(selenium.isTextPresent("Please enter Password"));

First assertion was failed and execution was stopped. But I want to continue the further snippet of code.

Answer

Stormy picture Stormy · Mar 24, 2014

I suggest you to use soft assertions, which are provided in TestNg natively

package automation.tests;

import org.testng.asserts.Assertion;
import org.testng.asserts.SoftAssert;

public class MyTest {
  private Assertion hardAssert = new Assertion();
  private SoftAssert softAssert = new SoftAssert();
}

@Test
public void testForSoftAssertionFailure() {
  softAssert.assertTrue(false);
  softAssert.assertEquals(1, 2);
  softAssert.assertAll();
}

Source: http://rameshbaskar.wordpress.com/2013/09/11/soft-assertions-using-testng/