5

I have some script which is written with selenium webdriver.

Actually, I have put some assert statement in between script.

When an assert statement fails my script stops execution.

Is there any way to continue my script after a failed assert statement.

1
  • Do you care about having information which assertions failed?
    – dzieciou
    Commented May 8, 2015 at 19:09

5 Answers 5

11

Yes, use soft assertions org.testng.asserts.SoftAssert. Soft assertions are assertions that do not terminate the test when they fail but their results are included in the test execution report.

More articles on that topic:

2

You can create extension methods for directly asserting elements. There you can wrap the assert statements with a try catch. In the catch, you can add the exception message to a log if you have one.

public static class SafeAssertExtensions
{
    public static void AreEquals(this IWebElement element, string expectedText)
    {
        try
        {
            if (element.Text == null)
            {
                throw new NullReferenceException("The element text property is null.");
            }
            Assert.AreEquals<string>(element.Text, expectedText, "The text- {0} was not as expected- {1}", element.Text, expectedText);
        }
        catch (Exception ex)
        {
            Log.WriteLine();
        }
    }
}

You can add similar methods for the other types of validations.

You can use it directly over your elements.

textFieldElement.AreEquals("your sample text content");
4
  • The only problem with this solution is that it's not Java, while the question contained java tag. I guess it would need to be modified to match both Java syntax and API from one of Java libraries for assertions (JUnit, TestNG, Hamcrest).
    – dzieciou
    Commented May 8, 2015 at 18:42
  • 1
    Yeah, I know you are right. But in my opinion the general idea is more important, you can write the same thing in Java, Python or any other language. Maybe some of the languages don't support extension methods but the main idea behind the provided solution about soft assert stays the same. Commented May 8, 2015 at 19:04
  • That is correct!
    – dzieciou
    Commented May 8, 2015 at 19:08
  • 1
    Guys programming language is not important after all. Sometime Logic is enough . :) Do not take tag as entry point.
    – Sagar007
    Commented May 9, 2015 at 6:09
1

Failed assertion is nothing more than AssertionError which is a subclass of Throwable so you can treat it as any other exception.

Example code:

try {
    //some assertion
} catch (AssertionError err) {
    //print error to the log
    //take screenshot
    //do whatever you want
    //do nothing
}

Usually it is being followed by finally block where the same (or other) exception is re-thrown to mark the test as failed (test case nature assumes that it is designed to test a single small bit of functionality and if some step has failed - further testing doesn't make much sense)

The WebDriver Sampler: Your Top 10 Questions Answered provides relevant code snippet (it's in JavaScript but the idea should be the same) under "How do I Take a Screenshot When I Hit Errors?" chapter.

1

You can use SoftAssertions.

public void foo() {
        SoftAssert soft = new SoftAssert();
        soft.assertTrue(false);
        soft.assertTrue(true);
        soft.assertTrue(false);
        soft.assertAll(); // Will mark the test as failed
}
4
  • Please edit your answer to explain how it adds to the existing answers to this question, particularly the accepted answer. You can find more information about writing good answers here: sqa.stackexchange.com/help/how-to-answer
    – dzieciou
    Commented Jul 23, 2019 at 11:23
  • I show the usage
    – user38046
    Commented Jul 23, 2019 at 11:30
  • How do you fail the test if at least one assertion failed? I think that would be solved if you call soft.assertAll() at the end of your test.
    – dzieciou
    Commented Jul 23, 2019 at 11:44
  • 1
    You are right. I just tested it and edited the answer accordingly.
    – user38046
    Commented Jul 23, 2019 at 11:47
0
try{
    Assert.assertEquals(true, false);
   }
catch(AssertionError e)
   {
   System.out.println("Assertion error. ");
   }

1
  • 2
    Please edit your answer to explain how it adds to the existing answers to this question, particularly the accepted answer. You can find more information about writing good answers here: sqa.stackexchange.com/help/how-to-answer
    – Kate Paulk
    Commented Jul 22, 2019 at 14:25

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.