Lessons in this course (10)

4. Waits and synchronisation — killing flaky tests

Intermediate★ Must knowLesson 4 of 10
WaitBehaviourVerdict
Thread.Sleep(5000)Always waits the full time❌ Slow and still flaky
Implicit wait (driver.Manage().Timeouts().ImplicitWait)Polls for every FindElement⚠️ Global, hides problems; don't mix with explicit
Explicit wait (WebDriverWait + condition)Waits until a specific condition or timeout✅ Use this
Fluent waitExplicit wait with polling interval and ignored exceptions✅ For special cases
csharp
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(15)) { PollingInterval = TimeSpan.FromMilliseconds(250) };
wait.IgnoreExceptionTypes(typeof(NoSuchElementException), typeof(StaleElementReferenceException));
var button = wait.Until(d => { var e = d.FindElement(By.Id("save")); return e.Displayed && e.Enabled ? e : null; });

Common exceptions: NoSuchElementException (wrong locator or not loaded), StaleElementReferenceException (DOM re-rendered — find the element again), ElementClickInterceptedException (overlay/spinner covering it — wait for it to disappear), TimeoutException.