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

1 Answers

Rafał Laskowski On Best Solutions

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