Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
921 views
in Technique[技术] by (71.8m points)

selenium - Differences between impilicit, explicit and fluentwait

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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.

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: 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);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...