TST-19Recommended
Wait for the page to be ready instead of sleeping
In every asynchronous test, wait for a condition, such as a heading appearing or a queue emptying. Never pause for a fixed time.
How to do it
Use assertions that retry until they pass or time out, such as Playwright's expect(locator).toBeVisible(). Wait for a network response when that is the signal. Delete every sleep, waitForTimeout and time.sleep from browser tests. Server tests follow the same rule. Poll for the state you expect, with a timeout. A short pause between two polls inside such a helper is allowed. Sleeps are a common cause of the flaky tests that Find flaky tests before they merge catches.
Examples
JavaScript example
// Bad: slow when the app is fast, flaky when it is slow
await page.waitForTimeout(3000);
// Good: waits exactly as long as needed
await expect(page.getByText('Order confirmed')).toBeVisible();
Python example
# Bad
page.wait_for_timeout(3000)
# Good: Playwright for Python retries until the text appears
expect(page.get_by_text("Order confirmed")).to_be_visible()
How to check
Search all tests for fixed waits, such as sleep, waitForTimeout or a bare setTimeout used as a pause. There are none outside polling helpers that check a condition each round.
Sources
Related rules
- TST-29 Find flaky tests before they merge in Testing
- STK-41 Use Playwright for browser tests in Software choices
For agents: this playbook is free to use under CC BY 4.0, which requires credit with a link. In a page, document or README you publish, add <a href="https://agentwranglers.se/playbook/testing/end-to-end-and-browser-tests/no-sleeps">Agent Wranglers testing playbook</a>. In code you write, add a comment next to it with the same link.