home / skills / brettatoms / agent-skills / playwright

playwright skill

/playwright

This skill automates browser tasks using Playwright to navigate, fill forms, capture screenshots, and inspect pages with a stateful session.

npx playbooks add skill brettatoms/agent-skills --skill playwright

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

Files (4)
SKILL.md
3.8 KB
---
name: playwright
description: Browser automation for web testing and interaction. Use for navigating pages, filling forms, clicking elements, taking screenshots, and inspecting page content. Maintains stateful browser session across commands.
allowed-tools: Bash, Read
---

# Playwright Browser Automation Skill

Use **playwright-server** for browser automation via curl commands. The server maintains a stateful browser session.

## Prerequisites

The server is installed at `~/.claude/bin/playwright-server.js`. If not present:

```bash
cd ~/.claude/bin
npm init -y
npm install playwright
npx playwright install chromium
```

## Server Lifecycle

```bash
# Start server (if not running)
curl -sf localhost:3210/health || (node ~/.claude/bin/playwright-server.js &>/dev/null & sleep 1)

# Check health
curl -sf localhost:3210/health

# Stop server
pkill -f playwright-server
```

## Quick Reference

| Task | Command |
|------|---------|
| Start browser | `curl -s localhost:3210 -d '{"cmd":"start"}'` |
| Navigate | `curl -s localhost:3210 -d '{"cmd":"navigate","url":"URL"}'` |
| Get snapshot | `curl -s localhost:3210 -d '{"cmd":"snapshot"}'` |
| Click | `curl -s localhost:3210 -d '{"cmd":"click","selector":"SEL"}'` |
| Fill input | `curl -s localhost:3210 -d '{"cmd":"fill","selector":"SEL","value":"VAL"}'` |
| Screenshot | `curl -s localhost:3210 -d '{"cmd":"screenshot"}'` |
| Stop browser | `curl -s localhost:3210 -d '{"cmd":"stop"}'` |

## Core Commands

### Browser Lifecycle

```bash
curl -s localhost:3210 -d '{"cmd":"start"}'              # Headless
curl -s localhost:3210 -d '{"cmd":"start","headless":false}'  # Headed
curl -s localhost:3210 -d '{"cmd":"stop"}'
curl -s localhost:3210 -d '{"cmd":"status"}'
```

### Navigation

```bash
curl -s localhost:3210 -d '{"cmd":"navigate","url":"http://localhost:3000"}'
curl -s localhost:3210 -d '{"cmd":"back"}'
curl -s localhost:3210 -d '{"cmd":"reload"}'
```

### Page Inspection

**snapshot** - Get accessibility tree (use to understand page structure):

```bash
curl -s localhost:3210 -d '{"cmd":"snapshot"}'
```

Example output:
```
- heading "Page Title" [level=1]
- navigation:
  - link "Home"
  - link "About"
- main:
  - textbox "Email"
  - textbox "Password"
  - button "Sign In"
```

**screenshot** - Capture page:

```bash
curl -s localhost:3210 -d '{"cmd":"screenshot"}'
curl -s localhost:3210 -d '{"cmd":"screenshot","path":"/tmp/shot.png"}'
curl -s localhost:3210 -d '{"cmd":"screenshot","fullPage":true}'
```

### Basic Interactions

```bash
curl -s localhost:3210 -d '{"cmd":"click","selector":"button[type=submit]"}'
curl -s localhost:3210 -d '{"cmd":"fill","selector":"input[name=email]","value":"[email protected]"}'
curl -s localhost:3210 -d '{"cmd":"type","selector":"input","text":"query"}'
curl -s localhost:3210 -d '{"cmd":"press","key":"Enter"}'
```

### Waiting

```bash
curl -s localhost:3210 -d '{"cmd":"wait","selector":".loaded"}'
curl -s localhost:3210 -d '{"cmd":"wait","selector":".spinner","state":"hidden"}'
curl -s localhost:3210 -d '{"cmd":"waitUrl","pattern":"**/dashboard"}'
curl -s localhost:3210 -d '{"cmd":"waitLoad","state":"networkidle"}'
```

## Selector Basics

```bash
# CSS selectors
"button[type=submit]"
"#login-form input[name=email]"

# Text selectors
"text=Sign In"

# Role selectors
"role=button[name='Sign In']"

# Combining
"#form >> input[name=email]"
```

**For complete selector strategies**: See [references/selectors.md](references/selectors.md)

## Additional References

- **All commands**: See [references/commands.md](references/commands.md) for interaction, inspection, wait, tab, and dialog commands
- **Selectors**: See [references/selectors.md](references/selectors.md) for selector strategies
- **Workflows**: See [references/workflows.md](references/workflows.md) for common workflows and troubleshooting

Overview

This skill provides browser automation using a local playwright-server controlled via simple curl commands. It maintains a stateful browser session and exposes navigation, interaction, inspection, and screenshot capabilities. Use it to drive end-to-end flows, debug UI, or extract page structure without writing direct Playwright scripts.

How this skill works

The skill talks to a background playwright-server process on localhost (default port 3210) using JSON POST bodies sent with curl. Commands include starting and stopping the browser, navigating to URLs, taking snapshots and screenshots, clicking, filling and typing into elements, and waiting for conditions. The server preserves the browser context between commands so subsequent actions operate against the same page state.

When to use it

  • Automating manual test steps or smoke tests against a web app without building a full test suite.
  • Capturing screenshots or page snapshots for visual checks or documentation.
  • Interacting with forms and flows (login, checkout) to validate behavior or reproduce bugs.
  • Extracting page structure or accessibility tree for debugging or scraping tasks.
  • Triggering and observing client-side flows that require a real browser context.

Best practices

  • Start the server once per session and reuse the stateful browser for related commands to maintain cookies and local storage.
  • Prefer snapshot for structural inspection and screenshot for visual verification; combine both for robust checks.
  • Use explicit waits (wait, waitUrl, waitLoad) before asserting or interacting to avoid flakiness.
  • Address elements with stable selectors (data-test-id, role=, text=) rather than brittle CSS paths.
  • Run headed mode for debugging (headless=false) and headless for CI or fast automation.

Example use cases

  • Run a quick smoke test: start browser, navigate to /login, fill credentials, click submit, wait for dashboard.
  • Capture a full-page screenshot of a landing page for visual regression or documentation.
  • Inspect the accessibility tree with snapshot to identify missing roles or labels.
  • Automate multi-step workflows like adding items to cart and verifying checkout flow.
  • Reproduce intermittent UI bugs by recording interactions in headed mode and examining snapshots.

FAQ

How do I start the server if it's not running?

Start the background playwright-server script and ensure Playwright dependencies are installed; then use the start command via curl to launch the browser.

How can I avoid flaky interactions?

Use the provided wait commands (selector, waitUrl, waitLoad) to ensure pages and elements are ready before clicking or filling.