Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Controlling poll frequency of browser.wait() (Fluent Wait)

The Story:

In Java selenium language bindings there is a FluentWait class, that allows to tightly control how the expected condition would be checked:

Each FluentWait instance defines the maximum amount of time to wait for a condition, as well as the frequency with which to check the condition. Furthermore, the user may configure the wait to ignore specific types of exceptions whilst waiting, such as NoSuchElementExceptions when searching for an element on the page.

In other words, it's possible to change the polling interval in which the expected condition check is applied, which is by default 500ms. Plus, it's possible to set exceptions to ignore.

It is also possible in Python, there are relevant poll_frequency and ignored_exceptions arguments to WebDriverWait class.

The Question:

Is it possible to control the poll frequency in which the expected condition is verified when using browser.wait() in Protractor/WebDriverJS?


According to the browser.wait() documentation, there are only 3 possible arguments: a function which is an expected condition, a timeout value and an optional timeout error message. I hope there is a different setting or way to change the poll frequency.

like image 245
alecxe Avatar asked Nov 14 '15 00:11

alecxe


People also ask

What is polling time in fluent wait?

Polling time is the frequency time on which it will check that particular element. We can specify exception for ignoring it.

What is poll frequency Selenium Python?

WebDriverWait by default calls the ExpectedCondition every 500 milliseconds, which is called poll frequency, until it returns successfully.

What is the syntax of fluent wait?

Fluent Wait syntax:xpath("/html/body/div[1]/section/div[2]/div/div[1]/div/div[1]/div/div/div/div[2]/div[2]/div/div/div/div/div[1]/div/div/a/i")); We have created a new function to identify the Web Element on the page.


2 Answers

With the help of @Kirill S., after the further research and inspecting the WebdriverJS source code, I can conclude that there is no such thing as "poll frequency" in javascript selenium bindings. The interval between subsequent condition check calls cannot be configured - it performs the check as quick as possible.

This is not the same as in, for instance Python or Java selenium bindings, where there is a configurable timeout between the expected condition state checks. By default, it would wait for 500ms before the next check:

WebDriverWait by default calls the ExpectedCondition every 500 milliseconds until it returns successfully. A successful return is for ExpectedCondition type is Boolean return true or not null return value for all other ExpectedCondition types.

like image 56
alecxe Avatar answered Sep 28 '22 09:09

alecxe


Well, according to the docs which I read, there doesn't seem to be an actual method that mirrors the functionality of the FluentWait. Instead, this snippet can be used to change poll frequency and it escapes almost all the exceptions (almost all).

def wait_until(predicate, timeout_seconds, period=0.25, show_log=True):
    """
    @summary Call repeatedly the predicate until it returns true or timeout_seconds passed.
    @param predicate: a condition, modelized as a callable,  that will valued either as True or False
    @param timeout_seconds: the timeout in second
    @param period: the time to sleep between 2 calls to predicate. Defaults to 0.25s.
    @return True if a call to predicate returned True before timeout_seconds passed, else False
    """
    if show_log:
        myLogger.logger.info("waiting until predicate is true for {end} seconds".format(end=timeout_seconds))
    ultimatum = time.time() + timeout_seconds
    while time.time() < ultimatum:
        myLogger.logger.debug("checking predicate for wait until : {spent} / {end}".format(spent=str(time.time() - (ultimatum - timeout_seconds)), end=timeout_seconds))
        try:
            if predicate():
                return True
            time.sleep(period)
        except Exception as e:
            myLogger.logger.warn("Exception found: {}".format(e))
    return False

Consequently, the predicate can be lambda that you pass which validates the state of the WebElement under question.

like image 28
Abhinav Avatar answered Sep 28 '22 09:09

Abhinav