Playwright + TS · Lesson 20 of 36
Module 20: Auto-Waiting & Synchronization
Try it yourself
Implement isActionableState:
The editor has a worked example. Read it, tweak it, then press Mark as complete.
In this lesson
Before executing an action like .click(), Playwright ensures the element passes every check:
display: none / visibility: hidden.disabled attribute is false.page.waitForTimeout() (Hard Sleeps)// ❌ 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);// Wait until DOM is loaded:
await page.waitForLoadState("domcontentloaded");
// Wait until all initial network requests are quiet:
await page.waitForLoadState("networkidle");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.
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.
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.