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

Michael Ray Lovett
  • 6,668
  • 7
  • 27
  • 36

2 Answers2

0

Selenium has two types of wait commands

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

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

The Example from:
How can I ask the Selenium-WebDriver to wait for few seconds in Java? shows something which you may be able to use

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

Also check http://selenium.googlecode.com/svn/trunk/docs/api/java/org/openqa/selenium/support/ui/FluentWait.html for more details.

Community
  • 1
  • 1
Lt_Shade
  • 590
  • 1
  • 7
  • 18
0

Since those elements already exist, if you use findElement() on those elements, then you'll avoid StaleReferenceException's, and you'll be fine.

Your test flow would look something like this (mind you this is using the framework found here

@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.. 
    }
}

Using the framework there, it's much easier, and handles it's own waits, as well as accounts for ajax. however if you prefer vanilla -

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.
}

An alternative to these two solutions, would be to use WebDriverWait's.. In your case, it'd be something like...

WebDriverWait.until(ExpectedConditions.textPresentIn(By.id("existingId"), "some text you'd expect"));
ddavison
  • 28,221
  • 15
  • 85
  • 110
  • Any idea how to do that in Python? I have WebDriverWait(ff, 10).until(expected_conditions.text_to_be_present_in_element(By.ID("tp0_fuel_price"), "40")) by By.id is not a function in Python's Selenium as it appears it is in Java. In short, I can't figure out how to specify the "locator" parameter to the text_to_be_present_in_element function.. – Michael Ray Lovett Oct 10 '13 at 19:23
  • I think I have my own answer. Here seems to be the way to do it: WebDriverWait(ff, 5).until(expected_conditions.text_to_be_present_in_element([By.ID,"tp0_fuel_price"], "3.40")) – Michael Ray Lovett Oct 11 '13 at 11:29