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
feat(mcp): allow clicking with modifiers (#37315)
Pavel Feldmancc69e07738a8
diff --git a/packages/playwright/src/mcp/browser/codegen.ts b/packages/playwright/src/mcp/browser/codegen.tsindex a3bc8d0e02..a67700fc4f 100644--- a/packages/playwright/src/mcp/browser/codegen.ts+++ b/packages/playwright/src/mcp/browser/codegen.ts@@ -35,7 +35,7 @@ export function quote(text: string) {return escapeWithQuotes(text, '\'');}-export function formatObject(value: any, indent = ' '): string {+export function formatObject(value: any, indent = ' ', mode: 'multiline' | 'oneline' = 'multiline'): string {if (typeof value === 'string')return quote(value);if (Array.isArray(value))@@ -47,7 +47,9 @@ export function formatObject(value: any, indent = ' '): string {const tokens: string[] = [];for (const key of keys)tokens.push(`${key}: ${formatObject(value[key])}`);- return `{\n${indent}${tokens.join(`,\n${indent}`)}\n}`;+ if (mode === 'multiline')+ return `{\n${tokens.join(`,\n${indent}`)}\n}`;+ return `{ ${tokens.join(', ')} }`;}return String(value);}diff --git a/packages/playwright/src/mcp/browser/tools/snapshot.ts b/packages/playwright/src/mcp/browser/tools/snapshot.tsindex 03c6852e14..5f2cb790d0 100644--- a/packages/playwright/src/mcp/browser/tools/snapshot.ts+++ b/packages/playwright/src/mcp/browser/tools/snapshot.ts@@ -43,6 +43,7 @@ export const elementSchema = z.object({const clickSchema = elementSchema.extend({doubleClick: z.boolean().optional().describe('Whether to perform a double click instead of a single click'),button: z.enum(['left', 'right', 'middle']).optional().describe('Button to click, defaults to left'),+ modifiers: z.array(z.enum(['Alt', 'Control', 'ControlOrMeta', 'Meta', 'Shift'])).optional().describe('Modifier keys to press'),});const click = defineTabTool({@@ -59,20 +60,23 @@ const click = defineTabTool({response.setIncludeSnapshot();const locator = await tab.refLocator(params);- const button = params.button;- const buttonAttr = button ? `{ button: '${button}' }` : '';+ const options = {+ button: params.button,+ modifiers: params.modifiers,+ };+ const formatted = javascript.formatObject(options, ' ', 'oneline');+ const optionsAttr = formatted !== '{}' ? formatted : '';if (params.doubleClick)- response.addCode(`await page.${await generateLocator(locator)}.dblclick(${buttonAttr});`);+ response.addCode(`await page.${await generateLocator(locator)}.dblclick(${optionsAttr});`);else- response.addCode(`await page.${await generateLocator(locator)}.click(${buttonAttr});`);-+ response.addCode(`await page.${await generateLocator(locator)}.click(${optionsAttr});`);await tab.waitForCompletion(async () => {if (params.doubleClick)- await locator.dblclick({ button });+ await locator.dblclick(options);else- await locator.click({ button });+ await locator.click(options);});},});diff --git a/tests/mcp/click.spec.ts b/tests/mcp/click.spec.tsindex 53db23152d..f7de50e3e7 100644--- a/tests/mcp/click.spec.ts+++ b/tests/mcp/click.spec.ts@@ -97,3 +97,57 @@ test('browser_click (right)', async ({ client, server }) => {pageState: expect.stringContaining(`- button "Right clicked"`),});});++test('browser_click (modifiers)', async ({ client, server, mcpBrowser }) => {+ server.setContent('/', `+ <title>Title</title>+ <button>Submit</button>+ <div id="div"></div>+ <script>+ document.addEventListener('click', event => {+ document.querySelector('div').textContent = 'ctrlKey:' + event.ctrlKey + ' metaKey:' + event.metaKey + ' shiftKey:' + event.shiftKey + ' altKey:' + event.altKey;+ });+ </script>+ `, 'text/html');++ await client.callTool({+ name: 'browser_navigate',+ arguments: { url: server.PREFIX },+ });++ expect(await client.callTool({+ name: 'browser_click',+ arguments: {+ element: 'Submit button',+ ref: 'e2',+ modifiers: ['Control'],+ },+ })).toHaveResponse({+ code: `await page.getByRole('button', { name: 'Submit' }).click({ modifiers: ['Control'] });`,+ pageState: expect.stringContaining(`- generic [ref=e3]: ctrlKey:true metaKey:false shiftKey:false altKey:false`),+ });++ expect(await client.callTool({+ name: 'browser_click',+ arguments: {+ element: 'Submit button',+ ref: 'e2',+ modifiers: ['Shift'],+ },+ })).toHaveResponse({+ code: `await page.getByRole('button', { name: 'Submit' }).click({ modifiers: ['Shift'] });`,+ pageState: expect.stringContaining(`- generic [ref=e3]: ctrlKey:false metaKey:false shiftKey:true altKey:false`),+ });++ expect(await client.callTool({+ name: 'browser_click',+ arguments: {+ element: 'Submit button',+ ref: 'e2',+ modifiers: ['Shift', 'Alt'],+ },+ })).toHaveResponse({+ code: `await page.getByRole('button', { name: 'Submit' }).click({ modifiers: ['Shift', 'Alt'] });`,+ pageState: expect.stringContaining(`- generic [ref=e3]: ctrlKey:false metaKey:false shiftKey:true altKey:true`),+ });+});