2012-11-27 42 views
6

我正在做硒測試的第一次。在主頁上,我調用了一些AJAX,並且我希望Selenium等待元素加載完成。我不知道它的工作原理,但我只是鍵入硒和waitForCondition可以選擇。Selenium waitForCondition

我無所謂,我選擇它總是返回「假」。我現在如果waitForCondition即使工作?

我該如何測試它是否有效? 這個代碼我做錯了什麼?

selenium.waitForCondition("//input[@name='Report'", "3000"); 
selenium.waitForCondition("//*[@id='MyTable']", "3000"); 
selenium.waitForCondition("css=.someClass2", "3000"); 

如果我通過自己的類實現 - 它返回 「真」

private boolean isElementPresent(By by) { 
    try { 
     driver.findElement(by); 
     return true; 
    } catch (NoSuchElementException e) { 
     return false; 
    } 
} 

isElementPresent(By.xpath( 「// * [@ ID = 'MyTable的']」)) - 返回「 true「

回答

3

waitForCondition僅適用於Javascript調用,不適用於等待元素加載。

你有什麼isElementPresent是好的。我會等待明確的結合它是關於當元件被實際加載並在屏幕上呈現一個位更準確:

http://seleniumhq.org/docs/04_webdriver_advanced.html

+0

好,這項工作。 是否有你在JAVA中使用的例子。我認爲這是我每次使用它時都需要編寫的代碼。 WebElement myDynamicElement =(新WebDriverWait(驅動程序,10))。直到(新ExpectedCondition (){ \t \t \t \t @Override \t \t \t \t公共WebElement申請(webdriver的d){ \t \t \t \t \t return d.findElement(By.id(「MyTable」)); \t \t \t \t}});' – boje

+0

@Arran此解決方案作品。我正在上調它。 Thanx解決方案。 – RNS

0

Aaran提到你正確的文檔Selenium WebDriver waits

你可以看到他們也寫了ExpectedConditions類。這包含ExpectedCondition類的幾個有用的實現,比如在「is element present one」,ExpectedConditions.presenceOfElementLocated。

下面是使用它的一個例子:

WebDriver driver = new ChromeDriver(); 
driver.get("http://www.degraeve.com/reference/simple-ajax-example.php"); 

driver.findElement(By.name("word")).sendKeys("bird is the word");; 
driver.findElement(By.cssSelector("input[type='button']")).click(); 
WebDriverWait driverWait = new WebDriverWait(driver, 10); 
WebElement dynamicElement = driverWait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("#result p"))); 
System.out.println(dynamicElement.getText()); 

如果你覺得它太冗長,你爲什麼不只是重構它,並提取其將接受元素定位和webdriver的,並返回一個函數你的元素?

接受ExpectedCondition實例的DriverWait.until()類似於傳遞謂詞函數的方法,只是通過類或文檔示例中的匿名嵌套類進行傳遞,因爲在Java下,您無法發送函數。

您傳遞的ExpectedCondition「函數」也會返回一個值,如果您正在等待某個元素的條件(或來自WebDriver的某個其他值),則返回該值,因此返回它將爲您節省一些額外呼叫。

+0

由於某些原因_ExpectedConditions_不知道。 Text:ExpectedConditions無法解析 – boje

+0

由於某種原因,我沒有_org.openqa.selenium.support.ui.ExpectedConditions_哪裏可以得到正確的jar文件?我確實有org.openqa.selenium.support.ui.ExpectedCondition(不包含「S」) – boje

+0

您使用的是什麼版本的硒?我通過maven在2.25.0上測試了代碼,但可以從http://seleniumhq.org/download/ –

1

你可以這樣說:

selenium.waitForCondition("selenium.isElementPresent(\"//input[@name='Report']\")", "30000"); 

這將等待被加載至30秒的元素。

+0

這不起作用。根據@Arran,_waitForCondition_僅適用於Javascript調用。這是通過JAVA和IE8完成 – boje

+0

它應該工作..對於測試基於AJAX的應用程序,我總是喜歡這種方法..參考鏈接http://www.seleniumwiki.com/selenium-rc/using-selenium-waitforcondition/ –

1

希望這對你的作品

new WebDriverWait(driver, 30).until(new ExpectedCondition<Boolean>() { 
public Boolean apply(WebDriver driver) { 
    JavascriptExecutor js = (JavascriptExecutor) driver; 
    return (Boolean) js.executeScript("return jQuery.active == 0"); 
} 

});

這將檢查jQuery庫是否有任何活動的AJAX請求30秒。

2

C#

你可以這樣說:

首先可以爲條件設置超時值。

然後您可以使用該條件。

var Wait = new WebDriverWait(GlobalDriver, TimeSpan.FromMinutes(1)); 
Wait.Until(ExpectedConditions.PresenceOfAllElementsLocatedBy(By.XPath("xPath")))); 

OR

Wait.Until(driver => driver.FindElement(By.XPath("xPath"))); 

這就是所有。

0

嘗試這一次,

await().atMost(10, SECONDS).until(() -> driver.findElements(By.id("elementId")).size() >1);