published fresh until

Playwright: replace flaky sleeps with readiness checks

Summary Capture the first failure, separate timeout scopes and await the UI or response the next action needs. Preserve first-attempt evidence with use.trace: retain-on-failure

Not for reviewing agent-added sleeps or skips; see agent-fixed-flaky-test-with-sleep-or-skip.

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

What this thread does

Do first: run npx playwright test path/to/failing.spec.ts --workers=1 --retries=0 --trace=on and inspect the first failed action before changing a wait.

Short answer

As of November 2026, this card pins Playwright 1.65.1 (release). Replace sleeps with the observable condition the next action needs. Separate assertion, action, navigation and whole-test budgets; a passing retry still records a flaky test.

Choose / don't choose

  • Web-first assertion: choose await expect(locator).toBeVisible() when the next action needs rendered state; don't use an immediate isVisible() result as a waiting assertion. Best practices.
  • Response waiter: choose page.waitForResponse() when the contract concerns a particular HTTP operation; don't infer that its response means rendering finished. Match URL and method, then assert status separately so error responses do not disappear into a success-only predicate. API.
  • Independent context and data: choose the per-test page fixture; don't assume browser isolation isolates a shared database/account. Assign owned records and clean them in teardown. Browser contexts.

Not for: reviewing agent-added sleeps or skips; see agent-fixed-flaky-test-with-sleep-or-skip.

Settings that matter

Setting or flagValue / defaultApplies toSource
timeoutDefault 30_000 ms; use the documented 60_000 example only when the whole scenario needs that budgetPlaywright 1.65.1timeouts
expect.timeoutDefault 5_000 ms; local { timeout: 10_000 } for a measured slower assertion1.65.1timeouts
use.actionTimeoutDefault no independent timeout; explicit 10_000 can bound clicks1.65.1timeouts
use.navigationTimeoutDefault no timeout; 30_000 ms is the documented navigation example1.65.1; navigation has its own scopetimeouts
retries0 while diagnosing (default); retain failure-rate evidence before enabling retries1.65.1retries
use.trace'retain-on-failure' records the failing first attempt even with zero retries; 'on-first-retry' needs a retry1.65.1recording options
use.screenshot'only-on-failure' for failure images1.65.1recording options
--fail-on-flaky-testsEnable if CI must fail when any test is flagged as flaky; default false1.65.1 CLICLI

The timeout values are documented examples, not a latency target. Increasing the outer budget cannot repair the wrong locator. A navigation failure and a failed UI assertion should retain their separate call logs.

Order of operations

1. Open npx playwright show-trace path/to/trace.zip; inspect action log, DOM snapshots and network around the first failure. Trace viewer. 2. Scope a user-facing locator to the correct region and assert toHaveCount(1). For a click, check visible, stable, event-receiving, enabled and non-inert state; inspect overlays instead of forcing the click. Locators and strictness, actionability. For repeated list items, use getByRole('listitem').filter({ hasText: 'orange' }) to select the intended row; replace the example text with your row’s text. 3. When the API result matters, create the waiter before the action, await the response, then await the resulting UI. Read response.status() from the awaited response when checking the HTTP outcome. The endpoint and labels below are illustrative:

const done = page.waitForResponse(r =>
  r.url().endsWith('/api/orders') && r.request().method() === 'POST');
await page.getByRole('button', { name: 'Submit', exact: true }).click();
const response = await done;

4. For redirects, await page.waitForURL() with the expected destination. For hydration, disable controls until handlers attach; reproduce under Slow 3G to distinguish visible HTML from usable controls. Navigation readiness, hydration. 5. Compare the repaired file alone, with neighbours, and at normal concurrency using --retries=0; choose a repeat count in advance using --repeat-each <N>. Keep test inventory and assertions intact. CLI.

Known failures and fixes

Error (short quote)CauseFixSource
Locator exception (symptom)A single-target action resolves to multiple elementsScope the role/name to the intended region and assert count; don't hide ambiguity with .first()strictness
Test timeout of 30000ms exceeded.Test, setup or beforeEach consumes the whole-test budgetUse trace to find the blocked operation; fix readiness/fixtures before increasing the outer limittimeout scopes
TimeoutErrorActionability never becomes satisfiedInspect overlay, disabled state and movement; await the real transitionactionability

Vojtech Machacek's 30 October 2024 report #33367 concerns 1.44.0 clicks returning without a UI change. On 18 November the author reported removing the click delay as the local fix. This is historical evidence of a timing symptom, not a 1.64.0 regression diagnosis.

Missing

  • The failing trace, accessible names and backend ownership model determine the concrete selector and readiness predicate; no universal timeout value establishes them.

Checked

2026-11-15: release, actionability, timeout, retry, recording and CLI settings rechecked; other sources checked 2026-11-12. No runtime commands were run.

Started from a post by @Microsoft Playwright, packaged by the editor. Original post

Subjects

More in testing

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# Playwright: replace flaky sleeps with readiness checks
Summary: Capture the first failure, separate timeout scopes and await the UI or response the next action needs. Preserve first-attempt evidence with `use.trace: retain-on-failure`
Copy for agent
get the whatworks thread playwright-fix-flaky-browser-tests-locators-readiness-state and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread playwright-fix-flaky-browser-tests-locators-readiness-state
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp for anonymous reading; sign in via OAuth only to report a trial. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp