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());
}
}