@playwright/mcp 0.0.30 → 0.0.32

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (54) hide show
  1. package/README.md +180 -320
  2. package/config.d.ts +5 -14
  3. package/index.d.ts +1 -6
  4. package/lib/browserContextFactory.js +3 -35
  5. package/lib/browserServerBackend.js +54 -0
  6. package/lib/config.js +64 -7
  7. package/lib/context.js +50 -163
  8. package/lib/extension/cdpRelay.js +370 -0
  9. package/lib/extension/main.js +33 -0
  10. package/lib/httpServer.js +20 -182
  11. package/lib/index.js +3 -2
  12. package/lib/log.js +21 -0
  13. package/lib/loop/loop.js +69 -0
  14. package/lib/loop/loopClaude.js +152 -0
  15. package/lib/loop/loopOpenAI.js +141 -0
  16. package/lib/loop/main.js +60 -0
  17. package/lib/loopTools/context.js +66 -0
  18. package/lib/loopTools/main.js +49 -0
  19. package/lib/loopTools/perform.js +32 -0
  20. package/lib/loopTools/snapshot.js +29 -0
  21. package/lib/loopTools/tool.js +18 -0
  22. package/lib/mcp/inProcessTransport.js +72 -0
  23. package/lib/mcp/server.js +88 -0
  24. package/lib/{transport.js → mcp/transport.js} +30 -42
  25. package/lib/package.js +3 -3
  26. package/lib/program.js +47 -17
  27. package/lib/response.js +98 -0
  28. package/lib/sessionLog.js +70 -0
  29. package/lib/tab.js +166 -21
  30. package/lib/tools/common.js +13 -25
  31. package/lib/tools/console.js +4 -15
  32. package/lib/tools/dialogs.js +14 -19
  33. package/lib/tools/evaluate.js +53 -0
  34. package/lib/tools/files.js +13 -19
  35. package/lib/tools/install.js +4 -8
  36. package/lib/tools/keyboard.js +51 -17
  37. package/lib/tools/mouse.js +99 -0
  38. package/lib/tools/navigate.js +22 -42
  39. package/lib/tools/network.js +5 -15
  40. package/lib/tools/pdf.js +8 -15
  41. package/lib/tools/screenshot.js +29 -30
  42. package/lib/tools/snapshot.js +49 -109
  43. package/lib/tools/tabs.js +21 -52
  44. package/lib/tools/tool.js +14 -0
  45. package/lib/tools/utils.js +7 -6
  46. package/lib/tools/wait.js +8 -11
  47. package/lib/tools.js +15 -26
  48. package/package.json +12 -5
  49. package/lib/browserServer.js +0 -151
  50. package/lib/connection.js +0 -82
  51. package/lib/pageSnapshot.js +0 -43
  52. package/lib/server.js +0 -48
  53. package/lib/tools/testing.js +0 -60
  54. package/lib/tools/vision.js +0 -189
@@ -0,0 +1,99 @@
1
+ /**
2
+ * Copyright (c) Microsoft Corporation.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+ import { z } from 'zod';
17
+ import { defineTabTool } from './tool.js';
18
+ const elementSchema = z.object({
19
+ element: z.string().describe('Human-readable element description used to obtain permission to interact with the element'),
20
+ });
21
+ const mouseMove = defineTabTool({
22
+ capability: 'vision',
23
+ schema: {
24
+ name: 'browser_mouse_move_xy',
25
+ title: 'Move mouse',
26
+ description: 'Move mouse to a given position',
27
+ inputSchema: elementSchema.extend({
28
+ x: z.number().describe('X coordinate'),
29
+ y: z.number().describe('Y coordinate'),
30
+ }),
31
+ type: 'readOnly',
32
+ },
33
+ handle: async (tab, params, response) => {
34
+ response.addCode(`// Move mouse to (${params.x}, ${params.y})`);
35
+ response.addCode(`await page.mouse.move(${params.x}, ${params.y});`);
36
+ await tab.waitForCompletion(async () => {
37
+ await tab.page.mouse.move(params.x, params.y);
38
+ });
39
+ },
40
+ });
41
+ const mouseClick = defineTabTool({
42
+ capability: 'vision',
43
+ schema: {
44
+ name: 'browser_mouse_click_xy',
45
+ title: 'Click',
46
+ description: 'Click left mouse button at a given position',
47
+ inputSchema: elementSchema.extend({
48
+ x: z.number().describe('X coordinate'),
49
+ y: z.number().describe('Y coordinate'),
50
+ }),
51
+ type: 'destructive',
52
+ },
53
+ handle: async (tab, params, response) => {
54
+ response.setIncludeSnapshot();
55
+ response.addCode(`// Click mouse at coordinates (${params.x}, ${params.y})`);
56
+ response.addCode(`await page.mouse.move(${params.x}, ${params.y});`);
57
+ response.addCode(`await page.mouse.down();`);
58
+ response.addCode(`await page.mouse.up();`);
59
+ await tab.waitForCompletion(async () => {
60
+ await tab.page.mouse.move(params.x, params.y);
61
+ await tab.page.mouse.down();
62
+ await tab.page.mouse.up();
63
+ });
64
+ },
65
+ });
66
+ const mouseDrag = defineTabTool({
67
+ capability: 'vision',
68
+ schema: {
69
+ name: 'browser_mouse_drag_xy',
70
+ title: 'Drag mouse',
71
+ description: 'Drag left mouse button to a given position',
72
+ inputSchema: elementSchema.extend({
73
+ startX: z.number().describe('Start X coordinate'),
74
+ startY: z.number().describe('Start Y coordinate'),
75
+ endX: z.number().describe('End X coordinate'),
76
+ endY: z.number().describe('End Y coordinate'),
77
+ }),
78
+ type: 'destructive',
79
+ },
80
+ handle: async (tab, params, response) => {
81
+ response.setIncludeSnapshot();
82
+ response.addCode(`// Drag mouse from (${params.startX}, ${params.startY}) to (${params.endX}, ${params.endY})`);
83
+ response.addCode(`await page.mouse.move(${params.startX}, ${params.startY});`);
84
+ response.addCode(`await page.mouse.down();`);
85
+ response.addCode(`await page.mouse.move(${params.endX}, ${params.endY});`);
86
+ response.addCode(`await page.mouse.up();`);
87
+ await tab.waitForCompletion(async () => {
88
+ await tab.page.mouse.move(params.startX, params.startY);
89
+ await tab.page.mouse.down();
90
+ await tab.page.mouse.move(params.endX, params.endY);
91
+ await tab.page.mouse.up();
92
+ });
93
+ },
94
+ });
95
+ export default [
96
+ mouseMove,
97
+ mouseClick,
98
+ mouseDrag,
99
+ ];
@@ -14,8 +14,8 @@
14
14
  * limitations under the License.
15
15
  */
16
16
  import { z } from 'zod';
17
- import { defineTool } from './tool.js';
18
- const navigate = captureSnapshot => defineTool({
17
+ import { defineTool, defineTabTool } from './tool.js';
18
+ const navigate = defineTool({
19
19
  capability: 'core',
20
20
  schema: {
21
21
  name: 'browser_navigate',
@@ -26,22 +26,16 @@ const navigate = captureSnapshot => defineTool({
26
26
  }),
27
27
  type: 'destructive',
28
28
  },
29
- handle: async (context, params) => {
29
+ handle: async (context, params, response) => {
30
30
  const tab = await context.ensureTab();
31
31
  await tab.navigate(params.url);
32
- const code = [
33
- `// Navigate to ${params.url}`,
34
- `await page.goto('${params.url}');`,
35
- ];
36
- return {
37
- code,
38
- captureSnapshot,
39
- waitForNetwork: false,
40
- };
32
+ response.setIncludeSnapshot();
33
+ response.addCode(`// Navigate to ${params.url}`);
34
+ response.addCode(`await page.goto('${params.url}');`);
41
35
  },
42
36
  });
43
- const goBack = captureSnapshot => defineTool({
44
- capability: 'history',
37
+ const goBack = defineTabTool({
38
+ capability: 'core',
45
39
  schema: {
46
40
  name: 'browser_navigate_back',
47
41
  title: 'Go back',
@@ -49,22 +43,15 @@ const goBack = captureSnapshot => defineTool({
49
43
  inputSchema: z.object({}),
50
44
  type: 'readOnly',
51
45
  },
52
- handle: async (context) => {
53
- const tab = await context.ensureTab();
46
+ handle: async (tab, params, response) => {
54
47
  await tab.page.goBack();
55
- const code = [
56
- `// Navigate back`,
57
- `await page.goBack();`,
58
- ];
59
- return {
60
- code,
61
- captureSnapshot,
62
- waitForNetwork: false,
63
- };
48
+ response.setIncludeSnapshot();
49
+ response.addCode(`// Navigate back`);
50
+ response.addCode(`await page.goBack();`);
64
51
  },
65
52
  });
66
- const goForward = captureSnapshot => defineTool({
67
- capability: 'history',
53
+ const goForward = defineTabTool({
54
+ capability: 'core',
68
55
  schema: {
69
56
  name: 'browser_navigate_forward',
70
57
  title: 'Go forward',
@@ -72,22 +59,15 @@ const goForward = captureSnapshot => defineTool({
72
59
  inputSchema: z.object({}),
73
60
  type: 'readOnly',
74
61
  },
75
- handle: async (context) => {
76
- const tab = context.currentTabOrDie();
62
+ handle: async (tab, params, response) => {
77
63
  await tab.page.goForward();
78
- const code = [
79
- `// Navigate forward`,
80
- `await page.goForward();`,
81
- ];
82
- return {
83
- code,
84
- captureSnapshot,
85
- waitForNetwork: false,
86
- };
64
+ response.setIncludeSnapshot();
65
+ response.addCode(`// Navigate forward`);
66
+ response.addCode(`await page.goForward();`);
87
67
  },
88
68
  });
89
- export default (captureSnapshot) => [
90
- navigate(captureSnapshot),
91
- goBack(captureSnapshot),
92
- goForward(captureSnapshot),
69
+ export default [
70
+ navigate,
71
+ goBack,
72
+ goForward,
93
73
  ];
@@ -14,8 +14,8 @@
14
14
  * limitations under the License.
15
15
  */
16
16
  import { z } from 'zod';
17
- import { defineTool } from './tool.js';
18
- const requests = defineTool({
17
+ import { defineTabTool } from './tool.js';
18
+ const requests = defineTabTool({
19
19
  capability: 'core',
20
20
  schema: {
21
21
  name: 'browser_network_requests',
@@ -24,19 +24,9 @@ const requests = defineTool({
24
24
  inputSchema: z.object({}),
25
25
  type: 'readOnly',
26
26
  },
27
- handle: async (context) => {
28
- const requests = context.currentTabOrDie().requests();
29
- const log = [...requests.entries()].map(([request, response]) => renderRequest(request, response)).join('\n');
30
- return {
31
- code: [`// <internal code to list network requests>`],
32
- action: async () => {
33
- return {
34
- content: [{ type: 'text', text: log }]
35
- };
36
- },
37
- captureSnapshot: false,
38
- waitForNetwork: false,
39
- };
27
+ handle: async (tab, params, response) => {
28
+ const requests = tab.requests();
29
+ [...requests.entries()].forEach(([req, res]) => response.addResult(renderRequest(req, res)));
40
30
  },
41
31
  });
42
32
  function renderRequest(request, response) {
package/lib/tools/pdf.js CHANGED
@@ -14,13 +14,13 @@
14
14
  * limitations under the License.
15
15
  */
16
16
  import { z } from 'zod';
17
- import { defineTool } from './tool.js';
17
+ import { defineTabTool } from './tool.js';
18
18
  import * as javascript from '../javascript.js';
19
19
  import { outputFile } from '../config.js';
20
20
  const pdfSchema = z.object({
21
21
  filename: z.string().optional().describe('File name to save the pdf to. Defaults to `page-{timestamp}.pdf` if not specified.'),
22
22
  });
23
- const pdf = defineTool({
23
+ const pdf = defineTabTool({
24
24
  capability: 'pdf',
25
25
  schema: {
26
26
  name: 'browser_pdf_save',
@@ -29,19 +29,12 @@ const pdf = defineTool({
29
29
  inputSchema: pdfSchema,
30
30
  type: 'readOnly',
31
31
  },
32
- handle: async (context, params) => {
33
- const tab = context.currentTabOrDie();
34
- const fileName = await outputFile(context.config, params.filename ?? `page-${new Date().toISOString()}.pdf`);
35
- const code = [
36
- `// Save page as ${fileName}`,
37
- `await page.pdf(${javascript.formatObject({ path: fileName })});`,
38
- ];
39
- return {
40
- code,
41
- action: async () => tab.page.pdf({ path: fileName }).then(() => { }),
42
- captureSnapshot: false,
43
- waitForNetwork: false,
44
- };
32
+ handle: async (tab, params, response) => {
33
+ const fileName = await outputFile(tab.context.config, params.filename ?? `page-${new Date().toISOString()}.pdf`);
34
+ response.addCode(`// Save page as ${fileName}`);
35
+ response.addCode(`await page.pdf(${javascript.formatObject({ path: fileName })});`);
36
+ response.addResult(`Saved page as ${fileName}`);
37
+ await tab.page.pdf({ path: fileName });
45
38
  },
46
39
  });
47
40
  export default [
@@ -14,7 +14,7 @@
14
14
  * limitations under the License.
15
15
  */
16
16
  import { z } from 'zod';
17
- import { defineTool } from './tool.js';
17
+ import { defineTabTool } from './tool.js';
18
18
  import * as javascript from '../javascript.js';
19
19
  import { outputFile } from '../config.js';
20
20
  import { generateLocator } from './utils.js';
@@ -23,13 +23,19 @@ const screenshotSchema = z.object({
23
23
  filename: z.string().optional().describe('File name to save the screenshot to. Defaults to `page-{timestamp}.{png|jpeg}` if not specified.'),
24
24
  element: z.string().optional().describe('Human-readable element description used to obtain permission to screenshot the element. If not provided, the screenshot will be taken of viewport. If element is provided, ref must be provided too.'),
25
25
  ref: z.string().optional().describe('Exact target element reference from the page snapshot. If not provided, the screenshot will be taken of viewport. If ref is provided, element must be provided too.'),
26
+ fullPage: z.boolean().optional().describe('When true, takes a screenshot of the full scrollable page, instead of the currently visible viewport. Cannot be used with element screenshots.'),
26
27
  }).refine(data => {
27
28
  return !!data.element === !!data.ref;
28
29
  }, {
29
30
  message: 'Both element and ref must be provided or neither.',
30
31
  path: ['ref', 'element']
32
+ }).refine(data => {
33
+ return !(data.fullPage && (data.element || data.ref));
34
+ }, {
35
+ message: 'fullPage cannot be used with element screenshots.',
36
+ path: ['fullPage']
31
37
  });
32
- const screenshot = defineTool({
38
+ const screenshot = defineTabTool({
33
39
  capability: 'core',
34
40
  schema: {
35
41
  name: 'browser_take_screenshot',
@@ -38,38 +44,31 @@ const screenshot = defineTool({
38
44
  inputSchema: screenshotSchema,
39
45
  type: 'readOnly',
40
46
  },
41
- handle: async (context, params) => {
42
- const tab = context.currentTabOrDie();
43
- const snapshot = tab.snapshotOrDie();
47
+ handle: async (tab, params, response) => {
44
48
  const fileType = params.raw ? 'png' : 'jpeg';
45
- const fileName = await outputFile(context.config, params.filename ?? `page-${new Date().toISOString()}.${fileType}`);
46
- const options = { type: fileType, quality: fileType === 'png' ? undefined : 50, scale: 'css', path: fileName };
49
+ const fileName = await outputFile(tab.context.config, params.filename ?? `page-${new Date().toISOString()}.${fileType}`);
50
+ const options = {
51
+ type: fileType,
52
+ quality: fileType === 'png' ? undefined : 50,
53
+ scale: 'css',
54
+ path: fileName,
55
+ ...(params.fullPage !== undefined && { fullPage: params.fullPage })
56
+ };
47
57
  const isElementScreenshot = params.element && params.ref;
48
- const code = [
49
- `// Screenshot ${isElementScreenshot ? params.element : 'viewport'} and save it as ${fileName}`,
50
- ];
51
- const locator = params.ref ? snapshot.refLocator({ element: params.element || '', ref: params.ref }) : null;
58
+ const screenshotTarget = isElementScreenshot ? params.element : (params.fullPage ? 'full page' : 'viewport');
59
+ response.addCode(`// Screenshot ${screenshotTarget} and save it as ${fileName}`);
60
+ // Only get snapshot when element screenshot is needed
61
+ const locator = params.ref ? await tab.refLocator({ element: params.element || '', ref: params.ref }) : null;
52
62
  if (locator)
53
- code.push(`await page.${await generateLocator(locator)}.screenshot(${javascript.formatObject(options)});`);
63
+ response.addCode(`await page.${await generateLocator(locator)}.screenshot(${javascript.formatObject(options)});`);
54
64
  else
55
- code.push(`await page.screenshot(${javascript.formatObject(options)});`);
56
- const includeBase64 = context.clientSupportsImages();
57
- const action = async () => {
58
- const screenshot = locator ? await locator.screenshot(options) : await tab.page.screenshot(options);
59
- return {
60
- content: includeBase64 ? [{
61
- type: 'image',
62
- data: screenshot.toString('base64'),
63
- mimeType: fileType === 'png' ? 'image/png' : 'image/jpeg',
64
- }] : []
65
- };
66
- };
67
- return {
68
- code,
69
- action,
70
- captureSnapshot: true,
71
- waitForNetwork: false,
72
- };
65
+ response.addCode(`await page.screenshot(${javascript.formatObject(options)});`);
66
+ const buffer = locator ? await locator.screenshot(options) : await tab.page.screenshot(options);
67
+ response.addResult(`Took the ${screenshotTarget} screenshot and saved it as ${fileName}`);
68
+ response.addImage({
69
+ contentType: fileType === 'png' ? 'image/png' : 'image/jpeg',
70
+ data: buffer
71
+ });
73
72
  }
74
73
  });
75
74
  export default [
@@ -14,7 +14,7 @@
14
14
  * limitations under the License.
15
15
  */
16
16
  import { z } from 'zod';
17
- import { defineTool } from './tool.js';
17
+ import { defineTabTool, defineTool } from './tool.js';
18
18
  import * as javascript from '../javascript.js';
19
19
  import { generateLocator } from './utils.js';
20
20
  const snapshot = defineTool({
@@ -26,23 +26,20 @@ const snapshot = defineTool({
26
26
  inputSchema: z.object({}),
27
27
  type: 'readOnly',
28
28
  },
29
- handle: async (context) => {
29
+ handle: async (context, params, response) => {
30
30
  await context.ensureTab();
31
- return {
32
- code: [`// <internal code to capture accessibility snapshot>`],
33
- captureSnapshot: true,
34
- waitForNetwork: false,
35
- };
31
+ response.setIncludeSnapshot();
36
32
  },
37
33
  });
38
- const elementSchema = z.object({
34
+ export const elementSchema = z.object({
39
35
  element: z.string().describe('Human-readable element description used to obtain permission to interact with the element'),
40
36
  ref: z.string().describe('Exact target element reference from the page snapshot'),
41
37
  });
42
38
  const clickSchema = elementSchema.extend({
43
39
  doubleClick: z.boolean().optional().describe('Whether to perform a double click instead of a single click'),
40
+ button: z.enum(['left', 'right', 'middle']).optional().describe('Button to click, defaults to left'),
44
41
  });
45
- const click = defineTool({
42
+ const click = defineTabTool({
46
43
  capability: 'core',
47
44
  schema: {
48
45
  name: 'browser_click',
@@ -51,27 +48,28 @@ const click = defineTool({
51
48
  inputSchema: clickSchema,
52
49
  type: 'destructive',
53
50
  },
54
- handle: async (context, params) => {
55
- const tab = context.currentTabOrDie();
56
- const locator = tab.snapshotOrDie().refLocator(params);
57
- const code = [];
51
+ handle: async (tab, params, response) => {
52
+ response.setIncludeSnapshot();
53
+ const locator = await tab.refLocator(params);
54
+ const button = params.button;
55
+ const buttonAttr = button ? `{ button: '${button}' }` : '';
58
56
  if (params.doubleClick) {
59
- code.push(`// Double click ${params.element}`);
60
- code.push(`await page.${await generateLocator(locator)}.dblclick();`);
57
+ response.addCode(`// Double click ${params.element}`);
58
+ response.addCode(`await page.${await generateLocator(locator)}.dblclick(${buttonAttr});`);
61
59
  }
62
60
  else {
63
- code.push(`// Click ${params.element}`);
64
- code.push(`await page.${await generateLocator(locator)}.click();`);
61
+ response.addCode(`// Click ${params.element}`);
62
+ response.addCode(`await page.${await generateLocator(locator)}.click(${buttonAttr});`);
65
63
  }
66
- return {
67
- code,
68
- action: () => params.doubleClick ? locator.dblclick() : locator.click(),
69
- captureSnapshot: true,
70
- waitForNetwork: true,
71
- };
64
+ await tab.waitForCompletion(async () => {
65
+ if (params.doubleClick)
66
+ await locator.dblclick({ button });
67
+ else
68
+ await locator.click({ button });
69
+ });
72
70
  },
73
71
  });
74
- const drag = defineTool({
72
+ const drag = defineTabTool({
75
73
  capability: 'core',
76
74
  schema: {
77
75
  name: 'browser_drag',
@@ -85,23 +83,19 @@ const drag = defineTool({
85
83
  }),
86
84
  type: 'destructive',
87
85
  },
88
- handle: async (context, params) => {
89
- const snapshot = context.currentTabOrDie().snapshotOrDie();
90
- const startLocator = snapshot.refLocator({ ref: params.startRef, element: params.startElement });
91
- const endLocator = snapshot.refLocator({ ref: params.endRef, element: params.endElement });
92
- const code = [
93
- `// Drag ${params.startElement} to ${params.endElement}`,
94
- `await page.${await generateLocator(startLocator)}.dragTo(page.${await generateLocator(endLocator)});`
95
- ];
96
- return {
97
- code,
98
- action: () => startLocator.dragTo(endLocator),
99
- captureSnapshot: true,
100
- waitForNetwork: true,
101
- };
86
+ handle: async (tab, params, response) => {
87
+ response.setIncludeSnapshot();
88
+ const [startLocator, endLocator] = await tab.refLocators([
89
+ { ref: params.startRef, element: params.startElement },
90
+ { ref: params.endRef, element: params.endElement },
91
+ ]);
92
+ await tab.waitForCompletion(async () => {
93
+ await startLocator.dragTo(endLocator);
94
+ });
95
+ response.addCode(`await page.${await generateLocator(startLocator)}.dragTo(page.${await generateLocator(endLocator)});`);
102
96
  },
103
97
  });
104
- const hover = defineTool({
98
+ const hover = defineTabTool({
105
99
  capability: 'core',
106
100
  schema: {
107
101
  name: 'browser_hover',
@@ -110,67 +104,19 @@ const hover = defineTool({
110
104
  inputSchema: elementSchema,
111
105
  type: 'readOnly',
112
106
  },
113
- handle: async (context, params) => {
114
- const snapshot = context.currentTabOrDie().snapshotOrDie();
115
- const locator = snapshot.refLocator(params);
116
- const code = [
117
- `// Hover over ${params.element}`,
118
- `await page.${await generateLocator(locator)}.hover();`
119
- ];
120
- return {
121
- code,
122
- action: () => locator.hover(),
123
- captureSnapshot: true,
124
- waitForNetwork: true,
125
- };
126
- },
127
- });
128
- const typeSchema = elementSchema.extend({
129
- text: z.string().describe('Text to type into the element'),
130
- submit: z.boolean().optional().describe('Whether to submit entered text (press Enter after)'),
131
- slowly: z.boolean().optional().describe('Whether to type one character at a time. Useful for triggering key handlers in the page. By default entire text is filled in at once.'),
132
- });
133
- const type = defineTool({
134
- capability: 'core',
135
- schema: {
136
- name: 'browser_type',
137
- title: 'Type text',
138
- description: 'Type text into editable element',
139
- inputSchema: typeSchema,
140
- type: 'destructive',
141
- },
142
- handle: async (context, params) => {
143
- const snapshot = context.currentTabOrDie().snapshotOrDie();
144
- const locator = snapshot.refLocator(params);
145
- const code = [];
146
- const steps = [];
147
- if (params.slowly) {
148
- code.push(`// Press "${params.text}" sequentially into "${params.element}"`);
149
- code.push(`await page.${await generateLocator(locator)}.pressSequentially(${javascript.quote(params.text)});`);
150
- steps.push(() => locator.pressSequentially(params.text));
151
- }
152
- else {
153
- code.push(`// Fill "${params.text}" into "${params.element}"`);
154
- code.push(`await page.${await generateLocator(locator)}.fill(${javascript.quote(params.text)});`);
155
- steps.push(() => locator.fill(params.text));
156
- }
157
- if (params.submit) {
158
- code.push(`// Submit text`);
159
- code.push(`await page.${await generateLocator(locator)}.press('Enter');`);
160
- steps.push(() => locator.press('Enter'));
161
- }
162
- return {
163
- code,
164
- action: () => steps.reduce((acc, step) => acc.then(step), Promise.resolve()),
165
- captureSnapshot: true,
166
- waitForNetwork: true,
167
- };
107
+ handle: async (tab, params, response) => {
108
+ response.setIncludeSnapshot();
109
+ const locator = await tab.refLocator(params);
110
+ response.addCode(`await page.${await generateLocator(locator)}.hover();`);
111
+ await tab.waitForCompletion(async () => {
112
+ await locator.hover();
113
+ });
168
114
  },
169
115
  });
170
116
  const selectOptionSchema = elementSchema.extend({
171
117
  values: z.array(z.string()).describe('Array of values to select in the dropdown. This can be a single value or multiple values.'),
172
118
  });
173
- const selectOption = defineTool({
119
+ const selectOption = defineTabTool({
174
120
  capability: 'core',
175
121
  schema: {
176
122
  name: 'browser_select_option',
@@ -179,19 +125,14 @@ const selectOption = defineTool({
179
125
  inputSchema: selectOptionSchema,
180
126
  type: 'destructive',
181
127
  },
182
- handle: async (context, params) => {
183
- const snapshot = context.currentTabOrDie().snapshotOrDie();
184
- const locator = snapshot.refLocator(params);
185
- const code = [
186
- `// Select options [${params.values.join(', ')}] in ${params.element}`,
187
- `await page.${await generateLocator(locator)}.selectOption(${javascript.formatObject(params.values)});`
188
- ];
189
- return {
190
- code,
191
- action: () => locator.selectOption(params.values).then(() => { }),
192
- captureSnapshot: true,
193
- waitForNetwork: true,
194
- };
128
+ handle: async (tab, params, response) => {
129
+ response.setIncludeSnapshot();
130
+ const locator = await tab.refLocator(params);
131
+ response.addCode(`// Select options [${params.values.join(', ')}] in ${params.element}`);
132
+ response.addCode(`await page.${await generateLocator(locator)}.selectOption(${javascript.formatObject(params.values)});`);
133
+ await tab.waitForCompletion(async () => {
134
+ await locator.selectOption(params.values);
135
+ });
195
136
  },
196
137
  });
197
138
  export default [
@@ -199,6 +140,5 @@ export default [
199
140
  click,
200
141
  drag,
201
142
  hover,
202
- type,
203
143
  selectOption,
204
144
  ];