Site icon TestingDocs.com

Selenium FluentWait Example

Overview

FluentWait instance defines the max amount of time to wait for 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.

We see an example program that wait fluently for a Bing search webpage element in the below program.

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

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

}

 

Selenium Tutorial on this website:

https://www.testingdocs.com/selenium-webdriver-tutorial/

More information on Selenium, visit official website:

https://www.selenium.dev/

Exit mobile version