@playwright/mcp 0.0.36 → 0.0.37-alpha-2025-09-08

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 (61) hide show
  1. package/README.md +82 -47
  2. package/cli.js +7 -1
  3. package/config.d.ts +24 -0
  4. package/index.d.ts +1 -1
  5. package/index.js +2 -2
  6. package/package.json +14 -40
  7. package/lib/browserContextFactory.js +0 -211
  8. package/lib/browserServerBackend.js +0 -77
  9. package/lib/config.js +0 -246
  10. package/lib/context.js +0 -226
  11. package/lib/extension/cdpRelay.js +0 -358
  12. package/lib/extension/extensionContextFactory.js +0 -56
  13. package/lib/extension/protocol.js +0 -18
  14. package/lib/index.js +0 -40
  15. package/lib/loop/loop.js +0 -69
  16. package/lib/loop/loopClaude.js +0 -152
  17. package/lib/loop/loopOpenAI.js +0 -141
  18. package/lib/loop/main.js +0 -60
  19. package/lib/loopTools/context.js +0 -67
  20. package/lib/loopTools/main.js +0 -54
  21. package/lib/loopTools/perform.js +0 -32
  22. package/lib/loopTools/snapshot.js +0 -29
  23. package/lib/loopTools/tool.js +0 -18
  24. package/lib/mcp/http.js +0 -135
  25. package/lib/mcp/inProcessTransport.js +0 -72
  26. package/lib/mcp/manualPromise.js +0 -111
  27. package/lib/mcp/mdb.js +0 -198
  28. package/lib/mcp/proxyBackend.js +0 -104
  29. package/lib/mcp/server.js +0 -123
  30. package/lib/mcp/tool.js +0 -32
  31. package/lib/program.js +0 -132
  32. package/lib/response.js +0 -165
  33. package/lib/sessionLog.js +0 -121
  34. package/lib/tab.js +0 -249
  35. package/lib/tools/common.js +0 -55
  36. package/lib/tools/console.js +0 -33
  37. package/lib/tools/dialogs.js +0 -47
  38. package/lib/tools/evaluate.js +0 -53
  39. package/lib/tools/files.js +0 -44
  40. package/lib/tools/form.js +0 -57
  41. package/lib/tools/install.js +0 -53
  42. package/lib/tools/keyboard.js +0 -78
  43. package/lib/tools/mouse.js +0 -99
  44. package/lib/tools/navigate.js +0 -54
  45. package/lib/tools/network.js +0 -41
  46. package/lib/tools/pdf.js +0 -40
  47. package/lib/tools/screenshot.js +0 -79
  48. package/lib/tools/snapshot.js +0 -139
  49. package/lib/tools/tabs.js +0 -59
  50. package/lib/tools/tool.js +0 -33
  51. package/lib/tools/utils.js +0 -74
  52. package/lib/tools/verify.js +0 -137
  53. package/lib/tools/wait.js +0 -55
  54. package/lib/tools.js +0 -54
  55. package/lib/utils/codegen.js +0 -49
  56. package/lib/utils/fileUtils.js +0 -36
  57. package/lib/utils/guid.js +0 -22
  58. package/lib/utils/log.js +0 -21
  59. package/lib/utils/package.js +0 -20
  60. package/lib/vscode/host.js +0 -128
  61. package/lib/vscode/main.js +0 -62
@@ -1,99 +0,0 @@
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
- ];
@@ -1,54 +0,0 @@
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 { defineTool, defineTabTool } from './tool.js';
18
- const navigate = defineTool({
19
- capability: 'core',
20
- schema: {
21
- name: 'browser_navigate',
22
- title: 'Navigate to a URL',
23
- description: 'Navigate to a URL',
24
- inputSchema: z.object({
25
- url: z.string().describe('The URL to navigate to'),
26
- }),
27
- type: 'destructive',
28
- },
29
- handle: async (context, params, response) => {
30
- const tab = await context.ensureTab();
31
- await tab.navigate(params.url);
32
- response.setIncludeSnapshot();
33
- response.addCode(`await page.goto('${params.url}');`);
34
- },
35
- });
36
- const goBack = defineTabTool({
37
- capability: 'core',
38
- schema: {
39
- name: 'browser_navigate_back',
40
- title: 'Go back',
41
- description: 'Go back to the previous page',
42
- inputSchema: z.object({}),
43
- type: 'readOnly',
44
- },
45
- handle: async (tab, params, response) => {
46
- await tab.page.goBack();
47
- response.setIncludeSnapshot();
48
- response.addCode(`await page.goBack();`);
49
- },
50
- });
51
- export default [
52
- navigate,
53
- goBack,
54
- ];
@@ -1,41 +0,0 @@
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 requests = defineTabTool({
19
- capability: 'core',
20
- schema: {
21
- name: 'browser_network_requests',
22
- title: 'List network requests',
23
- description: 'Returns all network requests since loading the page',
24
- inputSchema: z.object({}),
25
- type: 'readOnly',
26
- },
27
- handle: async (tab, params, response) => {
28
- const requests = tab.requests();
29
- [...requests.entries()].forEach(([req, res]) => response.addResult(renderRequest(req, res)));
30
- },
31
- });
32
- function renderRequest(request, response) {
33
- const result = [];
34
- result.push(`[${request.method().toUpperCase()}] ${request.url()}`);
35
- if (response)
36
- result.push(`=> [${response.status()}] ${response.statusText()}`);
37
- return result.join(' ');
38
- }
39
- export default [
40
- requests,
41
- ];
package/lib/tools/pdf.js DELETED
@@ -1,40 +0,0 @@
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
- import * as javascript from '../utils/codegen.js';
19
- const pdfSchema = z.object({
20
- filename: z.string().optional().describe('File name to save the pdf to. Defaults to `page-{timestamp}.pdf` if not specified.'),
21
- });
22
- const pdf = defineTabTool({
23
- capability: 'pdf',
24
- schema: {
25
- name: 'browser_pdf_save',
26
- title: 'Save as PDF',
27
- description: 'Save page as PDF',
28
- inputSchema: pdfSchema,
29
- type: 'readOnly',
30
- },
31
- handle: async (tab, params, response) => {
32
- const fileName = await tab.context.outputFile(params.filename ?? `page-${new Date().toISOString()}.pdf`);
33
- response.addCode(`await page.pdf(${javascript.formatObject({ path: fileName })});`);
34
- response.addResult(`Saved page as ${fileName}`);
35
- await tab.page.pdf({ path: fileName });
36
- },
37
- });
38
- export default [
39
- pdf,
40
- ];
@@ -1,79 +0,0 @@
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
- import * as javascript from '../utils/codegen.js';
19
- import { generateLocator } from './utils.js';
20
- const screenshotSchema = z.object({
21
- type: z.enum(['png', 'jpeg']).default('png').describe('Image format for the screenshot. Default is png.'),
22
- filename: z.string().optional().describe('File name to save the screenshot to. Defaults to `page-{timestamp}.{png|jpeg}` if not specified.'),
23
- 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.'),
24
- 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.'),
25
- 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
- }).refine(data => {
27
- return !!data.element === !!data.ref;
28
- }, {
29
- message: 'Both element and ref must be provided or neither.',
30
- path: ['ref', 'element']
31
- }).refine(data => {
32
- return !(data.fullPage && (data.element || data.ref));
33
- }, {
34
- message: 'fullPage cannot be used with element screenshots.',
35
- path: ['fullPage']
36
- });
37
- const screenshot = defineTabTool({
38
- capability: 'core',
39
- schema: {
40
- name: 'browser_take_screenshot',
41
- title: 'Take a screenshot',
42
- description: `Take a screenshot of the current page. You can't perform actions based on the screenshot, use browser_snapshot for actions.`,
43
- inputSchema: screenshotSchema,
44
- type: 'readOnly',
45
- },
46
- handle: async (tab, params, response) => {
47
- const fileType = params.type || 'png';
48
- const fileName = await tab.context.outputFile(params.filename ?? `page-${new Date().toISOString()}.${fileType}`);
49
- const options = {
50
- type: fileType,
51
- quality: fileType === 'png' ? undefined : 90,
52
- scale: 'css',
53
- path: fileName,
54
- ...(params.fullPage !== undefined && { fullPage: params.fullPage })
55
- };
56
- const isElementScreenshot = params.element && params.ref;
57
- const screenshotTarget = isElementScreenshot ? params.element : (params.fullPage ? 'full page' : 'viewport');
58
- response.addCode(`// Screenshot ${screenshotTarget} and save it as ${fileName}`);
59
- // Only get snapshot when element screenshot is needed
60
- const locator = params.ref ? await tab.refLocator({ element: params.element || '', ref: params.ref }) : null;
61
- if (locator)
62
- response.addCode(`await page.${await generateLocator(locator)}.screenshot(${javascript.formatObject(options)});`);
63
- else
64
- response.addCode(`await page.screenshot(${javascript.formatObject(options)});`);
65
- const buffer = locator ? await locator.screenshot(options) : await tab.page.screenshot(options);
66
- response.addResult(`Took the ${screenshotTarget} screenshot and saved it as ${fileName}`);
67
- // https://github.com/microsoft/playwright-mcp/issues/817
68
- // Never return large images to LLM, saving them to the file system is enough.
69
- if (!params.fullPage) {
70
- response.addImage({
71
- contentType: fileType === 'png' ? 'image/png' : 'image/jpeg',
72
- data: buffer
73
- });
74
- }
75
- }
76
- });
77
- export default [
78
- screenshot,
79
- ];
@@ -1,139 +0,0 @@
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, defineTool } from './tool.js';
18
- import * as javascript from '../utils/codegen.js';
19
- import { generateLocator } from './utils.js';
20
- const snapshot = defineTool({
21
- capability: 'core',
22
- schema: {
23
- name: 'browser_snapshot',
24
- title: 'Page snapshot',
25
- description: 'Capture accessibility snapshot of the current page, this is better than screenshot',
26
- inputSchema: z.object({}),
27
- type: 'readOnly',
28
- },
29
- handle: async (context, params, response) => {
30
- await context.ensureTab();
31
- response.setIncludeSnapshot();
32
- },
33
- });
34
- export const elementSchema = z.object({
35
- element: z.string().describe('Human-readable element description used to obtain permission to interact with the element'),
36
- ref: z.string().describe('Exact target element reference from the page snapshot'),
37
- });
38
- const clickSchema = elementSchema.extend({
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'),
41
- });
42
- const click = defineTabTool({
43
- capability: 'core',
44
- schema: {
45
- name: 'browser_click',
46
- title: 'Click',
47
- description: 'Perform click on a web page',
48
- inputSchema: clickSchema,
49
- type: 'destructive',
50
- },
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}' }` : '';
56
- if (params.doubleClick)
57
- response.addCode(`await page.${await generateLocator(locator)}.dblclick(${buttonAttr});`);
58
- else
59
- response.addCode(`await page.${await generateLocator(locator)}.click(${buttonAttr});`);
60
- await tab.waitForCompletion(async () => {
61
- if (params.doubleClick)
62
- await locator.dblclick({ button });
63
- else
64
- await locator.click({ button });
65
- });
66
- },
67
- });
68
- const drag = defineTabTool({
69
- capability: 'core',
70
- schema: {
71
- name: 'browser_drag',
72
- title: 'Drag mouse',
73
- description: 'Perform drag and drop between two elements',
74
- inputSchema: z.object({
75
- startElement: z.string().describe('Human-readable source element description used to obtain the permission to interact with the element'),
76
- startRef: z.string().describe('Exact source element reference from the page snapshot'),
77
- endElement: z.string().describe('Human-readable target element description used to obtain the permission to interact with the element'),
78
- endRef: z.string().describe('Exact target element reference from the page snapshot'),
79
- }),
80
- type: 'destructive',
81
- },
82
- handle: async (tab, params, response) => {
83
- response.setIncludeSnapshot();
84
- const [startLocator, endLocator] = await tab.refLocators([
85
- { ref: params.startRef, element: params.startElement },
86
- { ref: params.endRef, element: params.endElement },
87
- ]);
88
- await tab.waitForCompletion(async () => {
89
- await startLocator.dragTo(endLocator);
90
- });
91
- response.addCode(`await page.${await generateLocator(startLocator)}.dragTo(page.${await generateLocator(endLocator)});`);
92
- },
93
- });
94
- const hover = defineTabTool({
95
- capability: 'core',
96
- schema: {
97
- name: 'browser_hover',
98
- title: 'Hover mouse',
99
- description: 'Hover over element on page',
100
- inputSchema: elementSchema,
101
- type: 'readOnly',
102
- },
103
- handle: async (tab, params, response) => {
104
- response.setIncludeSnapshot();
105
- const locator = await tab.refLocator(params);
106
- response.addCode(`await page.${await generateLocator(locator)}.hover();`);
107
- await tab.waitForCompletion(async () => {
108
- await locator.hover();
109
- });
110
- },
111
- });
112
- const selectOptionSchema = elementSchema.extend({
113
- values: z.array(z.string()).describe('Array of values to select in the dropdown. This can be a single value or multiple values.'),
114
- });
115
- const selectOption = defineTabTool({
116
- capability: 'core',
117
- schema: {
118
- name: 'browser_select_option',
119
- title: 'Select option',
120
- description: 'Select an option in a dropdown',
121
- inputSchema: selectOptionSchema,
122
- type: 'destructive',
123
- },
124
- handle: async (tab, params, response) => {
125
- response.setIncludeSnapshot();
126
- const locator = await tab.refLocator(params);
127
- response.addCode(`await page.${await generateLocator(locator)}.selectOption(${javascript.formatObject(params.values)});`);
128
- await tab.waitForCompletion(async () => {
129
- await locator.selectOption(params.values);
130
- });
131
- },
132
- });
133
- export default [
134
- snapshot,
135
- click,
136
- drag,
137
- hover,
138
- selectOption,
139
- ];
package/lib/tools/tabs.js DELETED
@@ -1,59 +0,0 @@
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 { defineTool } from './tool.js';
18
- const browserTabs = defineTool({
19
- capability: 'core-tabs',
20
- schema: {
21
- name: 'browser_tabs',
22
- title: 'Manage tabs',
23
- description: 'List, create, close, or select a browser tab.',
24
- inputSchema: z.object({
25
- action: z.enum(['list', 'new', 'close', 'select']).describe('Operation to perform'),
26
- index: z.number().optional().describe('Tab index, used for close/select. If omitted for close, current tab is closed.'),
27
- }),
28
- type: 'destructive',
29
- },
30
- handle: async (context, params, response) => {
31
- switch (params.action) {
32
- case 'list': {
33
- await context.ensureTab();
34
- response.setIncludeTabs();
35
- return;
36
- }
37
- case 'new': {
38
- await context.newTab();
39
- response.setIncludeTabs();
40
- return;
41
- }
42
- case 'close': {
43
- await context.closeTab(params.index);
44
- response.setIncludeSnapshot();
45
- return;
46
- }
47
- case 'select': {
48
- if (params.index === undefined)
49
- throw new Error('Tab index is required');
50
- await context.selectTab(params.index);
51
- response.setIncludeSnapshot();
52
- return;
53
- }
54
- }
55
- },
56
- });
57
- export default [
58
- browserTabs,
59
- ];
package/lib/tools/tool.js DELETED
@@ -1,33 +0,0 @@
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
- export function defineTool(tool) {
17
- return tool;
18
- }
19
- export function defineTabTool(tool) {
20
- return {
21
- ...tool,
22
- handle: async (context, params, response) => {
23
- const tab = context.currentTabOrDie();
24
- const modalStates = tab.modalStates().map(state => state.type);
25
- if (tool.clearsModalState && !modalStates.includes(tool.clearsModalState))
26
- response.addError(`Error: The tool "${tool.schema.name}" can only be used when there is related modal state present.\n` + tab.modalStatesMarkdown().join('\n'));
27
- else if (!tool.clearsModalState && modalStates.length)
28
- response.addError(`Error: Tool "${tool.schema.name}" does not handle the modal state.\n` + tab.modalStatesMarkdown().join('\n'));
29
- else
30
- return tool.handle(tab, params, response);
31
- },
32
- };
33
- }
@@ -1,74 +0,0 @@
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
- // @ts-ignore
17
- import { asLocator } from 'playwright-core/lib/utils';
18
- export async function waitForCompletion(tab, callback) {
19
- const requests = new Set();
20
- let frameNavigated = false;
21
- let waitCallback = () => { };
22
- const waitBarrier = new Promise(f => { waitCallback = f; });
23
- const requestListener = (request) => requests.add(request);
24
- const requestFinishedListener = (request) => {
25
- requests.delete(request);
26
- if (!requests.size)
27
- waitCallback();
28
- };
29
- const frameNavigateListener = (frame) => {
30
- if (frame.parentFrame())
31
- return;
32
- frameNavigated = true;
33
- dispose();
34
- clearTimeout(timeout);
35
- void tab.waitForLoadState('load').then(waitCallback);
36
- };
37
- const onTimeout = () => {
38
- dispose();
39
- waitCallback();
40
- };
41
- tab.page.on('request', requestListener);
42
- tab.page.on('requestfinished', requestFinishedListener);
43
- tab.page.on('framenavigated', frameNavigateListener);
44
- const timeout = setTimeout(onTimeout, 10000);
45
- const dispose = () => {
46
- tab.page.off('request', requestListener);
47
- tab.page.off('requestfinished', requestFinishedListener);
48
- tab.page.off('framenavigated', frameNavigateListener);
49
- clearTimeout(timeout);
50
- };
51
- try {
52
- const result = await callback();
53
- if (!requests.size && !frameNavigated)
54
- waitCallback();
55
- await waitBarrier;
56
- await tab.waitForTimeout(1000);
57
- return result;
58
- }
59
- finally {
60
- dispose();
61
- }
62
- }
63
- export async function generateLocator(locator) {
64
- try {
65
- const { resolvedSelector } = await locator._resolveSelector();
66
- return asLocator('javascript', resolvedSelector);
67
- }
68
- catch (e) {
69
- throw new Error('Ref not found, likely because element was removed. Use browser_snapshot to see what elements are currently on the page.');
70
- }
71
- }
72
- export async function callOnPageNoTrace(page, callback) {
73
- return await page._wrapApiCall(() => callback(page), { internal: true });
74
- }