QA AND TEST AUTOMATION · PART 5 · UI WEB AUTOMATION · BASICS · 49 OF 112

Waits in UI Automation: Implicit, Explicit, Auto-Wait

Why UI tests race the page, and how to wait correctly: fixed sleeps, Selenium implicit and explicit waits with ExpectedConditions, fluent waits, and Playwright auto-waiting and web-first assertions. Shown for real on ShopRung with its slow-search bug: an immediate read returned the old "12 products", while a web-first assertion waited about 2.9 seconds and passed.

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  29. 29
  30. 30
  31. 31
  32. 32
  33. 33
  34. 34
  35. 35
  36. 36
  37. 37
  38. 38
  39. 39
  40. 40
  41. 41
  42. 42
  43. 43
  44. 44
  45. 45
  46. 46
  47. 47
  48. 48
  49. 49
  50. 50
  51. 51
  52. 52
  53. 53
  54. 54
  55. 55
  56. 56
  57. 57
  58. 58
  59. 59
  60. 60
  61. 61
  62. 67
  63. 68
  64. 69
  65. 70
  66. 71
  67. 72
  68. 73
  69. 74
  70. 75
  71. 76
  72. 77
  73. 78
  74. 79
  75. 80
  76. 81
  77. 82
  78. 83
  79. 84
  80. 85
  81. 86
  82. 87
  83. 88
  84. 89
  85. 90
  86. 91
  87. 92
  88. 93
  89. 94
  90. 95
  91. 96
  92. 97
  93. 98
  94. 99
  95. 100
  96. 101
  97. 102
  98. 103
  99. 104
  100. 105
  101. 106
  102. 107
  103. 108
  104. 109
  105. 110
  106. 111
  107. 112
1 / 12

Press play to hear the narration, or step through at your own pace.

Key takeaways

  • The page is async
  • no sleeps
  • Selenium explicit waits
  • Playwright auto-wait
  • retrying assertions
  • wait for responses

Notes

The race

Here is the race. The test clicks search, and the click returns immediately. The page now sends a request to the products API. Until the answer arrives, the old results are still on screen. With ShopRung's slow search bug switched on, that takes two and a half seconds. Then the page re-renders. If the test reads the result count during that gap, it reads the old page, and the test is wrong without any error.

What happens after a click
  • 1 test clicks Search
  • 2 click() returns at once
  • 3 page sends GET /api/products
  • 4 old results still on screen
  • 5 ... 2.5 s later the answer
  • 6 page re-renders the grid
  • if the test reads at step 4, it reads the OLD page

A real stale read

We ran this on ShopRung with slow search on. Fill mug, click search, then immediately read the result count's text. It printed twelve products: the count from before the search. The right answer is two. With a fast server the same code usually passes, which is exactly what makes it flaky instead of simply broken: green on your laptop, red in a busy CI machine.

Read immediately (bad)
await searchbox.fill("mug");
await searchButton.click();
const text = await page
  .getByTestId("result-count")
  .textContent();

printed: "12 products"
expected: "2 products"

with a fast server it "works",
so the test is flaky, not broken

Fixed sleeps

The tempting fix is a fixed sleep: wait three seconds. It is wrong both ways. When the server is fast, every test wastes three seconds; across five hundred tests, that is twenty five minutes per run. When CI is slow, three seconds is not enough and the test is flaky anyway. Sleeps belong only in temporary debugging. Instead, wait for a condition.

Why not just sleep?
Thread.sleep(3000);
time.sleep(3)
page.waitForTimeout(3000)

fast server: wastes ~3 s
  × 500 tests = 25 minutes
slow CI day: 3 s not enough
  → flaky anyway

use only when debugging

Selenium waits

Selenium has two kinds. An implicit wait makes every find element retry for a while if the element is missing, but it only waits for presence, not for the right text or state. An explicit wait, web driver wait, polls a specific condition until it is true or a timeout passes: here, until the result count's text is two products. Most teams set the implicit wait to zero and use explicit waits, because mixing both can cause unpredictable wait times.

Selenium 4 (Java)
// implicit: findElement retries
driver.manage().timeouts()
  .implicitlyWait(Duration.ZERO);
// explicit: wait for a condition
var wait = new WebDriverWait(
  driver, Duration.ofSeconds(10));
wait.until(ExpectedConditions
  .textToBe(By.cssSelector(
   "[data-testid=result-count]"),
   "2 products"));

Conditions

Wait for the condition you actually need. Visible. Clickable. Text present. A spinner invisible. The URL containing cart. Exactly two elements. Staleness, meaning an old element was removed after a re-render, which is a neat way to know the page has refreshed. A fluent wait lets you set your own polling interval and ignore certain exceptions while polling, for more unusual cases.

Common conditions
visibilityOfElementLocated
elementToBeClickable
textToBePresentInElement
invisibilityOf (spinner gone)
urlContains("#/cart")
numberOfElementsToBe(by, 2)
stalenessOf(oldElement)

FluentWait: custom polling
  interval + ignored exceptions

Playwright auto-wait

Playwright builds waiting into every action. Before a click, it waits until the element is attached, visible, stable rather than animating, enabled, and actually receives the click rather than being covered by an overlay. Only then does it click. This removes most explicit waits for actions. But it does not know what result you expect after the click. That is what assertions are for.

Actionability checks
  • before click(), Playwright waits until the element is: attached to the DOM visible stable (not animating) enabled receiving events (not covered by an overlay)
  • up to the action timeout

Web-first assertions

Playwright's web first assertions retry until they pass or time out. Expect the result count to have text two products. We ran it with slow search on: it passed after about two point nine seconds, with no sleep anywhere. By default it retries for up to five seconds. Notice the difference from reading the text yourself and comparing it once: the first waits for the page, the second just takes a snapshot.

Wait by asserting (good)
await searchButton.click();
await expect(page
  .getByTestId("result-count"))
  .toHaveText("2 products");

slow-search on: passed after
  ~2.9 s, no sleep anywhere
default: retries up to 5 s

not: expect(await x.textContent())
       .toBe(...)  (reads once)

Waiting on network

Sometimes the right thing to wait for is a network response, for example when the screen does not visibly change. Start waiting for the response before you click, then click, then await it; if you start waiting after the click, a fast response can arrive before you listen. Avoid waiting for the network to go idle on modern apps with polling or analytics, because it may never be idle.

Wait for a response
const resp = page.waitForResponse(
  r => r.url().includes(
    "/api/products") && r.ok());
await searchButton.click();
await resp;

start waiting BEFORE the click,
  or you may miss a fast reply
avoid: waitForLoadState(
  "networkidle") on busy apps

Common mistakes

Synchronization mistakes. Sleeps as the fix. Reading a value once and comparing it, instead of a retrying assertion. Mixing implicit and explicit waits in Selenium. Huge global timeouts, which hide real performance problems. Waiting for network idle. And turning on test retries to cover a missing wait, which turns a fixable bug into a permanent slow flake.

Avoid these
  • sleep() as the fix
  • reading text, then comparing once
  • mixing implicit + explicit waits in Selenium
  • huge global timeouts that hide slowness
  • waiting for networkidle
  • retrying whole tests instead of fixing the wait

Say it like this

In the interview: never fixed sleeps. In Selenium, I keep the implicit wait at zero and use explicit web driver waits with the exact condition, like specific text or a spinner disappearing. In Playwright, I rely on auto waiting actions and web first assertions that retry. When the screen does not change, I wait for the specific network response, starting the wait before the action.

In the interview
  • "Never fixed sleeps. In Selenium: implicit wait zero, explicit WebDriverWait with the exact condition, like text
  • or invisibility of a spinner.
  • In Playwright: auto-waiting
  • actions plus web-first
  • assertions that retry. For hidden work, I wait for the specific network response."

Interview questions

Q1What is the difference between implicit and explicit waits in Selenium?

Implicit waits make findElement retry for presence globally; explicit waits (WebDriverWait) poll a specific condition such as visibility, clickability or text for one step.

Q2Why should you not mix implicit and explicit waits?

They can combine unpredictably and produce much longer waits than expected; most teams set implicit to zero and use explicit waits.

Q3Why is Thread.sleep bad in tests?

It waits a fixed time: wasteful when the app is fast and too short when it is slow, so tests are both slow and flaky.

Q4What is a FluentWait?

A Selenium wait with a configurable timeout, polling interval and list of exceptions to ignore while polling.

Q5What is Playwright auto-waiting?

Before acting, Playwright waits for the element to be attached, visible, stable, enabled and receiving events, up to the action timeout.

Q6What is a web-first assertion?

An assertion like expect(locator).toHaveText() that retries until it passes or times out, instead of checking a value once.

Q7How do you wait for an API call in a UI test?

Start page.waitForResponse (or similar) before triggering the action, then await it after; in Selenium use a condition on the resulting UI state.

Q8What is a StaleElementReferenceException?

Selenium's error when a stored element was removed or re-rendered; locate the element again or wait for stalenessOf the old one.