mosaad
mosaad

Reputation: 2371

unstable tests due to stale element reference

Similar to the question here. My tests are unstable. Try and catch solution will not work here. Any best practices to solve this?

Upvotes: 0

Views: 250

Answers (2)

lauda
lauda

Reputation: 4173

Maybe there is a coding issue, you search for an element before and you are using that object later.

You should not get stale element if the page was not changed/reloaded.

Upvotes: 0

Breaks Software
Breaks Software

Reputation: 1761

As @lauda says above, the StaleElementReferenceException is a clear indication that the page you are interacting with has changed since you last obtained the object reference. You must carefully review what is happening between those two points in the code to determine what has caused the page to reload. That will help you decide how to handle the situation in the context of your automation framework.

A few suggestions:

  • instead of using @FindBy annotations, create CSS or XPath selector variables for your elements, then wherever you need to interact with an element use findElementBy to get the target WebElement just before you need to interact with it. (You may still need to wait for an AJAX event to complete first)
  • refactor your framework code so that the methods that are taking some action will wait for the page to reload, or for a relevant AJAX action to complete, before you get the target WebElement (e.g. after my test system performance dropped recently I found a place in my framework code that needed to wait for a table on the page to reload, so I used WebDriverWait to wait for the loading spinner to go away before I interacted with items in the table.)

Upvotes: 2

Related Questions