user2932876
user2932876

Reputation: 332

Using verify in selenium testing using NUnit

In the selenium IDE, there is a verify command. When I exported the command into c# I found that verify is basically an assert in a try catch statement and the error is added into a string.

In my code, I want to use the functionality of the verify command but I do not want to use a try and catch statement for every single assert.

Does anyone have a way to do this?

edit:

public static void AssertVerticalAlignment(CustomWebDriver webDriver, IElement elementA, IElement elementB, double tolerance = 0)
    {
        try
        {
            Assert.AreEqual(elementA.Location.X, elementB.Location.X, tolerance);
        }
        catch (Exception exception)
        {
            LogHelper.LogException(exception.Message, exception.StackTrace, webDriver.GetScreenshot());
            throw;
        }
    }

What I want to do is add a message in the assert. It should say nameOfElementA is not aligned with nameOfElementB. But I don't want to give elementA and elementB a name property.

This is how I call the method: AssertVerticalAlignment(webdriver, homepage.NameInput, homepage.AgeInput) Homepage is an object and NameInput is part of Homepage. NameInput is of type IElement, which is basically same as IWebElement, but it cannot interact with html, ie. it doesn't the ability to click, etc.

So I want the message to say NameInput is not aligned with AgeInput

Upvotes: 0

Views: 6601

Answers (1)

Arran
Arran

Reputation: 25056

You are essentially asking for a way to do "soft assertions". The way the IDE does it is correct. After all, that's what "soft assertions" are. If something fails a particular assertion, you want it to carry on regardless. That is what the IDE is doing, by catching that exception (notice that in it's code it's only catching the AssertionException).

In order to help avoid messy code, the best you can do is create your own verify methods. Sometimes you don't even need to catch exceptions. For instance take this basic verifyElementIsPresent method:

private class SoftVerifier
{
    private StringBuilder verificationErrors;

    public SoftVerifier()
    {
        verificationErrors = new StringBuilder();
    }

    public void VerifyElementIsPresent(IWebElement element)
    {
        try
        {
            Assert.IsTrue(element.Displayed);
        }
        catch (AssertionException)
        {
            verificationErrors.Append("Element was not displayed");
        }
    }
}

Why do you need the exception at all?

private class SoftVerifier
{
    private StringBuilder verificationErrors;

    public SoftVerifier()
    {
        verificationErrors = new StringBuilder();
    }

    public void VerifyElementIsPresent(IWebElement element)
    {
        if (!element.Displayed)
        {
            verificationErrors.Append("Element was not displayed");
        }
    }
}

The sort answer is there are some ways you can make it a little less messy, but overall, no, there isn't much you can do about it.

Upvotes: 1

Related Questions