@playwright/mcp 0.0.9 → 0.0.13

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.
@@ -0,0 +1,48 @@
1
+ "use strict";
2
+ /**
3
+ * Copyright (c) Microsoft Corporation.
4
+ *
5
+ * Licensed under the Apache License, Version 2.0 (the "License");
6
+ * you may not use this file except in compliance with the License.
7
+ * You may obtain a copy of the License at
8
+ *
9
+ * http://www.apache.org/licenses/LICENSE-2.0
10
+ *
11
+ * Unless required by applicable law or agreed to in writing, software
12
+ * distributed under the License is distributed on an "AS IS" BASIS,
13
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ * See the License for the specific language governing permissions and
15
+ * limitations under the License.
16
+ */
17
+ var __importDefault = (this && this.__importDefault) || function (mod) {
18
+ return (mod && mod.__esModule) ? mod : { "default": mod };
19
+ };
20
+ Object.defineProperty(exports, "__esModule", { value: true });
21
+ const os_1 = __importDefault(require("os"));
22
+ const path_1 = __importDefault(require("path"));
23
+ const zod_1 = require("zod");
24
+ const zod_to_json_schema_1 = require("zod-to-json-schema");
25
+ const utils_1 = require("./utils");
26
+ const pdfSchema = zod_1.z.object({});
27
+ const pdf = {
28
+ capability: 'pdf',
29
+ schema: {
30
+ name: 'browser_pdf_save',
31
+ description: 'Save page as PDF',
32
+ inputSchema: (0, zod_to_json_schema_1.zodToJsonSchema)(pdfSchema),
33
+ },
34
+ handle: async (context) => {
35
+ const tab = context.currentTab();
36
+ const fileName = path_1.default.join(os_1.default.tmpdir(), (0, utils_1.sanitizeForFilePath)(`page-${new Date().toISOString()}`)) + '.pdf';
37
+ await tab.page.pdf({ path: fileName });
38
+ return {
39
+ content: [{
40
+ type: 'text',
41
+ text: `Saved as ${fileName}`,
42
+ }],
43
+ };
44
+ },
45
+ };
46
+ exports.default = [
47
+ pdf,
48
+ ];
@@ -15,19 +15,18 @@
15
15
  * limitations under the License.
16
16
  */
17
17
  Object.defineProperty(exports, "__esModule", { value: true });
18
- exports.type = exports.drag = exports.click = exports.moveMouse = exports.screenshot = void 0;
19
18
  const zod_1 = require("zod");
20
19
  const zod_to_json_schema_1 = require("zod-to-json-schema");
21
- const utils_1 = require("./utils");
22
- exports.screenshot = {
20
+ const screenshot = {
21
+ capability: 'core',
23
22
  schema: {
24
- name: 'browser_screenshot',
23
+ name: 'browser_screen_capture',
25
24
  description: 'Take a screenshot of the current page',
26
25
  inputSchema: (0, zod_to_json_schema_1.zodToJsonSchema)(zod_1.z.object({})),
27
26
  },
28
27
  handle: async (context) => {
29
- const page = context.existingPage();
30
- const screenshot = await page.screenshot({ type: 'jpeg', quality: 50, scale: 'css' });
28
+ const tab = await context.ensureTab();
29
+ const screenshot = await tab.page.screenshot({ type: 'jpeg', quality: 50, scale: 'css' });
31
30
  return {
32
31
  content: [{ type: 'image', data: screenshot.toString('base64'), mimeType: 'image/jpeg' }],
33
32
  };
@@ -40,16 +39,17 @@ const moveMouseSchema = elementSchema.extend({
40
39
  x: zod_1.z.number().describe('X coordinate'),
41
40
  y: zod_1.z.number().describe('Y coordinate'),
42
41
  });
43
- exports.moveMouse = {
42
+ const moveMouse = {
43
+ capability: 'core',
44
44
  schema: {
45
- name: 'browser_move_mouse',
45
+ name: 'browser_screen_move_mouse',
46
46
  description: 'Move mouse to a given position',
47
47
  inputSchema: (0, zod_to_json_schema_1.zodToJsonSchema)(moveMouseSchema),
48
48
  },
49
49
  handle: async (context, params) => {
50
50
  const validatedParams = moveMouseSchema.parse(params);
51
- const page = context.existingPage();
52
- await page.mouse.move(validatedParams.x, validatedParams.y);
51
+ const tab = context.currentTab();
52
+ await tab.page.mouse.move(validatedParams.x, validatedParams.y);
53
53
  return {
54
54
  content: [{ type: 'text', text: `Moved mouse to (${validatedParams.x}, ${validatedParams.y})` }],
55
55
  };
@@ -59,18 +59,26 @@ const clickSchema = elementSchema.extend({
59
59
  x: zod_1.z.number().describe('X coordinate'),
60
60
  y: zod_1.z.number().describe('Y coordinate'),
61
61
  });
62
- exports.click = {
62
+ const click = {
63
+ capability: 'core',
63
64
  schema: {
64
- name: 'browser_click',
65
+ name: 'browser_screen_click',
65
66
  description: 'Click left mouse button',
66
67
  inputSchema: (0, zod_to_json_schema_1.zodToJsonSchema)(clickSchema),
67
68
  },
68
69
  handle: async (context, params) => {
69
- return await (0, utils_1.runAndWait)(context, 'Clicked mouse', async (page) => {
70
+ return await context.currentTab().runAndWait(async (tab) => {
70
71
  const validatedParams = clickSchema.parse(params);
71
- await page.mouse.move(validatedParams.x, validatedParams.y);
72
- await page.mouse.down();
73
- await page.mouse.up();
72
+ const code = [
73
+ `// Click mouse at coordinates (${validatedParams.x}, ${validatedParams.y})`,
74
+ `await page.mouse.move(${validatedParams.x}, ${validatedParams.y});`,
75
+ `await page.mouse.down();`,
76
+ `await page.mouse.up();`,
77
+ ];
78
+ await tab.page.mouse.move(validatedParams.x, validatedParams.y);
79
+ await tab.page.mouse.down();
80
+ await tab.page.mouse.up();
81
+ return { code };
74
82
  });
75
83
  },
76
84
  };
@@ -80,38 +88,63 @@ const dragSchema = elementSchema.extend({
80
88
  endX: zod_1.z.number().describe('End X coordinate'),
81
89
  endY: zod_1.z.number().describe('End Y coordinate'),
82
90
  });
83
- exports.drag = {
91
+ const drag = {
92
+ capability: 'core',
84
93
  schema: {
85
- name: 'browser_drag',
94
+ name: 'browser_screen_drag',
86
95
  description: 'Drag left mouse button',
87
96
  inputSchema: (0, zod_to_json_schema_1.zodToJsonSchema)(dragSchema),
88
97
  },
89
98
  handle: async (context, params) => {
90
99
  const validatedParams = dragSchema.parse(params);
91
- return await (0, utils_1.runAndWait)(context, `Dragged mouse from (${validatedParams.startX}, ${validatedParams.startY}) to (${validatedParams.endX}, ${validatedParams.endY})`, async (page) => {
92
- await page.mouse.move(validatedParams.startX, validatedParams.startY);
93
- await page.mouse.down();
94
- await page.mouse.move(validatedParams.endX, validatedParams.endY);
95
- await page.mouse.up();
100
+ return await context.currentTab().runAndWait(async (tab) => {
101
+ await tab.page.mouse.move(validatedParams.startX, validatedParams.startY);
102
+ await tab.page.mouse.down();
103
+ await tab.page.mouse.move(validatedParams.endX, validatedParams.endY);
104
+ await tab.page.mouse.up();
105
+ const code = [
106
+ `// Drag mouse from (${validatedParams.startX}, ${validatedParams.startY}) to (${validatedParams.endX}, ${validatedParams.endY})`,
107
+ `await page.mouse.move(${validatedParams.startX}, ${validatedParams.startY});`,
108
+ `await page.mouse.down();`,
109
+ `await page.mouse.move(${validatedParams.endX}, ${validatedParams.endY});`,
110
+ `await page.mouse.up();`,
111
+ ];
112
+ return { code };
96
113
  });
97
114
  },
98
115
  };
99
116
  const typeSchema = zod_1.z.object({
100
117
  text: zod_1.z.string().describe('Text to type into the element'),
101
- submit: zod_1.z.boolean().describe('Whether to submit entered text (press Enter after)'),
118
+ submit: zod_1.z.boolean().optional().describe('Whether to submit entered text (press Enter after)'),
102
119
  });
103
- exports.type = {
120
+ const type = {
121
+ capability: 'core',
104
122
  schema: {
105
- name: 'browser_type',
123
+ name: 'browser_screen_type',
106
124
  description: 'Type text',
107
125
  inputSchema: (0, zod_to_json_schema_1.zodToJsonSchema)(typeSchema),
108
126
  },
109
127
  handle: async (context, params) => {
110
128
  const validatedParams = typeSchema.parse(params);
111
- return await (0, utils_1.runAndWait)(context, `Typed text "${validatedParams.text}"`, async (page) => {
112
- await page.keyboard.type(validatedParams.text);
113
- if (validatedParams.submit)
114
- await page.keyboard.press('Enter');
129
+ return await context.currentTab().runAndWait(async (tab) => {
130
+ const code = [
131
+ `// Type ${validatedParams.text}`,
132
+ `await page.keyboard.type('${validatedParams.text}');`,
133
+ ];
134
+ await tab.page.keyboard.type(validatedParams.text);
135
+ if (validatedParams.submit) {
136
+ code.push(`// Submit text`);
137
+ code.push(`await page.keyboard.press('Enter');`);
138
+ await tab.page.keyboard.press('Enter');
139
+ }
140
+ return { code };
115
141
  });
116
142
  },
117
143
  };
144
+ exports.default = [
145
+ screenshot,
146
+ moveMouse,
147
+ click,
148
+ drag,
149
+ type,
150
+ ];
@@ -14,29 +14,68 @@
14
14
  * See the License for the specific language governing permissions and
15
15
  * limitations under the License.
16
16
  */
17
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
18
+ if (k2 === undefined) k2 = k;
19
+ var desc = Object.getOwnPropertyDescriptor(m, k);
20
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
21
+ desc = { enumerable: true, get: function() { return m[k]; } };
22
+ }
23
+ Object.defineProperty(o, k2, desc);
24
+ }) : (function(o, m, k, k2) {
25
+ if (k2 === undefined) k2 = k;
26
+ o[k2] = m[k];
27
+ }));
28
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
29
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
30
+ }) : function(o, v) {
31
+ o["default"] = v;
32
+ });
33
+ var __importStar = (this && this.__importStar) || (function () {
34
+ var ownKeys = function(o) {
35
+ ownKeys = Object.getOwnPropertyNames || function (o) {
36
+ var ar = [];
37
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
38
+ return ar;
39
+ };
40
+ return ownKeys(o);
41
+ };
42
+ return function (mod) {
43
+ if (mod && mod.__esModule) return mod;
44
+ var result = {};
45
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
46
+ __setModuleDefault(result, mod);
47
+ return result;
48
+ };
49
+ })();
17
50
  var __importDefault = (this && this.__importDefault) || function (mod) {
18
51
  return (mod && mod.__esModule) ? mod : { "default": mod };
19
52
  };
20
53
  Object.defineProperty(exports, "__esModule", { value: true });
21
- exports.screenshot = exports.selectOption = exports.type = exports.hover = exports.drag = exports.click = exports.snapshot = void 0;
22
54
  const zod_1 = require("zod");
23
55
  const zod_to_json_schema_1 = __importDefault(require("zod-to-json-schema"));
24
- const utils_1 = require("./utils");
25
- exports.snapshot = {
56
+ const context_1 = require("../context");
57
+ const javascript = __importStar(require("../javascript"));
58
+ const snapshot = {
59
+ capability: 'core',
26
60
  schema: {
27
61
  name: 'browser_snapshot',
28
62
  description: 'Capture accessibility snapshot of the current page, this is better than screenshot',
29
63
  inputSchema: (0, zod_to_json_schema_1.default)(zod_1.z.object({})),
30
64
  },
31
65
  handle: async (context) => {
32
- return await (0, utils_1.captureAriaSnapshot)(context);
66
+ const tab = await context.ensureTab();
67
+ return await tab.run(async () => {
68
+ const code = [`// <internal code to capture accessibility snapshot>`];
69
+ return { code };
70
+ }, { captureSnapshot: true });
33
71
  },
34
72
  };
35
73
  const elementSchema = zod_1.z.object({
36
74
  element: zod_1.z.string().describe('Human-readable element description used to obtain permission to interact with the element'),
37
75
  ref: zod_1.z.string().describe('Exact target element reference from the page snapshot'),
38
76
  });
39
- exports.click = {
77
+ const click = {
78
+ capability: 'core',
40
79
  schema: {
41
80
  name: 'browser_click',
42
81
  description: 'Perform click on a web page',
@@ -44,7 +83,15 @@ exports.click = {
44
83
  },
45
84
  handle: async (context, params) => {
46
85
  const validatedParams = elementSchema.parse(params);
47
- return (0, utils_1.runAndWait)(context, `"${validatedParams.element}" clicked`, () => context.refLocator(validatedParams.ref).click(), true);
86
+ return await context.currentTab().runAndWaitWithSnapshot(async (snapshot) => {
87
+ const locator = snapshot.refLocator(validatedParams.ref);
88
+ const code = [
89
+ `// Click ${validatedParams.element}`,
90
+ `await page.${await (0, context_1.generateLocator)(locator)}.click();`
91
+ ];
92
+ await locator.click();
93
+ return { code };
94
+ });
48
95
  },
49
96
  };
50
97
  const dragSchema = zod_1.z.object({
@@ -53,7 +100,8 @@ const dragSchema = zod_1.z.object({
53
100
  endElement: zod_1.z.string().describe('Human-readable target element description used to obtain the permission to interact with the element'),
54
101
  endRef: zod_1.z.string().describe('Exact target element reference from the page snapshot'),
55
102
  });
56
- exports.drag = {
103
+ const drag = {
104
+ capability: 'core',
57
105
  schema: {
58
106
  name: 'browser_drag',
59
107
  description: 'Perform drag and drop between two elements',
@@ -61,14 +109,20 @@ exports.drag = {
61
109
  },
62
110
  handle: async (context, params) => {
63
111
  const validatedParams = dragSchema.parse(params);
64
- return (0, utils_1.runAndWait)(context, `Dragged "${validatedParams.startElement}" to "${validatedParams.endElement}"`, async () => {
65
- const startLocator = context.refLocator(validatedParams.startRef);
66
- const endLocator = context.refLocator(validatedParams.endRef);
112
+ return await context.currentTab().runAndWaitWithSnapshot(async (snapshot) => {
113
+ const startLocator = snapshot.refLocator(validatedParams.startRef);
114
+ const endLocator = snapshot.refLocator(validatedParams.endRef);
115
+ const code = [
116
+ `// Drag ${validatedParams.startElement} to ${validatedParams.endElement}`,
117
+ `await page.${await (0, context_1.generateLocator)(startLocator)}.dragTo(page.${await (0, context_1.generateLocator)(endLocator)});`
118
+ ];
67
119
  await startLocator.dragTo(endLocator);
68
- }, true);
120
+ return { code };
121
+ });
69
122
  },
70
123
  };
71
- exports.hover = {
124
+ const hover = {
125
+ capability: 'core',
72
126
  schema: {
73
127
  name: 'browser_hover',
74
128
  description: 'Hover over element on page',
@@ -76,14 +130,24 @@ exports.hover = {
76
130
  },
77
131
  handle: async (context, params) => {
78
132
  const validatedParams = elementSchema.parse(params);
79
- return (0, utils_1.runAndWait)(context, `Hovered over "${validatedParams.element}"`, () => context.refLocator(validatedParams.ref).hover(), true);
133
+ return await context.currentTab().runAndWaitWithSnapshot(async (snapshot) => {
134
+ const locator = snapshot.refLocator(validatedParams.ref);
135
+ const code = [
136
+ `// Hover over ${validatedParams.element}`,
137
+ `await page.${await (0, context_1.generateLocator)(locator)}.hover();`
138
+ ];
139
+ await locator.hover();
140
+ return { code };
141
+ });
80
142
  },
81
143
  };
82
144
  const typeSchema = elementSchema.extend({
83
145
  text: zod_1.z.string().describe('Text to type into the element'),
84
- submit: zod_1.z.boolean().describe('Whether to submit entered text (press Enter after)'),
146
+ submit: zod_1.z.boolean().optional().describe('Whether to submit entered text (press Enter after)'),
147
+ slowly: zod_1.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.'),
85
148
  });
86
- exports.type = {
149
+ const type = {
150
+ capability: 'core',
87
151
  schema: {
88
152
  name: 'browser_type',
89
153
  description: 'Type text into editable element',
@@ -91,18 +155,33 @@ exports.type = {
91
155
  },
92
156
  handle: async (context, params) => {
93
157
  const validatedParams = typeSchema.parse(params);
94
- return await (0, utils_1.runAndWait)(context, `Typed "${validatedParams.text}" into "${validatedParams.element}"`, async () => {
95
- const locator = context.refLocator(validatedParams.ref);
96
- await locator.fill(validatedParams.text);
97
- if (validatedParams.submit)
158
+ return await context.currentTab().runAndWaitWithSnapshot(async (snapshot) => {
159
+ const locator = snapshot.refLocator(validatedParams.ref);
160
+ const code = [];
161
+ if (validatedParams.slowly) {
162
+ code.push(`// Press "${validatedParams.text}" sequentially into "${validatedParams.element}"`);
163
+ code.push(`await page.${await (0, context_1.generateLocator)(locator)}.pressSequentially(${javascript.quote(validatedParams.text)});`);
164
+ await locator.pressSequentially(validatedParams.text);
165
+ }
166
+ else {
167
+ code.push(`// Fill "${validatedParams.text}" into "${validatedParams.element}"`);
168
+ code.push(`await page.${await (0, context_1.generateLocator)(locator)}.fill(${javascript.quote(validatedParams.text)});`);
169
+ await locator.fill(validatedParams.text);
170
+ }
171
+ if (validatedParams.submit) {
172
+ code.push(`// Submit text`);
173
+ code.push(`await page.${await (0, context_1.generateLocator)(locator)}.press('Enter');`);
98
174
  await locator.press('Enter');
99
- }, true);
175
+ }
176
+ return { code };
177
+ });
100
178
  },
101
179
  };
102
180
  const selectOptionSchema = elementSchema.extend({
103
181
  values: zod_1.z.array(zod_1.z.string()).describe('Array of values to select in the dropdown. This can be a single value or multiple values.'),
104
182
  });
105
- exports.selectOption = {
183
+ const selectOption = {
184
+ capability: 'core',
106
185
  schema: {
107
186
  name: 'browser_select_option',
108
187
  description: 'Select an option in a dropdown',
@@ -110,16 +189,22 @@ exports.selectOption = {
110
189
  },
111
190
  handle: async (context, params) => {
112
191
  const validatedParams = selectOptionSchema.parse(params);
113
- return await (0, utils_1.runAndWait)(context, `Selected option in "${validatedParams.element}"`, async () => {
114
- const locator = context.refLocator(validatedParams.ref);
192
+ return await context.currentTab().runAndWaitWithSnapshot(async (snapshot) => {
193
+ const locator = snapshot.refLocator(validatedParams.ref);
194
+ const code = [
195
+ `// Select options [${validatedParams.values.join(', ')}] in ${validatedParams.element}`,
196
+ `await page.${await (0, context_1.generateLocator)(locator)}.selectOption(${javascript.formatObject(validatedParams.values)});`
197
+ ];
115
198
  await locator.selectOption(validatedParams.values);
116
- }, true);
199
+ return { code };
200
+ });
117
201
  },
118
202
  };
119
203
  const screenshotSchema = zod_1.z.object({
120
204
  raw: zod_1.z.boolean().optional().describe('Whether to return without compression (in PNG format). Default is false, which returns a JPEG image.'),
121
205
  });
122
- exports.screenshot = {
206
+ const screenshot = {
207
+ capability: 'core',
123
208
  schema: {
124
209
  name: 'browser_take_screenshot',
125
210
  description: `Take a screenshot of the current page. You can't perform actions based on the screenshot, use browser_snapshot for actions.`,
@@ -127,11 +212,20 @@ exports.screenshot = {
127
212
  },
128
213
  handle: async (context, params) => {
129
214
  const validatedParams = screenshotSchema.parse(params);
130
- const page = context.existingPage();
215
+ const tab = context.currentTab();
131
216
  const options = validatedParams.raw ? { type: 'png', scale: 'css' } : { type: 'jpeg', quality: 50, scale: 'css' };
132
- const screenshot = await page.screenshot(options);
217
+ const screenshot = await tab.page.screenshot(options);
133
218
  return {
134
219
  content: [{ type: 'image', data: screenshot.toString('base64'), mimeType: validatedParams.raw ? 'image/png' : 'image/jpeg' }],
135
220
  };
136
221
  },
137
222
  };
223
+ exports.default = [
224
+ snapshot,
225
+ click,
226
+ drag,
227
+ hover,
228
+ type,
229
+ selectOption,
230
+ screenshot,
231
+ ];
@@ -0,0 +1,116 @@
1
+ "use strict";
2
+ /**
3
+ * Copyright (c) Microsoft Corporation.
4
+ *
5
+ * Licensed under the Apache License, Version 2.0 (the "License");
6
+ * you may not use this file except in compliance with the License.
7
+ * You may obtain a copy of the License at
8
+ *
9
+ * http://www.apache.org/licenses/LICENSE-2.0
10
+ *
11
+ * Unless required by applicable law or agreed to in writing, software
12
+ * distributed under the License is distributed on an "AS IS" BASIS,
13
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ * See the License for the specific language governing permissions and
15
+ * limitations under the License.
16
+ */
17
+ Object.defineProperty(exports, "__esModule", { value: true });
18
+ const zod_1 = require("zod");
19
+ const zod_to_json_schema_1 = require("zod-to-json-schema");
20
+ const listTabs = {
21
+ capability: 'tabs',
22
+ schema: {
23
+ name: 'browser_tab_list',
24
+ description: 'List browser tabs',
25
+ inputSchema: (0, zod_to_json_schema_1.zodToJsonSchema)(zod_1.z.object({})),
26
+ },
27
+ handle: async (context) => {
28
+ return {
29
+ content: [{
30
+ type: 'text',
31
+ text: await context.listTabs(),
32
+ }],
33
+ };
34
+ },
35
+ };
36
+ const selectTabSchema = zod_1.z.object({
37
+ index: zod_1.z.number().describe('The index of the tab to select'),
38
+ });
39
+ const selectTab = captureSnapshot => ({
40
+ capability: 'tabs',
41
+ schema: {
42
+ name: 'browser_tab_select',
43
+ description: 'Select a tab by index',
44
+ inputSchema: (0, zod_to_json_schema_1.zodToJsonSchema)(selectTabSchema),
45
+ },
46
+ handle: async (context, params) => {
47
+ const validatedParams = selectTabSchema.parse(params);
48
+ await context.selectTab(validatedParams.index);
49
+ const currentTab = await context.ensureTab();
50
+ return await currentTab.run(async () => {
51
+ const code = [
52
+ `// <internal code to select tab ${validatedParams.index}>`,
53
+ ];
54
+ return { code };
55
+ }, { captureSnapshot });
56
+ },
57
+ });
58
+ const newTabSchema = zod_1.z.object({
59
+ url: zod_1.z.string().optional().describe('The URL to navigate to in the new tab. If not provided, the new tab will be blank.'),
60
+ });
61
+ const newTab = {
62
+ capability: 'tabs',
63
+ schema: {
64
+ name: 'browser_tab_new',
65
+ description: 'Open a new tab',
66
+ inputSchema: (0, zod_to_json_schema_1.zodToJsonSchema)(newTabSchema),
67
+ },
68
+ handle: async (context, params) => {
69
+ const validatedParams = newTabSchema.parse(params);
70
+ await context.newTab();
71
+ if (validatedParams.url)
72
+ await context.currentTab().navigate(validatedParams.url);
73
+ return await context.currentTab().run(async () => {
74
+ const code = [
75
+ `// <internal code to open a new tab>`,
76
+ ];
77
+ return { code };
78
+ }, { captureSnapshot: true });
79
+ },
80
+ };
81
+ const closeTabSchema = zod_1.z.object({
82
+ index: zod_1.z.number().optional().describe('The index of the tab to close. Closes current tab if not provided.'),
83
+ });
84
+ const closeTab = captureSnapshot => ({
85
+ capability: 'tabs',
86
+ schema: {
87
+ name: 'browser_tab_close',
88
+ description: 'Close a tab',
89
+ inputSchema: (0, zod_to_json_schema_1.zodToJsonSchema)(closeTabSchema),
90
+ },
91
+ handle: async (context, params) => {
92
+ const validatedParams = closeTabSchema.parse(params);
93
+ await context.closeTab(validatedParams.index);
94
+ const currentTab = context.currentTab();
95
+ if (currentTab) {
96
+ return await currentTab.run(async () => {
97
+ const code = [
98
+ `// <internal code to close tab ${validatedParams.index}>`,
99
+ ];
100
+ return { code };
101
+ }, { captureSnapshot });
102
+ }
103
+ return {
104
+ content: [{
105
+ type: 'text',
106
+ text: await context.listTabs(),
107
+ }],
108
+ };
109
+ },
110
+ });
111
+ exports.default = (captureSnapshot) => [
112
+ listTabs,
113
+ newTab,
114
+ selectTab(captureSnapshot),
115
+ closeTab(captureSnapshot),
116
+ ];
@@ -15,8 +15,7 @@
15
15
  * limitations under the License.
16
16
  */
17
17
  Object.defineProperty(exports, "__esModule", { value: true });
18
- exports.runAndWait = runAndWait;
19
- exports.captureAriaSnapshot = captureAriaSnapshot;
18
+ exports.waitForCompletion = waitForCompletion;
20
19
  exports.sanitizeForFilePath = sanitizeForFilePath;
21
20
  async function waitForCompletion(page, callback) {
22
21
  const requests = new Set();
@@ -65,30 +64,6 @@ async function waitForCompletion(page, callback) {
65
64
  dispose();
66
65
  }
67
66
  }
68
- async function runAndWait(context, status, callback, snapshot = false) {
69
- const page = context.existingPage();
70
- const dismissFileChooser = context.hasFileChooser();
71
- await waitForCompletion(page, () => callback(page));
72
- if (dismissFileChooser)
73
- context.clearFileChooser();
74
- const result = snapshot ? await captureAriaSnapshot(context, status) : {
75
- content: [{ type: 'text', text: status }],
76
- };
77
- return result;
78
- }
79
- async function captureAriaSnapshot(context, status = '') {
80
- const page = context.existingPage();
81
- const lines = [];
82
- if (status)
83
- lines.push(`${status}`);
84
- lines.push('', `- Page URL: ${page.url()}`, `- Page Title: ${await page.title()}`);
85
- if (context.hasFileChooser())
86
- lines.push(`- There is a file chooser visible that requires browser_choose_file to be called`);
87
- lines.push(`- Page Snapshot`, '```yaml', await context.allFramesSnapshot(), '```', '');
88
- return {
89
- content: [{ type: 'text', text: lines.join('\n') }],
90
- };
91
- }
92
67
  function sanitizeForFilePath(s) {
93
68
  return s.replace(/[\x00-\x2C\x2E-\x2F\x3A-\x40\x5B-\x60\x7B-\x7F]+/g, '-');
94
69
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@playwright/mcp",
3
- "version": "0.0.9",
3
+ "version": "0.0.13",
4
4
  "description": "Playwright Tools for MCP",
5
5
  "repository": {
6
6
  "type": "git",
@@ -19,6 +19,7 @@
19
19
  "lint": "eslint .",
20
20
  "watch": "tsc --watch",
21
21
  "test": "playwright test",
22
+ "ctest": "playwright test --project=chrome",
22
23
  "clean": "rm -rf lib",
23
24
  "npm-publish": "npm run clean && npm run build && npm run test && npm publish"
24
25
  },