| name | playwright |
| description | Browser automation for testing with Playwright - navigate pages, interact with elements, take screenshots, and verify UI behavior |
Playwright Browser Automation
You are a browser automation assistant using Playwright for testing and verification.
## Setup
Ensure Playwright is installed in the project:
npx playwright install chromium
Core Patterns
Navigate and Verify
import { chromium } from 'playwright'
const browser = await chromium.launch()
const page = await browser.newPage()
await page.goto('http://localhost:3000')
await page.waitForLoadState('networkidle')
Element Interaction
await page.click('button:has-text("Submit")')
await page.click('[data-testid="login-btn"]')
await page.fill('input[name="email"]', 'user@example.com')
await page.fill('input[name="password"]', 'password123')
await page.selectOption('select#role', 'admin')
await page.check('input[type="checkbox"]')
Assertions
await expect(page.locator('h1')).toHaveText('Dashboard')
await expect(page.locator('.error')).toBeVisible()
await expect(page.locator('.spinner')).toBeHidden()
await expect(page).toHaveURL(/.*dashboard/)
await expect(page.locator('li.item')).toHaveCount(5)
Screenshots
await page.screenshot({ path: 'screenshot.png', fullPage: true })
await page.locator('.chart').screenshot({ path: 'chart.png' })
Waiting
await page.waitForURL('**/dashboard')
await page.waitForSelector('.loaded')
await page.waitForResponse(resp => resp.url().includes('/api/data'))
Testing Strategy
- Start the dev server before running browser tests
- Use data-testid attributes for reliable element selection
- Wait for network idle after navigation to ensure page is fully loaded
- Take screenshots at key verification points for visual evidence
- Clean up: always close the browser in a finally block
Common Selectors Priority
[data-testid="..."] - most reliable
role=button[name="..."] - accessible
text="..." - readable but fragile
.class-name - depends on styling
- CSS selectors - last resort
Error Recovery
- If element not found, wait and retry with increased timeout
- If navigation fails, check if dev server is running
- Take a screenshot on failure for debugging