(Updated: )

Avoid Delays and "waitFor" Statements Using Playwright Auto-Waiting

Share on social

Table of contents

End-to-end testing is a critical phase in the development lifecycle, ensuring that applications behave as expected under simulated real-user scenarios. However, one of the longstanding challenges in this domain has been the management of asynchronous events, such as waiting for elements to become interactive or visible on a webpage. Traditionally, developers have resorted to implementing delays or "waitFor" statements to overcome these hurdles, a practice that not only introduces potential flakiness but also increases test execution time. Enter Playwright's auto-waiting feature, a game-changer that significantly simplifies the process, making tests more reliable and maintainable.

Understanding the Challenge

In end-to-end testing, particularly when dealing with dynamic web applications, testers frequently encounter situations where UI elements are not immediately actionable. These elements could be buttons that become enabled only under certain conditions, modals that appear upon triggering specific actions, or fields that await input once they're activated. Traditionally, handling these scenarios involved littering test scripts with arbitrary delays or explicit "waitFor" statements, hoping that the targeted elements would reach the desired state within the specified wait time. This approach, however, is inherently brittle and prone to errors, as it relies heavily on guesswork and often results in either wasted time (when waits are too long) or failed tests (when waits are too short).

Enter Playwright Auto-Waiting

Playwright, a modern framework for end-to-end testing of web applications, introduces an elegant solution to this problem with its auto-waiting feature. This powerful capability allows tests to automatically wait for elements to reach an actionable state before proceeding with the execution. This means that developers no longer need to manually insert pauses or wait statements in their test scripts, leading to cleaner, more readable, and more reliable tests.

How It Works

The auto-waiting feature in Playwright operates under the hood, seamlessly handling various scenarios where elements transition through states before becoming interactive. Here's a closer look at how it tackles common challenges:

  1. Dynamic Content: Playwright waits for elements that are dynamically injected into the DOM, ensuring they are fully loaded and actionable before any interaction is attempted.
  2. Visibility and Occlusion: For elements that are initially hidden or occluded, Playwright patiently waits until they become visible or come to the forefront, making sure that clicks and other interactions are accurately executed.
  3. Animations: Recognizing that animations can affect the interactability of elements, Playwright also waits for animations to conclude before initiating interactions, thereby avoiding premature actions.
  4. Disabled Elements: In cases where elements are initially disabled, Playwright ensures that it only attempts interactions once the elements become enabled.

Playwright Auto-waiting in Practice

To illustrate, let's consider a test scenario with a webpage containing five buttons, each becoming actionable at different times due to various reasons such as DOM injection, visibility changes, animations, or state changes (e.g., from disabled to enabled).

Testing Without "waitFor" Statements

With Playwright, testing these buttons becomes straightforward. Here's a simplified example of how you might test that a button becomes disabled after being clicked, without needing any waitFor or delay:

const { chromium } = require('playwright'); // Require Playwright's Chromium browser

(async () => {
  const browser = await chromium.launch(); // Launch the browser
  const page = await browser.newPage(); // Open a new page
  await page.goto('<https://your-test-website.com>'); // Navigate to your test website

  const button = page.locator('button#your-button-id'); // Locate the button by ID
  await button.click(); // Click the button
  await expect(button).toBeDisabled(); // Assert the button is disabled

  await browser.close(); // Close the browser

In this example, Playwright automatically waits for the button to become clickable, performs the click action, and then asserts that the button is disabled—all without explicit waitFor commands.

Benefits of Auto-Waiting

  • Reduced Flakiness: By eliminating arbitrary waits, tests become more reliable and less prone to timing-related failures.
  • Increased Efficiency: Tests run faster as they proceed as soon as elements are ready, without unnecessary delays.
  • Simplified Test Scripts: Test scripts are cleaner and more focused on the actual testing logic rather than the intricacies of timing and state changes.


Playwright's auto-waiting feature alters how we implement E2E testing, making tests more robust and easier to maintain. By intelligently waiting for elements to be in the right state, Playwright eliminates the need for manual waitFor statements and arbitrary delays, allowing developers to focus on what matters most—ensuring their applications work correctly for their users. To see auto-waiting in action, see Stefan’s video walkthrough on this feature.

Join the Checkly Community

If you have tips and tricks to share for making stable, useful tests with Playwright, we’d love to hear from you. Join the Checkly community Slack, follow us on X, or connect on LinkedIn.

Share on social