src/main/java/com/xceptance/neodymium/util/SelenideAddons.java
/**
     * Executes the given code at least once but potentially multiple times as long as a
     * {@link StaleElementReferenceException} occurs.
     * <p>
     * Attention: Since the SelenideElement class implements the InvocationHandler interface you have to make sure that
     * the element is retrieved in order to provoke a StaleElementReferenceException. You can do this by calling a
     * should function that uses a condition.
     * </p>
     * <p>
     * The following settings can be configured within the Neodymium configuration to tune the retry behavior:
     * </p>
     * <ul>
     * <li>neodymium.selenideAddons.staleElement.retry.count (default 3 retries)</li>
     * <li>neodymium.selenideAddons.staleElement.retry.timeout (default 500ms pause between retries)</li>
     * </ul>
     * <p>
     * <b>Example:</b>
     * </p>
     * 
     * <pre>
     * SelenideAddons.$safe(() -&gt; {
     *     return $("selector").should(exist);
     * });
     * </pre>
     *
     * @param code
     *            the code to run
     * @return the element of the execution or any exception that might bubble up
     */
Executes the given code at least once but potentially multiple times as long as a
StaleElementReferenceException occurs.

Attention: Since the SelenideElement class implements the InvocationHandler interface you have to make sure that
the element is retrieved in order to provoke a StaleElementReferenceException. You can do this by calling a
should function that uses a condition.


The following settings can be configured within the Neodymium configuration to tune the retry behavior:


neodymium.selenideAddons.staleElement.retry.count (default 3 retries)
neodymium.selenideAddons.staleElement.retry.timeout (default 500ms pause between retries)


Example:



SelenideAddons.$safe(() -> {
return $("selector").should(exist);
});


@param code
the code to run
@return the element of the execution or any exception that might bubble up
Executes the given code at least once but potentially multiple times as long as a StaleElementReferenceException occurs . Attention : Since the SelenideElement class implements the InvocationHandler interface you have to make sure that the element is retrieved in order to provoke a StaleElementReferenceException . You can do this by calling a should function that uses a condition . The following settings can be configured within the Neodymium configuration to tune the retry behavior : neodymium . selenideAddons . staleElement . retry . count ( default 3 retries ) neodymium . selenideAddons . staleElement . retry . timeout ( default 500ms pause between retries ) Example : SelenideAddons . $safe (( ) - > { return $ ( " selector " ) . should ( exist ) ; } ) ; @param code the code to run @return the element of the execution or any exception that might bubble up
public static SelenideElement $safe(final Supplier<SelenideElement> code)
    {
        final int maxRetryCount = Neodymium.configuration().staleElementRetryCount();
        int retryCounter = 0;
        while (retryCounter <= maxRetryCount)
        {
            try
            {
                return code.get();
            }
            catch (final Throwable t)
            {
                if (isThrowableCausedBy(t, StaleElementReferenceException.class))
                {
                    retryCounter++;
                    if (retryCounter > maxRetryCount)
                    {
                        throw t;
                    }
                    else
                    {
                        AllureAddons.addToReport("StaleElementReferenceException catched times: \"" + retryCounter + "\".", retryCounter);
                        Selenide.sleep(Neodymium.configuration().staleElementRetryTimeout());
                    }
                }
                else
                {
                    throw t;
                }
            }
        }
        return null;
    }
public static SelenideElement $safe ( final Supplier < SelenideElement > code ) { final int maxRetryCount = Neodymium . configuration ( ) . staleElementRetryCount ( ) ; int retryCounter = 0 ; while ( retryCounter <= maxRetryCount ) { try { return code . get ( ) ; } catch ( final Throwable t ) { if ( isThrowableCausedBy ( t , StaleElementReferenceException . class ) ) { retryCounter ++ ; if ( retryCounter > maxRetryCount ) { throw t ; } else { AllureAddons . addToReport ( "StaleElementReferenceException catched times: \"" + retryCounter + "\"." , retryCounter ) ; Selenide . sleep ( Neodymium . configuration ( ) . staleElementRetryTimeout ( ) ) ; } } else { throw t ; } } } return null ; }
Executes the given code at least once but potentially multiple times as long as a
 {@link StaleElementReferenceException} occurs.
Executes the given code at least once but potentially multiple times as long as a { @link StaleElementReferenceException } occurs .
// fail // not the kind of error we are looking for // never get here