Friday, 25 March 2016

What is fluent wait in Selenium Webdriver

Fluent wait is used to synchronize the elements in the browser.

Fluent wait allows you to
  1. Specify polling time
  2. Maximum time out
  3. Ignore Exceptions
Consider a scenario wherein you are expecting specific element to appear within 20 seconds. Time to load that element might change from 5 seconds to 20 seconds. In this situation, we can use Fluent Wait as mentioned in below example.

@Test
public void testWait(){
  String driverpath = 
  System.setProperty("webdriver.chrome.driver""C:\\chromedriver.exe");
  driver = new ChromeDriver();
  driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
  driver.manage().window().maximize();
  driver.get("http://www.softpost.org/selenium-test-page/");

  Wait wait = new FluentWait(driver)
            .withTimeout(20, TimeUnit.SECONDS)
            .pollingEvery(5, TimeUnit.SECONDS)
            .ignoring(NoSuchElementException.class);


  WebElement foo =(WebElement) wait.until(
   new Function<WebDriver, WebElement>() {
   public WebElement apply(WebDriver driver) {
   return driver.findElement(By.xpath("//input[@value='Sign up']"));
   }
   }
   );

    driver.close();
    driver.quit();
}

Above code will wait until element with xpath //input[@value='Sign up'] is found. The code will wait for 20 seconds maximum and throw exception at the end if element is not found even after 20 seconds.


What do you think on above selenium topic. Please provide your inputs and comments. You can write to me at reply2sagar@gmail.com

No comments:

Post a Comment

Buy Best Selenium Books

About Me

My photo

Self-motivated software professional