Dung (Donny) Nguyen

Senior Software Engineer

Playwright

Playwright is an open-source, end-to-end testing and browser automation framework developed by Microsoft. It lets us write reliable tests that drive real browsers—Chromium, Firefox, and WebKit—with a single API, across Windows, macOS, and Linux. Playwright is popular for testing modern web applications because it handles dynamic content, single-page apps, and complex user flows with minimal flakiness.

Key Features

Core Concepts

Installation

Using Node.js, we can scaffold a project with the official test runner:

npm init playwright@latest

This installs Playwright, downloads the browser binaries, and creates a sample configuration and example tests. To install browsers manually later:

npx playwright install

For Python:

pip install pytest-playwright
playwright install

A Basic Test

Here is a simple test using the @playwright/test runner:

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

test('has title', async ({ page }) => {
  await page.goto('https://playwright.dev/');

  // Expect the page title to contain "Playwright".
  await expect(page).toHaveTitle(/Playwright/);
});

test('get started link', async ({ page }) => {
  await page.goto('https://playwright.dev/');

  // Click the "Get started" link.
  await page.getByRole('link', { name: 'Get started' }).click();

  // Expect the page to have a heading named "Installation".
  await expect(
    page.getByRole('heading', { name: 'Installation' })
  ).toBeVisible();
});

Run the tests with:

npx playwright test

Locators and Actions

Playwright recommends user-facing locators that resemble how a person perceives the page:

// By role (recommended for accessibility and stability)
await page.getByRole('button', { name: 'Submit' }).click();

// By label text
await page.getByLabel('Email').fill('user@example.com');

// By placeholder, text, or test id
await page.getByPlaceholder('Search').fill('Playwright');
await page.getByText('Welcome').click();
await page.getByTestId('login-form').isVisible();

Common actions include click(), fill(), type(), check(), selectOption(), hover(), and press().

Debugging and Tooling

When to Use Playwright

Playwright is a strong choice for end-to-end and integration testing of web applications, cross-browser compatibility checks, and automating repetitive browser tasks or scraping. Its auto-waiting, isolation model, and rich tooling make it especially well suited to modern, JavaScript-heavy web apps where reliability matters.

References: