2016-03-29 16 views
0

Şu anda IWebDriver işlevselliğine ihtiyacım olmadığı şeyleri beklemek için Selenium WebDriverWait kullanıyorum. Benim kod şöyle görünür:Bir temsilci belirli bir değer döndürmek için nasıl beklerim?

public static T WaitForNotNull<T>(this IWebDriver driver, Func<T> func) 
{ 
    var result = default(T); 

    var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10)); 
    wait.Until(d => (result = func()) != null); 

    return result; 
} 

public static void WaitForNull<T>(this IWebDriver driver, Func<T> func) 
{ 
    var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10)); 
    wait.Until(d => func() == null); 
} 

Ben WebDriverWait yerine kullanabileceğiniz .NET benzer bir yapı var mı?

+0

Senkronize beklemeyi mi, senkronize olmayan beklemeyi mi arıyorsunuz? Eşzamansız için asenkronize etmek için [Task.Wait] (https://msdn.microsoft.com/en-us/library/dd235635 (v = vs.110) .aspx) 'i kullanabilirsiniz ve async anahtar kelimeleriniz var ve –

+0

Evet, buna "while" döngüsü denir. Bununla ilgili herhangi bir sorun görüyor musunuz? – nvoigt

+0

sadece Until yönteminin decompiled koduna bakıp aynı –

cevap

1
kullanabilirsiniz

cevap

No

olduğunu

.NET Framework'de böyle bir şey yok, böyle bir yöntemi kendiniz yazmanız gerekecek.

+0

Bu harika. – Drutten

0

Bu, özgünüme kadar olan uygulamadır (source). IMO'yu geliştirebilirsiniz.

Ve (UI iş parçacığı durumunda) çağıran iş parçacığı engellemek istemiyorsanız, kolayca zaman uyumsuz \ bekliyoruz deseni

public TResult Until<TResult>(Func<T, TResult> condition) 
    { 
     if (condition == null) 
     { 
      throw new ArgumentNullException("condition", "condition cannot be null"); 
     } 

     var resultType = typeof(TResult); 
     if ((resultType.IsValueType && resultType != typeof(bool)) || !typeof(object).IsAssignableFrom(resultType)) 
     { 
      throw new ArgumentException("Can only wait on an object or boolean response, tried to use type: " + resultType.ToString(), "condition"); 
     } 

     Exception lastException = null; 
     var endTime = this.clock.LaterBy(this.timeout); 
     while (true) 
     { 
      try 
      { 
       var result = condition(this.input); 
       if (resultType == typeof(bool)) 
       { 
        var boolResult = result as bool?; 
        if (boolResult.HasValue && boolResult.Value) 
        { 
         return result; 
        } 
       } 
       else 
       { 
        if (result != null) 
        { 
         return result; 
        } 
       } 
      } 
      catch (Exception ex) 
      { 
       if (!this.IsIgnoredException(ex)) 
       { 
        throw; 
       } 

       lastException = ex; 
      } 

      // Check the timeout after evaluating the function to ensure conditions 
      // with a zero timeout can succeed. 
      if (!this.clock.IsNowBefore(endTime)) 
      { 
       string timeoutMessage = string.Format(CultureInfo.InvariantCulture, "Timed out after {0} seconds", this.timeout.TotalSeconds); 
       if (!string.IsNullOrEmpty(this.message)) 
       { 
        timeoutMessage += ": " + this.message; 
       } 

       this.ThrowTimeoutException(timeoutMessage, lastException); 
      } 

      Thread.Sleep(this.sleepInterval); 
     } 
    } 
İlgili konular