What is Fluent Wait

Posted on

FluentWait is a type of explicit wait in Selenium WebDriver that allows you to specify the maximum amount of time to wait for a condition and the frequency with which to check for the condition. It is called "fluent" because it uses a method chaining syntax that makes the code more readable.
FluentWait is useful when you need to define a more detailed wait condition, such as waiting for an element to be visible, invisible, or clickable, and when you want to set a custom timeout and query interval for each condition. It is also useful if you need to ignore certain types of exceptions that might occur while waiting.
In the following code, we define a FluentWait with a maximum timeout of 30 seconds and a polling interval of 500 milliseconds by using the withTimeout() and pollingEvery() methods. Also, we use the ignoring() method to ignore the NoSuchElementException that might occur during the wait time.
Next, we use the until() method to wait for the expected condition, which in this case is to find the element with ID "elementID". To do this, we use a function that takes a WebDriver argument and returns a WebElement.
Java
Copy
// Define a FluentWait with a maximum timeout of 10 seconds
// And a polling interval of 500 milliseconds
Wait<webdriver> wait = new FluentWait<>(driver)
.withTimeout(Duration.ofSeconds(30));
.pollingEvery(Duration.ofMillis(500))
.ignoring(org.openqa.selenium.NoSuchElementException.class);
// Wait for an element to be visible and return the WebElement
WebElement element = wait.until(new Function<webdriver, webelement="">() 
{

   public WebElement apply(WebDriver driver) 
   {
      return driver.findElement(By.id("elementID"));
   }
});