I want to implement Fluent Wait in my test script, but I am new to Selenium and don't know much about Waits. Can anyone please help?

0 votes
I want to implement Fluent Wait in my test script, but I am new to Selenium and don't know  much about Waits. Can anyone please help?
Jul 22 in Selenium by Indu
102 views

1 answer to this question.

0 votes

Hey Indu, Fluent wait is a class which we can use to wait until specific conditions are met. In Fluent wait, we can change the default polling period based on our requirement. Each fluent wait instance defines the maximum amount of time to wait for a condition and we can give the frequency with which to check the condition

We can also ignore any exception while polling element such as NoSuchElement exception in Selenium. Following example shows how you can use Fluent Wait:

package fluentWaitTest;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.FluentWait;
import com.google.common.base.Function;

public class FluentWaitDemo {
public static void main(String[] args) throws InterruptedException {

          WebDriver driver = new ChromeDriver();
          driver.manage().window().maximize();
          driver.get("https://www.edureka.co");

          driver.findElement(By.xpath("//button[text()=''Sign Up']")).click();

          // Create object of FluentWait class and pass webdriver as input
          FluentWait<WebDriver> wait = new FluentWait<WebDriver>(driver);

           // It should poll webelement after every single second
          wait.pollingEvery(1, TimeUnit.SECONDS);

          // Max time for wait- If conditions are not met within this time frame then it will fail the script
          wait.withTimeout(10, TimeUnit.SECONDS);

         // we are creating Function here which accept webdriver and output as WebElement-
          WebElement element = wait.until(new Function<WebDriver, WebElement>() {

               // apply method- which accept webdriver as input
               @Override               
               public WebElement apply(WebDriver arg0) 
               {
                    WebElement element = arg0.findElement(By.xpath("//p[@id='demo']"));

                    // If condition is true then it will return the element and wait will be over
                    if (element.getAttribute("innerHTML").equalsIgnoreCase("WebDriver")) 
                    {
                       System.out.println("Value is >>> " + element.getAttribute("innerHTML"));
                       return element;
                     }

                    // If condition is not true then it will return null and it will keep checking until condition is not true
                    else {
                       System.out.println("Value is >>> " + ele.getAttribute("innerHTML"));
                       return null;
                    }
               }
          });

          // If element is found then it will display the status
          System.out.println("Final visible status is >>>>> " + element.isDisplayed());
     }
}
answered Jul 22 by Abha
• 27,180 points

Related Questions In Selenium

0 votes
1 answer

Impilicit wait vs Explicit wait vs Fluent wait

Implicit wait: Your telling the WebDriver the ...READ MORE

answered Apr 13, 2018 in Selenium by king_kenny
• 3,650 points
1,028 views
0 votes
2 answers
0 votes
1 answer

Finding WebDriver element with Class Name in java

The better way to handle this element ...READ MORE

answered Apr 10, 2018 in Selenium by nsv999
• 5,110 points
1,147 views
0 votes
1 answer
0 votes
2 answers

Can anyone help me that how to run Selenium WebDriver test cases in Chrome?

You first need to download chrome driver ...READ MORE

answered Aug 26 in Selenium by Abha
• 27,180 points
341 views