2017-06-08 13 views
3

次のカスタムRetryAttributeを持っています:NUnit retry dynamic attribute。それはうまく動作しますが、私はセレンのタイムアウトエラーが発生したとき、それは動作しません。C# - Selenium再試行属性が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() { 
    //.... 
} 

どのように解決できますか?

答えて

3

テストは予期しない例外がある場合は、ここでのドキュメント

NUnitのドキュメントRetry Attributeによると、エラー結果が返され、 それが再試行されていません。 アサーションエラーのみが、再試行をトリガーできます。 に予期しない例外をアサーションの失敗に変換するには、 ThrowsConstraintを参照してください。

強調ミーン。

関連ThrowsNothingConstraintは単にデリゲート が例外をスローしないことを主張します。

例外が検出されない場合は、例外をキャッチして、アサーションに失敗する必要があります。

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

したがって、上記のコードではアクションを実行すると記載されているため、例外が発生するとは限りません。例外がスローされた場合、それは失敗したアサーションです。属性がテストに適用された場合、再試行が実行されます。

3

WebDriverの例外をキャッチするには、もう1つの解決方法は自分自身のRetryAttributeを実装することです。この方法では、テストを変更する必要はありません。

[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 != ResultState.Failure) 
        break; 

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

      return context.CurrentResult; 
     } 
    } 

} 
関連する問題