控制browser.wait()的轮询频率(Fluent wait)

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

本文关键字:wait 频率 Fluent 控制 browser      更新时间:2023-09-26

故事:

在Java硒语言绑定中,有一个FluentWait类,它允许严格控制如何检查预期条件:

每个FluentWait实例都定义了等待的最大时间对于条件,以及检查的频率条件此外,用户可以将等待配置为忽略等待时的特定类型的异常,例如在页面上搜索元素时出现NoSuchElementException。

换句话说,可以更改应用预期条件检查的轮询间隔,默认情况下为500ms。此外,还可以将异常设置为忽略。

在Python中,WebDriverWait类也可能存在相关的poll_frequencyignored_exceptions参数。

问题:

在Protractor/WebDriverJS中使用browser.wait()时,是否可以控制轮询频率,以验证预期条件?


根据browser.wait()文档,只有3个可能的参数:一个是预期条件的函数、一个超时值和一个可选的超时错误消息。我希望有一个不同的设置或方式来改变投票频率。

在@Kirill S.的帮助下,经过对WebdriverJS源代码的进一步研究和检查,我可以得出结论:在javascript硒绑定中不存在"轮询频率"。无法配置后续条件检查调用之间的间隔-它会尽快执行检查。

这与中的不同,例如Python或Java selenium绑定,其中在预期的条件状态检查之间存在可配置的超时。默认情况下,它会等待500毫秒,然后再进行下一次检查:

WebDriverWait默认情况下每500调用一次ExpectedCondition毫秒,直到它成功返回。成功退货是为了ExpectedCondition类型为布尔返回true或非null返回值用于所有其他ExpectedCondition类型。

根据我阅读的文档,似乎没有一个实际的方法可以反映FluentWait的功能。相反,这个片段可以用于更改轮询频率,并且它可以跳过几乎所有的异常(几乎所有)。

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

因此,谓词可以是您传递的lambda,用于验证所讨论的WebElement的状态。