Understanding Execution Context
The first part of mastering Playwright is understanding how it communicates with the browser. Unlike traditional tools like Selenium, Playwright uses the Chrome DevTools Protocol (CDP) to issue low-level commands directly to the browser. Let's break it down now.
Core Philosophy
There is no "wait for element". Playwright enforces auto-waiting by default. If your test is flaky, it means your assertions are trying to check a state before the application has settled.
Browser vs Context vs Page
A Browser instance is heavy. A Context is cheap. Always use browser contexts instead of launching new browsers between tests.
playwright-init.tsconst browser = await chromium.launch(); const context = await browser.newContext(); const page = await context.newPage();
Actionability Checks
Before Playwright executes any action on an element, it performs actionable checks. The element must be:
- Attached to the DOM
- Visible
- Stable, as in not animating or completed animation
- Receives Events, as in not obscured by other elements
- Enabled