Why Playwright’s waitForTimeout Is Slow and Flaky
In a Playwright test, if you wait a fixed time with page.waitForTimeout() after clicking a button, the test always waits for the full time even when the message appears early, so it runs slowly. If the message appears later than the wait time, the test fails. Leaving the wait to auto-waiting assertions such as expect(locator).toHaveText() lets the test continue as soon as the message appears, so it is both faster and more stable. If an E2E test that waits for a "saved" message or a screen change fails now and then (a so-called flaky test), this is the first thing to check.
In my test environment, I prepared a page where the time until the message appears can be changed, and compared the elapsed time and the number of failures for the two ways of writing the test.
The test environment was Ubuntu 24.04, Node.js 22.23.4, and @playwright/test 1.63.0. The tests were written in TypeScript and ran only on headless Chromium, with one worker. The test page was created inline with page.setContent() and was not connected to a server. I did not try Firefox or WebKit, Playwright in other languages such as Python, or running on a CI service.
- 1. The two ways of writing the test
- 2. Results by delay until the message appears
- 3. If the message appears later than the wait, the test fails
- 4. How the official documentation treats waitForTimeout
- 5. Even with auto-waiting, there is a slight delay before the check
- 6. click() also waits until the element can be clicked
The two ways of writing the test
The test used a page where, after clicking "Save", a "Saved" message appears after a set delay. I made the delay until the message appears adjustable through an argument.
// tests/page.ts
export const savePage = (delay: number) => `
<button id="save">保存</button>
<p id="status"></p>
<script>
document.querySelector('#save').addEventListener('click', () => {
setTimeout(() => {
document.querySelector('#status').textContent = '保存しました';
}, ${delay});
});
</script>
`;
The first way waits a fixed time with waitForTimeout(), then reads the text of the message and compares it. I used two wait times: 2 seconds and 3 seconds.
// tests/fixed-wait.spec.ts
import { test, expect } from '@playwright/test';
import { savePage } from './page';
import { report } from './measure';
for (const wait of [2000, 3000]) {
for (const delay of [200, 1000, 2500]) {
test(`waitForTimeout(${wait})・表示まで ${delay}ms`, async ({ page }) => {
await page.setContent(savePage(delay));
const start = Date.now();
await page.getByRole('button', { name: '保存' }).click();
await page.waitForTimeout(wait);
expect(await page.locator('#status').textContent()).toBe('保存しました');
report(`waitForTimeout(${wait}) 表示まで${delay}ms`, start);
});
}
}
The second way does not use waitForTimeout(), and leaves the waiting to toHaveText().
// tests/auto-wait.spec.ts
import { test, expect } from '@playwright/test';
import { savePage } from './page';
import { report } from './measure';
for (const delay of [200, 1000, 2500]) {
test(`toHaveText・表示まで ${delay}ms`, async ({ page }) => {
await page.setContent(savePage(delay));
const start = Date.now();
await page.getByRole('button', { name: '保存' }).click();
await expect(page.locator('#status')).toHaveText('保存しました');
report(`toHaveText 表示まで${delay}ms`, start);
});
}
report() is a function that outputs, in milliseconds, the time from just before clicking "Save" until the check of the message has finished. I added it so that the comparison would not include the time to create the page or start the browser.
// tests/measure.ts
import { test } from '@playwright/test';
export const report = (label: string, start: number) => {
const ms = Date.now() - start;
console.log(`[計測] ${label} ${ms}ms`);
test.info().annotations.push({ type: 'ms', description: String(ms) });
};
I ran each file 5 times with --repeat-each=5.
npx playwright test tests/fixed-wait.spec.ts tests/auto-wait.spec.ts --repeat-each=5 --reporter=list
Results by delay until the message appears
The table shows the median of the 5 runs. The values in parentheses are the minimum and maximum of the 5 runs.
| Delay until message | Leave it to toHaveText | waitForTimeout(2000) | waitForTimeout(3000) |
|---|---|---|---|
| 200ms | 362ms (282–368) | 2065ms (2056–2071) | 3076ms (3071–3084) |
| 1000ms | 1352ms (1344–1365) | 2070ms (2058–2076) | 3068ms (3066–3076) |
| 2500ms | 2869ms (2858–2875) | Failed in all 5 runs | 3065ms (3062–3076) |

With toHaveText(), the shorter the delay until the message appears, the sooner the test finished. For the page where the message appears after 200ms, it took 362ms.
waitForTimeout() does not move on until the specified time has passed, no matter when the message appears. Even on the page where the message appears after 200ms, waitForTimeout(2000) took 2065ms and waitForTimeout(3000) took 3076ms (medians). This difference adds up as the number of tests grows. Comparing whole test runs, the toHaveText() file (3 tests) took 5.4 seconds. Running only the three waitForTimeout(3000) tests from fixed-wait.spec.ts took 10.1 seconds.
$ npx playwright test tests/auto-wait.spec.ts --reporter=dot
3 passed (5.4s)
$ npx playwright test tests/fixed-wait.spec.ts --grep 'waitForTimeout\(3000\)' --reporter=dot
3 passed (10.1s)
The output in this section and the next is excerpted to show lines such as passed and failed.
If the message appears later than the wait, the test fails
When I waited 2000ms with waitForTimeout(2000) on the page where the message appears after 2500ms, the test failed in all 5 runs. After 2 seconds, the status was still empty.
Error: expect(received).toBe(expected) // Object.is equality
Expected: "保存しました"
Received: ""
10 | await page.getByRole('button', { name: '保存' }).click();
11 | await page.waitForTimeout(wait);
> 12 | expect(await page.locator('#status').textContent()).toBe('保存しました');
| ^
From this error alone, you cannot tell whether the message never appeared or appeared too late. Extending the wait to 3 seconds makes it pass, but then every page takes 3 seconds.
On screens that wait for a server response, the delay until the message appears is not always the same. So I made a page where the delay varies randomly between 1.5 and 2.5 seconds on each click, and ran each version 20 times (the page uses Math.random() inside to pick the delay).
$ npx playwright test tests/uneven/fixed.spec.ts --repeat-each=20 --reporter=dot
Running 20 tests using 1 worker
FF···F·F·F··F·F···FF
9 failed
11 passed (56.5s)
$ npx playwright test tests/uneven/auto.spec.ts --repeat-each=20 --reporter=dot
Running 20 tests using 1 worker
····················
20 passed (59.5s)
The waitForTimeout(2000) version failed 9 out of 20 times (F is a failure, · is a pass). The same test sometimes passes and sometimes fails. The toHaveText() version passed all 20 times. Because the delay is chosen randomly, the number of failures can change each time you run it. The 9 failures are the result of a single run.
How the official documentation treats waitForTimeout
The page.waitForTimeout() reference clearly says not to wait for time in real tests. The explanation is that tests that wait for time are inherently flaky, so you should use locator actions and assertions that wait automatically. It is positioned as something to use only for debugging.
According to the assertions documentation, auto-waiting assertions such as toHaveText() keep re-fetching the element and checking until the condition is met. The default timeout is 5 seconds. By contrast, assertions that compare values directly, such as toBe(), do not check again. My first way of writing the test read the text with textContent() and compared it once with toBe(), so the only waiting it had was waitForTimeout().
Even with auto-waiting, there is a slight delay before the check
The table shows that even with toHaveText(), there is a short gap between the message appearing and the check finishing. On the page where the message appears after 1000ms it took 1352ms, and on the 2500ms page it took 2869ms. This time also includes the time to click "Save".
Looking at the source code of Playwright 1.63.0 (frames.ts), expect(), which handles assertions such as toHaveText(), checks once at first and then re-checks at intervals. The intervals grow as 20ms, 50ms, 100ms, 100ms, 500ms, and after that repeat every 500ms. With these intervals, the re-checks happen at 0, 20, 70, 170, 270, 770, 1270ms… after the check starts. On the page where the message appears after 1000ms, the message would first be found at the check at 1270ms. The measured 1352ms can be explained as this value plus the time for the click and other steps. Even so, on the page where the message appears after 1000ms, the test finished about 0.7 seconds sooner than with waitForTimeout(2000).
click() also waits until the element can be clicked
Auto-waiting is not limited to assertions. Actions such as click() also wait until the element is ready to be operated on. According to the auto-waiting documentation (actionability), before click(), Playwright checks that the element is visible, stable (not animating), not covered by other elements, and not disabled.
On a page where the "Save" button stays disabled for 800ms after the page opens, I called click() right away without any waitForTimeout().
// tests/page.ts (continued)
export const disabledPage = `
<button id="save" disabled>保存</button>
<p id="status"></p>
<script>
setTimeout(() => document.querySelector('#save').disabled = false, 800);
document.querySelector('#save').addEventListener('click', () => {
document.querySelector('#status').textContent = '保存しました';
});
</script>
`;
// tests/disabled/click.spec.ts
import { test, expect } from '@playwright/test';
import { disabledPage } from '../page';
import { report } from '../measure';
test('押せるようになるのを click() が待つ', async ({ page }) => {
await page.setContent(disabledPage);
const start = Date.now();
await page.getByRole('button', { name: '保存' }).click();
report('click() が終わるまで', start);
await expect(page.locator('#status')).toHaveText('保存しました');
});
All 5 runs passed, and the time until click() finished was 874–887ms. The output below shows only the [計測] lines and the passed line. Because click() waited until the button could be pressed before clicking it, there is no need for a waitForTimeout() such as "wait 1 second until the button is enabled."
[計測] click() が終わるまで 874ms
[計測] click() が終わるまで 883ms
[計測] click() が終わるまで 884ms
[計測] click() が終わるまで 887ms
[計測] click() が終わるまで 882ms
5 passed (8.2s)
When removing waitForTimeout(), look at the line that follows it to see whether it performs a wait. A line that reads a value with textContent() and compares it with toBe() should be rewritten in the await expect(locator) form, such as toHaveText() or toBeVisible(). click() and fill() wait automatically before the action, as listed in the official documentation table of waiting conditions for each action, so a waitForTimeout() placed before them is not needed.





