Auto-Waiting and Actionability
Relevant source files
The following files were used as context for generating this wiki page:
Playwright's Auto-Waiting mechanism is a foundational feature designed to enhance test reliability and eliminate flakiness. It ensures that before any action is performed on an element, the element is in an "actionable" state. This means Playwright automatically waits for all necessary checks to pass before proceeding with an action, such as a click or text input. This approach removes the need for artificial and often unreliable timeouts, which are a primary cause of flaky tests in web automation.
The core of this system is a series of actionability checks that validate an element's readiness for interaction. These checks are automatically retried until the conditions are met or a configurable timeout is exceeded. This same principle extends to web-first assertions, which are also retried, making tests for the dynamic web more robust and predictable.
Sources: README.md
Actionability Checks
Before Playwright performs an action on a locator, it runs a series of checks on the corresponding DOM element. An action is only executed if all the required checks pass. If any check fails, Playwright retries the checks until a timeout is reached.
The following diagram illustrates the general flow of an action that involves auto-waiting.
This diagram shows the iterative process of checking an element's state before performing an action.
Sources: packages/playwright-core/src/server/dom.ts
Key Actionability States
The server-side implementation defines several states that an element must satisfy to be considered actionable.
| Check | Description |
|---|---|
| Connected | The element must be connected to the DOM. |
| Visible | The element must be visible, meaning it does not have display: none or visibility: hidden CSS properties. |
| Stable | The element must be stable, meaning it has stopped moving and is not undergoing animation. |
| Enabled | The element must be enabled, meaning it is not disabled by an attribute or an aria-disabled property. |
| Editable | For text input actions, the element must be editable. |
If an element fails any of these checks, an appropriate error reason is generated, such as kElementIsNotVisible or kElementIsDisabled.
Sources: packages/playwright-core/src/server/dom.ts
Navigation and Lifecycle Waiting
Auto-waiting is also integral to navigation actions like page.goto() and page.setContent(). These methods can be configured to wait for specific browser lifecycle events to occur before considering the navigation complete.
The waitUntil Option
The waitUntil option controls when a navigation is considered finished.
waitUntil Value | Description |
|---|---|
load | (Default) Wait for the load event to be fired. |
domcontentloaded | Wait for the DOMContentLoaded event to be fired. |
networkidle | Wait until there are no more than a few network connections for at least 500 ms. |
commit | Wait for the navigation to be committed, which is when the response is received and the document starts parsing. |
Sources: packages/playwright/types/test.d.ts:1451-1454, tests/page/page-goto.spec.ts, tests/page/page-set-content.spec.ts
The following diagram shows the sequence of these lifecycle events during a page navigation.
This diagram illustrates the progression of events that waitUntil can target during a page load.
Sources: packages/playwright/types/test.d.ts
Assertions and Timeouts
Playwright's expect library is built with auto-waiting in mind. Assertions are automatically retried until the expected condition is met or a timeout is reached. This makes assertions resilient to delays caused by dynamic web content.
For example, expect(locator).toBeVisible() will retry until the element becomes visible on the page.
Sources: packages/playwright/types/test.d.ts:2519-2537
Timeout Configuration
Several configuration options are available to control the duration of auto-waiting for different operations.
| Option | Scope | Default | Description |
|---|---|---|---|
timeout | Test | 30s | Timeout for each test. |
actionTimeout | Action | 0 (no timeout) | Default timeout for each Playwright action (e.g., click()). |
navigationTimeout | Navigation | 0 (no timeout) | Default timeout for navigation actions (e.g., goto()). |
expect.timeout | Assertion | 5s | Default timeout for expect assertions. |
These timeouts can be configured globally in playwright.config.ts or overridden for specific tests or actions.
Sources: packages/playwright/types/test.d.ts:1012-1017, packages/playwright/types/test.d.ts:1462-1473, packages/playwright/types/test.d.ts:1474-1485, packages/playwright/types/test.d.ts:400-403
Client-Side Waiter
The Waiter class is a client-side component responsible for managing waiting operations. It orchestrates the waiting logic, handles timeouts, and communicates with the server to check for conditions.
The following sequence diagram shows a simplified interaction for an auto-waiting action.
This diagram illustrates the back-and-forth communication between the client and server as Playwright waits for an element to become actionable.
Sources: packages/playwright-core/src/client/waiter.ts
Summary
Auto-waiting and actionability are cornerstone features of Playwright that significantly contribute to creating stable and reliable automated tests. By automatically waiting for elements to be ready and for navigation events to complete, Playwright abstracts away the complexities of timing in modern web applications. This allows developers to write cleaner, more readable tests that focus on application logic rather than on managing arbitrary waits and sleeps. The extensive configuration options for timeouts and wait conditions provide fine-grained control when needed, ensuring both robustness and flexibility.
Sources: README.md, packages/playwright/types/test.d.ts