Playwright MCP Server

repository·main·Indexed 26 days ago

https://github.com/executeautomation/mcp-playwright

A Model Context Protocol (MCP) server that provides LLMs with browser automation capabilities using Playwright. It enables AI assistants to interact with web pages, execute JavaScript, take screenshots, scrape content, and emulate 143 real-world device profiles. The server supports Chromium, Firefox, and WebKit engines and can be deployed via npm, Docker, or HTTP mode for integration with clients like Claude Desktop and VS Code GitHub Copilot.

Tokens
15.9K
Snippets
65
Records
123
Agent score
90%

What's inside @executeautomation/playwright-mcp-server

  1. Overview of Playwright MCP Server

    main

    The Playwright Model Context Protocol (MCP) server enables LLMs to interact with web pages in a real browser environment. Key capabilities include:

    • Executing JavaScript
    • Taking screenshots
    • Navigating web elements
    • Performing API testing to validate endpoints
    • Testing across multiple browser engines: Chromium, Firefox, and WebKit.
  2. Overview of Playwright MCP Supported Features

    main

    The Playwright MCP server provides the following browser automation capabilities:

    • Console log monitoring
    • Code Generation (recording actions into Playwright test scripts)
    • Web Scraping
    • Screenshot capabilities
    • JavaScript execution
    • Basic web interaction (navigation, clicking, form filling, dropdown selection, and hovering)
    • Content retrieval (extracting visible text and HTML)
  3. Best practices for playwright_resize prompts

    main

    To ensure the AI assistant correctly invokes playwright_resize, follow these prompting best practices:

    Do's

    • Be Specific: Use exact model names (e.g., iPhone 13 instead of iPhone).
    • Combine Actions: Link resizing with navigation or interaction (e.g., Navigate to example.com and test on iPad).
    • Request Visuals: Explicitly ask for screenshots to verify the resize (e.g., Test on Galaxy S24 and take a screenshot).
    • Use Natural Language for Orientation: Use phrases like Rotate the phone or Switch to landscape.

    Don'ts

    • Avoid Ambiguity: Do not use vague terms like Make it smaller or Phone view without specifying a device or resolution.
  4. Perform BDD Testing with Playwright MCP Server

    main

    You can perform Behavior Driven Development (BDD) testing using the Playwright MCP Server by providing Gherkin-style scenarios to an AI-enabled editor (like Cursor) that supports MCP servers. This allows for hands-free automation where the AI interprets the natural language steps to execute browser actions without manual code writing.

    To use this approach, ensure the Playwright MCP Server is configured and available to your agent/editor.

  5. Automate UI and Database testing with Cursor Rules

    main

    You can automate end-to-end testing workflows (UI, API, and Database) by defining custom Cursor Rules (.mdc files). By combining the Playwright MCP Server with other MCP servers like the SQLite MCP Server, you can instruct AI agents to perform complex sequences such as navigating to a URL, interacting with UI elements, and verifying data persistence in a database without writing manual test scripts.

    To implement this, create a Cursor Rule file that defines a trigger command (e.g., /test) and provides the agent with the necessary context, such as URLs, element IDs, and schema locations.

    When I say /test, then perform following
    
    Navigate to http://localhost:8000/Product/List. 
    
    Create product by clicking Create link . 
    
    Then create a product with some realistic data for Name, Price and Select ProductType as CPU and click create input type with id as Create. 
    
    Check the Database for the created record
    
    Use the Schema: http://localhost:8001/swagger/v1/swagger.json
    
    Here is the baseURL of API Base URL: http://localhost:8001/
    
    Also check the API which performs the GET operation if the values are correctly retreived for the created product from the above schema
  6. Resize browser viewport using playwright_resize

    main

    You can use natural language prompts to instruct an AI assistant to resize the browser viewport using the playwright_resize tool. This allows for testing responsiveness across various device presets or custom dimensions.

    Device Presets

    You can request specific devices by name, including:

    • iOS Phones: iPhone 13, iPhone 14, iPhone 15, iPhone SE, iPhone X, etc.
    • iOS Tablets: iPad Pro 11, iPad Air, iPad Mini, iPad Pro 12.9.
    • Android Phones: Pixel 7, Galaxy S24, Galaxy S9+, Pixel 5.
    • Android Tablets: Galaxy Tab S4, Pixel Tablet.
    • Desktop Browsers: Desktop Chrome, Desktop Firefox, Desktop Safari.

    Custom Dimensions

    You can also request specific pixel resolutions:

    • Resize to 1024x768
    • Set viewport to 800x600
    • Test at 1920x1080 resolution

    Orientation

    You can control device orientation using prompts like Rotate to landscape or Switch to portrait mode.

  7. Perform multi-device testing workflows

    main

    You can instruct the AI to perform complex testing workflows across multiple devices and orientations in a single request.

    Example Workflows:

    • Responsive Design: "Start mobile (iPhone 13), then tablet (iPad), then desktop"
    • Cross-Platform Comparison: "Compare iPhone 13 vs Galaxy S24 vs Pixel 7"
    • Edge Case Testing: "Test on the smallest (iPhone SE) and largest (iPad Pro 12.9) screens"
    • Form/Navigation Verification: "Fill the signup form on iPhone 13, then switch to desktop and verify everything still looks good"
  8. Use HTTP/SSE Transport Mode

    main

    The Playwright MCP Server supports an HTTP/SSE transport mode, allowing for remote access, multiple clients, and health monitoring. This mode is useful for VS Code Copilot or when you need to access the server over a network.

    Migration from stdio to HTTP

    If you are currently using stdio mode in your MCP configuration, follow these steps to switch to HTTP mode:

    1. Start the server on a specific port:

      playwright-mcp-server --port 8931
    2. Update your MCP configuration to use the url key instead of command and args:

      {
        "mcpServers": {
          "playwright": {
            "url": "http://localhost:8931/mcp"
          }
        }
      }
    {
      "mcpServers": {
        "playwright": {
          "url": "http://localhost:8931/mcp"
        }
      }
    }
  9. Capture browser console logs with Playwright MCP Server

    main

    The Playwright MCP Server supports capturing browser console logs during development and testing. This allows you to monitor browser activity and debug issues by retrieving logs emitted by the browser's console while performing actions.

    Supported log types include:

    • log
    • info
    • warn
    • error
    • debug
    • all (captures all types)

    To trigger this capability via an MCP-enabled client, use a prompt that instructs the server to monitor or retrieve logs during its operations.

    Get the console log from the browser whenever you perform any action.
  10. Authenticate API requests using Bearer tokens

    main

    To perform Bearer token authentication, use the token parameter in your Playwright MCP Server API calls (playwright_get, playwright_post, playwright_delete, etc.). This automatically handles the Bearer token injection.

    // GET request with Bearer token
    await playwright_get({
      url: 'https://api.example.com/protected-data',
      token: 'your-bearer-token-here'
    });
    
    // POST request with Bearer token
    await playwright_post({
      url: 'https://api.example.com/create',
      value: '{"name":"test"}',
      token: 'your-bearer-token-here'
    });
    
    // DELETE request with Bearer token
    await playwright_delete({
      url: 'https://api.example.com/resource/123',
      token: 'your-bearer-token-here'
    });
  11. Install Playwright MCP Server

    main

    You can install the Playwright MCP server using several methods depending on your client:

    Using npm

    npm install -g @executeautomation/playwright-mcp-server

    Using mcp-get

    npx @michaellatman/mcp-get@latest install @executeautomation/playwright-mcp-server

    Using Smithery (for Claude Desktop)

    npx @smithery/cli install @executeautomation/playwright-mcp-server --client claude

    Using Claude Code

    claude mcp add --transport stdio playwright npx @executeautomation/playwright-mcp-server
    npm install -g @executeautomation/playwright-mcp-server