2016-12-21 6 views
1

私はこのトピックについて多くの場所で検索していましたが、私はここで同様の答えを見つけましたが、もう少ししたいです。ISearchContextがクリック可能なときを知る必要があります

マイコード:それは、時にはそれが前にクリックされていますボタンが利用でき得るということです

var element = myWebDriver.FindElement("User_Login"); 
var finalelement = element.FindElement(By.XPath("//button[contains(text(),'OK')]")); 

の問題は、私は、そのための明示的な待ち時間を作りたいです。今、私はこれらを持っている:

public static class WebDriverExtensions 
    { 
     public static IWebElement NGFindElementWhenClickable(this IWebDriver driver, By by) 
     { 
      var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(int.Parse(ConfigurationManager.AppSettings["WebDriverExplicitWait"]))); 
      return wait.Until(ExpectedConditions.ElementToBeClickable(by)); 
     } 

     public static IWebElement NGFindElementWhenVisible(this IWebDriver driver, By by) 
     { 
      var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(int.Parse(ConfigurationManager.AppSettings["WebDriverExplicitWait"]))); 
      return wait.Until(ExpectedConditions.ElementIsVisible(by)); 
     } 

     public static IWebElement NGElementExist(this IWebDriver driver, By by) 
     { 
      var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(int.Parse(ConfigurationManager.AppSettings["WebDriverExplicitWait"]))); 
      return wait.Until(ExpectedConditions.ElementExists(by)); 
     } 

     public static void NGElementInvisible(this IWebDriver driver, By by) 
     { 
      var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(int.Parse(ConfigurationManager.AppSettings["WebDriverExplicitWait"]))); 
      wait.Until(ExpectedConditions.InvisibilityOfElementLocated(by)); 
     } 

私は、そのクラスでこのような何かを追加したい:

public static IWebElement NGFindElement(this ISearchContext context, By by) 
     { 
      var wait = new DefaultWait<ISearchContext>(context); 
      wait.Timeout = TimeSpan.FromSeconds(int.Parse(ConfigurationManager.AppSettings["WebDriverExplicitWait"]));    
      return wait.Until(ctx => { 
       var elem = ctx.FindElement(by); 
       return elem; 
      }); 
     } 

...しかし、明示的に待機(条件待ち)で。

+0

をすでに 'NGFindElementWhenClickable'は、メソッドを使用して問題を解決しましたExpectedConditions.ElementToBeClickableを使用して(クリック可能にする)条件のために。それとも、私はその質問を誤解しましたか? –

+0

@Naveen Charlieが追加したいメソッドは、NGFindElementWhenClickableで動作しないドライバまたは要素になるようにSearchContextを取ります。 – jibbs

答えて

0

私はこの問題を解決するため、私はここに私はそれをやったか、それは:)少し遅く

知っている:あなたが待っているよう

public static IWebElement NGFindElement(this ISearchContext context, By by, int timeOut = 0) 
{ 
     //-> If the timeOut remain the same (= 0) use the time defined on "ConfigurationManager.AppSettings["WebDriverExplicitWait"]" 
     //-> on the other hand if the value is defined by the user, will use the defined value 
     int totalTime = (timeOut == 0) ? int.Parse(ConfigurationManager.AppSettings["WebDriverExplicitWait"]) : timeOut; 
     var wait = new DefaultWait<ISearchContext>(context); 
     wait.Timeout = TimeSpan.FromSeconds(totalTime); 
     //-> To avaid exceptions related to page refreshed 
     wait.IgnoreExceptionTypes(typeof(StaleElementReferenceException)); 
     return wait.Until(ctx => (ctx.FindElement(by).Displayed && ctx.FindElement(by).Enabled) ? ctx.FindElement(by) : null);    
} 
関連する問題