FluentWait is a customized type of Explicit Wait, here we can define polling period according to our need 2 Secs or 5 Secs, We can also configure the wait to ignore some exceptions like NoSuchElementExceptions.
This wait will check for the elements in the DOM for every 5 Secs until the elements found, if the element found within the period it breaks the search for the element and goes for the further action.
Fluent wait = Explicit Wait + frequency with which control need to be checked on application + exceptions to be ignored like NoSuchElementExceptions.
Syntax:
Wait<WebDriver> wait = new FluentWait<WebDriver>(driver) //Wait for the condition .withTimeout(30, TimeUnit.SECONDS) // which to check for the condition with interval of 5 seconds. .pollingEvery(5, TimeUnit.SECONDS) //Which will ignore the NoSuchElementException .ignoring(NoSuchElementException.class);
Features of Fluent Wait:
In Fluent Wait we define a Maximum amount of time to wait for a condition. Also, we will be defining a Polling frequency time while declaring the Fluent Wait.Fluent Wait will be polling the web pages every Polling frequency time to verify the given condition.
Fluent Wait has Inbuilt exception handler, which will handle NoSuchElementException until given Maximum amount of time has breached.
Refer below Example for Fluent Wait –
package demoPackage1; import java.util.NoSuchElementException; import java.util.concurrent.TimeUnit; import javax.lang.model.element.Element; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.FluentWait; import org.openqa.selenium.support.ui.Wait; import org.testng.annotations.Test; import com.google.common.base.Function; public class ExplicitWait { @Test public void FluentWaitTest(){ System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.navigate().to("http://demowebshop.tricentis.com"); driver.manage().window().maximize(); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); // Define fluent wait Wait<WebDriver> wait = new FluentWait<WebDriver>(driver) .withTimeout(30, TimeUnit.SECONDS) .pollingEvery(1, TimeUnit.SECONDS) .ignoring(NoSuchElementException.class); WebElement element = wait.until(new Function<WebDriver, WebElement>() { public WebElement apply(WebDriver driver) { return driver.findElement(By.xpath("//a[@class='ico-register']")); } } ); element.click(); driver.close(); driver.quit(); } }
FluentWait in Selenium WebDriver
To study more about Synchronization in Selenium WebDriver, visit –
a. Why not to use Thread.sleep(); to achieve synchronization in selenium?
b. What is Implicit wait? How to Achieve Implicit wait in Selenium?
c. What is Explicit wait? How to Achieve Explicit wait in Selenium?
d. What is Fluent wait? How to Achieve Fluent wait in Selenium?
e. Which is the best wait in selenium?
Hope this helps !!!!