Fluent Wait

Click on the button below. The paragraph field will tell you when 1, 2, 3, 4, 5, 6, 7, 8, 9 and 10 seconds have passed.



0


package com.wait.type;

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.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.FluentWait;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;
import org.testng.ITestResult;
import org.testng.Reporter;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import com.google.common.base.Function;
import com.google.common.base.Predicate;
import utility.Helper;

public class FluentWaitDemo {
 WebDriver driver;
 
 @BeforeMethod
 public void beforeFluentWait(){
  driver = new ChromeDriver();
  driver.get("http://anish-selenium.blogspot.in/p/fluent-wait.html");
 }
 @Test
 public void fluentWaitEx1() throws InterruptedException{  
  driver.findElement(By.xpath("//*[text()='Display Time in Seconds']")).click();
  
  FluentWait<WebDriver> wait = new FluentWait<WebDriver>(driver);
  wait.withTimeout(15, TimeUnit.SECONDS);
  wait.pollingEvery(250, TimeUnit.MILLISECONDS);
  wait.ignoring(NoSuchElementException.class);
  
  /*Function<WebDriver, WebElement> function = new Function<WebDriver, WebElement>() {
   @Override
   public WebElement apply(WebDriver driv) {
    WebElement element = driv.findElement(By.id("txt"));
    String value = element.getText();
    if(element.getText().equals("10")){
     System.out.println("Element is found: " + value);
     return element;
    }else{
     System.out.println("Element is not found: " + value);
     return null;
    }
   }
  };*/
  
  Function<WebDriver, Boolean> f = new Function<WebDriver, Boolean>() {
   
   @Override
   public Boolean apply(WebDriver dr) {
    WebElement element = dr.findElement(By.id("txt"));
    
    if(element.getText().equals("10")){
     System.out.println("Element is found");
     return true;
    } else{
     System.out.println("Current value inside is: " + element.getText());
     return false;
    }
   }
  };
  
  //Wait until using Function interface of com.google.common.base package
  wait.until(f); 
  
  Assert.assertEquals(driver.findElement(By.id("txt")).getText(), "10");
 }
 
 @Test
 public void fluentWaitEx2() throws InterruptedException{  
  driver.findElement(By.xpath("//*[text()='Display Time in Seconds']")).click();
  
  FluentWait<WebDriver> wait = new FluentWait<WebDriver>(driver);
  wait.withTimeout(15, TimeUnit.SECONDS);
  wait.pollingEvery(250, TimeUnit.MILLISECONDS);
  wait.ignoring(NoSuchElementException.class);
  
  Predicate<WebDriver> predicate = new Predicate<WebDriver>() {
   @Override
   public boolean apply(WebDriver driver) {
    WebElement element = driver.findElement(By.id("txt"));
    String value = element.getText();
    if(element.getText().equals("10")){
     System.out.println("Element is found: " + value);
     return true;
    }else{
     System.out.println("Element is not found: " + value);
     return false;
    }
   }
  };
  
  //Wait until using Predicate interface of com.google.common.base package
  wait.until(predicate);
  Assert.assertTrue(driver.findElement(By.id("txt")).getText().equals("10"));
 }
 
 @Test
 public void fluentWaitEx3() throws InterruptedException{    
  //Wait until using Predicate interface of com.google.common.base package
  driver.findElement(By.xpath("//*[text()='Display Time in Seconds']")).click();
  Thread.sleep(1200);
  
  WebDriverWait driverWait = new WebDriverWait(driver, 12);
  driverWait.withTimeout(12, TimeUnit.SECONDS);
  driverWait.pollingEvery(250, TimeUnit.MILLISECONDS);
  driverWait.ignoring(NoSuchElementException.class);
  
  //Wait until using ExpectedConditions class of org.openqa.selenium.support.ui package
  driverWait.until(ExpectedConditions.
    textToBePresentInElement(driver.findElement(By.id("txt")), "10"));
  Assert.assertTrue(driver.findElement(By.id("txt")).getText().equals("10"));
 }
 
 @AfterMethod
 public void afterFluentWait(ITestResult result){
  Reporter.setCurrentTestResult(result);
  Reporter.log("Test is Passed" + String.valueOf(result.getStatus()));
  driver.quit();
 }
}

3 comments: