3

Could you please help me to understand the ExpectedConditions.refresh and ExpectedConditions.stalenessOf.

Fenio
  • 3,528
  • 1
  • 13
  • 27
Sai
  • 389
  • 6
  • 18

1 Answers1

4

ExpectedCondtion.referesh accepts ExpectedCondtion as an argument.

Let's suppose you have a text field element which is manipulated by the application and it's redrawn. Normally, you would get StaleElementReferenceException because when WebDriver invokes findElement method, it saves the REFERENCE to the object. If the object is redrawn, there reference to the object is no longer actual and StaleElementReferenceException is thrown.

ExpectedCondition.stalenessOf waits until the element is redrawn. This might be helpful to wait if DOM manipulation has occured. Then, you can find your element again and perform the operation (or use element created by PageFactory instead of refinding it).

However, the element might be manipulated many times (for example via jQuery calls of the front-end). In this case, waiting until the element is stale, and trying to find it, might throw StaleElementReferenceException anyway, because the element got stale AGAIN.

In that case, you can use ExpectedCondition.refresh(<my-expected-condition>). This will allow you to perform operations within the time frame, regardless of staleness of the element

Fenio
  • 3,528
  • 1
  • 13
  • 27
  • 1
    ExpectedCondition.stalenessOf does not wait until the element is redrawn, it waits until the current reference of the element is detached from the DOM. It may be redrawn after that has happened, but it also may not be redrawn. – Ardesco Apr 12 '19 at 08:16
  • I wrote that `StaleElementReferenceException` is thrown if the reference to the object is no longer actual. Redrawn of the object is just an example so OP can comprehend how this happens – Fenio Apr 12 '19 at 08:17
  • @Fenio ExpectedCondition.stalenessOf() waits until element becomes stale i.e. deattached from DOM.It does NOT wait until element is redrawn. – a Learner Feb 06 '22 at 15:46
  • @aLearner read the comments above – Fenio Feb 07 '22 at 08:09