微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

C# – Selenium – 重试属性不与Selenium超时一起使用

我从这篇文章获取了以下自定义RetryAttributeNUnit retry dynamic attribute.它工作正常但是当我在Selenium中出现超时错误时它无效.

@H_502_5@webdriverwait wait = new webdriverwait(driver, TimeSpan.FromSeconds(5));
            wait.Until(ExpectedConditions.ElementToBeClickable(element));

重试自定义属性

@H_502_5@/// <summary>
/// RetryDynamicAttribute may be applied to test case in order
/// to run it multiple times based on app setting.
/// </summary>
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = false)]
public class RetryDynamicAttribute : RetryAttribute {
    private const int DEFAULT_TRIES = 1;
    static Lazy<int> numberOfRetries = new Lazy<int>(() => {
        int count = 0;
        return int.TryParse(ConfigurationManager.AppSettings["retryTest"], out count) ? count : DEFAULT_TRIES;
    });

    public RetryDynamicAttribute() :
        base(numberOfRetries.Value) {
    }
}

然后应用自定义属性.

@H_502_5@[Test]
[RetryDynamic]
public void test() {
    //.... 
}

怎么解决这个问题?

解决方法:

根据这里的文件

NUnit Docs Retry Attribute

If a test has an unexpected exception, an error result is returned and
it is not retried. Only assertion failures can trigger a retry. To
convert an unexpected exception into an assertion failure, see the
07001.

强调我的.

The related 07002 simply asserts that the delegate
does not throw an exception.

如果不期望异常,您需要捕获异常并导致断言失败.

@H_502_5@webdriverwait wait = new webdriverwait(driver, TimeSpan.FromSeconds(5));
Assert.That(() => {
         wait.Until(ExpectedConditions.ElementToBeClickable(element));
     }, Throws.nothing);

所以上面的代码只是说执行动作而不应该期待异常.如果抛出异常,那么它是一个失败的断言.如果将属性应用于测试,则将执行重试.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐