Claude Code Playwright

From AI Wiki
Short description: AI-assisted test automation using Claude Code with Playwright framework

Template:Infobox software

Claude Code Playwright refers to the practice and integration of using Claude Code, Anthropic's agentic coding tool available in the terminal, IDEs, and via API, with Playwright test automation framework through the Model Context Protocol (MCP). This combination enables developers to generate, execute, and maintain browser automation and end-to-end tests using natural language commands and AI-assisted automation.[1][2] It is both a workflow pattern and a technical integration that allows large language models (LLMs) like Claude to interact with web browsers in a structured, deterministic manner, effectively giving Claude Code "vision" into web applications without relying on screenshot-based tools.[3]

The term "Claude Code Playwright" emerged in 2025 as shorthand for this specific workflow, popularized through developer blogs, Reddit discussions, and YouTube tutorials that demonstrate how the combination addresses limitations in AI coding assistants, such as the inability to visually inspect or iterate on web-based outputs.[4][5]

Overview

The integration consists of three main components that work together to provide AI-driven browser automation:

  • Claude Code - A CLI-based agentic coding assistant that uses Anthropic's AI models to understand codebases, execute routine development tasks, and automate coding from natural language instructions[2][6]
  • Playwright - An open-source browser automation and testing framework developed by Microsoft, providing one API to drive Chromium, Firefox, and WebKit[7]
  • Model Context Protocol (MCP) - A standardized protocol for connecting AI systems with external tools and data sources, enabling structured browser interaction[8]

The Playwright MCP server acts as a bridge between Claude Code and the browser, providing deterministic, text-based snapshots of web pages through Playwright's accessibility tree rather than pixel-based inputs, making it lightweight and LLM-friendly.[9][10]

History

Playwright was released by Microsoft in 2020 as an evolution of the Puppeteer project, gaining rapid adoption for cross-browser testing with built-in test runner, assertions, tracing, and parallelization capabilities.[11]

Claude Code was introduced by Anthropic in early 2025 as a terminal-based agentic coding tool designed to accelerate software development. It integrates with Claude 3.5 Sonnet and newer models to perform tasks like mapping entire codebases in seconds and executing routine coding operations.[12][13]

The Model Context Protocol was open-sourced by Anthropic on November 25, 2024, as a standard for connecting AI assistants to external systems.[8] Following this release, ExecuteAutomation and Microsoft developed Playwright MCP server implementations to enable browser automation capabilities for Claude and other AI systems.[10][14]

The integration of Claude Code with Playwright MCP was first documented in July 2025 by developer Simon Willison, who detailed how to add the MCP server to Claude Code for browser automation tasks.[4] Subsequent tutorials expanded on its use for self-correcting AI workflows, such as fixing UI bugs in real-time and generating Playwright tests.[15] By mid-2025, it had become a key technique for enhancing AI-driven web development.

Architecture

Technical Components

Component Description Function
Claude Code CLI Command-line interface for AI coding assistance Processes natural language commands and generates code
MCP Server Middleware implementing the Model Context Protocol Bridges communication between AI and browser
Playwright Core Browser automation framework Controls actual browser interactions
JSON-RPC Communication protocol Facilitates message passing between components
Accessibility Tree Structured page representation Provides deterministic element identification

Dynamic Element Identification

Unlike traditional Playwright automation that relies on CSS selectors or XPath expressions, the Playwright MCP uses a dynamic element identification system:[16]

  1. Web page is loaded in browser
  2. Playwright MCP scans the page's accessibility tree
  3. Unique `ref_id` is assigned to each interactive element
  4. Structured accessibility snapshot is created
  5. Claude Code receives the element map with semantic information
  6. User writes natural language test steps
  7. Claude matches descriptions to `ref_id` values or uses accessible locators
  8. Precise browser actions are executed

This approach enables Claude to "see" the browser output and iterate on code by identifying visual or functional issues and generating fixes autonomously.[3]

Features

Core Capabilities

  • Natural Language Test Generation - Write tests using plain English descriptions that Claude converts to Playwright code[17]
  • Self-Correction Capabilities - Claude can iterate on code by analyzing browser output, identifying issues, and generating fixes autonomously[3]
  • Structured Browser Interaction - Utilizes Playwright's accessibility tree for deterministic, text-based page interactions without visual processing[14]
  • Browser Control - Launch, navigate, and interact with web browsers programmatically across Chromium, Firefox, and WebKit
  • Screenshot and PDF Generation - Capture screenshots and generate PDFs during test execution for debugging
  • Code Generation - Automatically generate Playwright test scripts from recorded sessions or natural language
  • Context Extraction - Retrieve DOM elements and page structure for analysis
  • Session Persistence - Maintain browser state across test runs with persistent user profiles
  • Custom JavaScript Execution - Run arbitrary JavaScript in the browser context

Supported Operations

The Playwright MCP server provides several tools accessible through Claude:[18]

Tool Name Function Parameters
browser_navigate Navigates to specified URLs url (string)
browser_click Performs click actions on elements selector (string), options (object)
browser_fill Fills form fields with data selector (string), value (string)
browser_snapshot Captures structured snapshot of current page None
browser_screenshot Takes screenshot of page path (string), fullPage (boolean)
browser_generate_pdf Generates PDF of current page path (string)
browser_execute_js Executes custom JavaScript on page script (string)
get_context Retrieves page accessibility tree None
launch_browser Opens new browser instance headless (boolean), channel (string)

Language Bindings

Playwright supports multiple first-party client libraries that Claude Code can target by adjusting prompts and file paths:[7]

Language Package/Artifact Example Run Command Installation
TypeScript/JavaScript @playwright/test npx playwright test npm init playwright@latest
Python playwright + pytest pytest pip install playwright pytest-playwright
.NET (C#) Microsoft.Playwright dotnet test dotnet add package Microsoft.Playwright
Java com.microsoft.playwright mvn test / gradle test Maven/Gradle dependency

Installation and Configuration

Prerequisites

  • Node.js (version 14 or higher, 18+ recommended)
  • npm or yarn package manager
  • Claude Pro subscription or Claude Code access
  • MCP-compatible client (VS Code, Cursor, or Claude Desktop)

Installation Methods

Using npm (Global Installation)

# Install globally
npm install -g @executeautomation/playwright-mcp-server

# Or use with npx
npx @executeautomation/playwright-mcp-server

Claude Code CLI Integration

Add Playwright MCP to Claude Code using the command:[4]

# Initialize Playwright in project
npm init playwright@latest

# Add MCP server to Claude Code
claude mcp add playwright npx @playwright/mcp@latest

# For headless mode or custom options
claude mcp add playwright npx @playwright/mcp@latest --headless --no-sandbox

# Start Claude Code
claude

This persists the configuration in `~/.claude.json`.

Claude Desktop Configuration

Add to `claude_desktop_config.json`:[19]

{
  "mcpServers": {
    "playwright": {
      "command": "npx",
      "args": ["-y", "@executeautomation/playwright-mcp-server"]
    }
  }
}

Project-Level Configuration

Create `.mcp.json` in project root:[20]

{
  "mcpServers": {
    "playwright": {
      "type": "stdio",
      "command": "npx",
      "args": ["@playwright/mcp@latest"],
      "env": {
        "HEADLESS": "false",
        "BROWSER": "chromium"
      }
    }
  }
}

Docker Deployment

For scalable headless deployment:

FROM mcr.microsoft.com/playwright:v1.40.0-focal
RUN npm install -g @playwright/mcp@latest
ENTRYPOINT ["npx", "@playwright/mcp@latest"]

Usage

Basic Test Generation

To generate a Playwright test using Claude Code:[1]

  1. Navigate to project directory
  2. Run `claude` to start interactive session
  3. Use natural language to describe test scenario
  4. Claude will generate and execute Playwright code

Example prompt:

Please test the login functionality:
1. Navigate to https://example.com/login
2. Enter username "testuser"
3. Enter password "testpass"
4. Click login button
5. Verify successful login by checking for welcome message

Common Claude Code Prompts

These illustrate natural-language tasks developers often ask Claude Code to perform with Playwright:[2]

  • "Migrate selectors to `getByRole`/`getByLabelText` accessible locators and update failing specs accordingly."
  • "Split long E2E test into smaller specs and add `test.step` annotations for trace readability."
  • "Refactor to Page Object Model and deduplicate auth flows across tests."
  • "Create CI workflow that runs on push, uploads HTML report, and retains traces on failure."
  • "Generate tests for the shopping cart flow including add, remove, and checkout operations."
  • "Debug why the login test is flaky and add proper waits or assertions."

Advanced Features

Custom Commands

Store reusable test templates in `.claude/commands/` directory:[21]

# .claude/commands/test-login.md
Test user authentication flow with following steps:
$ARGUMENTS

YAML-Based Testing

The Claude Code Playwright integration supports YAML-based test definitions:[16]

name: E2E Test Suite
tags:
  - smoke
  - regression
environment:
  url: https://staging.example.com
steps:
  - include: "login"
  - "Click Add to Cart button for first product"
  - "Verify cart contains 1 item"
  - "Navigate to checkout"
  - "Complete purchase with test card"
  - "Verify order confirmation"

Example TypeScript Test

Below is a minimal Playwright test that Claude Code could generate or edit:[22]

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

test.describe('User Authentication', () => {
  test('successful login flow', async ({ page }) => {
    await page.goto('https://example.com/login');
    
    // Using accessible locators
    await page.getByLabel('Email').fill('[email protected]');
    await page.getByLabel('Password').fill('secure_password');
    await page.getByRole('button', { name: 'Sign In' }).click();
    
    // Verify successful login
    await expect(page.getByText('Welcome back')).toBeVisible();
    await expect(page).toHaveURL('/dashboard');
  });
});

Best Practices

Context Management

  • Use `/clear` command frequently between unrelated tasks to manage token limits[23]
  • Implement `/compact` when approaching context limits
  • Store project-specific instructions in `CLAUDE.md` file
  • Keep prompts focused on specific test scenarios

Test Organization

Practice Description Example
Page Object Model Encapsulate page interactions in reusable classes LoginPage, CheckoutPage classes
Test Fixtures Share common setup between tests Authentication, database seeding
Parameterized Steps Create flexible, reusable step libraries Generic click, fill, verify functions
Environment Configuration Support multiple test environments dev/staging/production configs
Session Management Maintain browser state efficiently Persistent auth contexts
Parallel Execution Run tests concurrently for speed Sharding, worker configuration

Security and Licensing Considerations

  • Source Review - Treat AI-proposed diffs as suggestions, review for data exposure, flaky waits, or brittle selectors before committing[6]
  • Permissions - Avoid using `--dangerously-skip-permissions` in production environments[24]
  • Tool Access - Define specific tool permissions for Claude in configuration
  • Sensitive Data - Store credentials and sensitive data in environment variables, never in code
  • Licensing - Playwright is Apache License 2.0; Claude Code usage is governed by Anthropic's service terms[11]

Running at Scale

  • CI/CD Integration - Playwright runs on most CI systems with official guides for GitHub Actions, Azure Pipelines, Jenkins, CircleCI[25]
  • Cloud Execution - Microsoft offers Azure Playwright Testing for at-scale hosted parallel execution[26]
  • Model Access - Claude models are accessible via Claude API and cloud platforms like Amazon Bedrock, enabling enterprise routing and governance[27]

Challenges and Limitations

Technical Limitations

  • Browser storage APIs (localStorage, sessionStorage) are not supported in Claude.ai artifacts[8]
  • Maximum of 20 tool calls per research session
  • Context window limitations requiring periodic clearing
  • Manual permission granting for each operation (unless overridden)

Performance Issues

  • Users have reported slow browser interactions (>4 seconds for navigation) in environments like WSL2[5]
  • Token usage for repeated snapshots can be inefficient for large-scale testing
  • Generated scripts may fail due to non-unique selectors, requiring manual overrides[5]

Workarounds

  • For WSL2 performance: Consider using native Windows or Linux environments
  • For token efficiency: Use targeted snapshots rather than full-page captures
  • For selector issues: Prefer accessible locators (getByRole, getByLabel) over CSS selectors

Applications and Use Cases

Primary Use Cases

  • Exploratory Testing - Discover unexpected issues through AI-driven exploration[20]
  • Test Generation - Automatically create comprehensive test suites from specifications
  • Self-Correcting UI Development - Build and refine UIs with automatic visual verification[3]
  • Regression Testing - Maintain and update existing test suites as applications evolve
  • API Testing - Test REST APIs and GraphQL endpoints with Playwright's request context[17]
  • Visual Testing - Verify UI components, layouts, and visual regressions
  • Cross-browser Testing - Ensure compatibility across Chromium, Firefox, and WebKit
  • Migration Projects - Convert existing Selenium or Puppeteer tests to Playwright

Industry Adoption

Companies and organizations using Claude Code Playwright include:

  • Block - Integrated MCP for agentic systems development[8]
  • Apollo - Adopted for test automation workflows
  • Development tool companies including Replit, Codeium, and Sourcegraph

Relationship to Tool/Use Protocols

Although not required for this workflow, teams can connect Playwright-related tools or repositories to Claude via the Model Context Protocol (MCP) so the assistant can fetch code, open issues, or retrieve documentation through standardized tool connectors. This enables:[28]

  • Direct access to test repositories
  • Integration with issue tracking systems
  • Real-time documentation retrieval
  • Cross-tool workflow automation

Community and Ecosystem

Related Projects

  • claude-test CLI - YAML-based testing framework for Claude Code[16]
  • Playwright MCP Registry - Community-driven collection of MCP servers
  • Awesome Claude Code - Curated list of Claude Code commands and workflows[29]
  • Playwright Test Generator - Chrome extension for recording user interactions

Development Tools

Community Resources

  • Official Playwright Discord server with 30,000+ members
  • Claude Code community forums
  • Stack Overflow tags: `playwright`, `claude-code`
  • YouTube tutorials and courses on Claude Code Playwright workflows

See Also

References

  1. 1.0 1.1 Schaeflein, Stefan. "Generating end-to-end tests with AI and Playwright MCP." Checkly. July 17, 2025.
  2. 2.0 2.1 2.2 Anthropic. "Claude Code overview." docs.claude.com/en/docs/claude-code/overview.
  3. 3.0 3.1 3.2 3.3 Turn Claude Code into a Self-Correcting Designer with Playwright MCP. YouTube. 2025.
  4. 4.0 4.1 4.2 Willison, Simon. "TIL: Using Playwright MCP with Claude Code." July 1, 2025.
  5. 5.0 5.1 5.2 Reddit. "Claude code + playwright mcp - how did you speed up the browser?" r/ClaudeAI. 2025.
  6. 6.0 6.1 Anthropic Engineering. "Claude Code: Best practices for agentic coding." April 18, 2025.
  7. 7.0 7.1 Playwright. "Fast and reliable end-to-end testing." playwright.dev.
  8. 8.0 8.1 8.2 8.3 Anthropic. "Introducing the Model Context Protocol." November 25, 2024.
  9. Kumar, Karthik. "Make Playwright UI Testing Smart with Model Context Protocol of Claude AI." Medium. December 9, 2024.
  10. 10.0 10.1 ExecuteAutomation. "Playwright Model Context Protocol Server." GitHub. 2024.
  11. 11.0 11.1 Microsoft. "microsoft/playwright" (repository and license: Apache-2.0). github.com/microsoft/playwright.
  12. Anthropic. "Coding - Claude Code." claude.com/solutions/coding.
  13. Kumar, Harry. "What is Claude Code? The AI coding tool anyone can use." Zapier. August 5, 2025.
  14. 14.0 14.1 Microsoft. "microsoft/playwright-mcp: Playwright MCP server." GitHub. 2025.
  15. Playwright MCP + Claude Code = The END of Manual Debugging. YouTube. July 14, 2025.
  16. 16.0 16.1 16.2 So, Terry. "A YAML-based Playwright MCP automation testing framework designed for Claude Code." GitHub. 2025.
  17. 17.0 17.1 Pathak, Kailash. "API Testing with LLM(Claude) and Playwright MCP." Medium. August 26, 2025.
  18. Bansal, Ashish. "How to Use playwright-mcp with Claude Desktop - Complete Guide." Playwright MCP Docs. 2025.
  19. Wijnands, Sander. "Claude as tester using Playwright and GitHub MCP." MadeWithLove. July 22, 2025.
  20. 20.0 20.1 Nikiforov, Oleksii. "Testing with Playwright and Claude Code." September 6, 2025.
  21. Cherny, Boris. "Claude Code Best Practices." Anthropic Engineering Blog. 2025.
  22. Playwright Docs. "Introduction / Getting started." playwright.dev/docs/intro.
  23. Bharath, Siddharth. "Cooking with Claude Code: The Complete Guide." July 8, 2025.
  24. Builder.io. "How I use Claude Code (+ my best tips)." July 16, 2025.
  25. Playwright Docs. "Setting up CI." playwright.dev/docs/ci-intro.
  26. Microsoft Learn. "Microsoft Playwright Testing." learn.microsoft.com/en-us/azure/playwright-testing/.
  27. AWS News Blog. "Introducing Claude Sonnet 4.5 in Amazon Bedrock." September 29, 2025.
  28. Claude Docs. "Model Context Protocol (MCP)." docs.claude.com/en/docs/mcp.
  29. Him, Hesreally. "Awesome Claude Code." GitHub. 2025.

External Links