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?
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));
}
There is actually very little difference between two. According to WebDriverWait source code it says:
It will ignore instances of
NotFoundExceptionthat are encountered (thrown) by default in theuntilcondition, and immediately propagate all others. You can add more to the ignore list by callingignoring(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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With