Hardware FixRecommendedDevice not working? Your driver may be the problemCheck updates for common hardware issues.Fix DriversHalloween DealsAmazon USHalloween week deals: useful picks before month-endAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan Now×
FREEDOM251

Freedom251 › Blog › flaky tests

I Deleted 200 Lines of Playwright Waits—and My Automation Got More Stable

By Freedom251 Team · · 4 min read

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Removing fixed waits can make a Playwright suite faster and less brittle when they are replaced with checks for the state the test actually needs. Playwright already waits for locator actions to become actionable, and its web-first assertions retry until the expected UI condition is true. But deleting waits by itself does not prove a suite is more stable: that claim needs comparable before-and-after results.

The 200-line reduction and stability improvement here are the author’s reported experience, not independently verified results. The useful lesson is how to distinguish a necessary synchronization point from a delay that merely hopes the application will be ready.

As an Amazon Associate I earn from qualifying purchases.

Why so many waits can make tests less reliable

A fixed sleep waits for elapsed time, not readiness. If the page becomes ready sooner, the test wastes time; if it takes longer, the test continues too early and may fail. Playwright’s Page API puts it plainly: “Tests that wait for time are inherently flaky.” Playwright’s guidance on page.waitForTimeout is to avoid it in production tests.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

This is not a claim that every delay in every test is wrong. It is a reason to ask what observable condition the test needs before it proceeds. If that condition is a visible confirmation, a successful action, or a particular text value, wait for that state instead of guessing how many milliseconds it will take.

What Playwright already waits for

Locator actions wait for actionability

Before locator.click() proceeds, Playwright checks that the locator resolves to one element and that the element is visible, stable, able to receive events, and enabled. If those checks do not pass within the timeout, the action fails with a timeout error rather than clicking an unready target. See Playwright’s actionability documentation.

That makes a sleep immediately before an ordinary locator click often redundant. It does not mean the click waits for every application-specific operation to finish: actionability checks do not establish, for example, that a save request has completed or that a success message has appeared.

Web-first assertions retry against the UI

For a result that should appear after an action, assert that result. Playwright’s asynchronous web assertions retry until the expected condition passes or the assertion timeout is reached. For example:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
await page.getByRole('button', { name: 'Save' }).click();
await expect(page.getByRole('status')).toHaveText('Saved');

The button action waits for actionability; the assertion waits for the user-visible result. The locator and expected text must match the application. See Playwright’s assertion documentation.

Replace a sleep with the condition the test needs

Consider a test that pauses for one second after a save. That pause neither verifies that the save succeeded nor adapts to how quickly the result appears:

// Fixed delay: detached from whether the save has finished
await page.waitForTimeout(1000);

// Check the actual result instead
await expect(page.getByRole('status')).toHaveText('Saved');

Use an assertion that represents the state required for the next step, such as the expected status text or an element becoming visible. Avoid replacing a short sleep with an arbitrary longer timeout; first identify the signal that demonstrates readiness.

Why networkidle is not a universal readiness check

Playwright defines networkidle as no network connections for at least 500 ms, but its Page API discourages using that state as a testing readiness condition and recommends web assertions instead. A quiet network does not necessarily prove that the particular UI result needed by the next test step is present. Conversely, some applications may continue making background requests after the relevant UI is ready. Check the meaningful page state rather than treating general network inactivity as a universal “ready” signal. Playwright’s Page API documents both the definition and the testing guidance.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

How to tell whether removing waits actually helped

The reported cleanup removed 200 lines, but that number alone says nothing about whether the suite became more stable. A credible before-and-after account would identify what the removed waits were doing, what observable condition replaced each important one, and whether the comparison used the same tests and execution conditions.

  • Show representative changes: the old wait and the new actionability check or assertion.
  • Report the test count, browsers, CI environment, and comparable run conditions.
  • Compare failures or flaky-test rates across repeated runs, not just one successful run.
  • Separate a reduction in runtime from a reduction in failures; they are different outcomes.

Broader studies provide context, not proof of this refactor. A 2023 paper on time-based repair reports an 11.1% reduction in test execution time from its repair method suggesting shorter waits; that figure is not an outcome from deleting these 200 lines. The paper’s abstract describes that separate work. An empirical study analyzing 452 JavaScript-project commits identified concurrency-related causes—including asynchronous waits, race conditions, and deadlocks—as the dominant category among the flaky-test causes it examined; that sample is not a universal rate and is not specific to Playwright. The study’s abstract gives its scope.

If the replacement assertion still times out

A timeout is useful evidence that the expected condition was not observed in time; making the timeout longer may hide the cause without fixing it. Check the failure in this order:

  1. Confirm that the locator identifies the intended element and, for an action, resolves to a unique match.
  2. Confirm that the expected state actually occurs in the application and that the assertion matches it precisely.
  3. Inspect whether an application or API error prevents the expected result from appearing.
  4. Only after checking those causes, consider whether the configured timeout is appropriate for the operation.

Replacing time-based waits with state-based synchronization is a sound way to remove unnecessary delays. Whether it made this particular automation more stable is a claim that only comparable run data can establish.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

flaky testsPlaywrightsoftware testingtest automation

Leave a Reply

Your email address will not be published. Required fields are marked *

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Recommended PC Tool
Recommended PC Tool
Crashes, No Sound, or Screen Glitches?Free driver scan
PC Slower Than It Used to Be?Free scan - under a minute

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.