Skip to content

Lesson 1 of 9 · 24m

The Playwright Mental Model: Browsers, Contexts, and Auto-Waiting

Understand what Playwright actually controls, how browser contexts isolate state, and why actionability checks matter more than sleeps.

The worst end-to-end suites I inherit usually have the same origin story: somebody treated the browser like a remote-control puppet instead of a concurrent, asynchronous application. They clicked coordinates, slept for three seconds, and declared victory. It worked on their laptop, then failed every Tuesday in CI. Playwright's core value is not that it can drive Chromium. It is that it models browser behavior honestly.

The object hierarchy is the isolation model

Playwright separates the browser process, isolated contexts, and pages:

Browser       one Chromium/Firefox/WebKit process
  Context     isolated cookies, storage, permissions, viewport
    Page      one tab or application window
      Frame   documents inside the page

A context is cheap compared with launching a browser. Use one per test so cookies, localStorage, IndexedDB, cache storage, and permissions do not bleed across cases. A page is not isolated merely because it is new; two pages in the same context share storage.

In the normal Playwright Test runner, the page fixture already gives each test a fresh context. If you create pages manually, know which context owns them.

Actions are gated by actionability

Playwright does not blindly click because an element exists. Before click, it generally waits for the element to be visible, stable, enabled, and receiving events. Before fill, it waits for editability. This is deliberate: the browser is animated, reactive, and sometimes still hydrating.

await page.getByRole('button', { name: 'Save changes' }).click();

That one line encodes several waits. Replacing it with this loses the contract:

await page.click('#save', { force: true });
await page.waitForTimeout(2000);

force bypasses important diagnostics. waitForTimeout waits for a number, not a condition. Neither tells you whether the application was actually ready.

Assertions are the synchronization point

End-to-end tests synchronize around observable outcomes. Prefer web-first assertions because they retry until timeout:

await expect(page.getByText('Invoice paid')).toBeVisible();
await expect(page).toHaveURL(/\/invoices\/inv_123$/);
await expect(page.getByLabel('Email')).toHaveValue('alice@example.test');

The assertion is not just validation. It tells the test when the asynchronous application has reached the next meaningful state.

Field notes

  • Browser, context, page, and frame are different failure domains; know which one leaked before adding cleanup code.
  • force: true, coordinate clicks, and sleeps are occasionally useful diagnostics but poor defaults.
  • Auto-waiting does not know your business state. Wait for the response, element, or URL that proves that state.
  • A new tab is not a new identity. Identity isolation comes from the context, not the number of tabs.
  • Read timeout errors carefully: Playwright often tells you whether the element was hidden, detached, covered, or unstable.

Was this lesson helpful?

Sign in to rate