0

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

Community
  • 1
  • 1
mosaad
  • 2,276
  • 5
  • 27
  • 49
  • 1
    You need to make sure the page is fully loaded, sometimes the developer can use code that loads some things after the page is loaded the first time, doing a reload. – lauda Aug 09 '16 at 09:56
  • sometimes I am looping on a table and reading values which means nothing is being loaded but it throws the exception – mosaad Aug 09 '16 at 11:31
  • 1
    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. – lauda Aug 09 '16 at 11:41
  • yes you are right, in that case it was a coding issue – mosaad Aug 09 '16 at 11:58

2 Answers2

2

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.)
Breaks Software
  • 1,721
  • 1
  • 10
  • 15
0

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.

lauda
  • 4,153
  • 2
  • 14
  • 28