隐式、显式和流式等待之间的区别流式、区别、隐式

2023-09-07 15:55:44 作者:跑到坟场去吓鬼

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 驱动程序由于其可用性而无法立即找到它,则 Web 驱动程序将定期轮询 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);