0

在 selenium IDE 中,有一个验证命令。当我将命令导出到 c# 中时,我发现 verify 基本上是 try catch 语句中的断言,并且错误被添加到字符串中。

在我的代码中,我想使用验证命令的功能,但我不想对每个断言都使用 try 和 catch 语句。

有没有人有办法做到这一点?

编辑:

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;
        }
    }

我想要做的是在断言中添加一条消息。它应该说 nameOfElementA 与 nameOfElementB 不一致。但我不想给 elementA 和 elementB 一个 name 属性。

这就是我调用该方法的方式:AssertVerticalAlignment(webdriver, homepage.NameInput, homepage.AgeInput) Homepage 是一个对象,NameInput 是 Homepage 的一部分。NameInput 是 IElement 类型,与 IWebElement 基本相同,但不能与 html 交互,即。它不能点击等。

所以我想让消息说 NameInput 与 AgeInput 不一致

4

1 回答 1

1

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.

于 2013-10-29T16:21:59.490 回答