Playwright MCP: Browser Automation with AI Agents (Setup and Examples)

Playwright MCP gives AI assistants a real browser they can drive with Playwright. In this lesson you will understand how it works, install it in popular AI clients, learn its tools and options, and use it for exploration, test drafting and debugging.

Advertisement

๐Ÿ“– What You’ll Learn: what MCP and Playwright MCP are, accessibility snapshots vs screenshots, installation in Claude Code, VS Code and Cursor, the browser tools, configuration options, practical tester workflows, safety practices, and MCP vs Test Agents.


๐Ÿค– What Is Playwright MCP?

MCP (Model Context Protocol) is an open standard that lets AI assistants use external tools. Playwright MCP (@playwright/mcp) is Microsoft’s MCP server that gives an AI assistant, such as Claude, GitHub Copilot in VS Code or Cursor, a real browser it can control through Playwright.

With it, you can ask an assistant in plain English:

  • “Open the Sauce Demo site, log in as standard_user and tell me what is on the products page.”
  • “Check whether the checkout form shows an error when the postcode is empty.”
  • “Explore this page and write a Playwright TypeScript test for adding two items to the cart.”

The assistant then navigates, clicks, types and reads the page step by step, using Playwright under the hood.

New to AI agents? Read Fundamentals of Agentic AI for Beginners first.


๐Ÿง  How It Works: Accessibility Snapshots, Not Screenshots

By default, Playwright MCP does not send screenshots to the AI. It sends an accessibility snapshot: a structured, text-based view of the page with roles, names and references for each element, the same information behind getByRole locators.

ApproachPlaywright MCP defaultScreenshot-based agents
Input to the AIStructured accessibility tree (text)Pixels
Speed and costFast, smallSlower, larger
PrecisionActs on exact elementsGuesses coordinates
Needs a vision modelNoYes

Screenshots are still available as a separate tool when the assistant needs to “see” the page.


โš™๏ธ Install Playwright MCP

You need Node.js (see Install Playwright) and an MCP-capable AI client.

Standard configuration (most clients)

{
  "mcpServers": {
    "playwright": {
      "command": "npx",
      "args": ["@playwright/mcp@latest"]
    }
  }
}

Claude Code

claude mcp add playwright npx @playwright/mcp@latest

VS Code (GitHub Copilot agent mode)

code --add-mcp '{"name":"playwright","command":"npx","args":["@playwright/mcp@latest"]}'

Cursor

Open Cursor Settings, MCP, Add new MCP Server, choose the command type and enter npx @playwright/mcp@latest.

After installing, restart the client and check that the Playwright tools are listed and enabled.


๐Ÿงฐ The Tools the AI Gets

The server exposes browser actions as tools. The most important ones:

ToolWhat the AI can do
browser_navigate, browser_navigate_backOpen URLs, go back
browser_snapshotRead the page as an accessibility snapshot
browser_click, browser_hover, browser_dragMouse interactions
browser_type, browser_fill_form, browser_press_keyTyping and keyboard
browser_select_option, browser_file_uploadDropdowns and uploads
browser_handle_dialogAccept or dismiss alerts
browser_wait_forWait for text to appear or disappear
browser_tabsList, open, switch and close tabs
browser_take_screenshotCapture an image when visual context is needed
browser_console_messages, browser_network_requestsRead console logs and network traffic
browser_evaluateRun JavaScript on the page
browser_close, browser_resizeManage the browser window

๐ŸŽ›๏ธ Useful Options

Add options to the args array in the configuration:

{
  "mcpServers": {
    "playwright": {
      "command": "npx",
      "args": ["@playwright/mcp@latest", "--browser", "chrome", "--isolated", "--device", "iPhone 15"]
    }
  }
}
OptionPurpose
--headlessRun without a visible window (headed is the default)
--browserchrome, firefox, webkit or msedge
--isolatedKeep the browser profile in memory; nothing is saved between sessions
--user-data-dirUse a persistent profile folder
--storage-stateStart an isolated session with saved cookies and storage (login sessions)
--deviceEmulate a device such as "iPhone 15" (mobile emulation)
--capsEnable extra capabilities: vision, pdf, devtools
--output-dirFolder for screenshots and other output files

๐Ÿงช Practical Workflows for Testers

1. Exploratory testing assistant

๐Ÿ’ฌ “Using the Playwright tools, open https://www.saucedemo.com, log in as standard_user with password secret_sauce, sort products by price high to low, and report the first three products and their prices.”

The assistant performs the steps and reports what it saw, which is useful for quick checks and reproducing bugs.

2. Generate a first draft of a test

๐Ÿ’ฌ “Explore the checkout flow on Sauce Demo with the browser, then write a Playwright Test in TypeScript that adds the backpack, checks out with test data, and asserts the order confirmation. Use getByRole and data-test locators.”

Because the assistant has seen the real page, its locators are usually accurate. Treat the result like Codegen output: review it, add assertions and move it into your framework structure.

3. Debug a failing test

๐Ÿ’ฌ “This test fails at the checkout button. Open the page, take a snapshot at that step, and explain why the button cannot be clicked.”

The assistant can read console errors and network requests to suggest causes.


โš ๏ธ Safety and Good Practice

  • Use test environments and test accounts. The AI acts with whatever access the browser has.
  • Prefer --isolated so sessions do not reuse real cookies or logins.
  • Review every action your client asks to approve, especially form submissions.
  • Do not share secrets in prompts; use --storage-state with a test session instead.
  • Keep humans in charge of assertions. The AI can draft tests, but you decide what “correct” means.

๐Ÿ”„ Playwright MCP vs Playwright Test Agents

Playwright MCPPlaywright Test Agents
What it isAn MCP server giving any AI client browser controlReady-made planner, generator and healer agents for Playwright Test
Best forExploration, ad-hoc tasks, drafting tests, debuggingA structured loop: plan, generate and heal a test suite
OutputWhatever you ask forMarkdown test plans and Playwright test files

๐Ÿ“ Assignment

  1. Install Playwright MCP in your AI client (Claude Code, VS Code or Cursor) and confirm the tools are listed.
  2. Ask the assistant to open https://www.saucedemo.com and describe the login page using a snapshot.
  3. Ask it to log in, add two products to the cart and report the cart badge number.
  4. Ask it to write a Playwright TypeScript test for that flow, then run the test locally and fix anything that fails.
  5. Restart the server with --isolated --device "iPhone 15" and repeat task 2. What changed?
  6. Bonus: compare the AI-generated test with your own test from your first Playwright test. Which locators and assertions are better?

โ“ FAQ

๐Ÿค” What is Playwright MCP?

An MCP server from Microsoft (@playwright/mcp) that lets AI assistants control a real browser through Playwright, using accessibility snapshots to understand the page.

๐Ÿค” How do I install Playwright MCP in Claude Code?

Run claude mcp add playwright npx @playwright/mcp@latest, then restart Claude Code.

๐Ÿค” Does Playwright MCP use screenshots?

Not by default. It uses accessibility snapshots, which are faster and more precise. A screenshot tool is available when visual context is needed.

๐Ÿค” Can Playwright MCP replace my test suite?

No. It is excellent for exploration, drafting and debugging, but a reliable regression suite still needs reviewed, deterministic Playwright tests running in CI.

๐Ÿค” Which browsers does Playwright MCP support?

Chrome, Firefox, WebKit and Microsoft Edge, selected with the --browser option.


๐ŸŽฏ Next Steps

๐ŸŽฌ Watch the full Playwright tutorial series on YouTube!

Subscribe to SqaToolsByDY • Practice on sqatools.in

1 thought on “Playwright MCP: Browser Automation with AI Agents (Setup and Examples)”

Leave a Comment