2012-01-13 79 views
1

使用webdriver並測試使用gwt構建的應用程序。我想先聲明一個元素成爲現在。這個元素是動態加載的,並不是微不足道的測試。不過,我需要斷言元素不再存在。就像在它不再存在的那樣。我最初的做法是構建ExpectedCondition,然後等待條件,即 斷言元素不再存在

 ExpectedCondition<Boolean> e = new ExpectedCondition<Boolean>() { 
     public Boolean apply(WebDriver d) { 
      return d.findElements(By.cssSelector(someCSSpath)).size() == 0; 
    }}; 

    WebDriverWait wait = new WebDriverWait(driver, 15); 
    wait.until(e); 

I would have expected that would have waited for 15 seconds for the element to disappear from the dom, I would expected that it would poll every 500 milis. I would expect that at every poll the bool would be false as the element would be found until such time as it disappears and at which point the condition becomes true. What I find in my output is that indeed it polls every 500 milis however once the condition becomes true I actually wait for an entire minute before moving onto the next step. This seems incorrect behavior, am I missing something? Any suggestions?

+0

每該文檔[JavaDoc的用於WebDriverWait](http://selenium.googlecode.com/svn/trunk/docs/api/java/index.html)這個特定的構造函數有2個參數,所述第二一個是很長的,在幾秒鐘內。 – Kilthorn 2012-01-17 16:23:35

+0

你是否指定了隱式等待?如果是的話,你可能需要關閉這個條件,因爲findElements()會等待(在隱式等待期間)元素至少存在一次。 – 2012-04-09 23:40:41

回答

0

Try to use : ExpectedConditions.stalenessOf(..)以檢查元素是否已成功從DOM中刪除。適用於我。

0

你是否將「implicitlyWait」設置爲60秒?

driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS); 

當對象消失時,它會在返回之前60秒等待「findElements」方法。

我建議在運行「wait.until(e);」之前將「implicitlyWait」設置爲0。方法,然後將其設置回60秒。

0

如果你有WebElement的句柄,你可以使用它。

new WebDriverWait(driver, 2).until(ExpectedConditions.stalenessOf(webElement));