Playwright Auto-Waiting and Timeouts
This chapter provides an in-depth introduction to Playwright's auto-waiting mechanism, as well as complete knowledge of manual waiting and timeout configuration.
Why Auto-Waiting Is Needed
Modern web applications make extensive use of asynchronous rendering, and page content does not all load instantly.
In traditional testing frameworks, developers had to write sleep or waitFor to wait, but these operations are not flexible enough — either waiting too long and slowing down tests, or waiting too little and causing flakiness.
Playwright builds waiting logic into every Locator and Action, fundamentally solving this problem.
Three Layers of Auto-Waiting
Layer 1: Locator Auto-Waiting
When you create a Locator, Playwright does not immediately search for the element; it starts searching and auto-waiting only when you perform an action or assertion.
Example
// Create a Locator (no element search at this point)
const
btn
=
page.
getByRole
(
'button'
,
{
name
:
'Delayed Button'
}
)
;
// Clicking automatically waits for the button to appear (up to the action timeout)
await btn.
click
(
)
;
Layer 2: Actionability Checks for Actions
Before performing an action (click, fill, etc.), Playwright ensures the element is: Attached → Visible → Stable → Receives Events → Enabled.
Layer 3: Automatic Retry of Assertions
A failed assertion does not report an error immediately; it keeps retrying until the condition is met or a timeout occurs.
Example
// This assertion keeps retrying for 5 seconds until the text appears or a timeout occurs
await expect
(
page.
getByText
(
'Data loading complete'
)
)
.
toBeVisible
(
)
;
Manual Waiting Methods
In special scenarios where auto-waiting is insufficient, Playwright also provides manual waiting methods.
page.waitForTimeout(ms) — Fixed Wait
Example
// Fixed wait of 2 seconds (generally not recommended)
await page.
waitForTimeout
(
2000
)
;
Try to avoid using
waitForTimeout, because fixed waits are highly unstable when device performance differs. Prefer the event-driven waits described below,waitForURL、waitForResponseand other event-driven waits.
page.waitForURL() — Wait for URL Changes
Example
// Wait for the URL to match exactly
await page.
waitForURL
(
'https://www.runoob.com/dashboard'
)
;
// Wait for the URL to contain a specific pattern
await page.
waitForURL
(
/dashboard/
)
;
page.waitForLoadState() — Wait for Load State
Example
// Wait for the load event
await page.
waitForLoadState
(
'load'
)
;
// Wait for DOMContentLoaded
await page.
waitForLoadState
(
'domcontentloaded'
)
;
// Wait for the network to be idle
await page.
waitForLoadState
(
'networkidle'
)
;
page.waitForResponse() — Wait for Network Response
Example
// Wait for a specific API response
const
response
=
await page.
waitForResponse
(
resp
=>
resp.
url
(
)
.
includes
(
'/api/data'
)
&&
resp.
status
(
)
===
200
)
;
// Get response data
const
data
=
await response.
json
(
)
;
page.waitForEvent() — Wait for an Event
Example
// Wait for a popup to appear
const
dialogPromise
=
page.
waitForEvent
(
'dialog'
)
;
await page.
getByRole
(
'button'
,
{
name
:
'Delete'
}
)
.
click
(
)
;
const
dialog
=
await dialogPromise
;
await dialog.
accept
(
)
;
// Wait for a new page to open
const
pagePromise
=
page.
context
(
)
.
waitForEvent
(
'page'
)
;
await page.
getByRole
(
'link'
,
{
name
:
'New window opened'
}
)
.
click
(
)
;
const
newPage
=
await pagePromise
;
Timeout Configuration in Detail
Playwright has 4 types of timeouts, each applying at a different level:
| Timeout Type | Config file locations | Default value | Applies To |
|---|---|---|---|
| Test timeout | timeout |
30000ms | Entire test case |
| Expect timeout | expect.timeout |
5000ms | Assertion retries |
| Action timeout | use.actionTimeout |
None (unlimited) | Element actions (click, fill, etc.) |
| Navigation timeout | use.navigationTimeout |
None (unlimited) | Page navigation (goto, goBack, etc.) |
Global configuration
Example
// File path: playwright.config.ts
import
{
defineConfig
}
from
'@playwright/test'
;
export
default
defineConfig
(
{
// Total timeout for each test (in milliseconds)
timeout
:
60000
,
// Timeout for automatic assertion retries
expect
:
{
timeout
:
10000
,
}
,
use
:
{
// Timeout for each action (click, fill, etc.)
actionTimeout
:
15000
,
// Timeout for each navigation
navigationTimeout
:
30000
,
}
,
}
)
;
Per-instance settings (higher priority than global configuration)
Example
// Set total timeout for a single test
test.
setTimeout
(
120000
)
;
// Set timeout for a single action
await page.
getByRole
(
'button'
)
.
click
(
{
timeout
:
30000
}
)
;
// Set timeout for a single navigation
await page.
goto
(
'https://www.runoob.com/'
,
{
timeout
:
60000
}
)
;
Timeout Priority
Per-instance settings > global settings in the configuration file > default values.
Recommendations for Setting Timeouts Reasonably
| Scenario | Recommended Timeout | Description |
|---|---|---|
| Local development | Default value | Local services respond quickly; the default value is sufficient |
| CI environment | 2~3 times the default value | CI machine performance varies greatly |
| Slow pages | actionTimeout: 15000 |
Large forms and complex pages |
| Third-party service calls | test.setTimeout(120000) |
External APIs respond slowly |