18

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.

1
  • 2
    So if you don't want a failure in the first assertion to stop your program: what effect should it have, then? Commented Mar 23, 2011 at 8:22

5 Answers 5

28

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/

1
  • This answer is more relevant to OP's question.
    – Mugen
    Commented Sep 13, 2016 at 2:08
8

Selenium IDE uses verify to perform a soft assertion, meaning that the test will continue even if the check fails and either report the failures at the end of the test or on the event of a hard assertion.

With TestNG it is possible to have these soft assertions by using custom test listeners. I have documented how to do this on my blog: http://davehunt.co.uk/2009/10/08/using-soft-assertions-in-testng.html

Basically, you need to create your own verify* methods, in these you can catch assertion failures and add them to a map. Then in a custom afterInvocation listener you can set the test to failed if the map is not empty.

5
  • 1
    Dave, i was looking for same thing but your blog link is not working. Can you please give me some other link for above answer.
    – Kapil
    Commented May 2, 2013 at 12:11
  • 1
    @Dave Is the blog no longer available? Commented Dec 24, 2015 at 8:41
  • This answer is now outdated.
    – Mugen
    Commented Sep 13, 2016 at 2:08
  • Dave Hunt: the blog is Not Found. Why is it inactive? Commented Sep 27, 2017 at 9:18
  • The blog was migrated, this article is now available at davehunt.co.uk/2009/10/08/using-soft-assertions-in-testng.html but note that this article is now 8 years old and likely out of date.
    – Dave Hunt
    Commented Dec 6, 2017 at 13:33
7

I am adding again one of the easiest ways to continue on assertion failure. This was asked here.

try{
        Assert.assertEquals(true, false);
        }catch(AssertionError e)
        {
            System.out.println("Assertion error. ");
        }

        System.out.println("Test Completed.");
5

Change your assertions to verifications:

verifyTrue(selenium.isTextPresent("Please enter Email ID"));
verifyTrue(selenium.isTextPresent("Please enter Password"));
1
  • 5
    Where is the verifyTrue method defined?
    – vadipp
    Commented Dec 29, 2016 at 5:47
1

Once an assertion fails, execution should stop, that's the point of using them.

You can declare an assertion that tests both things, but then you're testing two things at once. Better to fix the cause of the first failure, then move on to the second assertion.

1
  • 6
    Ofc, that test should succeed in 100% and fail othervise, but this particular functionality of "soft assertions" is usefull when you want to see all failed assertions (eg. failed assertions for 4 of 12 field values of tested object) in single test case, and not to break up after first one. Commented Sep 27, 2013 at 7:08

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.