Code Reference

Getting Started

Playwright · Reference cheat sheet

Playwright · Reference cheat sheet


📋 Overview

Playwright automates Chromium, Firefox, and WebKit for E2E tests. Same API in JavaScript/TypeScript and Python. Prefer role/label locators and auto-waiting assertions.

🔧 Core concepts

PieceRole
Browser / Context / PageIsolation hierarchy
LocatorsFind + act (auto-wait)
expectAssertions with retry
Trace / videoDebug flakes
ProjectsMulti-browser / auth setup

💡 Examples

Install (JS):

npm init playwright@latest
npx playwright test
npx playwright show-report

Install (Python):

pip install pytest-playwright
playwright install
pytest --browser chromium

First test (JS):

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

test('home has title', async ({ page }) => {
  await page.goto('/');
  await expect(page).toHaveTitle(/Docs/i);
});

First test (Python):

import re
from playwright.sync_api import Page, expect

def test_home_has_title(page: Page):
    page.goto("/")
    expect(page).to_have_title(re.compile("Docs", re.I))

⚠️ Pitfalls

  • Hard waitForTimeout / time.sleep → flake; assert on UI instead.
  • CSS-only selectors break often — prefer getByRole / get_by_role.
  • Keep secrets in env; never commit storageState with prod cookies.

On this page