Playwright

On this page 7

Playwright is an open-source browser automation and end-to-end testing framework that controls Chromium, Firefox and WebKit through a single API. In web testing it is used to simulate real user journeys, such as signing in, filling forms and completing a checkout, and to verify that a web application behaves correctly across rendering engines.

Playwright provides official libraries for JavaScript and TypeScript, Python, Java and .NET, and its Node.js package ships a complete test runner. Its design focuses on reliable, fast and isolated tests, addressing the flakiness that has long troubled browser-based test suites.

How Playwright controls browsers

Playwright communicates with browsers over persistent connections rather than through a separate driver per command. For Chromium it uses the Chrome DevTools Protocol, while for Firefox and WebKit it relies on patched browser builds that expose equivalent protocols. Each Playwright release is pinned to specific browser versions, which keeps behavior reproducible.

The object model has three main layers. A browser is a launched engine, a browser context is an isolated session similar to a fresh incognito profile with its own cookies and storage, and a page is a tab inside a context. Creating a new context takes only milliseconds, so each test can start from a clean state without relaunching the browser.

The table below summarizes the core objects and what each isolates or controls.

ObjectWhat it representsTypical use
BrowserOne running engine instanceShared across many tests in a worker
BrowserContextIsolated session with separate storageOne per test for clean state
PageA single tab or popupNavigation, actions and assertions
LocatorA lazy, retrying reference to elementsFinding buttons, fields and text
APIRequestContextHTTP client sharing context cookiesSeeding data or testing APIs directly
Core Playwright objects

Auto-waiting, locators and assertions

Playwright’s main defense against flaky tests is auto-waiting. Before clicking, typing or checking an element, it waits until the element is attached, visible, stable, enabled and able to receive events. This removes most of the arbitrary sleep statements that older test suites relied on.

Locators re-query the page every time they are used, so they survive re-renders in frameworks such as React or Vue. Playwright recommends user-facing locators, such as getByRole, getByLabel and getByText, because they mirror how people and assistive technology perceive the page and break less often than CSS selectors tied to markup.

Web-first assertions such as toBeVisible or toHaveText retry until the condition holds or a timeout expires. The default expectation timeout is 5 seconds, and the default test timeout is 30 seconds; both can be configured globally or per test.

Playwright compared with other testing tools

Playwright competes and overlaps with several established tools in the web testing topic. The main differences concern browser coverage, architecture and the scope of the bundled tooling.

ToolBrowser coverageArchitectureStrength
PlaywrightChromium, Firefox, WebKitOut-of-process, protocol connectionCross-engine tests with built-in runner
SeleniumAll major browsers via WebDriverW3C WebDriver with browser driversBroad language and grid support
CypressChromium family, Firefox, experimental WebKitRuns inside the browser alongside the appInteractive debugging for front-end teams
PuppeteerChromium, FirefoxDevTools protocol and WebDriver BiDiScripting and automation of Chrome
JestNo real browser by defaultUnit test runner with simulated DOMFast unit and component tests
Playwright and neighboring automation tools

Playwright was created by engineers who previously built Puppeteer, which explains the similar API. The decisive differences are first-class WebKit support, which approximates Safari behavior, and a test runner with parallelism, retries, fixtures and reporting included rather than assembled from separate packages.

Configuring Playwright for a project

A Playwright project is driven by a configuration file that defines projects, browsers, devices, base URL, retries and reporters. Tests run in parallel across worker processes by default, and each project can target a different engine or an emulated device such as a specific phone viewport.

SettingTypical valueEffect
workersHalf the CPU cores locally, fewer in CIControls parallel test processes
retries0 locally, 1 to 2 in CIRe-runs failed tests to expose flakiness
traceon-first-retryRecords a trace only when a retry happens
projectschromium, firefox, webkit, mobileRuns the suite against several targets
webServerCommand to start the appLaunches the app before tests begin
Important Playwright settings and typical values

The Trace Viewer is one of the most valuable debugging tools. A trace records actions, DOM snapshots, network requests and console output for each step, so a failure in continuous integration can be replayed step by step on a developer machine. Codegen, which records interactions and generates test code, helps beginners but usually needs editing for maintainability.

Limitations and common pitfalls

Playwright reduces flakiness but does not eliminate it. Most remaining instability comes from shared test data, timing assumptions and environment differences between local machines and CI runners.

ProblemCauseRemedy
Tests pass alone but fail in parallelShared accounts or database recordsCreate isolated data per test or worker
Selectors break after redesignsCSS selectors tied to markupUse role, label and test-id locators
WebKit results differ from SafariPlaywright WebKit is not branded SafariConfirm critical flows on real Apple devices
Slow suitesLogging in through the UI in every testReuse saved authentication state
Timeouts in CI onlySlower runners, cold cachesTune workers and investigate traces
Frequent Playwright problems

Playwright is also not a replacement for every test layer. Unit tests remain faster for pure logic, and visual regression or accessibility testing require additional assertions or integrations, although Playwright offers screenshot comparison and works well with accessibility rule engines.

Stripped to the essentials

Playwright is a tool that lets a program use a website the way a person would. It opens a browser, clicks buttons, types into forms and then checks that the right things appear on screen.

Teams write these steps once and run them automatically after every code change. Because Playwright can drive the engines behind Chrome, Firefox and Safari, one set of tests checks the site in all three, catching problems before real visitors find them.

Where it is applied

An online travel agency has a booking flow that searches flights, selects seats and takes payment in a test mode. The team writes forty Playwright tests in TypeScript and configures three projects for Chromium, Firefox and WebKit, plus one emulated phone viewport.

In continuous integration, the suite runs on every pull request with four workers and one retry with tracing enabled. One week, a WebKit test fails because a date picker overlaps the confirm button on narrow screens. The trace shows the overlap step by step, the developer fixes the layout, and the bug never reaches Safari users in production.