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

    • ChatGPT Plans Free and PlusChatGPT Subscription Plans
    • Stellar Converter for Database ToolStellar Converter for Database
    • Stellar MySQL Log AnalyzerStellar Log Analyzer for MySQL
    • Stellar Repair for MySQLStellar Repair for MySQL
    • ChatGPT IntroductionChatGPT Capabilities
    • How to secure your SQL Database: Tips and Tricks
    • ChatGPT4 Conversational AI FeaturesChatGPT4 Conversational AI Features
    • Trends in Software EngineeringShaping the Future of Development: Exploring Key Trends in Software Engineering
    • Java PerformanceImproving Java Performance with Multithreading
    • QDrant Vector DatabaseOpen-source Vector Databases
    • Difference between PHP and JavaScript?
    • Bing AI Browser Web ContentBing Conversation Styles
    • ChatGPT PreviewChatGPT Introduction
    • Open Source AI Frameworks TensorFlowOpen Source AI Frameworks
    • Artificial Intelligence Tools

    Back to Top

    Links

    • Contact
    • Privacy Policy
    • Cookie Policy

    www.TestingDocs.com