Testing

What Is Playwright? Browser Testing and Automation Explained

Learn how Playwright automates Chromium, Firefox, and WebKit, including auto-waiting, locators, browser contexts, projects, network mocking, tracing, and visual testing.

What Is Playwright? Browser Testing and Automation Explained

Playwright is an open-source browser automation project from Microsoft. Its test tooling can drive Chromium, Firefox, and WebKit and includes features for test isolation, auto-waiting, parallel execution, tracing, screenshots, and network control.

A small test can exercise a real user path:

import { test, expect } from '@playwright/test';

test('signs in', async ({ page }) => {
  await page.goto('/login');
  await page.getByLabel('Email').fill('user@example.com');
  await page.getByLabel('Password').fill('secret');
  await page.getByRole('button', { name: 'Sign in' }).click();

  await expect(page.getByRole('heading', { name: 'Dashboard' })).toBeVisible();
});

The same style of test can run against several browser projects.

Playwright Test Combines the Runner and Browser Automation

For JavaScript and TypeScript projects, Playwright Test supplies the test runner, assertions, browser fixtures, isolation, retries, parallelism, reports, and debugging tools.

Playwright also provides browser automation libraries for TypeScript/JavaScript, Python, .NET, and Java.

The browser engines commonly used by Playwright are:

Chromium
Firefox
WebKit

Playwright can also run branded Chrome and Microsoft Edge channels where supported. Its WebKit build is based on WebKit, but it is not the branded Safari browser.

Auto-Waiting Happens Around Actions

Timing is a common source of browser-test failures. A test may try to click an element while it is still animating, disabled, hidden, or covered by another element.

Playwright performs actionability checks before many actions. For locator.click(), for example, it checks that the locator resolves appropriately and that the target is visible, stable, able to receive events, and enabled.

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

That removes many explicit waits, but it does not mean every asynchronous condition is automatic. Tests still need assertions or waits for application-specific outcomes such as a background job completing or a particular response arriving.

Prefer assertions about the state the user or application needs instead of arbitrary sleeps.

Locators Describe How to Find UI Elements

Playwright recommends locators as the main way to find and act on elements.

Useful examples include:

page.getByRole('button', { name: 'Submit' });
page.getByLabel('Email');
page.getByText('Order complete');
page.getByTestId('checkout-total');

Role, label, and text locators often follow the user-visible interface. Test IDs are useful when the UI does not expose a stable semantic locator.

Locators are also central to Playwright’s waiting and retry behaviour, so replacing them with long CSS selectors can make tests more coupled to markup structure.

Browser Contexts Isolate Tests

A browser context is an isolated browser session with its own cookies, storage, and session state.

Browser
├── Context A
│   └── Page
└── Context B
    └── Page

Playwright Test creates isolated browser contexts for tests by default. This lets tests start from clean state without launching a completely separate browser process for each one.

Authentication state can be prepared and reused deliberately when a suite needs signed-in tests, but shared state should not make tests depend on execution order.

Projects Run Tests Under Different Configurations

Playwright projects let the same suite run with different browser, device, environment, or other configuration.

export default defineConfig({
  projects: [
    { name: 'chromium', use: { ...devices['Desktop Chrome'] } },
    { name: 'firefox', use: { ...devices['Desktop Firefox'] } },
    { name: 'webkit', use: { ...devices['Desktop Safari'] } },
  ],
});

Projects can also represent logged-in versus logged-out state, staging versus another environment, or different retry and timeout settings.

Cross-browser coverage should reflect the browsers the product actually supports rather than running every test everywhere by default.

Network Control Helps Create Deterministic Tests

Playwright can observe, modify, abort, or fulfill network requests.

A test can replace an API response:

await page.route('**/api/profile', async route => {
  await route.fulfill({
    status: 200,
    contentType: 'application/json',
    body: JSON.stringify({ name: 'Sarah', plan: 'Pro' }),
  });
});

This is useful for controlled error states, third-party dependencies, and tests that need deterministic data.

Network mocking narrows the test to the behaviour around the simulated response. A checkout test with a mocked payment request can verify the UI around a decline, but it does not verify the real payment integration.

Trace Viewer Helps Investigate Failures

Playwright traces can record information such as actions, DOM snapshots, network activity, console messages, and screenshots.

A failed CI test can therefore be inspected after the run instead of relying only on a stack trace or rerunning the failure locally.

Tracing can add storage and runtime overhead, so suites often configure it selectively, such as retaining traces for failures or retries.

Screenshots and video can provide additional evidence when a failure is visual or timing-related.

Visual Comparisons Are Built In

Playwright Test supports screenshot assertions:

await expect(page).toHaveScreenshot('checkout.png');

A later run can compare the current rendering with an approved reference image.

Visual comparisons need controlled test data and rendering environments. Browser version, operating system, fonts, animations, and dynamic content can create diffs that are unrelated to a product regression.

Use screenshot assertions for states where appearance itself is part of the requirement.

Playwright Can Test APIs Alongside the UI

Playwright Test includes an API request context, so a suite can make HTTP requests without driving a page.

That is useful for:

creating test data before a browser test
checking an API response
cleaning up test state
combining API setup with a UI workflow

It does not turn every browser suite into an API integration suite. Keep assertions at the level needed for the behaviour under test.

Playwright and Selenium Use Different Tooling Models

Selenium WebDriver is a W3C-standard browser automation ecosystem with broad language and browser support. Playwright provides its own automation stack and tightly integrated testing tools.

A simple feature table can hide important differences in deployment requirements, browser versions, language bindings, grid infrastructure, and existing team investment.

For a new web project, Playwright’s integrated runner and debugging tools may reduce setup. An established Selenium suite may have little reason to migrate unless the team has a specific maintenance or capability problem to solve.

Evaluate the test environment rather than treating one framework as a universal replacement for the other.

Use Playwright Where Browser Behaviour Matters

Playwright is a good fit for tests that need to exercise a rendered web application:

sign-in and account flows
checkout and forms
cross-browser behaviour
responsive UI
network failure states
visual comparisons
browser permissions
multi-page workflows

It can also automate browsers outside a test suite, but browser automation is more expensive and slower than unit-level checks. Keep business rules and pure transformations at lower test layers where a browser adds no useful evidence.

Playwright is strongest when the browser itself, the rendered interface, or the interaction between frontend and network behaviour is part of what needs to be verified.

Top