TestingDocs.com
Software Testing website
  • Automation
    • Selenium
    • JBehave Framework
  • Tutorials
    • MySQL Tutorials
    • Testlink
    • Maven
    • Git
  • IDEs
    • IntelliJ IDEA
    • Eclipse
  • Flowcharts
    • Flowgorithm
    • Raptor
  • About

Selenium

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/

Related Posts

Windows 10 Settings

Selenium /

Add Microsoft Webdriver on Windows OS

Download Selenium Components

Selenium /

Getting Started with Selenium Webdriver

LambdaTest Testing Cloud SaaS Platform

Selenium /

LambdaTest – Testing Cloud SaaS Platform

Selenium /

Selenium 3.0 and Mozilla GeckoDriver

SauceLabs Website

Selenium /

Run an Example Test on SauceLabs

‹ BrowserStack : Parallel Testing› Selenium Automation: Page Objects

Recent Posts

  • MS Access Data Types
  • Install RAPTOR Avalonia on CentOS
  • Download RAPTOR Avalonia Edition on Windows
  • npm doctor command
  • Build & Run CLion Project
  • Create New CLion C Project on Windows
  • Configure CLion Toolchains on Windows
  • Launch CLion IDE on Windows
  • Activate CLion IDE
  • CLion IDE for C/C++ Development

Back to Top

Links

  • Contact
  • Privacy Policy
  • Cookie Policy

www.TestingDocs.com

Go to mobile version