0

I'm trying to learn automation with Selenium Webdriver using c#. I have my custom method Assert. What I did to continue the test after catching an AssertFailedException is using try-catch below is my code

public static void assert(string value, IWebElement element)
    {
        try
        {
            Assert.AreEqual(value, element.Text);
        }
        catch (AssertFailedException e)
        {
            Console.WriteLine(e.Message.ToString());
        }
    }

My problem is it catches all AssertFailedException(which is my goal) but the result of the test is PASSED in visual studio. My question is, how do I implement Continue on Failure and Fail the test if the Console contains Exceptions. Thankyou in advance guys!

3
  • Don't catch AssertFailedException inside your custom method. remove the try catch block. Commented Sep 27, 2016 at 9:47
  • Assign a boolean variable in catch block and at the end of test use the same value to fail the test. Commented Sep 27, 2016 at 10:06
  • @SudharsanSelvaraj If i remove the try catch block, It will stop the execution of the test when the Assert method fails. What I need is to continue the test until the end of the execution. Thankyou! Commented Sep 28, 2016 at 5:06

2 Answers 2

1

As far as I understand, you want to do several checks inside your test, and at the very end of it to determine whether any of of them failed. You may need to write some custom code to achieve this. For example, you may introduce a class Assertion:

internal class Assertion
{
    private readonly string title;
    private readonly object expected;
    private readonly object actual;

    public Assertion(string title, object expected, object actual)
    {
        this.title = title;
        this.expected = expected;
        this.actual = actual;
    }

    public bool IsMatch()
    {
        return this.actual == this.expected;
    }

    public override string ToString()
    {
        return $"Title: {title}. Expected: {expected}. Actual: {actual}";
    }
}

When your test is running, you will create new instances of Assertion class and store them in a list. At the end of the test, you can use the following method:

    private static void VerifyAssertions(Assertion[] assertions)
    {
        var failedAssertions = assertions.Where(a => !a.IsMatch()).ToArray();
        if (failedAssertions.Any())
        {
            throw new AssertFailedException(string.Join<Assertion>("; ", failedAssertions));
        }
    }
Sign up to request clarification or add additional context in comments.

1 Comment

Very nice logic and code. I will try this now and report the result to you after. Thankyou very much @oldbam
0

You could try using verify instead of assert for minor checks. Assert by default indicates a major checkpoint and script execution will be terminated on fail and if you catch that exception the reporting will be ignored - this is expected behaviour. However, a verify indicates that the script can continue even on fail - in this case the failed step will be reported and the script will continue.

To put it simply, use assert when you don't want the script to proceed on failure and use verify when you want the script to report the failure and proceed.

1 Comment

Now I understand what Assert really does. Can I ask how do I make a verify method? :)

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.