2017-06-05 81 views
2

爲什麼我的等待方法不等到30秒?爲什麼我的等待方法不等到30秒?

如果方法找不到元素,我的方法應該在30秒後超時,目前返回true或false,沒有超時,有什麼想法?

public boolean WaitUntilWebElementIsVisiblePredicate(WebElement element) { 
FluentWait<WebElement> wait = new FluentWait<WebElement>(element) 
     .withTimeout(30, TimeUnit.SECONDS) 
     .pollingEvery(100, TimeUnit.MILLISECONDS) 
     .ignoring(NoSuchFieldException.class); 

Function<WebElement, Boolean> f = new Function<WebElement, Boolean>() { 
    @Override 
    public Boolean apply(WebElement element) { 
     if (!element.isDisplayed()) { 
      System.out.println("Method failed: WaitUntilWebElementIsVisiblepPredicate, using locator: " + element.toString()); 
      return false; 
     } 
     System.out.println("Element visible, using method: WaitUntilWebElementIsVisiblepPredicate, Locator: " + element.toString()); 
     return true;     
    } 
}; 
return wait.until(f); 
} 

回答

0

一旦apply(WebElement element)條件返回false,在pollingEvery定義的代碼等待的時間。然後,將實際開始時間(由FluentWait在開始時捕獲)與當前時間進行比較。如果wait起始時間與當前時間的差值小於withTimeout(30, TimeUnit.SECONDS)中設置的時間,則函數f將再次執行,直到timeout expirescondition becomes true

所以IMO,如果它早於超時時間返回,apply(WebElement element)似乎已返回true

看看方法簽名here

+0

感謝您的評論,我甚至當該方法返回false時,它dosnt保持輪詢,並在等待最大時間30秒超時過期 – Gbru

+0

我沒有看到這個代碼中的任何錯誤,除非你正在returing'返回wait.until(f);'到布爾函數,但我猜'直到'方法返回類型是WebElement。 – YuVi

+0

我唯一的問題是我需要傳遞一個WebElement(id定位器)作爲參數 – Gbru