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

Wednesday, 18 September 2013

Step Object as pattern

Step is a set of sequential page interactions wrapped into a business meaning

  1. Should never be a verification / assertion 
  2. Should be used as a preconditions provider for test data / state
  3. Do not overlap steps, make them sequential 
  4. Follow principles of public API creation 
  5. Try to design Step API beforehand depending on a business logic.
what else?