Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

chore(mcp): test prefixes, fix bugs (#37547)

Pavel Feldmaneb9ebbe1ec9e
diff --git a/packages/playwright/src/mcp/browser/tab.ts b/packages/playwright/src/mcp/browser/tab.ts
index 9babbbc4c5..7c58d1b9a7 100644
--- a/packages/playwright/src/mcp/browser/tab.ts
+++ b/packages/playwright/src/mcp/browser/tab.ts
@@ -21,6 +21,8 @@ import { ManualPromise } from 'playwright-core/lib/utils';
import { callOnPageNoTrace, waitForCompletion } from './tools/utils';
import { logUnhandledError } from '../log';
import { ModalState } from './tools/tool';
+import { handleDialog } from './tools/dialogs';
+import { uploadFile } from './tools/files';
import type { Context } from './context';
@@ -71,7 +73,7 @@ export class Tab extends EventEmitter<TabEventsInterface> {
type: 'fileChooser',
description: 'File chooser',
fileChooser: chooser,
- clearedBy: 'browser_file_upload',
+ clearedBy: uploadFile.schema.name,
});
});
page.on('dialog', dialog => this._dialogShown(dialog));
@@ -127,7 +129,7 @@ export class Tab extends EventEmitter<TabEventsInterface> {
type: 'dialog',
description: `"${dialog.type()}" dialog with message "${dialog.message()}"`,
dialog,
- clearedBy: 'browser_handle_dialog',
+ clearedBy: handleDialog.schema.name
});
}
diff --git a/packages/playwright/src/mcp/browser/tools/dialogs.ts b/packages/playwright/src/mcp/browser/tools/dialogs.ts
index f6954b7225..d606433f1a 100644
--- a/packages/playwright/src/mcp/browser/tools/dialogs.ts
+++ b/packages/playwright/src/mcp/browser/tools/dialogs.ts
@@ -17,7 +17,7 @@
import { z } from '../../sdk/bundle';
import { defineTabTool } from './tool';
-const handleDialog = defineTabTool({
+export const handleDialog = defineTabTool({
capability: 'core',
schema: {
diff --git a/packages/playwright/src/mcp/browser/tools/files.ts b/packages/playwright/src/mcp/browser/tools/files.ts
index 79514a0b99..fcd77ec4c4 100644
--- a/packages/playwright/src/mcp/browser/tools/files.ts
+++ b/packages/playwright/src/mcp/browser/tools/files.ts
@@ -17,7 +17,7 @@
import { z } from '../../sdk/bundle';
import { defineTabTool } from './tool';
-const uploadFile = defineTabTool({
+export const uploadFile = defineTabTool({
capability: 'core',
schema: {
diff --git a/packages/playwright/src/mcp/browser/tools/tool.ts b/packages/playwright/src/mcp/browser/tools/tool.ts
index c3ef466c5d..131c4f0115 100644
--- a/packages/playwright/src/mcp/browser/tools/tool.ts
+++ b/packages/playwright/src/mcp/browser/tools/tool.ts
@@ -26,14 +26,14 @@ export type FileUploadModalState = {
type: 'fileChooser';
description: string;
fileChooser: playwright.FileChooser;
- clearedBy: 'browser_file_upload';
+ clearedBy: string;
};
export type DialogModalState = {
type: 'dialog';
description: string;
dialog: playwright.Dialog;
- clearedBy: 'browser_handle_dialog';
+ clearedBy: string;
};
export type ModalState = FileUploadModalState | DialogModalState;
diff --git a/tests/mcp/fixtures.ts b/tests/mcp/fixtures.ts
index 26f239f6be..c17a0030cc 100644
--- a/tests/mcp/fixtures.ts
+++ b/tests/mcp/fixtures.ts
@@ -331,3 +331,22 @@ export async function writeFiles(files: Files, options?: { update?: boolean }) {
return baseDir;
}
+
+export async function prepareDebugTest(startClient: StartClient, testFile?: string, clientArgs?: Parameters<StartClient>[0]) {
+ await writeFiles({
+ 'a.test.ts': testFile || `
+ import { test, expect } from '@playwright/test';
+ test('fail', async ({ page }) => {
+ await page.setContent('<button>Submit</button>');
+ await expect(page.getByRole('button', { name: 'Missing' })).toBeVisible({ timeout: 1000 });
+ });
+ `
+ });
+
+ const { client } = await startClient(clientArgs);
+ const listResult = await client.callTool({
+ name: 'test_list',
+ });
+ const [, id] = listResult.content[0].text.match(/\[id=([^\]]+)\]/);
+ return { client, id };
+}
diff --git a/tests/mcp/prefix.spec.ts b/tests/mcp/prefix.spec.ts
new file mode 100644
index 0000000000..6a096eef0d
--- /dev/null
+++ b/tests/mcp/prefix.spec.ts
@@ -0,0 +1,122 @@
+/**
+ * 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 { test, expect, prepareDebugTest } from './fixtures';
+
+test('tool prefix', async ({ startClient, server }) => {
+ const { client } = await startClient({ env: { 'PLAYWRIGHT_MCP_TOOL_PREFIX': 'test_' } });
+ expect(await client.callTool({
+ name: 'test_browser_navigate',
+ arguments: { url: server.HELLO_WORLD },
+ })).toHaveResponse({
+ code: `await page.goto('${server.HELLO_WORLD}');`,
+ pageState: `- Page URL: ${server.HELLO_WORLD}
+- Page Title: Title
+- Page Snapshot:
+\`\`\`yaml
+- generic [active] [ref=e1]: Hello, world!
+\`\`\``,
+ });
+});
+
+test('alert dialog', async ({ server, startClient }) => {
+ const { client } = await startClient({ env: { 'PLAYWRIGHT_MCP_TOOL_PREFIX': 'test_' } });
+ server.setContent('/', `<button onclick="alert('Alert')">Button</button>`, 'text/html');
+ expect(await client.callTool({
+ name: 'test_browser_navigate',
+ arguments: { url: server.PREFIX },
+ })).toHaveResponse({
+ pageState: expect.stringContaining(`- button "Button" [ref=e2]`),
+ });
+
+ expect(await client.callTool({
+ name: 'test_browser_click',
+ arguments: {
+ element: 'Button',
+ ref: 'e2',
+ },
+ })).toHaveResponse({
+ code: `await page.getByRole('button', { name: 'Button' }).click();`,
+ modalState: `- ["alert" dialog with message "Alert"]: can be handled by the "test_browser_handle_dialog" tool`,
+ });
+});
+
+test('browser_file_upload', async ({ server, startClient }) => {
+ const { client } = await startClient({ env: { 'PLAYWRIGHT_MCP_TOOL_PREFIX': 'test_' } });
+ server.setContent('/', `
+ <input type="file" />
+ <button>Button</button>
+ `, 'text/html');
+
+ expect(await client.callTool({
+ name: 'test_browser_navigate',
+ arguments: { url: server.PREFIX },
+ })).toHaveResponse({
+ pageState: expect.stringContaining(`- generic [active] [ref=e1]:
+ - button "Choose File" [ref=e2]
+ - button "Button" [ref=e3]`),
+ });
+
+ expect(await client.callTool({
+ name: 'test_browser_file_upload',
+ arguments: { paths: [] },
+ })).toHaveResponse({
+ isError: true,
+ result: expect.stringContaining(`The tool "test_browser_file_upload" can only be used when there is related modal state present.`),
+ modalState: expect.stringContaining(`- There is no modal state present`),
+ });
+});
+
+test.describe(() => {
+ test.use({ mcpServerType: 'test-mcp' });
+
+ test('test_debug (browser_snapshot/network/console)', async ({ startClient, server }) => {
+ const { client, id } = await prepareDebugTest(startClient, `
+ import { test, expect } from '@playwright/test';
+ test('fail', async ({ page }) => {
+ await page.goto(${JSON.stringify(server.HELLO_WORLD)});
+ await page.evaluate(() => {
+ console.log('hello from console');
+ setTimeout(() => { throw new Error('error from page'); }, 0);
+ });
+ await expect(page.getByRole('button', { name: 'Missing' })).toBeVisible({ timeout: 1000 });
+ });
+ `, {
+ env: { 'PLAYWRIGHT_MCP_TOOL_PREFIX': 'test_' },
+ });
+ await client.callTool({
+ name: 'test_debug',
+ arguments: {
+ test: { id, title: 'fail' },
+ },
+ });
+ await expect.poll(() => client.callTool({
+ name: 'test_browser_network_requests',
+ })).toHaveResponse({
+ result: expect.stringContaining(`[GET] ${server.HELLO_WORLD} => [200] OK`),
+ });
+ expect(await client.callTool({
+ name: 'test_browser_console_messages',
+ })).toHaveResponse({
+ result: expect.stringMatching(/\[LOG\] hello from console.*\nError: error from page/),
+ });
+ expect(await client.callTool({
+ name: 'test_browser_snapshot',
+ })).toHaveResponse({
+ pageState: expect.stringContaining(`generic [active] [ref=e1]: Hello, world!`),
+ });
+ });
+});
diff --git a/tests/mcp/test-tools.spec.ts b/tests/mcp/test-tools.spec.ts
index 9da107e4f4..e7e9193fa2 100644
--- a/tests/mcp/test-tools.spec.ts
+++ b/tests/mcp/test-tools.spec.ts
@@ -14,7 +14,7 @@
* limitations under the License.
*/
-import { test, expect, writeFiles, StartClient } from './fixtures';
+import { test, expect, writeFiles, prepareDebugTest } from './fixtures';
import fs from 'fs';
import path from 'path';
@@ -620,22 +620,3 @@ test('test_setup_page without location respects testsDir', async ({ startClient
`);
expect(fs.existsSync(test.info().outputPath('tests', 'default.seed.spec.ts'))).toBe(true);
});
-
-async function prepareDebugTest(startClient: StartClient, testFile?: string) {
- await writeFiles({
- 'a.test.ts': testFile || `
- import { test, expect } from '@playwright/test';
- test('fail', async ({ page }) => {
- await page.setContent('<button>Submit</button>');
- await expect(page.getByRole('button', { name: 'Missing' })).toBeVisible({ timeout: 1000 });
- });
- `
- });
-
- const { client } = await startClient();
- const listResult = await client.callTool({
- name: 'test_list',
- });
- const [, id] = listResult.content[0].text.match(/\[id=([^\]]+)\]/);
- return { client, id };
-}
diff --git a/tests/mcp/vscode.spec.ts b/tests/mcp/vscode.spec.ts
index f563d39ed0..fc0b8187e8 100644
--- a/tests/mcp/vscode.spec.ts
+++ b/tests/mcp/vscode.spec.ts
@@ -118,28 +118,3 @@ test('browser_connect(debugController) works', async ({ startClient }) => {
await expect.poll(() => messages).toContainEqual(expect.objectContaining({ method: 'stateChanged' }));
});
-
-test('tool prefix', async ({ startClient, server }) => {
- const { client } = await startClient({ env: { 'PLAYWRIGHT_MCP_TOOL_PREFIX': 'test_' } });
- expect(await client.callTool({
- name: 'test_browser_navigate',
- arguments: { url: server.HELLO_WORLD },
- })).toHaveResponse({
- code: `await page.goto('${server.HELLO_WORLD}');`,
- pageState: `- Page URL: ${server.HELLO_WORLD}
-- Page Title: Title
-- Page Snapshot:
-\`\`\`yaml
-- generic [active] [ref=e1]: Hello, world!
-\`\`\``,
- });
-});
-
-test.describe(() => {
- test.use({ mcpServerType: 'test-mcp' });
- test('tool prefix does not affect test tools', async ({ startClient }) => {
- const { client } = await startClient({ env: { 'PLAYWRIGHT_MCP_TOOL_PREFIX': 'test_' } });
- const { tools } = await client.listTools();
- expect(tools.map(t => t.name)).toContain('test_setup_page');
- });
-});