Selenium FluentWait Example
Selenium FluentWait Example
FluentWait is a type of wait that allows you to specify how long the WebDriver should wait for a condition to be true before proceeding. It’s an enhancement over other waiting mechanisms like WebDriverWait because it provides more flexibility.
FluentWait instance defines the maximum time to wait for a condition and the frequency with which to check the condition. We can configure the wait to ignore specific types of exceptions while waiting, for example, NoSuchElementException when searching for an element on the page.
Environment
Windows 10 , Mozilla Firefox 45 , BrowserStack Cloud.
DesiredCapabilities caps = new DesiredCapabilities(); caps.setCapability("browser", "Firefox"); caps.setCapability("browser_version", "45"); caps.setCapability("os", "Windows"); caps.setCapability("os_version", "10"); caps.setCapability("browserstack.debug", "true"); WebDriver driver = new RemoteWebDriver(new URL(URL), caps);
FluentWait
In the below program, we see an example program that waits fluently for a Bing search webpage element.
private static WebElement findElement(final WebDriver driver, final By locator, final int timeoutSeconds) { FluentWait<WebDriver> wait = new FluentWait<WebDriver>(driver) .withTimeout(timeoutSeconds, TimeUnit.SECONDS) .pollingEvery(1000, TimeUnit.MILLISECONDS) .ignoring(NoSuchElementException.class); return wait.until(new Function<WebDriver, WebElement>() { public WebElement apply(WebDriver webDriver) { return driver.findElement(locator); } }); }
Code Snippet
Code snippet to search Bing with a sample search keyword and fluent wait :
package com.testingdocs.testng.sample; import java.net.URL; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.NoSuchElementException; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.remote.DesiredCapabilities; import org.openqa.selenium.remote.RemoteWebDriver; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.FluentWait; import org.openqa.selenium.support.ui.WebDriverWait; import org.testng.Assert; import org.testng.annotations.Test; import com.google.common.base.Function; public class FluentWaitExample { public static final String USERNAME="your_browser_stack_username"; public static final String AUTOMATE_KEY = "your_key"; public static final String URL = "https://" + USERNAME + ":" + AUTOMATE_KEY + "@hub-cloud.browserstack.com/wd/hub"; @Test public void Bing_FluentWait() throws Exception { DesiredCapabilities caps = new DesiredCapabilities(); caps.setCapability("browser", "Firefox"); caps.setCapability("browser_version", "45"); caps.setCapability("os", "Windows"); caps.setCapability("os_version", "10"); caps.setCapability("browserstack.debug", "true"); WebDriver driver = new RemoteWebDriver(new URL(URL), caps); driver.get("https://www.bing.com/"); WebElement element = findElement(driver, By.id("sb_form_q"), 45); element.sendKeys("Hello Bing!"); driver.findElement(By.id("sb_form_go")).click(); WebDriverWait wait = new WebDriverWait(driver, 45); wait.until(ExpectedConditions.titleContains("Hello")); Assert.assertEquals("Hello Bing! - Bing", driver.getTitle()); driver.quit(); } private static WebElement findElement(final WebDriver driver, final By locator, final int timeoutSeconds) { FluentWait<WebDriver> wait = new FluentWait<WebDriver>(driver) .withTimeout(timeoutSeconds, TimeUnit.SECONDS) .pollingEvery(1000, TimeUnit.MILLISECONDS) .ignoring(NoSuchElementException.class); return wait.until(new Function<WebDriver, WebElement>() { public WebElement apply(WebDriver webDriver) { return driver.findElement(locator); } }); } }
With FluentWait, you can:
Specify the polling frequency: You can define how often the condition should be checked (e.g., every 1000 milliseconds).
Ignore specific exceptions: It allows you to specify which exceptions (e.g., NoSuchElementException) to ignore during the waiting period.
- The .withTimeout method sets the total time to wait.
- The .pollingEvery method sets how frequently Selenium checks for the element’s visibility.
- The .ignoring method tells Selenium to ignore NoSuchElementException during the wait, preventing unnecessary retries.
FluentWait is particularly useful in scenarios where elements take varying amounts of time to load or become interactable. It lets you customize the waiting strategy according to the needs of your test.
Selenium Tutorials
Selenium Tutorial on this website: