Deepak Kakkar
Deepak Kakkar

Reputation: 111

Fluent wait vs WebDriver wait

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?

Upvotes: 11

Views: 14857

Answers (5)

Ahamed Abdul Rahman
Ahamed Abdul Rahman

Reputation: 526

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.

Upvotes: 0

Chan Sri
Chan Sri

Reputation: 15

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.

Upvotes: 0

Shashi
Shashi

Reputation: 63

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"));    
    }
    });

Upvotes: 1

devx
devx

Reputation: 81

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.

Upvotes: 8

Priyanshu
Priyanshu

Reputation: 3058

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

Upvotes: 8

Related Questions