Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 547
Deepak Kakkar Avatar asked Nov 22 '16 23:11

Deepak Kakkar


2 Answers

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));
    }
like image 58
Priyanshu Shekhar Avatar answered Oct 04 '22 07:10

Priyanshu Shekhar


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.

like image 27
devx Avatar answered Oct 04 '22 07:10

devx