Wait, But Don’t Wait for Godart

When automate tests against dynamic web, we often run into many kinds of exceptions indicating that the element is not interact-able. While most exceptions are thrown because elements are not displayed on the screen yet, and we solve the problem via wait till it is shown, we don’t wait on the following scenarios:

Got StaleElementReferenceException
Root cause:the element no longer appears on the DOM of the page.

When to occur: A StaleElementException is thrown when the element you were interacting is destroyed and then recreated. Most web these days will move things about on the fly as the user interacts with it and this requires elements in the DOM to be destroyed and recreated, for example:

  • Elements being re-rendered and page is refreshed – A good example is that user chooses some option from the first drop down, then an ajax call is triggered, and the subsequent dropdown content is updated using info retrieved by the ajax call
  • JavaScript MVC frameworks – Many JavaScript MVC frameworks can update contents of a list or a control by rapidly tearing down and recreating the same element. There are times an element looks like only the text is updating, but in reality the entire element is torn down and rebuilt.
  • Elements in transition
  • Elements being moved or reordered – If an element disappears off the screen to be relocated, it’ll cause the original reference to go stale.

How to Handle

  • Re-locate the element for a new reference
  • Check explicitly that the page re-rendering/refresh is done, then interact with the element
    //Code to check whether or not Ajax call is done using jQUERY
    boolean ajaxIsComplete =(Boolean) ((JavascriptExecutor)driver ).executeScript(“return jQuery.active == 0”);

WebDriverException:Element is not clickable at point (xxx, xxx)
Root cause: The element is not visible to click.

Likely to happen when:

  • A play button won’t show until you hover over to its area.
  • Need to scroll down the page in order to make the element visible.
  • The element is covered by another layer such as running ad, a popup box etc.

How to address
For the first case, use Actions to hover over the box first:

WebElement element = driver.findElement(By(“element_path”));
Actions actions = new Actions(driver);
actions.moveToElement(element).click().perform();

For the second case, use JavascriptExecutor to scroll the page so that element is visible:

JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript(“scroll(250, 0)”); // if the element is on top.
jse.executeScript(“scroll(0, 250)”); // if the element is on bottom.

or:

JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript(“arguments[0].scrollIntoView()”, Webelement);

Then click on the element.

For the third case, make the page to wait for few seconds using WebDriverWait or FluentWait till the top layer running ad disappears, or dismiss the popup explicitly.

ElementNotVisibleException
Root cause: the element’s locator matches with more than 1 element; or the element is indeed not in not currently visible. Make your locator more specific, or make sure that the element is visible.

MoveTargetOutOfBoundsException
Indicates that the target provided to the actions move() method is invalid – outside of the size of the window.

Leave a Reply

Your email address will not be published. Required fields are marked *