The Playwright Playbook — Part 8: Playwright Meets AI — Agents, MCP & Self-Healing Tests A developer built a production-ready Playwright testing framework across seven parts and added AI capabilities in Part 8, including Playwright MCP, AI test agents, and self-healing selectors. The framework now includes AI-generated tests, a test planner, and a test healer, all designed to amplify QA engineers rather than replace them. "AI doesn't replace QA engineers. It gives them superpowers." Seven parts. One framework. Built from scratch. POM-based UI tests. Network interception. Multi-user contexts. A full API testing layer with typed clients. Visual regression across four viewports. A complete debugging toolkit. A production CI/CD pipeline with sharding, Docker, and Slack notifications. This is what we built. Now we add AI on top of it. Not as a replacement — as an amplifier. The engineers who will define the next decade of QA are the ones who understand how to use AI tools deliberately — knowing exactly what they're good for, exactly where they fall short, and how to combine them with the solid engineering we've spent seven parts building. That's what Part 8 is about. Playwright MCP. AI test agents. Self-healing selectors. Let's finish this. šŸŽÆ After Part 7, our framework is complete and production-ready: playwright-playbook/ ā”œā”€ā”€ .github/workflows/ │ ā”œā”€ā”€ playwright.yml āœ… Part 7 │ └── playwright-visual.yml āœ… Part 7 ā”œā”€ā”€ tests/ │ ā”œā”€ā”€ auth/login.spec.ts āœ… Part 1 │ ā”œā”€ā”€ tasks/task-management.spec.ts āœ… Part 1 │ ā”œā”€ā”€ network/ āœ… Part 2 │ ā”œā”€ā”€ multi-user/ āœ… Part 3 │ ā”œā”€ā”€ multi-tab/ āœ… Part 3 │ ā”œā”€ā”€ api/ āœ… Part 4 │ ā”œā”€ā”€ visual/ āœ… Part 5 │ └── debug/ āœ… Part 6 ā”œā”€ā”€ pages/ │ ā”œā”€ā”€ LoginPage.ts āœ… Part 1 │ ā”œā”€ā”€ TaskPage.ts āœ… Part 1 │ └── DashboardPage.ts āœ… Part 3 ā”œā”€ā”€ api/ │ ā”œā”€ā”€ TaskApiClient.ts āœ… Part 4 │ └── AuthApiClient.ts āœ… Part 4 ā”œā”€ā”€ fixtures/ │ ā”œā”€ā”€ auth.fixture.ts āœ… Part 1 │ ā”œā”€ā”€ tasks.json āœ… Part 2 │ ā”œā”€ā”€ empty-tasks.json āœ… Part 2 │ ā”œā”€ā”€ tasks-har.har āœ… Part 2 │ ā”œā”€ā”€ multi-user.fixture.ts āœ… Part 3 │ └── api.fixture.ts āœ… Part 4 ā”œā”€ā”€ scripts/ │ ā”œā”€ā”€ record-har.ts āœ… Part 2 │ └── notify-slack.ts āœ… Part 7 ā”œā”€ā”€ utils/ │ ā”œā”€ā”€ schema-validator.ts āœ… Part 4 │ ā”œā”€ā”€ visual-helpers.ts āœ… Part 5 │ └── debug-helpers.ts āœ… Part 6 ā”œā”€ā”€ docker/ āœ… Part 7 ā”œā”€ā”€ snapshots/ āœ… Part 5 ā”œā”€ā”€ .vscode/ āœ… Part 6 ā”œā”€ā”€ global-setup.ts āœ… Part 1 ā”œā”€ā”€ playwright.config.ts āœ… Parts 1–7 ā”œā”€ā”€ .gitignore āœ… Part 7 └── .env By the end of Part 8, we add: playwright-playbook/ ā”œā”€ā”€ tests/ │ └── ai/ ← NEW │ └── ai-generated.spec.ts ā”œā”€ā”€ ai/ ← NEW │ ā”œā”€ā”€ TestPlanner.ts │ └── TestHealer.ts ā”œā”€ā”€ scripts/ │ ā”œā”€ā”€ generate-test-plan.ts ← NEW │ └── heal-selectors.ts ← NEW └── .mcp.json ← NEW Every file gets fully built below. šŸ‘‡ Before we write code, be clear on what we're actually dealing with. There are three separate AI capabilities in the Playwright ecosystem, and they do very different things: Tool What it does Status ────────────────────── ─────────────────────────────────── ────────────── Playwright MCP Exposes Playwright as a tool for Available now AI assistants Claude, Copilot v1.47+ to control a real browser AI Test Agents Playwright's built-in agents that Experimental Planner/Generator explore your app and generate v1.47+ with flag test skeletons automatically Self-Healing Automatically patches broken Custom we build locators when selectors change this in Part 8 We'll build proper, practical implementations of all three — and be honest about where each one earns its place. šŸŽÆ MCP stands for Model Context Protocol — an open standard for connecting AI assistants to external tools. Playwright's MCP server lets an AI assistant Claude, GitHub Copilot, Cursor directly control a real Chromium browser. This means you can say to Claude or Copilot: "Navigate to the task creation flow and write me a Playwright test for it." And it will literally open a browser, click around, inspect the DOM, and generate real test code. Install the MCP server: npm install -D @playwright/mcp Configure it for your project: // .mcp.json — MCP configuration for your project { "mcpServers": { "playwright": { "command": "npx", "args": "@playwright/mcp", "--browser", "chromium", "--base-url", "http://localhost:3000", "--output-dir", "tests/ai" , "env": { "PLAYWRIGHT STORAGE STATE": ".auth/admin.json" } } } } For VS Code with GitHub Copilot, add to .vscode/settings.json : { "mcp": { "servers": { "playwright": { "command": "npx", "args": "@playwright/mcp", "--browser", "chromium" , "env": { "BASE URL": "http://localhost:3000" } } } } } With MCP configured, your AI assistant can: You say: "Write a test for the task deletion flow including the confirmation dialog" AI does: 1. Opens Chromium via MCP 2. Navigates to http://localhost:3000/tasks 3. Inspects the DOM — finds task items, hover targets, delete buttons 4. Identifies the confirmation dialog structure 5. Generates a TypeScript test using getByRole/getByTestId locators 6. Writes it to tests/ai/ directory You get: A real, runnable test file — not a hallucinated one The key difference from AI generating test code from a description: MCP sees the actual DOM. It generates locators that actually exist, not ones it imagines. šŸ”„ Once MCP is configured in Claude Desktop or Claude.ai: Prompt: "I have a Playwright project at http://localhost:3000. Navigate to the task management page, explore the UI, and write a complete TypeScript Playwright test for creating, editing, and deleting a task. Use our TaskPage POM from pages/TaskPage.ts." Claude will: → Open browser via MCP → Navigate and inspect the real UI → Generate tests that match actual element structure → Reference your existing POM correctly The Test Planner uses AI to analyse your existing test suite and your app's DOM structure, then suggests missing test scenarios. // ai/TestPlanner.ts import as fs from 'fs'; import as path from 'path'; import { chromium } from '@playwright/test'; export interface TestScenario { feature: string; scenario: string; priority: 'high' | 'medium' | 'low'; covered: boolean; suggestedTestFile: string; } export interface TestPlan { url: string; generatedAt: string; totalScenarios: number; coveredCount: number; uncoveredCount: number; scenarios: TestScenario ; } export class TestPlanner { private readonly baseUrl: string; private readonly testsDir: string; constructor baseUrl: string, testsDir: string = './tests' { this.baseUrl = baseUrl; this.testsDir = testsDir; } / Crawl the app and extract all interactive elements. Returns a structured map of page → interactive elements. / async crawlApp storageState?: string : Promise