☰ See All Chapters |
Fluent Wait in Selenium WebDriver
When we use selenium “WebDriverWait” and “ExpectedCondition”, webdriver continuously checks for the element and which may reduce the performance. Solution for this is configuring to make webdriver to check for element at regular interval of time instead of checking continuously. This regular interval of time is called as fluent time.
Syntax to configure fluent wait
Version | Syntax | Example |
Before Selenium 3.11 | Wait<WebDriver> wait = new FluentWait(WebDriverInstance) .withTimeout(duration, unit) .pollingEvery(duration, unit) .ignoring(Exception.class); | WebDriver driver = new ChromeDriver(); Wait<WebDriver> wait = new FluentWait(driver) .withTimeout(10, TimeUnit.SECONDS) .pollingEvery(2, TimeUnit.SECONDS) .ignoring(Exception.class); |
Selenium 3.11 and above | Wait<WebDriver> wait = new FluentWait(driver) .withTimeout(DurationInstance) .pollingEvery(DurationInstance) .ignoring(Exception.class); | WebDriver driver = new ChromeDriver(); Wait<WebDriver> wait = new FluentWait(driver) .withTimeout(Duration.ofSeconds(15)) .pollingEvery(Duration.ofSeconds(3)) .ignoring(Exception.class); |
import java.time.Duration;
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.ExpectedConditions; import org.openqa.selenium.support.ui.FluentWait; import org.openqa.selenium.support.ui.Wait;
public class Example {
public static void main(String[] args) {
// configure chromedriver System.setProperty("webdriver.chrome.driver", "F:\\My_Programs\\Selenium\\ChromeDriver\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
Wait<WebDriver> wait = new FluentWait(driver) .withTimeout(Duration.ofSeconds(15)) .pollingEvery(Duration.ofSeconds(2)) .ignoring(Exception.class);
// Launch website driver.get("https://www.registration.tools4testing.com/");
// Click on the Login Button driver.findElement(By.id("loginopener")).click();
// Focus on the dialog window by click on dialog window title WebElement loginWindow; loginWindow= wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id=\"ui-id-1\"]"))); loginWindow.click();
// Enter user name driver.findElement(By.id("loginUsername")).sendKeys("manu.m@tools4testing.com");
// Enter user password driver.findElement(By.id("loginPassword")).sendKeys("hello");
// Click on the Login Button driver.findElement(By.id("loginButton")).click();
WebElement loginSuccessWindow; loginSuccessWindow = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id=\"ui-id-3\"]"))); loginSuccessWindow.click();
driver.findElement(By.xpath("//*[@id=\"loginSuccessDialog\"]/div/span/input")).click();
//close the driver driver.quit();
} } |
All Chapters