簡體   English   中英

C# - Selenium - 重試屬性不與Selenium超時一起使用

[英]C# - Selenium - Retry Attribute not working with Selenium timeout

我從這篇文章中獲取了以下自定義RetryAttributeNUnit重試動態屬性 它工作正常但是當我在Selenium中出現超時錯誤時它無效。

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

重試自定義屬性:

/// <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) {
    }
}

然后應用自定義屬性。

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

怎么解決這個問題?

另一種解決方案是實現自己的RetryAttribute以捕獲WebDriver異常。 這樣您就不必改變測試:

[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = false)]
public class RetryAttributeEx : PropertyAttribute, IWrapSetUpTearDown
{
    private int _count;

    public RetryAttributeEx(int count) : base(count) {
        _count = count;
    }

    public TestCommand Wrap(TestCommand command) {
        return new RetryCommand(command, _count);
    }

    public class RetryCommand : DelegatingTestCommand {

        private int _retryCount;

        public RetryCommand(TestCommand innerCommand, int retryCount)
            : base(innerCommand) {
            _retryCount = retryCount;
        }

        public override TestResult Execute(TestExecutionContext context) {

            for (int count = _retryCount; count-- > 0; ) {

                try {
                    context.CurrentResult = innerCommand.Execute(context);
                }
                catch (WebDriverTimeoutException ex) {
                    if (count == 0)
                      throw;

                    continue;
                }

                if (context.CurrentResult.ResultState.Status != ResultState.Failure.Status)
                    break;

                if (count > 0)
                    context.CurrentResult = context.CurrentTest.MakeTestResult();
            }

            return context.CurrentResult;
        }
    }

}

根據這里的文件

NUnit文檔重試屬性

如果測試具有意外異常,則會返回錯誤結果,並且不會重試該錯誤。 只有斷言失敗才能觸發重試 要將意外異常轉換為斷言失敗,請參閱ThrowsConstraint

強調我的。

相關的ThrowsNothingConstraint只是斷言委托不會拋出異常。

如果不期望異常,您需要捕獲異常並導致斷言失敗。

WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(5));
Assert.That(() => {
         wait.Until(ExpectedConditions.ElementToBeClickable(element));
     }, Throws.Nothing);

所以上面的代碼只是說執行動作而不應該期待異常。 如果拋出異常,那么它是一個失敗的斷言。 如果將屬性應用於測試,則將執行重試。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM