Saturday, 21 September 2013

Avoiding StaleElementReferenceException

Have you ever got this annoying exception when invoking any method from WebElement interface? 

If answer is positive it means that inner DOM of element is highly dynamic and is changing between the moment of getting webelement object and it's actual usage. Some people might complain that "tests are not stable" or even reuse popular phrase "Flakey Tests" ... but let's see how we can solve it
  protected Boolean isDisplayed(int index) {
    try {
      return driver.findElements(By.cssSelector(".listElement")).get(index).isDisplayed();
    } catch (StaleElementReferenceException e) {
      return isDisplayed(index);
making recursive call helps simulate some kind of wait for webdriver ready DOM

No comments:

Post a Comment