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
41 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
• 19,130 points

Related Questions In Selenium

0 votes
1 answer

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

Download the executable driver from: ChromeDriver Download Before ...READ MORE

answered Mar 29, 2018 in Selenium by nsv999
• 5,050 points
203 views
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,610 points
695 views
0 votes
1 answer

Need to wait until page is completely loaded - Selenium WebDriver

Using Thread.Sleep(); is one approach; but its ...READ MORE

answered Apr 2, 2018 in Selenium by nsv999
• 5,050 points
4,107 views
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,050 points
403 views
0 votes
1 answer