✋Waits
Implicit Wait
Implicit wait sets a global timeout that WebDriver will wait for elements to be available on the page after any page loading actions. It applies to all element lookups during the life of the WebDriver instance. While convenient, it isn't reliable as page refreshes reset the timer.
Explicit Wait
With explicit waits, you define the wait condition and element you want to apply it to. This allows waiting for specific elements rather than a global timeout. The condition is explicitly coded such as waiting for an element to be clickable. It is more reliable than implicit wait as the wait is applied only when needed.
Fluent Wait
Fluent wait builds on explicit wait by allowing you to configure the frequency of checking the wait condition, timeout and polling interval. This gives you more control over waiting compared to a traditional explicit wait. For dynamic web pages where elements appear asynchronously, fluent wait can check for the element more frequently within the given timeout window compared to a fixed interval explicit wait.
What is Synchronization? ⏱
Synchronization is like getting all the instruments in an orchestra to play at the exact same tempo 🎶. It coordinates the timing between the test code and the application.
Why is it Needed? 🤔
Websites load and update asynchronously ⏳.
Elements may not be immediately ready for interaction 🛑.
Without synchronization, tests will be flaky and unreliable ⚠️.
Synchronization Techniques 🛠
Implicit Waits - Global wait time for all findElement calls 🕐
Explicit Waits - Wait for specific conditions before interacting 🔍
Fluent Waits - Smart configurable waits to control polling and timeouts ⏲️
Golden Rules of Synchronization 📜
Avoid hard coded sleeps and fixed waits ❌
Leverage built-in waits like Implicit, Explicit and Fluent 👍
Identify root cause of timing issues rather than arbitrary waits ✅
Fine tune waits and timeouts for optimal wait time ⚡️
Key Benefits 💡
Reliable tests immune to timing issues 💪
Improved test stability and performance 📈
Better automation framework resilience 🛡️
Implicit Wait Conditions
Implicit Wait
The implicit wait is a global wait that applies to all WebDriver methods that look for elements. It tells the WebDriver to poll the DOM for a certain amount of time before throwing an exception if an element is not found.
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
The implicit wait is set in seconds. In the above code, the implicit wait is set to 10 seconds. This means that the WebDriver will poll the DOM for 10 seconds before throwing an exception if an element is not found.
Timeout
The timeout is the maximum amount of time that the WebDriver will wait for an element to be found before throwing an exception.
driver.findElement(By.id("myElement")).wait(10);
The timeout is set in milliseconds. In the above code, the timeout is set to 10 milliseconds. This means that the WebDriver will wait for 10 milliseconds for the element with the ID "myElement" to be found before throwing an exception.
Expected Conditions
Expected Conditions are predicates that can be used to wait for a specific condition to be met before continuing with the test.
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("myElement")));
Expected Conditions are used in conjunction with the implicit wait to wait for a specific condition to be met before continuing with the test. In the above code, the ExpectedConditions.visibilityOfElementLocated()
condition is used to wait for the element with the ID "myElement" to be visible before continuing with the test.
Explicit Wait Conditions
elementToBeClickable
Wait until an element is clickable
WebDriverWait wait = new WebDriverWait(driver, 10); WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.id("submit")));
Element must be in DOM and visible
visibilityOfElementLocated
Wait until an element is visible
WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("header")));
Element must be present in DOM
textToBePresentInElement
Wait until text appears in an element
WebElement element = wait.until(ExpectedConditions.textToBePresentInElement(By.id("message"), "Success"));
Element must be present in DOM
titleContains
Wait until title contains text
wait.until(ExpectedConditions.titleContains("Login")));
Title must contain given text
elementToBeSelected
Wait until element is selected
WebElement element = wait.until(ExpectedConditions.elementToBeSelected(By.id("checkbox")));
Element must be a checkbox/radio
numberOfElementsToBeMoreThan
Wait until number of elements is more than x
int count = wait.until(ExpectedConditions.numberOfElementsToBeMoreThan(By.className("button"), 5));
Used to wait for number of elements
Fluent Wait Conditions
withTimeout
Sets maximum timeout for wait
Wait<WebDriver> wait = new FluentWait<WebDriver>(driver).withTimeout(10, TimeUnit.SECONDS);
Required, sets max timeout
pollingEvery
Sets interval between retries
wait.pollingEvery(5, TimeUnit.SECONDS);
Optional, default is 500 ms
ignoring
Specifies exceptions to ignore
wait.ignoring(NoSuchElementException.class);
Optional
until
Specifies expected condition
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("header")));
Required, expected condition to wait for
withMessage
Sets error message for timeouts
wait.withMessage("Timed out waiting for header");
Optional, sets custom timeout message
ignoringAll
Ignores all exceptions
wait.ignoringAll();
Optional, ignores ALL exceptions
What are the differences between Implicit Wait, Explicit Wait, and Fluent Wait?
🚦 Implicit Wait:
Waits a global maximum time period for elements 👁
Applied once to the driver ✅
🕑 Applies for the lifetime of the WebDriver 🔁
🚦 Explicit Wait:
Waits only for a specific condition 🕰
Applied for each WebElement 🔢
🕛 Waits only for the needed time ⏳
🚦 Fluent Wait:
similar to Explicit Wait ⏱
Configurable polling time and timeout 🕦
✅ Ignores specific exceptions if needed ❌
Selenium Wait Rules
Waits are used to handle asynchronous actions and dynamic elements in Selenium.
🕑 Implicit Wait Rules
Sets timeout for all findElement/s calls
Applies globally for all elements
Timeout only occurs once per call
Does not retry after timeout
⏱️ Explicit Wait Rules
Targets specific element vs global
Retries until timeout or condition met
Common conditions are visibility, clickability etc.
Only throws exception after timeout
♻️ Fluent Wait Rules
Retries at regular intervals until timeout
Custom retries, timeout, exceptions
Implemented via polling and callback
More flexible but complex
So each wait handles synchronization differently - implicit wait globally, explicit wait for elements, fluent wait customly.
Last updated