Playwright
Playwright
Section titled “Playwright”Playwright MCP provides browser automation using accessibility tree for fast, LLM-friendly interaction. Built by Microsoft, it enables cross-browser testing without vision models.
Configuration
Section titled “Configuration”{ "mcpServers": { "playwright": { "command": "npx", "args": ["-y", "@playwright/mcp"] } }}Command-Line Options
Section titled “Command-Line Options”| Option | Description | Example |
|---|---|---|
--browser | Browser to use | chrome, firefox, webkit, msedge |
--headless | Run without UI | Flag only |
--viewport-size | Window size | 1280x720 |
--device | Device emulation | iPhone 15, Pixel 7 |
Example with options:
{ "playwright": { "command": "npx", "args": ["-y", "@playwright/mcp", "--browser", "chrome", "--headless"] }}Available Tools
Section titled “Available Tools”Navigation
Section titled “Navigation”| Tool | Description |
|---|---|
browser_navigate | Go to a URL |
browser_go_back | Navigate back |
browser_go_forward | Navigate forward |
browser_wait | Wait for page load |
Interaction
Section titled “Interaction”| Tool | Description |
|---|---|
browser_click | Click element |
browser_type | Type text |
browser_fill | Fill input field |
browser_select_option | Select dropdown option |
browser_press_key | Press keyboard key |
Content
Section titled “Content”| Tool | Description |
|---|---|
browser_snapshot | Get accessibility tree |
browser_take_screenshot | Capture screenshot |
browser_get_text | Get element text |
| Tool | Description |
|---|---|
browser_tab_list | List open tabs |
browser_tab_new | Open new tab |
browser_tab_select | Switch to tab |
browser_tab_close | Close tab |
Use Cases
Section titled “Use Cases”E2E Testing
Section titled “E2E Testing”/test e2e Login flow should redirect to dashboardPlaywright:
- Navigates to login page
- Fills credentials
- Clicks submit
- Verifies dashboard redirect
UI Review
Section titled “UI Review”/review Visual regression on mobilePlaywright:
- Opens page with device emulation
- Takes screenshots
- Compares with expected layout
Form Testing
Section titled “Form Testing”/test Form validation for user registrationPlaywright:
- Fills form with various inputs
- Tests validation messages
- Verifies submission behavior
Command Integration
Section titled “Command Integration”| Command | How Playwright Helps |
|---|---|
/test | E2E browser tests |
/fix | Reproduce UI bugs in browser |
/review | Visual verification of changes |
/feature | Test new UI features |
Example Workflow
Section titled “Example Workflow”Testing Login Flow
Section titled “Testing Login Flow”// Playwright MCP workflowbrowser_navigate("https://app.example.com/login")browser_fill("#email", "test@example.com")browser_fill("#password", "password123")browser_click("#login-button")browser_wait() // Wait for navigationbrowser_take_screenshot("after-login.png")Mobile Testing
Section titled “Mobile Testing”// With device emulation (--device "iPhone 15")browser_navigate("https://app.example.com")browser_snapshot() // Get accessibility treebrowser_take_screenshot("mobile-view.png")Key Features
Section titled “Key Features”Accessibility Tree
Section titled “Accessibility Tree”Playwright uses the accessibility tree instead of pixels:
- Faster — No image processing needed
- LLM-friendly — Structured element data
- Cross-browser — Works identically everywhere
Device Emulation
Section titled “Device Emulation”Test on different devices without physical hardware:
- iPhone, iPad, Pixel, Galaxy
- Custom viewport sizes
- Touch vs mouse input
Profile Management
Section titled “Profile Management”Playwright can maintain browser state across sessions for authenticated testing.
Best Practices
Section titled “Best Practices”Use Headless Mode
Section titled “Use Headless Mode”For automated testing, use --headless to run without UI overhead.
Wait for Elements
Section titled “Wait for Elements”Always use browser_wait or wait for specific elements before interaction.
Accessibility First
Section titled “Accessibility First”Use accessibility tree (browser_snapshot) to understand page structure before clicking.
Troubleshooting
Section titled “Troubleshooting”Element Not Found
Section titled “Element Not Found”If clicking fails:
- Use
browser_snapshotto see available elements - Check if element needs scrolling
- Wait for dynamic content to load
Browser Not Starting
Section titled “Browser Not Starting”- Ensure browser is installed
- Check Node.js version (18+)
- Try different browser with
--browser