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.
- 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.
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.
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.
// 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.
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.
- 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.
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.
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.
- 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.
- "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."