Waiting Strategies and Timing
What you'll learn: How to handle asynchronous UI updates reliably without fragile fixed delays.
When testing UIs, elements don't always appear instantly—data loads from APIs, animations run, or JavaScript updates the page. The naive approach is adding sleep(5000) everywhere, but this creates brittle tests: too short and tests fail randomly; too long and your suite crawls.
The Problem with Fixed Waits
Imagine ordering coffee and the barista says "come back in exactly 5 minutes." If it's ready in 2, you waste 3 minutes waiting. If it takes 6, you arrive too early and think they forgot your order. Fixed waits work the same way—they're either too long (slow tests) or too short (flaky failures).
Explicit Waits and Polling
Explicit waits repeatedly check a condition until it's true or a timeout expires. Instead of "wait 5 seconds," you say "wait up to 10 seconds for the login button to appear, checking every 500ms."
wait until element "#submit-button" is visible
timeout: 10 seconds
poll interval: 500ms
This returns immediately when the condition is met—no wasted time.
Retry Logic
For operations that might fail temporarily (network requests, database queries during tests), retry logic attempts the action multiple times before giving up:
retry up to 3 times with 1 second between attempts:
fetch user data from API
This handles transient failures without making tests flaky.
Smart Waiting
Modern E2E tools often provide built-in smart waits that automatically retry actions. When you click a button, the tool waits for it to be clickable, not obscured, and enabled—all without explicit waits.
Key Takeaway: Replace fixed sleep() calls with explicit waits and retry logic that poll for specific conditions, making tests both faster and more reliable.