0

I see many examples of waiting for html controls to become "present" , ie as a result of an ajax call, java event handler, and so on.

But in my case, my ajax code does not instantiate, or make visible, a new control; it repopulates existing controls with new values.

What I want to do is implicitly wait for these values to "show up", but I can't tell if this is possible in Selenium 2.0?

Michael

4

2 回答 2

0

Selenium 有两种等待命令

1 driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

2 WebDriverWait.until(condition-that-finds-the-element)

示例来自:
如何让 Selenium-WebDriver 在 Java 中等待几秒钟? 显示您可以使用的东西

public WebElement fluentWait(final By locator) {
Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
        .withTimeout(30, TimeUnit.SECONDS)
        .pollingEvery(5, TimeUnit.SECONDS)
        .ignoring(NoSuchElementException.class);
}

另请查看http://selenium.googlecode.com/svn/trunk/docs/api/java/org/openqa/selenium/support/ui/FluentWait.html了解更多详情。

于 2013-10-10T13:54:55.547 回答
0

由于这些元素已经存在,如果你findElement()在这些元素上使用,那么你将避免 StaleReferenceException,你会没事的。

您的测试流程看起来像这样(请注意,这是使用此处找到的框架

@Config(url="http://systemunder.test", browser=Browsers.CHROME)
public class MyTest extends AutomationTest {
    @Test
    public void myTest() {
        click(By.id("somethingThatTriggersAjax")
        .validateText(By.id("existingId"), "test");  // this would work.. 
    }
}

使用那里的框架,它更容易,并处理它自己的等待,以及 ajax 帐户。但是,如果您更喜欢香草-

public void test() {
    WebElement element;
    element = driver.findElement(By.id("somthingThatTriggersAjax"));
    // now ajax has done something.
    element = driver.findElement(By.id("existingId")); // now this will be updated with the new element information.
}

这两种解决方案的替代方法是使用WebDriverWait's.. 在您的情况下,它类似于...

WebDriverWait.until(ExpectedConditions.textPresentIn(By.id("existingId"), "some text you'd expect"));
于 2013-10-10T13:56:47.197 回答