Playwright Explicit Waits

A food delivery app sends a notification when the pizza arrives. Nobody stares at the door for thirty minutes. An explicit wait gives a test the same notification. The test pauses until one named condition becomes true, such as a spinner vanishing or a web address changing, and moves on the instant it happens.

When Auto Waiting Needs Help

Playwright already waits for elements to be ready before it clicks or types. Some situations hide the signal from the page itself. A report may build on the server for ten seconds, or the browser may redirect after a slow background job. An explicit wait names the exact event to watch.

The Wait Timeline

Click "Generate Report"
        |
        v
Spinner appears
        |
        v
Server builds the report  (test waits here for a named event)
        |
        v
Spinner disappears  <-- condition met
        |
        v
Test reads the report

Waiting for an Element State

The waitFor method on a locator accepts four states: visible, hidden, attached, and detached.

await page.getByRole('button', { name: 'Generate' }).click();
await page.getByTestId('spinner').waitFor({ state: 'hidden' });

Waiting for a Web Address

After a login, the browser may travel through several redirects. The waitForURL method holds the test until the address matches.

await page.getByRole('button', { name: 'Sign in' }).click();
await page.waitForURL('**/dashboard');

Waiting for a Network Response

A test can wait for a specific server reply. Start listening before the action that triggers the request.

const responsePromise = page.waitForResponse('**/api/report');
await page.getByRole('button', { name: 'Generate' }).click();
const response = await responsePromise;
expect(response.status()).toBe(200);

Waiting for Page Load States

The waitForLoadState method accepts three values.

  • load fires after the page and its images finish loading.
  • domcontentloaded fires after the page structure is ready.
  • networkidle fires after network traffic stops for a short time.

Modern pages send background requests constantly, so networkidle often misleads. Prefer a named element or response.

Waiting for a Custom Condition

await page.waitForFunction(() => document.querySelectorAll('.row').length >= 10);

The function runs inside the browser again and again until it returns a true value.

Avoid Fixed Pauses

The page.waitForTimeout(5000) call freezes a test for a fixed time. Slow servers outlast the pause, and fast servers waste it. Keep this call for debugging sessions only.

A Real-World Scenario

A finance tool exports a yearly statement as a PDF. The export takes between three and twenty seconds. A test clicks Export, waits for the spinner to turn hidden, and confirms the download link appears. The test finishes in three seconds on a quiet day and waits patiently on a busy one.

Quick Practice Task

Pick a page that shows a loading indicator. Write a test that clicks the trigger and uses waitFor with the hidden state on the indicator. Replace any fixed pause with that wait.

Common Questions

Which wait works best for a loading spinner?

Wait for the spinner to reach the hidden state. This condition describes exactly what the user observes and stays valid when the delay changes.

Why do some experts avoid networkidle?

Live chat widgets, analytics scripts, and polling calls keep the network busy forever. The condition may never arrive, or may arrive too early.

How long does an explicit wait last?

Each wait obeys the timeout setting. A wait that never meets its condition fails with a clear message that names the condition.

Key Takeaways

  • An explicit wait watches one named condition.
  • Locator states, web addresses, responses, and functions all serve as conditions.
  • Fixed pauses make tests slow or unreliable.
  • Auto waiting handles most cases, and explicit waits cover the rest.