0%
Selectors and Waits

Selectors and Waits

Nearly every flaky test comes from a race between your assertion and the browser

10-15 min

Selectors and Waits

A payment test fails one run in twelve in continuous integration. Always the same test, always on the same line, and never on your machine. You add await page.waitForTimeout(500) before the assertion: the test goes green for three weeks, then breaks again on a day when the agent is under load. You raise the delay to 2,000 ms. The suite goes from six minutes to eleven, and the problem will come back.

What you did was bet on a duration. The browser doesn't follow a schedule: it waits for a network response, computes a layout, plays a transition, replaces a DOM node. Your assertion lands in the middle of one of those intervals. A fixed delay doesn't eliminate the race, it only changes the odds, and you pay for that illusion in execution time on every passing test. The way out of this trap fits in one sentence: never wait for a duration, always wait for an observable condition. Playwright provides everything you need for this, as long as you know which locator to choose and which assertion to write.

Locators That Survive Redesigns

A locator is a description of an element, re-evaluated every time it is used. That re-evaluation is what allows it to wait, but it's also what makes the choice of locator so important.

Comments

Comments are powered by GitHub Discussions

Sign in with GitHub to join the discussion

Link copied!