home / skills / laurigates / claude-plugins / playwright-testing

playwright-testing skill

/testing-plugin/skills/playwright-testing

This skill helps you write reliable end-to-end tests across Chromium, Firefox, and WebKit with Playwright.

npx playbooks add skill laurigates/claude-plugins --skill playwright-testing

Review the files below or copy the command above to add this skill to your agents.

Files (2)
SKILL.md
5.4 KB
---
model: haiku
created: 2025-12-16
modified: 2026-02-14
reviewed: 2025-12-16
name: playwright-testing
description: |
  Playwright end-to-end testing for web applications. Cross-browser testing (Chromium, Firefox, WebKit),
  visual regression, API testing, mobile emulation. Use when writing E2E tests, testing across browsers,
  or setting up automated UI testing workflows.
allowed-tools: Glob, Grep, Read, Bash, Edit, Write, TodoWrite, WebFetch, WebSearch, BashOutput, KillShell
---

# Playwright Testing

Playwright is a modern end-to-end testing framework for web applications. It provides reliable, fast, and cross-browser testing with excellent developer experience.

## When to Use This Skill

| Use this skill when... | Use another skill instead when... |
|------------------------|----------------------------------|
| Writing E2E browser tests | Writing unit tests (use vitest-testing) |
| Testing across Chromium, Firefox, WebKit | Testing Python code (use python-testing) |
| Setting up visual regression testing | Analyzing test quality (use test-quality-analysis) |
| Mocking network requests in E2E tests | Generating property-based tests (use property-based-testing) |
| Testing mobile viewports | Testing API contracts only (use api-testing) |

## Core Expertise

- **Cross-browser**: Test on Chromium, Firefox, WebKit (Safari)
- **Reliable**: Auto-wait, auto-retry, no flaky tests
- **Fast**: Parallel execution, browser context isolation
- **Modern**: TypeScript-first, async/await, auto-complete
- **Multi-platform**: Windows, macOS, Linux

## Installation

```bash
bun create playwright                  # Initialize (recommended)
bun add --dev @playwright/test         # Or install manually
bunx playwright install                # Install browsers
bunx playwright install --with-deps    # With system deps (Linux)
bunx playwright --version              # Verify
```

## Configuration (playwright.config.ts)

```typescript
import { defineConfig, devices } from '@playwright/test';

export default defineConfig({
  testDir: './tests',
  timeout: 30000,
  use: {
    baseURL: 'http://localhost:3000',
    trace: 'on-first-retry',
  },
  projects: [
    {
      name: 'chromium',
      use: { ...devices['Desktop Chrome'] },
    },
  ],
});
```

## Essential Commands

```bash
bunx playwright test                           # Run all tests
bunx playwright test tests/login.spec.ts       # Specific file
bunx playwright test --headed                  # See browser
bunx playwright test --debug                   # Debug mode
bunx playwright test --project=chromium        # Specific browser
bunx playwright test --ui                      # UI mode
bunx playwright codegen http://localhost:3000  # Record tests
bunx playwright show-report                    # Last report
bunx playwright show-trace trace.zip           # Trace viewer
bunx playwright test --update-snapshots        # Update snapshots
```

## Writing Tests

### Basic Test Structure

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

test('basic test', async ({ page }) => {
  await page.goto('https://example.com');
  await expect(page).toHaveTitle(/Example/);
});

test.describe('login flow', () => {
  test('should login successfully', async ({ page }) => {
    await page.goto('/login');
    await page.fill('input[name="email"]', '[email protected]');
    await page.fill('input[name="password"]', 'password123');
    await page.click('button[type="submit"]');
    await expect(page).toHaveURL('/dashboard');
  });
});
```

### Selectors and Locators

```typescript
// Text/Role selectors (recommended)
await page.getByText('Sign in').click();
await page.getByRole('button', { name: 'Submit' }).click();
await page.getByLabel('Email').fill('[email protected]');
await page.getByPlaceholder('Enter your name').fill('John');
await page.getByTestId('login-button').click();

// CSS/XPath selectors
await page.locator('.button-primary').click();
await page.locator('xpath=//button[text()="Submit"]').click();

// Chaining
await page.locator('.card').filter({ hasText: 'Product' }).getByRole('button').click();
```

### Key Assertions

| Assertion | Description |
|-----------|-------------|
| `toHaveTitle(title)` | Page title |
| `toHaveURL(url)` | Page URL |
| `toBeVisible()` | Element visible |
| `toBeEnabled()` | Element enabled |
| `toHaveText(text)` | Element text |
| `toContainText(text)` | Partial text |
| `toHaveAttribute(name, value)` | Attribute value |
| `toHaveClass(class)` | CSS class |
| `toHaveValue(value)` | Input value |
| `toBeEmpty()` | Empty input |
| `toHaveCount(n)` | Element count |
| `not.toBeDisabled()` | Negation |

## Agentic Optimizations

| Context | Command |
|---------|---------|
| Quick test | `bunx playwright test --reporter=line --bail` |
| CI test | `bunx playwright test --reporter=github` |
| Single browser | `bunx playwright test --project=chromium --reporter=line` |
| Debug failing | `bunx playwright test --trace on --reporter=line` |
| Headed debug | `bunx playwright test --headed --debug` |

For detailed examples, advanced patterns, and best practices, see [REFERENCE.md](REFERENCE.md).

## References

- Official docs: https://playwright.dev
- Configuration: https://playwright.dev/docs/test-configuration
- API reference: https://playwright.dev/docs/api/class-test
- Best practices: https://playwright.dev/docs/best-practices
- CI/CD: https://playwright.dev/docs/ci
- Trace viewer: https://playwright.dev/docs/trace-viewer

Overview

This skill provides end-to-end Playwright testing capabilities for web applications, including cross-browser runs (Chromium, Firefox, WebKit), visual regression, API testing, and mobile emulation. It is designed to set up fast, reliable automated UI tests and integrate them into local dev workflows and CI pipelines. Use it to create robust E2E suites, capture traces, and validate UI across platforms.

How this skill works

The skill configures Playwright projects and test runners, installs required browsers, and exposes common commands to run, debug, and report tests. It supports TypeScript-first test files, browser-specific projects, auto-waiting and retry traces, snapshot updates, and test recording via codegen. You can run tests headless or headed, target single browsers or parallel cross-browser matrices, and collect traces or HTML reports for failures.

When to use it

  • When writing end-to-end browser tests that exercise real UI flows.
  • When you need reliable cross-browser coverage across Chromium, Firefox, and WebKit.
  • When adding visual regression or snapshot testing for UI changes.
  • When you want to emulate mobile devices and viewports in CI.
  • When you need integrated tracing, debugging, and test reporting for CI pipelines.

Best practices

  • Define projects for each target browser in playwright.config and reuse device presets.
  • Use page.getByRole / getByLabel / getByTestId locators to make tests resilient and accessible-aware.
  • Enable trace on-first-retry and capture artifacts (screenshots, videos) for failures.
  • Run tests in CI with a concise reporter (e.g., github or line) and set --bail for fast feedback.
  • Keep tests focused on user flows; mock network requests where unit or contract testing is more appropriate.

Example use cases

  • Verify a login flow across desktop Chrome, Safari (WebKit), and Firefox in CI.
  • Record interactions with codegen to bootstrap flaky UI tests and refine selectors.
  • Run visual snapshot tests and update snapshots after a verified UI change.
  • Emulate iPhone viewport and test responsive layouts and touch interactions.
  • Capture trace.zip on failure to inspect DOM snapshots, network, and screenshots.

FAQ

How do I run tests for a single browser?

Use the project flag, for example: bunx playwright test --project=chromium.

How can I debug a failing test locally?

Run tests in headed mode or with --debug and enable trace capture: bunx playwright test --headed --debug and use show-trace on the generated trace.zip.