Skip to content

Commit faf317c

Browse files
authored
chore: tune press/type via cli (#38898)
1 parent ba4b983 commit faf317c

File tree

4 files changed

+36
-12
lines changed

4 files changed

+36
-12
lines changed

packages/playwright/src/mcp/browser/tools/keyboard.ts

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,37 @@ const pressKey = defineTabTool({
3232
},
3333

3434
handle: async (tab, params, response) => {
35-
response.setIncludeSnapshot();
3635
response.addCode(`// Press ${params.key}`);
3736
response.addCode(`await page.keyboard.press('${params.key}');`);
37+
await tab.page.keyboard.press(params.key);
38+
},
39+
});
3840

39-
await tab.waitForCompletion(async () => {
40-
await tab.page.keyboard.press(params.key);
41-
});
41+
const pressSequentially = defineTabTool({
42+
capability: 'internal',
43+
44+
schema: {
45+
name: 'browser_press_sequentially',
46+
title: 'Press sequentially',
47+
description: 'Press text sequentially on the keyboard',
48+
inputSchema: z.object({
49+
text: z.string().describe('Text to press sequentially'),
50+
submit: z.boolean().optional().describe('Whether to submit entered text (press Enter after)'),
51+
}),
52+
type: 'input',
53+
},
54+
55+
handle: async (tab, params, response) => {
56+
response.addCode(`// Press ${params.text}`);
57+
response.addCode(`await page.keyboard.type('${params.text}');`);
58+
await tab.page.keyboard.type(params.text);
59+
if (params.submit) {
60+
response.addCode(`await page.keyboard.press('Enter');`);
61+
response.setIncludeSnapshot();
62+
await tab.waitForCompletion(async () => {
63+
await tab.page.keyboard.press('Enter');
64+
});
65+
}
4266
},
4367
});
4468

@@ -84,4 +108,5 @@ const type = defineTabTool({
84108
export default [
85109
pressKey,
86110
type,
111+
pressSequentially,
87112
];

packages/playwright/src/mcp/terminal/commands.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ const networkRequests = declareCommand({
156156
});
157157

158158
const pressKey = declareCommand({
159-
name: 'press-key',
159+
name: 'press',
160160
description: 'Press a key on the keyboard',
161161
args: z.object({
162162
key: z.string().describe('Name of the key to press or a character to generate, such as `ArrowLeft` or `a`'),
@@ -226,15 +226,13 @@ const type = declareCommand({
226226
name: 'type',
227227
description: 'Type text into editable element',
228228
args: z.object({
229-
ref: z.string().describe('Exact target element reference from the page snapshot'),
230229
text: z.string().describe('Text to type into the element'),
231230
}),
232231
options: z.object({
233232
submit: z.boolean().optional().describe('Whether to submit entered text (press Enter after)'),
234-
slowly: z.boolean().optional().describe('Whether to type one character at a time. Useful for triggering key handlers in the page.'),
235233
}),
236-
toolName: 'browser_type',
237-
toolParams: ({ ref, text }, { submit, slowly }) => ({ ref, text, submit, slowly }),
234+
toolName: 'browser_press_sequentially',
235+
toolParams: ({ text }, { submit }) => ({ text, submit }),
238236
});
239237

240238
const waitFor = declareCommand({

packages/playwright/src/mcp/terminal/help.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"global": "Usage: playwright-cli <command> [options]\nCommands:\n click <ref> perform click on a web page\n close close the page\n dblclick <ref> perform double click on a web page\n console <level> returns all console messages\n drag <startRef> <endRef> perform drag and drop between two elements\n evaluate <function> <ref> evaluate javascript expression on page or element\n upload-file upload one or multiple files\n handle-dialog <accept> <promptText> handle a dialog\n hover <ref> hover over element on page\n open <url> open url\n go-back go back to the previous page\n network-requests returns all network requests since loading the page\n press-key <key> press a key on the keyboard\n resize <width> <height> resize the browser window\n run-code <code> run playwright code snippet\n select-option <ref> <values> select an option in a dropdown\n snapshot capture accessibility snapshot of the current page, this is better than screenshot\n screenshot <ref> take a screenshot of the current page. you can't perform actions based on the screenshot, use browser_snapshot for actions.\n type <ref> <text> type text into editable element\n wait-for wait for text to appear or disappear or a specified time to pass\n tab <action> <index> close a browser tab\n mouse-click-xy <x> <y> click left mouse button at a given position\n mouse-drag-xy <startX> <startY> <endX> <endY> drag left mouse button to a given position\n mouse-move-xy <x> <y> move mouse to a given position\n pdf-save save page as pdf\n start-tracing start trace recording\n stop-tracing stop trace recording",
2+
"global": "Usage: playwright-cli <command> [options]\nCommands:\n click <ref> perform click on a web page\n close close the page\n dblclick <ref> perform double click on a web page\n console <level> returns all console messages\n drag <startRef> <endRef> perform drag and drop between two elements\n evaluate <function> <ref> evaluate javascript expression on page or element\n upload-file upload one or multiple files\n handle-dialog <accept> <promptText> handle a dialog\n hover <ref> hover over element on page\n open <url> open url\n go-back go back to the previous page\n network-requests returns all network requests since loading the page\n press <key> press a key on the keyboard\n resize <width> <height> resize the browser window\n run-code <code> run playwright code snippet\n select-option <ref> <values> select an option in a dropdown\n snapshot capture accessibility snapshot of the current page, this is better than screenshot\n screenshot <ref> take a screenshot of the current page. you can't perform actions based on the screenshot, use browser_snapshot for actions.\n type <text> type text into editable element\n wait-for wait for text to appear or disappear or a specified time to pass\n tab <action> <index> close a browser tab\n mouse-click-xy <x> <y> click left mouse button at a given position\n mouse-drag-xy <startX> <startY> <endX> <endY> drag left mouse button to a given position\n mouse-move-xy <x> <y> move mouse to a given position\n pdf-save save page as pdf\n start-tracing start trace recording\n stop-tracing stop trace recording",
33
"commands": {
44
"click": "playwright-cli click <ref>\n\nPerform click on a web page\n\nArguments:\n <ref>\tExact target element reference from the page snapshot\nOptions:\n --button\tbutton to click, defaults to left\n --modifiers\tmodifier keys to press",
55
"close": "playwright-cli close \n\nClose the page\n",
@@ -13,13 +13,13 @@
1313
"open": "playwright-cli open <url>\n\nOpen URL\n\nArguments:\n <url>\tThe URL to navigate to\nOptions:\n --headed\trun browser in headed mode",
1414
"go-back": "playwright-cli go-back \n\nGo back to the previous page\n",
1515
"network-requests": "playwright-cli network-requests \n\nReturns all network requests since loading the page\n\nOptions:\n --includeStatic\twhether to include successful static resources like images, fonts, scripts, etc. defaults to false.",
16-
"press-key": "playwright-cli press-key <key>\n\nPress a key on the keyboard\n\nArguments:\n <key>\tName of the key to press or a character to generate, such as `ArrowLeft` or `a`",
16+
"press": "playwright-cli press <key>\n\nPress a key on the keyboard\n\nArguments:\n <key>\tName of the key to press or a character to generate, such as `ArrowLeft` or `a`",
1717
"resize": "playwright-cli resize <width> <height>\n\nResize the browser window\n\nArguments:\n <width>\tWidth of the browser window\n <height>\tHeight of the browser window",
1818
"run-code": "playwright-cli run-code <code>\n\nRun Playwright code snippet\n\nArguments:\n <code>\tA JavaScript function containing Playwright code to execute. It will be invoked with a single argument, page, which you can use for any page interaction.",
1919
"select-option": "playwright-cli select-option <ref> <values>\n\nSelect an option in a dropdown\n\nArguments:\n <ref>\tExact target element reference from the page snapshot\n <values>\tArray of values to select in the dropdown. This can be a single value or multiple values.",
2020
"snapshot": "playwright-cli snapshot \n\nCapture accessibility snapshot of the current page, this is better than screenshot\n\nOptions:\n --filename\tsave snapshot to markdown file instead of returning it in the response.",
2121
"screenshot": "playwright-cli screenshot <ref>\n\nTake a screenshot of the current page. You can't perform actions based on the screenshot, use browser_snapshot for actions.\n\nArguments:\n <ref>\tExact target element reference from the page snapshot.\nOptions:\n --filename\tfile name to save the screenshot to. defaults to `page-{timestamp}.{png|jpeg}` if not specified.\n --fullPage\twhen true, takes a screenshot of the full scrollable page, instead of the currently visible viewport.",
22-
"type": "playwright-cli type <ref> <text>\n\nType text into editable element\n\nArguments:\n <ref>\tExact target element reference from the page snapshot\n <text>\tText to type into the element\nOptions:\n --submit\twhether to submit entered text (press enter after)\n --slowly\twhether to type one character at a time. useful for triggering key handlers in the page.",
22+
"type": "playwright-cli type <text>\n\nType text into editable element\n\nArguments:\n <text>\tText to type into the element\nOptions:\n --submit\twhether to submit entered text (press enter after)",
2323
"wait-for": "playwright-cli wait-for \n\nWait for text to appear or disappear or a specified time to pass\n\nOptions:\n --time\tthe time to wait in seconds\n --text\tthe text to wait for\n --textGone\tthe text to wait for to disappear",
2424
"tab": "playwright-cli tab <action> <index>\n\nClose a browser tab\n\nArguments:\n <action>\tAction to perform on tabs, 'list' | 'new' | 'close' | 'select'\n <index>\tTab index. If omitted, current tab is closed.",
2525
"mouse-click-xy": "playwright-cli mouse-click-xy <x> <y>\n\nClick left mouse button at a given position\n\nArguments:\n <x>\tX coordinate\n <y>\tY coordinate",

tests/mcp/generator.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ test('generator tools intent', async ({ startClient }) => {
4747
'browser_navigate_back',
4848
'browser_open',
4949
'browser_press_key',
50+
'browser_press_sequentially',
5051
'browser_resize',
5152
'browser_run_code',
5253
'browser_select_option',

0 commit comments

Comments
 (0)