Fluent wait vs WebDriver wait
Asked Answered
R

5

11

I am confused between FluentWait and WebDriverWait.

FluentWait and WebDriverwait both uses the same features like ignoring exceptions, change polling time interval, expected conditions etc.

As per my understanding both implements the Wait interface. Additionally WebDriverWait extends FluentWait (which means all the functionalities are present also in WebDriverWait).

What are the extra features WebDriverWait holds that are not present in FluentWait?

Rolanderolando answered 22/11, 2016 at 23:9 Comment(0)
I
8

FluentWait and WebDriverWait both are the implementations of Wait interface.

The goal to use Fluent WebDriver Explicit Wait and WebDriver Explicit Wait is more or less same. However, in few cases, FluentWait can be more flexible. Since both the classes are the implementations of same Wait interface so more or less both have the same feature except The FluentWait has a feature to accept a predicate or a function as an argument in until method. On the other hand, WebDriverWait accepts only function as an ExpectedCondition in until method which restricts you to use a boolean return only.When you use Predicate in FluentWait, it allows you to return any Object from until method.

Look at here carefully: https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/support/ui/FluentWait.html#until-com.google.common.base.Predicate-

Examples: A FluentWait having Function as an argument in until with String return:

public void exampleOfFluentWait() {
        WebElement foo = driver.findElement(By.id("foo"));
        new FluentWait<WebElement>(foo)
            .withTimeout(10, TimeUnit.SECONDS)
            .pollingEvery(2, TimeUnit.SECONDS)
                    .until(new Function<WebElement, String>() {
                        @Override
                        public String apply(WebElement element) {
                            return element.getText();
                        }
                    });
    }

The Same FluentWait having Function with Boolean return as an argument in until method.

public void exampleOfFluentWait() {
            WebElement foo = driver.findElement(By.id("foo"));
            new FluentWait<WebElement>(foo)
                .withTimeout(10, TimeUnit.SECONDS)
                .pollingEvery(2, TimeUnit.SECONDS)
                        .until(new Function<WebElement, Boolean>() {
                            @Override
                            public Boolean apply(WebElement element) {
                                return element.getText().contains("foo");
                            }
                        });
        }

One more FluentWait with Predicate.

public void exampleOfFluentWithPredicate() {
    WebElement foo = driver.findElement(By.id("foo"));
    new FluentWait<WebElement>(foo)
        .withTimeout(10, TimeUnit.SECONDS)
        .pollingEvery(100, TimeUnit.MILLISECONDS)
                .until(new Predicate<WebElement>() {
                    @Override
                    public boolean apply(WebElement element) {
                        return element.getText().endsWith("04");
                    }
                });
}

Example of WebDriverWait:

public void exampleOfWebDriverWait() {
        WebElement foo = driver.findElement(By.id("foo"));

        new WebDriverWait(driver, 10)
        .pollingEvery(2, TimeUnit.SECONDS)
        .withTimeout(10, TimeUnit.SECONDS)
        .until(ExpectedConditions.visibilityOf(foo));
    }
Infantilism answered 23/11, 2016 at 7:19 Comment(4)
You can also use and define polling time in WebDriverWait.Rolanderolando
@DeepakKakkarHave changed my answer.Infantilism
@PriyanshuShekhar I am little skeptical with your answer, I think predicate does not support apply() method, it should be test() method. Secondly, predicate only returns boolean value but you mentioned it will return any object. Can you please clarify a bit on this?Dwinnell
@priyanshu, until method accepts Function<> parameter only. Predicate cant be used. wait interface has only one method - until(Function<T,K>)Comte
F
8

There is actually very little difference between two. According to WebDriverWait source code it says:

It will ignore instances of NotFoundException that are encountered (thrown) by default in the until condition, and immediately propagate all others. You can add more to the ignore list by calling ignoring(exceptions to add)

The only difference is that by default element not found exception is ignored in WebDriverWait. The rest of features is all exactly the same with FluentWait since WebDriverWait extends it.

Fragonard answered 19/9, 2017 at 6:56 Comment(0)
I
1

The main difference is that in a Webdriver wait we cannot perform pooling for wait scenario where as in Fluent wait, we can set pooling time which isn't possible in Webdriver wait.

Webdriver wait example

WebElement dynamicElement = (new WebDriverWait(driver, 10))
  .until(ExpectedConditions.presenceOfElementLocated(By.id("dynamicElement")));

Fluent wait example Below code is to wait 30 seconds for an element to be present on the page, checking for its presence once every 5 seconds.

 Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
        .withTimeout(30, SECONDS)
        .pollingEvery(5, SECONDS)
        .ignoring(NoSuchElementException.class);

    WebElement foo = wait.until(new Function<WebDriver, WebElement>() 
    {   
      public WebElement apply(WebDriver driver) {
      driver.findElement(By.id("foo"));    
    }
    });
Interrogate answered 16/10, 2019 at 9:58 Comment(1)
Polling time is possible in both waits.Comte
S
0

In FluentWait, The polling period is controlled, whereas in Explicilit wait it is 250 ms.

The user also has the flexibility to ignore exceptions that may occur during the polling period using the IgnoreExceptionTypes command.

Subcontraoctave answered 12/6, 2021 at 0:43 Comment(0)
C
0

https://medium.com/@ahamedabdulrahman/differences-between-fluentwait-and-webdriverwait-explicit-wait-7adc2d68935d

FluentWait is general purpose wait whereas ExplicitWait is typed to WebDriver and ignores NoElementException by default. FluentWait can be used with any type including WebDriver too. WebDriverWait can only be used with WebDriver.

Apart from that both can use pollingEvery(), ignoring() etc.

Comte answered 23/6, 2023 at 23:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.