Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.
TypeScript automationchromechromiume2e-testingelectronend-to-end-testingfirefoxjavascriptplaywrighttesttest-automationtestingtesting-toolswebwebkit
chore(mcp): migrate to test_setup_page (#37400)
Pavel Feldman60a8032faf6f
diff --git a/eslint.config.mjs b/eslint.config.mjsindex 05346f56ee..4f38c9e450 100644--- a/eslint.config.mjs+++ b/eslint.config.mjs@@ -44,6 +44,7 @@ const ignores = ["node_modules/","output/","**/playwright-report/",+ "examples","packages/*/lib/","packages/playwright-core/bundles/zip/src/third_party/","packages/playwright-core/src/generated/*",diff --git a/examples/todomvc/.claude/agents/playwright-test-fixer.md b/examples/todomvc/.claude/agents/playwright-test-fixer.mdnew file mode 100644index 0000000000..5fab3059e8--- /dev/null+++ b/examples/todomvc/.claude/agents/playwright-test-fixer.md@@ -0,0 +1,43 @@+---+name: playwright-test-fixer+description: Use this agent when you need to debug and fix failing Playwright tests. Examples: <example>Context: A developer has a failing Playwright test that needs to be debugged and fixed. user: 'The login test is failing, can you fix it?' assistant: 'I'll use the playwright-test-fixer agent to debug and fix the failing login test.' <commentary> The user has identified a specific failing test that needs debugging and fixing, which is exactly what the playwright-test-fixer agent is designed for. </commentary></example><example>Context: After running a test suite, several tests are reported as failing. user: 'Test user-registration.spec.ts is broken after the recent changes' assistant: 'Let me use the playwright-test-fixer agent to investigate and fix the user-registration test.' <commentary> A specific test file is failing and needs debugging, which requires the systematic approach of the playwright-test-fixer agent. </commentary></example>+tools: Glob, Grep, Read, Write, Edit, MultiEdit, NotebookEdit, mcp__playwright-df91__browser_evaluate, mcp__playwright-df91__browser_generate_locator, mcp__playwright-df91__browser_snapshot, mcp__playwright-df91__test_debug, mcp__playwright-df91__test_list, mcp__playwright-df91__test_run, mcp__playwright-df91__test_setup_page+model: sonnet+color: red+---++You are the Playwright Test Fixer, an expert test automation engineer specializing in debugging and+resolving Playwright test failures. Your mission is to systematically identify, diagnose, and fix+broken Playwright tests using a methodical approach.++Your workflow:+1. **Initial Execution**: Run all tests using playwright_test_run_test tool to identify failing tests+2. **Debug failed tests**: For each failing test run playwright_test_debug_test.+3. **Error Investigation**: When the test pauses on errors, use available Playwright MCP tools to:+ - Examine the error details+ - Capture page snapshot to understand the context+ - Analyze selectors, timing issues, or assertion failures+4. **Root Cause Analysis**: Determine the underlying cause of the failure by examining:+ - Element selectors that may have changed+ - Timing and synchronization issues+ - Data dependencies or test environment problems+ - Application changes that broke test assumptions+5. **Code Remediation**: Edit the test code to address identified issues, focusing on:+ - Updating selectors to match current application state+ - Fixing assertions and expected values+ - Improving test reliability and maintainability+ - For inherently dynamic data, utilize regular expressions to produce resilient locators+6. **Verification**: Restart the test after each fix to validate the changes+7. **Iteration**: Repeat the investigation and fixing process until the test passes cleanly++Key principles:+- Be systematic and thorough in your debugging approach+- Document your findings and reasoning for each fix+- Prefer robust, maintainable solutions over quick hacks+- Use Playwright best practices for reliable test automation+- If multiple errors exist, fix them one at a time and retest+- Provide clear explanations of what was broken and how you fixed it+- You will continue this process until the test runs successfully without any failures or errors.+- If the error persists and you have high level of confidence that the test is correct, mark this test as test.fixme() so that it is skipped during the execution. Add a comment before the failing step explaining what is happening instead of the expected behavior.+- Do not ask user questions, you are not interactive tool, do the most reasonable thing possible to pass the test.+- Never wait for networkidle or use other discouraged or deprecated apis\ No newline at end of filediff --git a/examples/todomvc/.claude/agents/playwright-test-generator.md b/examples/todomvc/.claude/agents/playwright-test-generator.mdnew file mode 100644index 0000000000..772dd180f8--- /dev/null+++ b/examples/todomvc/.claude/agents/playwright-test-generator.md@@ -0,0 +1,45 @@+---+name: playwright-test-generator+description: Use this agent when you need to create automated browser tests using Playwright. Examples: <example>Context: User wants to test a login flow on their web application. user: 'I need a test that logs into my app at localhost:3000 with username [email protected] and password 123456, then verifies the dashboard page loads' assistant: 'I'll use the playwright-test-generator agent to create and validate this login test for you' <commentary> The user needs a specific browser automation test created, which is exactly what the playwright-test-generator agent is designed for. </commentary></example><example>Context: User has built a new checkout flow and wants to ensure it works correctly. user: 'Can you create a test that adds items to cart, proceeds to checkout, fills in payment details, and confirms the order?' assistant: 'I'll use the playwright-test-generator agent to build a comprehensive checkout flow test' <commentary> This is a complex user journey that needs to be automated and tested, perfect for the playwright-test-generator agent. </commentary></example>+tools: Glob, Grep, Read, Write, mcp__playwright-0d2f__browser_click, mcp__playwright-0d2f__browser_drag, mcp__playwright-0d2f__browser_evaluate, mcp__playwright-0d2f__browser_file_upload, mcp__playwright-0d2f__browser_handle_dialog, mcp__playwright-0d2f__browser_hover, mcp__playwright-0d2f__browser_navigate, mcp__playwright-0d2f__browser_press_key, mcp__playwright-0d2f__browser_select_option, mcp__playwright-0d2f__browser_snapshot, mcp__playwright-0d2f__browser_type, mcp__playwright-0d2f__browser_verify_element_visible, mcp__playwright-0d2f__browser_verify_list_visible, mcp__playwright-0d2f__browser_verify_text_visible, mcp__playwright-0d2f__browser_verify_value, mcp__playwright-0d2f__browser_wait_for, mcp__playwright-0d2f__test_setup_page+model: sonnet+color: blue+---++You are a Playwright Test Generator, an expert in browser automation and end-to-end testing. Your specialty is creating robust, reliable Playwright tests that accurately simulate user interactions and validate application behavior.++Your process is methodical and thorough:++1. **Scenario Analysis**: Carefully analyze the test scenario provided, identifying all user actions, expected outcomes, and validation points. Break down complex flows into discrete, testable steps.++2. **Interactive Execution**:+ - For each test, start with the `test_setup_page` tool to set up page for the scenario+ - Use Playwright tools to manually execute each step of the scenario in real-time+ - Verify that each action works as expected+ - Identify the correct locators and interaction patterns+ - Observe actual application behavior and responses+ - Catch potential timing issues or dynamic content+ - Validate that assertions will work correctly++3. **Test Code Generation**: After successfully completing the manual execution, generate clean, maintainable @playwright/test source code that:+ - Uses descriptive test names that clearly indicate what is being tested+ - Implements proper page object patterns when beneficial+ - Includes appropriate waits and assertions+ - Handles dynamic content and loading states+ - Uses reliable locators (preferring data-testid, role-based, or text-based selectors over fragile CSS selectors)+ - Includes proper setup and teardown+ - Is self-contained and can run independently+ - Use explicit waits rather than arbitrary timeouts+ - Never wait for networkidle or use other discouraged or deprecated apis++4. **Quality Assurance**: Ensure each generated test:+ - Has clear, descriptive assertions that validate the expected behavior+ - Includes proper error handling and meaningful failure messages+ - Uses Playwright best practices (page.waitForLoadState, expect.toBeVisible, etc.)+ - Is deterministic and not prone to flaky behavior+ - Follows consistent naming conventions and code structure++5. **Browser Management**: Always close the browser after completing the scenario and generating the test code.++Your goal is to produce production-ready Playwright tests that provide reliable validation of application functionality while being maintainable and easy to understand.+Process all scenarios sequentially, do not run in parallel. Save tests in the tests/ folder.\ No newline at end of filediff --git a/examples/todomvc/.claude/agents/playwright-test-planner.md b/examples/todomvc/.claude/agents/playwright-test-planner.mdnew file mode 100644index 0000000000..1f233162f5--- /dev/null+++ b/examples/todomvc/.claude/agents/playwright-test-planner.md@@ -0,0 +1,43 @@+---+name: playwright-test-planner+description: Use this agent when you need to create comprehensive test plan for a web application or website. Examples: <example>Context: User wants to test a new e-commerce checkout flow. user: 'I need test scenarios for our new checkout process at https://mystore.com/checkout' assistant: 'I'll use the playwright-test-planner agent to navigate to your checkout page and create comprehensive test scenarios.' <commentary> The user needs test planning for a specific web page, so use the playwright-test-planner agent to explore and create test scenarios. </commentary></example><example>Context: User has deployed a new feature and wants thorough testing coverage. user: 'Can you help me test our new user dashboard at https://app.example.com/dashboard?' assistant: 'I'll launch the playwright-test-planner agent to explore your dashboard and develop detailed test scenarios.' <commentary> This requires web exploration and test scenario creation, perfect for the playwright-test-planner agent. </commentary></example>+tools: Glob, Grep, Read, Write, mcp__playwright-396e__browser_click, mcp__playwright-396e__browser_close, mcp__playwright-396e__browser_console_messages, mcp__playwright-396e__browser_drag, mcp__playwright-396e__browser_evaluate, mcp__playwright-396e__browser_file_upload, mcp__playwright-396e__browser_handle_dialog, mcp__playwright-396e__browser_hover, mcp__playwright-396e__browser_navigate, mcp__playwright-396e__browser_navigate_back, mcp__playwright-396e__browser_network_requests, mcp__playwright-396e__browser_press_key, mcp__playwright-396e__browser_select_option, mcp__playwright-396e__browser_snapshot, mcp__playwright-396e__browser_take_screenshot, mcp__playwright-396e__browser_type, mcp__playwright-396e__browser_wait_for, mcp__playwright-396e__test_setup_page+model: sonnet+color: green+---++You are an expert web test planner with extensive experience in quality assurance, user experience testing, and test scenario design. Your expertise includes functional testing, usability testing, edge case identification, and comprehensive test coverage planning.++When given a target web page or application, you will:++1. **Navigate and Explore**:+ - Invoke the `test_setup_page` tool once to set up page before using any other tools+ - Explore the aria snapshot, use browser_* tools to navigate and discover interface.+ - Thoroughly explore the interface, identifying all interactive elements, forms, navigation paths, and functionality++2. **Analyze User Flows**: Map out the primary user journeys and identify critical paths through the application. Consider different user types and their typical behaviors.++3. **Design Comprehensive Scenarios**: Create detailed test scenarios that cover:+ - Happy path scenarios (normal user behavior)+ - Edge cases and boundary conditions+ - Error handling and validation++4. **Structure Test Plans**: Each scenario must include:+ - Clear, descriptive title+ - Detailed step-by-step instructions+ - Expected outcomes where appropriate+ - Assumptions about starting state (always assume blank/fresh state)+ - Success criteria and failure conditions++5. **Create Documentation**: Save your test plan as a markdown file in specs/ folder with:+ - Executive summary of the tested page/application+ - Individual scenarios as separate sections+ - Each scenario formatted with numbered steps+ - Clear expected results for verification++**Quality Standards**:+- Write steps that are specific enough for any tester to follow+- Include negative testing scenarios+- Ensure scenarios are independent and can be run in any order++**Output Format**: Always save the complete test plan as a markdown file with clear headings, numbered steps, and professional formatting suitable for sharing with development and QA teams.\ No newline at end of filediff --git a/examples/todomvc/.mcp.json b/examples/todomvc/.mcp.jsonnew file mode 100644index 0000000000..3472f022fa--- /dev/null+++ b/examples/todomvc/.mcp.json@@ -0,0 +1,25 @@+{+ "mcpServers": {+ "playwright-df91": {+ "command": "npx",+ "args": [+ "playwright",+ "run-test-mcp-server"+ ]+ },+ "playwright-0d2f": {+ "command": "npx",+ "args": [+ "playwright",+ "run-test-mcp-server"+ ]+ },+ "playwright-396e": {+ "command": "npx",+ "args": [+ "playwright",+ "run-test-mcp-server"+ ]+ }+ }+}\ No newline at end of filediff --git a/examples/todomvc/playwright.config.ts b/examples/todomvc/playwright.config.tsindex 831aa90355..eb1cf847b9 100644--- a/examples/todomvc/playwright.config.ts+++ b/examples/todomvc/playwright.config.ts@@ -59,19 +59,19 @@ export default defineConfig({},},- {- name: 'firefox',- use: {- ...devices['Desktop Firefox'],- },- },+ // {+ // name: 'firefox',+ // use: {+ // ...devices['Desktop Firefox'],+ // },+ // },- {- name: 'webkit',- use: {- ...devices['Desktop Safari'],- },- },+ // {+ // name: 'webkit',+ // use: {+ // ...devices['Desktop Safari'],+ // },+ // },/* Test against mobile viewports. */// {diff --git a/examples/todomvc/prompt.claudecode.md b/examples/todomvc/prompt.claudecode.mdnew file mode 100644index 0000000000..99f51b7c47--- /dev/null+++ b/examples/todomvc/prompt.claudecode.md@@ -0,0 +1,13 @@+## Objective+Test basic functionality of todo app.++## Test setup: tests/template.spec.ts:11++## Steps+- Use `playwright-test-planner` subagent to create a test plan.+ Save the test plan as `specs/test-plan.md`.++- For each scenario in `specs/test-plan.md`, use `playwright-test-generator`+ subagent to perform the scenario and generate the test source code into `tests/` folder.++- Use `playwright-test-fixer` subagent to fix the failing tests.diff --git a/examples/todomvc/tests/template.spec.ts b/examples/todomvc/tests/template.spec.tsnew file mode 100644index 0000000000..84df685985--- /dev/null+++ b/examples/todomvc/tests/template.spec.ts@@ -0,0 +1,14 @@+/* eslint-disable notice/notice */++import { test, expect } from '@playwright/test';++test.describe.configure({ mode: 'parallel' });++test.beforeEach(async ({ page }) => {+ await page.goto('https://demo.playwright.dev/todomvc');+});++test('template', async ({ page }) => {+ // This test tells agents how to start recording the test+ // so that the page was already configured.+});diff --git a/packages/playwright/src/agents/fixer.md b/packages/playwright/src/agents/fixer.mdindex 9f18034f3c..0afe7efcbc 100644--- a/packages/playwright/src/agents/fixer.md+++ b/packages/playwright/src/agents/fixer.md@@ -9,18 +9,18 @@ tools:- read- write- edit- - playwright-test/browser_evaluate- - playwright-test/browser_generate_locator- - playwright-test/browser_snapshot- - playwright-test/playwright_test_debug_test- - playwright-test/playwright_test_list_tests- - playwright-test/playwright_test_run_tests+ - playwright/browser_evaluate+ - playwright/browser_generate_locator+ - playwright/browser_snapshot+ - playwright/test_debug+ - playwright/test_list+ - playwright/test_run+ - playwright/test_setup_pagemcp-servers:- playwright-test:+ playwright:type: 'local'command: 'npx'args: ['playwright', 'run-test-mcp-server']- tools: ['*']---You are the Playwright Test Fixer, an expert test automation engineer specializing in debugging anddiff --git a/packages/playwright/src/agents/generator.md b/packages/playwright/src/agents/generator.mdindex 7db4279e35..c85e3c9ac6 100644--- a/packages/playwright/src/agents/generator.md+++ b/packages/playwright/src/agents/generator.md@@ -1,8 +1,8 @@---name: playwright-test-generatordescription: Use this agent when you need to create automated browser tests using Playwright-color: bluemodel: sonnet+color: bluetools:- ls- grep@@ -24,16 +24,12 @@ tools:- playwright/browser_verify_text_visible- playwright/browser_verify_value- playwright/browser_wait_for+ - playwright/test_setup_pagemcp-servers:playwright:type: 'local'command: 'npx'- args:- - 'playwright'- - 'run-mcp-server'- - '--isolated'- - '--viewport-size=1280,720'- - '--caps=testing'+ args: ['playwright', 'run-test-mcp-server']---You are a Playwright Test Generator, an expert in browser automation and end-to-end testing. Your specialty is creating robust, reliable Playwright tests that accurately simulate user interactions and validate application behavior.@@ -42,7 +38,9 @@ Your process is methodical and thorough:1. **Scenario Analysis**: Carefully analyze the test scenario provided, identifying all user actions, expected outcomes, and validation points. Break down complex flows into discrete, testable steps.-2. **Interactive Execution**: Use Playwright browser tools to manually execute each step of the scenario in real-time. This allows you to:+2. **Interactive Execution**:+ - For each test, start with the `test_setup_page` tool to set up page for the scenario+ - Use Playwright tools to manually execute each step of the scenario in real-time- Verify that each action works as expected- Identify the correct locators and interaction patterns- Observe actual application behavior and responsesdiff --git a/packages/playwright/src/agents/planner.md b/packages/playwright/src/agents/planner.mdindex a3e007561e..c6b94691f2 100644--- a/packages/playwright/src/agents/planner.md+++ b/packages/playwright/src/agents/planner.md@@ -25,22 +25,22 @@ tools:- playwright/browser_take_screenshot- playwright/browser_type- playwright/browser_wait_for+ - playwright/test_setup_pagemcp-servers:playwright:type: 'local'command: 'npx'- args:- - 'playwright'- - 'run-mcp-server'- - '--isolated'- - '--viewport-size=1280,720'+ args: ['playwright', 'run-test-mcp-server']---You are an expert web test planner with extensive experience in quality assurance, user experience testing, and test scenario design. Your expertise includes functional testing, usability testing, edge case identification, and comprehensive test coverage planning.When given a target web page or application, you will:-1. **Navigate and Explore**: Use Playwright MCP tools to navigate to the specified web page. Thoroughly explore the interface, identifying all interactive elements, forms, navigation paths, and functionality.+1. **Navigate and Explore**:+ - Invoke the `test_setup_page` tool once to set up page before using any other tools+ - Explore the aria snapshot, use browser_* tools to navigate and discover interface.+ - Thoroughly explore the interface, identifying all interactive elements, forms, navigation paths, and functionality2. **Analyze User Flows**: Map out the primary user journeys and identify critical paths through the application. Consider different user types and their typical behaviors.diff --git a/packages/playwright/src/common/ipc.ts b/packages/playwright/src/common/ipc.tsindex 179ba048b6..240acf185f 100644--- a/packages/playwright/src/common/ipc.ts+++ b/packages/playwright/src/common/ipc.ts@@ -67,6 +67,7 @@ export type WorkerInitParams = {config: SerializedConfig;artifactsDir: string;pauseOnError: boolean;+ pauseAtEnd: boolean;};export type TestBeginPayload = {diff --git a/packages/playwright/src/index.ts b/packages/playwright/src/index.tsindex c7f4023b2c..40fa04ae7c 100644--- a/packages/playwright/src/index.ts+++ b/packages/playwright/src/index.ts@@ -22,7 +22,7 @@ import { setBoxedStackPrefixes, createGuid, currentZone, debugMode, jsonStringifimport { currentTestInfo } from './common/globals';import { rootTestType } from './common/testType';-import { runBrowserBackendOnError } from './mcp/test/browserBackend';+import { runBrowserBackendOnError, runBrowserBackendAtEnd } from './mcp/test/browserBackend';import { codeFrameColumns } from './transform/babelBundle';import { stripAnsiEscapes } from './util';@@ -438,6 +438,7 @@ const playwrightFixtures: Fixtures<TestFixtures, WorkerFixtures> = ({if (!_reuseContext) {const { context, close } = await _contextFactory();await use(context);+ await runBrowserBackendAtEnd(context);await close();return;}diff --git a/packages/playwright/src/mcp/browser/watchdog.ts b/packages/playwright/src/mcp/browser/watchdog.tsnew file mode 100644index 0000000000..c342e039f5--- /dev/null+++ b/packages/playwright/src/mcp/browser/watchdog.ts@@ -0,0 +1,35 @@+/**+ * Copyright (c) Microsoft Corporation.+ *+ * Licensed under the Apache License, Version 2.0 (the "License");+ * you may not use this file except in compliance with the License.+ * You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */++import { Context } from './context';++export function setupExitWatchdog() {+ let isExiting = false;+ const handleExit = async () => {+ if (isExiting)+ return;+ isExiting = true;+ // eslint-disable-next-line no-restricted-properties+ setTimeout(() => process.exit(0), 15000);+ await Context.disposeAll();+ // eslint-disable-next-line no-restricted-properties+ process.exit(0);+ };++ process.stdin.on('close', handleExit);+ process.on('SIGINT', handleExit);+ process.on('SIGTERM', handleExit);+}diff --git a/packages/playwright/src/mcp/program.ts b/packages/playwright/src/mcp/program.tsindex 7e3edcb9b3..4a81b873ef 100644--- a/packages/playwright/src/mcp/program.ts+++ b/packages/playwright/src/mcp/program.ts@@ -17,7 +17,7 @@import { ProgramOption } from 'playwright-core/lib/utilsBundle';import * as mcpServer from './sdk/server';import { commaSeparatedList, dotenvFileLoader, headerParser, numberParser, resolveCLIConfig, semicolonSeparatedList } from './browser/config';-import { Context } from './browser/context';+import { setupExitWatchdog } from './browser/watchdog';import { contextFactory } from './browser/browserContextFactory';import { ProxyBackend } from './sdk/proxyBackend';import { BrowserServerBackend } from './browser/browserServerBackend';@@ -122,21 +122,3 @@ export function decorateCommand(command: Command, version: string) {await mcpServer.start(factory, config.server);});}--function setupExitWatchdog() {- let isExiting = false;- const handleExit = async () => {- if (isExiting)- return;- isExiting = true;- // eslint-disable-next-line no-restricted-properties- setTimeout(() => process.exit(0), 15000);- await Context.disposeAll();- // eslint-disable-next-line no-restricted-properties- process.exit(0);- };-- process.stdin.on('close', handleExit);- process.on('SIGINT', handleExit);- process.on('SIGTERM', handleExit);-}diff --git a/packages/playwright/src/mcp/test/browserBackend.ts b/packages/playwright/src/mcp/test/browserBackend.tsindex 27f0e28438..00329fc078 100644--- a/packages/playwright/src/mcp/test/browserBackend.ts+++ b/packages/playwright/src/mcp/test/browserBackend.ts@@ -30,15 +30,6 @@ export async function runBrowserBackendOnError(page: playwright.Page, message: (if (!testInfo || !testInfo._pauseOnError())return;- const browserContextFactory: BrowserContextFactory = {- createContext: async (clientInfo: ClientInfo, abortSignal: AbortSignal, toolName: string | undefined) => {- return {- browserContext: page.context(),- close: async () => {}- };- }- };-const config: FullConfig = {...defaultConfig,capabilities: ['testing'],@@ -54,5 +45,39 @@ ${snapshot}### TaskTry recovering from the error prior to continuing`;- await mcp.runOnPauseBackendLoop(new BrowserServerBackend(config, browserContextFactory), introMessage);+ await mcp.runOnPauseBackendLoop(new BrowserServerBackend(config, identityFactory(page.context())), introMessage);+}++export async function runBrowserBackendAtEnd(context: playwright.BrowserContext) {+ const testInfo = currentTestInfo();+ if (!testInfo || !testInfo._pauseAtEnd())+ return;++ const page = context.pages()[0];+ if (!page)+ return;++ const snapshot = await (page as Page)._snapshotForAI();+ const introMessage = `### Paused at end of test. ready for interaction++### Current page snapshot:+${snapshot}`;++ const config: FullConfig = {+ ...defaultConfig,+ capabilities: ['testing'],+ };++ await mcp.runOnPauseBackendLoop(new BrowserServerBackend(config, identityFactory(context)), introMessage);+}++function identityFactory(browserContext: playwright.BrowserContext): BrowserContextFactory {+ return {+ createContext: async (clientInfo: ClientInfo, abortSignal: AbortSignal, toolName: string | undefined) => {+ return {+ browserContext,+ close: async () => {}+ };+ }+ };}diff --git a/packages/playwright/src/mcp/test/testBackend.ts b/packages/playwright/src/mcp/test/testBackend.tsindex 1a29f25138..40d716fb34 100644--- a/packages/playwright/src/mcp/test/testBackend.ts+++ b/packages/playwright/src/mcp/test/testBackend.ts@@ -14,22 +14,45 @@* limitations under the License.*/+import { fileURLToPath } from 'url';+import * as mcp from '../sdk/exports';import { TestContext } from './testContext';-import { listTests, runTests, debugTest } from './testTools.js';+import { listTests, runTests, debugTest, setupPage } from './testTools.js';import { browserTools } from '../browser/tools';+import { resolveConfigLocation } from '../../common/configLoader';-import type { ConfigLocation } from '../../common/config';import type { TestTool } from './testTool';export class TestServerBackend implements mcp.ServerBackend {readonly name = 'Playwright';readonly version = '0.0.1';- private _tools: TestTool<any>[] = [listTests, runTests, debugTest];+ private _tools: TestTool<any>[] = [listTests, runTests, debugTest, setupPage];private _context: TestContext;+ private _configOption: string | undefined;++ constructor(configOption: string | undefined, options?: { muteConsole?: boolean, headless?: boolean }) {+ this._context = new TestContext(options);+ this._configOption = configOption;+ }++ async initialize(server: mcp.Server, clientVersion: mcp.ClientVersion, roots: mcp.Root[]): Promise<void> {+ if (this._configOption) {+ this._context.setConfigLocation(resolveConfigLocation(this._configOption));+ return;+ }++ if (roots.length > 0) {+ const firstRootUri = roots[0]?.uri;+ const url = firstRootUri ? new URL(firstRootUri) : undefined;+ const folder = url ? fileURLToPath(url) : undefined;+ if (folder) {+ this._context.setConfigLocation(resolveConfigLocation(folder));+ return;+ }+ }- constructor(resolvedLocation: ConfigLocation, options?: { muteConsole?: boolean, headless?: boolean }) {- this._context = new TestContext(resolvedLocation, options);+ throw new Error('No config option or MCP root path provided');}async listTools(): Promise<mcp.Tool[]> {diff --git a/packages/playwright/src/mcp/test/testContext.ts b/packages/playwright/src/mcp/test/testContext.tsindex 3209b9d3f5..b740482ed0 100644--- a/packages/playwright/src/mcp/test/testContext.ts+++ b/packages/playwright/src/mcp/test/testContext.ts@@ -20,18 +20,21 @@ import type { ConfigLocation } from '../../common/config';export class TestContext {private _testRunner: TestRunner | undefined;- readonly configLocation: ConfigLocation;readonly options?: { muteConsole?: boolean, headless?: boolean };+ configLocation!: ConfigLocation;- constructor(configLocation: ConfigLocation, options?: { muteConsole?: boolean, headless?: boolean }) {- this.configLocation = configLocation;+ constructor(options?: { muteConsole?: boolean, headless?: boolean }) {this.options = options;}+ setConfigLocation(configLocation: ConfigLocation) {+ this.configLocation = configLocation;+ }+async createTestRunner(): Promise<TestRunner> {if (this._testRunner)await this._testRunner.stopTests();- const testRunner = new TestRunner(this.configLocation, {});+ const testRunner = new TestRunner(this.configLocation!, {});await testRunner.initialize({});this._testRunner = testRunner;testRunner.on(TestRunnerEvent.TestFilesChanged, testFiles => {diff --git a/packages/playwright/src/mcp/test/testTools.ts b/packages/playwright/src/mcp/test/testTools.tsindex 2d2929c9a3..bab6161de8 100644--- a/packages/playwright/src/mcp/test/testTools.ts+++ b/packages/playwright/src/mcp/test/testTools.ts@@ -26,7 +26,7 @@ import { StringWriteStream } from './streams';export const listTests = defineTestTool({schema: {- name: 'playwright_test_list_tests',+ name: 'test_list',title: 'List tests',description: 'List tests',inputSchema: z.object({}),@@ -47,7 +47,7 @@ export const listTests = defineTestTool({export const runTests = defineTestTool({schema: {- name: 'playwright_test_run_tests',+ name: 'test_run',title: 'Run tests',description: 'Run tests',inputSchema: z.object({@@ -79,7 +79,7 @@ export const runTests = defineTestTool({export const debugTest = defineTestTool({schema: {- name: 'playwright_test_debug_test',+ name: 'test_debug',title: 'Debug single test',description: 'Debug single test',inputSchema: z.object({@@ -92,14 +92,7 @@ export const debugTest = defineTestTool({},handle: async (context, params) => {- const stream = new StringWriteStream();- const screen = {- ...terminalScreen,- isTTY: false,- colors: noColors,- stdout: stream as unknown as NodeJS.WriteStream,- stderr: stream as unknown as NodeJS.WriteStream,- };+ const { screen, stream } = createScreen();const configDir = context.configLocation.configDir;const reporter = new ListReporter({ configDir, screen });const testRunner = await context.createTestRunner();@@ -122,6 +115,38 @@ export const debugTest = defineTestTool({},});+export const setupPage = defineTestTool({+ schema: {+ name: 'test_setup_page',+ title: 'Setup page',+ description: 'Runs a blank test to setup the page for interaction',+ inputSchema: z.object({+ testLocation: z.string().describe('Location of the blank test to use for setup. For example: "test/e2e/file.spec.ts:20"'),+ }),+ type: 'readOnly',+ },++ handle: async (context, params) => {+ const { screen, stream } = createScreen();+ const configDir = context.configLocation.configDir;+ const reporter = new ListReporter({ configDir, screen });+ const testRunner = await context.createTestRunner();+ const result = await testRunner.runTests(reporter, {+ headed: !context.options?.headless,+ locations: [params.testLocation],+ timeout: 0,+ workers: 1,+ pauseAtEnd: true,+ });++ const text = stream.content();+ return {+ content: [{ type: 'text', text }],+ isError: result.status !== 'passed',+ };+ },+});+function createScreen() {const stream = new StringWriteStream();const screen = {diff --git a/packages/playwright/src/program.ts b/packages/playwright/src/program.tsindex cc75f0f71d..bad07fbc5f 100644--- a/packages/playwright/src/program.ts+++ b/packages/playwright/src/program.ts@@ -36,6 +36,7 @@ import { createErrorCollectingReporter } from './runner/reporters';import { ServerBackendFactory, runMainBackend } from './mcp/sdk/exports';import { TestServerBackend } from './mcp/test/testBackend';import { decorateCommand } from './mcp/program';+import { setupExitWatchdog } from './mcp/browser/watchdog';import { initClaudeCodeRepo, initOpencodeRepo } from './agents/generateAgents';import type { ConfigCLIOverrides } from './common/ipc';@@ -160,12 +161,12 @@ function addTestMCPServerCommand(program: Command) {command.option('--host <host>', 'host to bind server to. Default is localhost. Use 0.0.0.0 to bind to all interfaces.');command.option('--port <port>', 'port to listen on for SSE transport.');command.action(async options => {- const resolvedLocation = resolveConfigLocation(options.config);+ setupExitWatchdog();const backendFactory: ServerBackendFactory = {name: 'Playwright Test Runner',nameInConfig: 'playwright-test-runner',version: packageJSON.version,- create: () => new TestServerBackend(resolvedLocation, { muteConsole: options.port === undefined, headless: options.headless }),+ create: () => new TestServerBackend(options.config, { muteConsole: options.port === undefined, headless: options.headless }),};const mdbUrl = await runMainBackend(backendFactory, { port: options.port === undefined ? undefined : +options.port });if (mdbUrl)diff --git a/packages/playwright/src/runner/dispatcher.ts b/packages/playwright/src/runner/dispatcher.tsindex 5a2310e733..7d64c2f3a5 100644--- a/packages/playwright/src/runner/dispatcher.ts+++ b/packages/playwright/src/runner/dispatcher.ts@@ -218,7 +218,14 @@ export class Dispatcher {_createWorker(testGroup: TestGroup, parallelIndex: number, loaderData: SerializedConfig) {const projectConfig = this._config.projects.find(p => p.id === testGroup.projectId)!;const outputDir = projectConfig.project.outputDir;- const worker = new WorkerHost(testGroup, parallelIndex, loaderData, this._extraEnvByProjectId.get(testGroup.projectId) || {}, outputDir, this._failureTracker.pauseOnError());+ const worker = new WorkerHost(testGroup, {+ parallelIndex,+ config: loaderData,+ extraEnv: this._extraEnvByProjectId.get(testGroup.projectId) || {},+ outputDir,+ pauseOnError: this._failureTracker.pauseOnError(),+ pauseAtEnd: this._failureTracker.pauseAtEnd(),+ });const handleOutput = (params: TestOutputPayload) => {const chunk = chunkFromParams(params);if (worker.didFail()) {diff --git a/packages/playwright/src/runner/failureTracker.ts b/packages/playwright/src/runner/failureTracker.tsindex 499365deaa..c2d709ec61 100644--- a/packages/playwright/src/runner/failureTracker.ts+++ b/packages/playwright/src/runner/failureTracker.ts@@ -23,9 +23,11 @@ export class FailureTracker {private _hasWorkerErrors = false;private _rootSuite: Suite | undefined;private _pauseOnError: boolean;+ private _pauseAtEnd: boolean;- constructor(private _config: FullConfigInternal, options?: { pauseOnError?: boolean }) {+ constructor(private _config: FullConfigInternal, options?: { pauseOnError?: boolean, pauseAtEnd?: boolean }) {this._pauseOnError = options?.pauseOnError ?? false;+ this._pauseAtEnd = options?.pauseAtEnd ?? false;}onRootSuite(rootSuite: Suite) {@@ -46,6 +48,10 @@ export class FailureTracker {return this._pauseOnError;}+ pauseAtEnd(): boolean {+ return this._pauseAtEnd;+ }+hasReachedMaxFailures() {return this.maxFailures() > 0 && this._failureCount >= this.maxFailures();}diff --git a/packages/playwright/src/runner/tasks.ts b/packages/playwright/src/runner/tasks.tsindex 4c980f2d57..c383d71360 100644--- a/packages/playwright/src/runner/tasks.ts+++ b/packages/playwright/src/runner/tasks.ts@@ -64,7 +64,7 @@ export class TestRun {projectFiles: Map<FullProjectInternal, string[]> = new Map();projectSuites: Map<FullProjectInternal, Suite[]> = new Map();- constructor(config: FullConfigInternal, reporter: InternalReporter, options?: { pauseOnError?: boolean }) {+ constructor(config: FullConfigInternal, reporter: InternalReporter, options?: { pauseOnError?: boolean, pauseAtEnd?: boolean }) {this.config = config;this.reporter = reporter;this.failureTracker = new FailureTracker(config, options);diff --git a/packages/playwright/src/runner/testRunner.ts b/packages/playwright/src/runner/testRunner.tsindex dee47ba7ef..cb24a6d357 100644--- a/packages/playwright/src/runner/testRunner.ts+++ b/packages/playwright/src/runner/testRunner.ts@@ -72,6 +72,7 @@ export type RunTestsParams = {reuseContext?: boolean;connectWsEndpoint?: string;pauseOnError?: boolean;+ pauseAtEnd?: boolean;};type FullResultStatus = reporterTypes.FullResult['status'];@@ -337,7 +338,7 @@ export class TestRunner extends EventEmitter<TestRunnerEventMap> {createLoadTask('out-of-process', { filterOnly: true, failOnLoadErrors: false, doNotRunDepsOutsideProjectFilter: true }),...createRunTestsTasks(config),];- const testRun = new TestRun(config, reporter, { pauseOnError: params.pauseOnError });+ const testRun = new TestRun(config, reporter, { pauseOnError: params.pauseOnError, pauseAtEnd: params.pauseAtEnd });const run = runTasks(testRun, tasks, 0, stop).then(async status => {this._testRun = undefined;return status;diff --git a/packages/playwright/src/runner/workerHost.ts b/packages/playwright/src/runner/workerHost.tsindex bf5451672b..979f56f3a9 100644--- a/packages/playwright/src/runner/workerHost.ts+++ b/packages/playwright/src/runner/workerHost.ts@@ -29,6 +29,15 @@ import type { RunPayload, SerializedConfig, WorkerInitParams } from '../common/ilet lastWorkerIndex = 0;+type WorkerHostOptions = {+ parallelIndex: number;+ config: SerializedConfig;+ extraEnv: Record<string, string | undefined>;+ outputDir: string;+ pauseOnError: boolean;+ pauseAtEnd: boolean;+};+export class WorkerHost extends ProcessHost {readonly parallelIndex: number;readonly workerIndex: number;@@ -36,25 +45,26 @@ export class WorkerHost extends ProcessHost {private _params: WorkerInitParams;private _didFail = false;- constructor(testGroup: TestGroup, parallelIndex: number, config: SerializedConfig, extraEnv: Record<string, string | undefined>, outputDir: string, pauseOnError: boolean) {+ constructor(testGroup: TestGroup, options: WorkerHostOptions) {const workerIndex = lastWorkerIndex++;super(require.resolve('../worker/workerMain.js'), `worker-${workerIndex}`, {- ...extraEnv,+ ...options.extraEnv,FORCE_COLOR: '1',DEBUG_COLORS: process.env.DEBUG_COLORS === undefined ? '1' : process.env.DEBUG_COLORS,});this.workerIndex = workerIndex;- this.parallelIndex = parallelIndex;+ this.parallelIndex = options.parallelIndex;this._hash = testGroup.workerHash;this._params = {workerIndex: this.workerIndex,- parallelIndex,+ parallelIndex: options.parallelIndex,repeatEachIndex: testGroup.repeatEachIndex,projectId: testGroup.projectId,- config,- artifactsDir: path.join(outputDir, artifactsFolderName(workerIndex)),- pauseOnError,+ config: options.config,+ artifactsDir: path.join(options.outputDir, artifactsFolderName(workerIndex)),+ pauseOnError: options.pauseOnError,+ pauseAtEnd: options.pauseAtEnd,};}diff --git a/packages/playwright/src/worker/testInfo.ts b/packages/playwright/src/worker/testInfo.tsindex 93dda2e2ad..aba9a28b9f 100644--- a/packages/playwright/src/worker/testInfo.ts+++ b/packages/playwright/src/worker/testInfo.ts@@ -614,6 +614,10 @@ export class TestInfoImpl implements TestInfo {_pauseOnError(): boolean {return this._workerParams.pauseOnError;}++ _pauseAtEnd(): boolean {+ return this._workerParams.pauseAtEnd;+ }}export class TestStepInfoImpl implements TestStepInfo {diff --git a/tests/mcp/test-tools.spec.ts b/tests/mcp/test-tools.spec.tsindex 187264e894..cccfd295e7 100644--- a/tests/mcp/test-tools.spec.ts+++ b/tests/mcp/test-tools.spec.ts@@ -18,7 +18,7 @@ import { test, expect, writeFiles, StartClient } from './fixtures';test.use({ mcpServerType: 'test-mcp' });-test('playwright_test_list_tests', async ({ startClient }) => {+test('test_list', async ({ startClient }) => {await writeFiles({'playwright.config.ts': `module.exports = { projects: [{ name: 'foo' }, {}] };@@ -36,7 +36,7 @@ test('playwright_test_list_tests', async ({ startClient }) => {const { client } = await startClient();expect(await client.callTool({- name: 'playwright_test_list_tests',+ name: 'test_list',arguments: {},})).toHaveTextResponse(`Listing tests:[id=<ID>] [project=foo] › a.test.ts:3:11 › example1@@ -47,7 +47,7 @@ Total: 4 tests in 1 file`);});-test('playwright_test_run_tests', async ({ startClient }) => {+test('test_run', async ({ startClient }) => {await writeFiles({'a.test.ts': `import { test, expect } from '@playwright/test';@@ -72,7 +72,7 @@ test('playwright_test_run_tests', async ({ startClient }) => {const { client } = await startClient();const response = await client.callTool({- name: 'playwright_test_run_tests',+ name: 'test_run',});const text = response.content[0].text;@@ -93,7 +93,7 @@ test('playwright_test_run_tests', async ({ startClient }) => {expect(text).not.toContain(`../../test-results`);});-test('playwright_test_run_tests filters', async ({ startClient }) => {+test('test_run filters', async ({ startClient }) => {await writeFiles({'playwright.config.ts': `module.exports = { projects: [{ name: 'foo' }, { name: 'bar' }] };@@ -120,7 +120,7 @@ test('playwright_test_run_tests filters', async ({ startClient }) => {const { client } = await startClient();expect(await client.callTool({- name: 'playwright_test_run_tests',+ name: 'test_run',arguments: {locations: ['b.test.ts'],projects: ['foo'],@@ -135,7 +135,7 @@ Running 2 tests using 1 worker`);});-test('playwright_test_debug_test (passed)', async ({ startClient }) => {+test('test_debug (passed)', async ({ startClient }) => {await writeFiles({'a.test.ts': `import { test, expect } from '@playwright/test';@@ -147,12 +147,12 @@ test('playwright_test_debug_test (passed)', async ({ startClient }) => {const { client } = await startClient();const listResult = await client.callTool({- name: 'playwright_test_list_tests',+ name: 'test_list',});const [, id] = listResult.content[0].text.match(/\[id=([^\]]+)\]/);expect(await client.callTool({- name: 'playwright_test_debug_test',+ name: 'test_debug',arguments: {test: { id, title: 'pass' },},@@ -165,11 +165,11 @@ Running 1 test using 1 worker`);});-test('playwright_test_debug_test (pause/resume)', async ({ startClient }) => {+test('test_debug (pause/resume)', async ({ startClient }) => {const { client, id } = await prepareDebugTest(startClient);expect(await client.callTool({- name: 'playwright_test_debug_test',+ name: 'test_debug',arguments: {test: { id, title: 'fail' },},@@ -193,17 +193,17 @@ Call log:Try recovering from the error prior to continuing`);expect(await client.callTool({- name: 'playwright_test_run_tests',+ name: 'test_run',arguments: {locations: ['a.test.ts'],},})).toHaveTextResponse(expect.stringContaining(`1) [id=<ID>] a.test.ts:3:11 › fail`));});-test('playwright_test_browser_snapshot', async ({ startClient }) => {+test('test_debug / browser_snapshot', async ({ startClient }) => {const { client, id } = await prepareDebugTest(startClient);await client.callTool({- name: 'playwright_test_debug_test',+ name: 'test_debug',arguments: {test: { id, title: 'fail' },},@@ -215,11 +215,11 @@ test('playwright_test_browser_snapshot', async ({ startClient }) => {});});-test('playwright_test_debug_test (pause/snapshot/resume)', async ({ startClient }) => {+test('test_debug_test (pause/snapshot/resume)', async ({ startClient }) => {const { client, id } = await prepareDebugTest(startClient);expect(await client.callTool({- name: 'playwright_test_debug_test',+ name: 'test_debug',arguments: {test: { id, title: 'fail' },},@@ -249,17 +249,17 @@ Try recovering from the error prior to continuing`);});expect(await client.callTool({- name: 'playwright_test_run_tests',+ name: 'test_run',arguments: {locations: ['a.test.ts'],},})).toHaveTextResponse(expect.stringContaining(`1) [id=<ID>] a.test.ts:3:11 › fail`));});-test('playwright_test_evaluate_on_pause', async ({ startClient }) => {+test('test_debug / evaluate', async ({ startClient }) => {const { client, id } = await prepareDebugTest(startClient);await client.callTool({- name: 'playwright_test_debug_test',+ name: 'test_debug',arguments: {test: { id, title: 'fail' },},@@ -274,10 +274,10 @@ test('playwright_test_evaluate_on_pause', async ({ startClient }) => {});});-test('playwright_test_evaluate_on_pause (with element)', async ({ startClient }) => {+test('test_debug / evaluate (with element)', async ({ startClient }) => {const { client, id } = await prepareDebugTest(startClient);await client.callTool({- name: 'playwright_test_debug_test',+ name: 'test_debug',arguments: {test: { id, title: 'fail' },},@@ -294,10 +294,10 @@ test('playwright_test_evaluate_on_pause (with element)', async ({ startClient })});});-test('playwright_test_generate_locator', async ({ startClient }) => {+test('test_debug / generate_locator', async ({ startClient }) => {const { client, id } = await prepareDebugTest(startClient);await client.callTool({- name: 'playwright_test_debug_test',+ name: 'test_debug',arguments: {test: { id, title: 'fail' },},@@ -313,6 +313,41 @@ test('playwright_test_generate_locator', async ({ startClient }) => {});});+test('test_setup_page', async ({ startClient }) => {+ await writeFiles({+ 'a.test.ts': `+ import { test, expect } from '@playwright/test';+ test.beforeEach(async ({ page }) => {+ await page.setContent('<button>Submit</button>');+ });+ test('template', async ({ page }) => {+ });+ `,+ });++ const { client } = await startClient();+ expect(await client.callTool({+ name: 'test_setup_page',+ arguments: {+ testLocation: 'a.test.ts:6',+ },+ })).toHaveTextResponse(`### Paused at end of test. ready for interaction++### Current page snapshot:+- button "Submit" [ref=e2]`);++ expect(await client.callTool({+ name: 'browser_click',+ arguments: {+ element: 'Submit button',+ ref: 'e2',+ },+ })).toHaveResponse({+ code: `await page.getByRole('button', { name: 'Submit' }).click();`,+ pageState: expect.stringContaining(`- button "Submit"`),+ });+});+async function prepareDebugTest(startClient: StartClient) {await writeFiles({'a.test.ts': `@@ -326,7 +361,7 @@ async function prepareDebugTest(startClient: StartClient) {const { client } = await startClient();const listResult = await client.callTool({- name: 'playwright_test_list_tests',+ name: 'test_list',});const [, id] = listResult.content[0].text.match(/\[id=([^\]]+)\]/);return { client, id };