0.0.4 • Published 4 months ago
playwright-mcp v0.0.4
How to Use playwright-mcp?
Introduction
playwright-mcp (Model Context Protocol) is a powerful tool that bridges the gap between AI assistants and browser automation. It enables AI models to interact with web browsers, inspect DOM elements, and generate Playwright test scripts with higher accuracy. This guide will walk you through setting up and using playwright-mcp effectively.
Tools
Available tools in the browser interface:
Browser Toolbox
- Pick DOM (🎯): Click to select and capture HTML elements from the page. Use this to record selectors for your test cases.
- Pick Image (📸): Capture screenshots of specific elements. Useful for visual testing or documentation.
- Text Input (✍️): Type and send text messages to record custom notes or descriptions.
MCP Commands
init-browser
: Initialize a browser session and navigate to a URLget-context
: Get the current URL and recorded elements/screenshots (Recommended)get-full-dom
: Retrieve the complete HTML structure of the current pageget-screenshot
: Capture a full screenshot of the entire webpagevalidate-selectors
: Verify that your selectors are unique and valid on the page
Prerequisites
- Node.js installed on your system
- Playwright and its Chromium browser installed on your system
- An IDE that supports MCP (Model Context Protocol) such as Cursor
- Good familiarity with Playwright testing framework
Setup Instructions
Setting Up playwright-mcp with Cursor IDE
- Open Cursor IDE
- Navigate to Settings (⚙️)
- Select "Cursor settings"
- Go to the "MCP" tab
- Click "Add new MCP server"
- Fill in the following details:
- Server Name:
playwright-mcp
- Command to run:
npx -y playwright-mcp
(or specify version withnpx -y playwright-mcp@x.y.z
)
- Server Name:
- Click "Add" to confirm
Note: While playwright-mcp works with Claude and other AI assistants, it performs best when integrated directly with an IDE like Cursor/WindSurf.
Using playwright-mcp
Opening a Browser Session
- Navigate to the file where you want to write or update a test case
- Open the Cursor AI window using
Cmd + I
(macOS) or the equivalent shortcut - Enter the prompt:
Just open browser for me
- Approve running the tool when prompted
- A new browser window will open
Inspecting Elements
- In the browser window, navigate to the webpage you want to test
- Look for the toolbox that appears alongside the browser
- Click on the "Pick DOM" / "Pick Image" button (Note: Cursor doesn't support images yet through MCP)
- Select elements relevant to your test case by clicking on them in the browser, to capture context like element selectors/image layout
Generating Test Code
- Return to your Cursor IDE
- In the Chat Window, add a few existing test files for context and then enter a prompt like:
## DON'T ASSUME ANYTHING. Whatever you write in code, it must be found in the context. Otherwise leave comments.
## Goal
Help me write playwright code with following functionalities:
- [[add semi-high level functionality you want here]]
- [[more]]
- [[more]]
- [[more]]
## Steps
- First fetch the context from `get-context` tool, until it returns no elements remaining.
- Write required code in the POM format
- Validate all the selectors using `validate-selectors` tool.
## Notes
Here are the priorities for the attributes that you should use to write the testcase:
const ATTR_PRIORITIES = {
'id': 1,
'data-testid': 2,
'data-test-id': 2,
'data-pw': 2,
'data-cy': 2,
'data-id': 2,
'data-name': 3,
'name': 3,
'aria-label': 3,
'title': 3,
'placeholder': 4,
'href': 4,
'alt': 4,
'data-index': 5,
'data-role': 5,
'role': 5,
}
- Press Enter and wait for the code generation to complete
- Review the generated test code
Best Practices
- Select Relevant Elements: Only pick elements that are directly related to your test case
- Step-by-Step Debugging: Use debug mode to ensure each step of your test works correctly
- Iterative Refinement: Don't expect perfect tests on the first try; refine them iteratively