隐式、显式和 fluentwait 之间的差异 [英] Differences between impilicit, explicit and fluentwait

查看:89
本文介绍了隐式、显式和 fluentwait 之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

implicitwait()explicitwait()fluentwait() 之间的确切区别是什么?能否举例说明?

What are the exact differences between implicitwait(), explicitwait() and fluentwait()? Could you explain with examples?

推荐答案

我发布了 一篇关于此的博客文章,我想我提供了一些其他答案遗漏的细节.

I posted a blog article about this, and I think I provide a few very details that these other answers missed.

隐式等待:在隐式等待期间,如果 Web Driver 由于其可用性而无法立即找到它,WebDriver 将定期轮询 DOM(间隔为 0.5 秒或取决于在驱动程序浏览器实现上),直到达到默认的隐式最大等待时间.一旦指定的隐式等待最大时间结束,它将尝试在抛出 WebDriverException(例如 NoSuchElementException)之前的最后一次再次搜索元素.默认设置为 0,这意味着对 driver.findElement 的调用不需要轮询 DOM,如果元素确实存在,它将在 0-999 毫秒内立即返回,否则会抛出一个NoSuchElementException 如果它在同一时间段内不存在.要覆盖默认的最大时间,请执行以下操作:

Implicit Wait: During an Implicit wait, if the Web Driver cannot find it immediately because of its availability, the WebDriver will periodically poll the DOM (at an interval of 0.5 seconds or depending on the driver-browser implementation) until the default implicit max wait time is reached. Once the specified implicit wait max time is over, it will try to search the element once again the last time before throwing a WebDriverException such as NoSuchElementException. With the default setting of 0, meaning that a call to driver.findElement will not need to poll the DOM and it will immediately return in 0–999 milliseconds if the element actually does exist or it will throw a NoSuchElementException if it doesn’t exist in the same time period. To override the default max time, do it like this:

driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

显式等待:可能会出现特定元素加载时间超过一秒或更长时间的情况.在这种情况下,您绝对不想设置一个巨大的隐式等待时间,因为如果您这样做了,那么您的浏览器将等待与每个驱动程序调用相同的最大时间来查找元素.因此,您可能会注意到测试性能略有下降.

Explicit Wait: There can be instances when a particular element takes more than a second to load, or longer. In that case you definitely do not want to set a huge implicit wait time, because if you do, then your browser is going to wait up to the same max time for every driver call to find an element. Because of this you would likely notice a minor decline in test performance.

为了避免这种情况,您可以简单地仅在所需元素上定义单独的等待时间.通过遵循此规则,您的浏览器隐式等待时间对于每个驱动程序调用查找元素来说会很短,而对于一个特定元素,视具体情况而定,它可能会很长.

To avoid that situation you can simply define a separate wait time on the required element only. By following this rule, your browser implicit wait time would be short for every driver call to find an element and it could be large for one specific element on a case by case basis.

显式等待总是首先定义 FluentWait,例如 WebDriverWait 对象,然后通常使用预期条件来解决等待.

An explicit wait always first defines a FluentWait, such as a WebDriverWait object and then usually uses an expected condition to resolve the wait.

WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.id("aId")));

Fluent Wait: 假设您有一个元素,有时仅在 1 秒内出现,有时需要几分钟才能出现.在这种情况下,最好使用 FluentWait 定义显式等待,因为这将一次又一次地尝试查找元素,直到找到它或直到最终计时器用完为止.WebDriverWait 是 Fl​​uentWait 的一种,因为 WebDriverWait 扩展了 FluentWait 并具有 FluentWait 类的所有功能,例如能够调整 DOM 轮询间隔、忽略异常.

Fluent Wait: Let’s say you have an element which sometime appears in just 1 second and some times it takes minutes to appear. In that case it is better to define a explicit wait using FluentWait, as this will try to find element again and again until it find it or until the final timer runs out. A WebDriverWait is a type of FluentWait since WebDriverWait extends FluentWait and has all the capabilities of the FluentWait class, such as being able to adjust the DOM polling interval, ignore exceptions.

FluentWait<WebDriver> wait = new FluentWait<WebDriver>(driver)
            .withTimeout(timeoutSeconds, TimeUnit.SECONDS)
            .pollingEvery(500, TimeUnit.MILLISECONDS)
            .ignoring(NoSuchElementException.class);

这篇关于隐式、显式和 fluentwait 之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆