Saturday 3 December 2011

Testing asynchronous actions using WebDriver (f.e. AJAX)

Here is a code snippet showing how to test ajax actions using WebDriver. You can use WebDriverWait.until method defining condition that checks if web page's structure has changed. In the WebDriverWait constructor you have to define timeout in seconds. If this time passes then method ends with an exception.
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(new ExpectedCondition() {
  public Object apply(Object input) {
      try {
        WebElement elem = driver.findElement(By.id(changedElem));
        boolean condition = Here you can state condition
        return condition;
      } catch (StaleElementReferenceException e) {
        return false;
      }
    }
  });

No comments: