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