Playwright + TS · Lesson 20 of 36

0/36 done

Module 20: Auto-Waiting & Synchronization

20. Actionability Checks & Intelligent Synchronization

16 minlesson

Try it yourself

Implement isActionableState:

  1. Accept status with attached, visible, stable, and enabled booleans.
  2. Return true only if all 4 conditions are true.

The editor has a worked example. Read it, tweak it, then press Mark as complete.

Playwright's Actionability Matrix

Before executing an action like .click(), Playwright ensures the element passes every check:

  1. Attached: Element is present in the DOM.
  2. Visible: Element has non-empty bounding box and no display: none / visibility: hidden.
  3. Stable: CSS animations/transitions have finished moving.
  4. Receives Events: Not obscured by modal backdrops or loading spinners.
  5. Enabled: disabled attribute is false.

Never Use page.waitForTimeout() (Hard Sleeps)

typescript
// ❌ ANTI-PATTERN: Arbitrary sleep freezes execution and causes flakiness
await page.waitForTimeout(5000);

// ✅ BEST PRACTICE: Wait for the exact network response or UI condition
const responsePromise = page.waitForResponse("**/api/checkout");
await page.getByRole("button", { name: "Place Order" }).click();
const response = await responsePromise;
expect(response.status()).toBe(200);

Waiting for Page Load States

typescript
// Wait until DOM is loaded:
await page.waitForLoadState("domcontentloaded");

// Wait until all initial network requests are quiet:
await page.waitForLoadState("networkidle");

Key Takeaways & Walkthrough Summary

Say goodbye to arbitrary sleep timers! Hard sleeps slow down test suites and create flaky runs. In this lesson, we will explore Playwright's actionability checks, auto-waiting mechanics, and how to synchronize with network responses and route navigation cleanly. Let's eliminate flakiness forever!

Part of the Playwright + TS course (Playwright with TypeScript). Practice this concept live in the Playwright Online Compiler or explore our free automation practice form.

Common Doubts & Lesson Q&A

Why should networkidle be used cautiously in modern Single Page Apps?

Many SPAs use persistent WebSockets or polling intervals. 'networkidle' might wait for 30 seconds if background analytics keep polling. Prefer waiting for specific API responses or UI assertions.

How do I change the timeout for a single specific click?

Pass { timeout: 10000 } directly into the action options: await page.getByRole('button').click({ timeout: 10000 });

Finished reading?

Review the worked example in the editor, then mark the lesson complete.

TypeScript
Skip