When it comes to automating web applications, one of the most common challenges testers face is dealing with dynamic content that may not be immediately available. In such cases, using effective waiting strategies becomes essential to ensure your tests run smoothly and reliably. Selenium WebDriver provides several waiting methods to handle these scenarios efficiently. In this blog post, we’ll explore the different types of waits in Selenium with Java, when to use them, and how they can improve your test automation.
Types of Waits in Selenium WebDriver
Selenium WebDriver provides three types of waits:
- Implicit Wait
- Explicit Wait
- Fluent Wait
Let’s discuss each in detail.
1. Implicit Wait
Implicit waits tell WebDriver to poll the DOM for a specified amount of time when trying to find an element if it’s not immediately available. Once set, the implicit wait remains for the entire session.
Usage Example:
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.By;
public class ImplicitWaitExample {
public static void main(String[] args) {
// Set the path for the ChromeDriver executable
System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
// Create a new instance of the Chrome driver
WebDriver driver = new ChromeDriver();
// Set implicit wait time
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
// Open a website
driver.get("http://example.com");
// Try to find an element
driver.findElement(By.id("someElementId"));
// Close the browser
driver.quit();
}
}
2. Explicit Wait
Explicit waits allow you to wait for a specific condition to occur before proceeding further in the code. This is useful when you want to wait for a certain element to be visible, clickable, or present in the DOM. You can specify conditions using the ExpectedConditions
class.
Usage Example:
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.By;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class ExplicitWaitExample {
public static void main(String[] args) {
// Set the path for the ChromeDriver executable
System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
// Create a new instance of the Chrome driver
WebDriver driver = new ChromeDriver();
// Open a website
driver.get("http://example.com");
// Create a WebDriverWait instance
WebDriverWait wait = new WebDriverWait(driver, 10);
// Wait for the element to be visible
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("someElementId")));
// Interact with the element
driver.findElement(By.id("someElementId")).click();
// Close the browser
driver.quit();
}
}
3. Fluent Wait
Fluent waits are a more advanced form of explicit waits, allowing you to define the polling interval and the conditions to be met before proceeding. This is particularly useful when you need to wait for an element that might take varying amounts of time to appear.
Usage Example:
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.By;
import org.openqa.selenium.support.ui.FluentWait;
import org.openqa.selenium.support.ui.Wait;
import java.time.Duration;
public class FluentWaitExample {
public static void main(String[] args) {
// Set the path for the ChromeDriver executable
System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
// Create a new instance of the Chrome driver
WebDriver driver = new ChromeDriver();
// Open a website
driver.get("http://example.com");
// Create a FluentWait instance
Wait<WebDriver> wait = new FluentWait<>(driver)
.withTimeout(Duration.ofSeconds(30))
.pollingEvery(Duration.ofSeconds(5))
.ignoring(NoSuchElementException.class);
// Wait for the element to be present
wait.until(driver -> driver.findElement(By.id("someElementId")));
// Interact with the element
driver.findElement(By.id("someElementId")).click();
// Close the browser
driver.quit();
}
}