@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.
- package/README.md +76 -74
- package/index.d.ts +7 -0
- package/lib/context.js +223 -106
- package/lib/index.js +30 -65
- package/lib/javascript.js +54 -0
- package/lib/program.js +8 -3
- package/lib/resources/console.js +1 -1
- package/lib/tools/common.js +29 -121
- package/lib/tools/files.js +47 -0
- package/lib/tools/install.js +59 -0
- package/lib/tools/keyboard.js +49 -0
- package/lib/tools/navigate.js +91 -0
- package/lib/tools/pdf.js +48 -0
- package/lib/tools/{screenshot.js → screen.js} +63 -30
- package/lib/tools/snapshot.js +121 -27
- package/lib/tools/tabs.js +116 -0
- package/lib/tools/utils.js +1 -26
- package/package.json +2 -1
package/lib/index.js
CHANGED
|
@@ -14,84 +14,49 @@
|
|
|
14
14
|
* See the License for the specific language governing permissions and
|
|
15
15
|
* limitations under the License.
|
|
16
16
|
*/
|
|
17
|
-
var
|
|
18
|
-
|
|
19
|
-
|
|
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
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
18
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
19
|
+
};
|
|
50
20
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
51
21
|
exports.createServer = createServer;
|
|
52
22
|
const server_1 = require("./server");
|
|
53
|
-
const
|
|
54
|
-
const
|
|
55
|
-
const
|
|
23
|
+
const common_1 = __importDefault(require("./tools/common"));
|
|
24
|
+
const files_1 = __importDefault(require("./tools/files"));
|
|
25
|
+
const install_1 = __importDefault(require("./tools/install"));
|
|
26
|
+
const keyboard_1 = __importDefault(require("./tools/keyboard"));
|
|
27
|
+
const navigate_1 = __importDefault(require("./tools/navigate"));
|
|
28
|
+
const pdf_1 = __importDefault(require("./tools/pdf"));
|
|
29
|
+
const snapshot_1 = __importDefault(require("./tools/snapshot"));
|
|
30
|
+
const tabs_1 = __importDefault(require("./tools/tabs"));
|
|
31
|
+
const screen_1 = __importDefault(require("./tools/screen"));
|
|
56
32
|
const console_1 = require("./resources/console");
|
|
57
|
-
const commonTools = [
|
|
58
|
-
common.pressKey,
|
|
59
|
-
common.wait,
|
|
60
|
-
common.pdf,
|
|
61
|
-
common.close,
|
|
62
|
-
common.install,
|
|
63
|
-
];
|
|
64
33
|
const snapshotTools = [
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
snapshot.selectOption,
|
|
74
|
-
snapshot.screenshot,
|
|
75
|
-
...commonTools,
|
|
34
|
+
...(0, common_1.default)(true),
|
|
35
|
+
...(0, files_1.default)(true),
|
|
36
|
+
...install_1.default,
|
|
37
|
+
...(0, keyboard_1.default)(true),
|
|
38
|
+
...(0, navigate_1.default)(true),
|
|
39
|
+
...pdf_1.default,
|
|
40
|
+
...snapshot_1.default,
|
|
41
|
+
...(0, tabs_1.default)(true),
|
|
76
42
|
];
|
|
77
43
|
const screenshotTools = [
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
screenshot.type,
|
|
87
|
-
...commonTools,
|
|
44
|
+
...(0, common_1.default)(false),
|
|
45
|
+
...(0, files_1.default)(false),
|
|
46
|
+
...install_1.default,
|
|
47
|
+
...(0, keyboard_1.default)(false),
|
|
48
|
+
...(0, navigate_1.default)(false),
|
|
49
|
+
...pdf_1.default,
|
|
50
|
+
...screen_1.default,
|
|
51
|
+
...(0, tabs_1.default)(false),
|
|
88
52
|
];
|
|
89
53
|
const resources = [
|
|
90
54
|
console_1.console,
|
|
91
55
|
];
|
|
92
56
|
const packageJSON = require('../package.json');
|
|
93
57
|
function createServer(options) {
|
|
94
|
-
const
|
|
58
|
+
const allTools = options?.vision ? screenshotTools : snapshotTools;
|
|
59
|
+
const tools = allTools.filter(tool => !options?.capabilities || tool.capability === 'core' || options.capabilities.includes(tool.capability));
|
|
95
60
|
return (0, server_1.createServerWithTools)({
|
|
96
61
|
name: 'Playwright',
|
|
97
62
|
version: packageJSON.version,
|
|
@@ -0,0 +1,54 @@
|
|
|
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
|
+
exports.escapeWithQuotes = escapeWithQuotes;
|
|
19
|
+
exports.quote = quote;
|
|
20
|
+
exports.formatObject = formatObject;
|
|
21
|
+
// adapted from:
|
|
22
|
+
// - https://github.com/microsoft/playwright/blob/76ee48dc9d4034536e3ec5b2c7ce8be3b79418a8/packages/playwright-core/src/utils/isomorphic/stringUtils.ts
|
|
23
|
+
// - https://github.com/microsoft/playwright/blob/76ee48dc9d4034536e3ec5b2c7ce8be3b79418a8/packages/playwright-core/src/server/codegen/javascript.ts
|
|
24
|
+
// NOTE: this function should not be used to escape any selectors.
|
|
25
|
+
function escapeWithQuotes(text, char = '\'') {
|
|
26
|
+
const stringified = JSON.stringify(text);
|
|
27
|
+
const escapedText = stringified.substring(1, stringified.length - 1).replace(/\\"/g, '"');
|
|
28
|
+
if (char === '\'')
|
|
29
|
+
return char + escapedText.replace(/[']/g, '\\\'') + char;
|
|
30
|
+
if (char === '"')
|
|
31
|
+
return char + escapedText.replace(/["]/g, '\\"') + char;
|
|
32
|
+
if (char === '`')
|
|
33
|
+
return char + escapedText.replace(/[`]/g, '`') + char;
|
|
34
|
+
throw new Error('Invalid escape char');
|
|
35
|
+
}
|
|
36
|
+
function quote(text) {
|
|
37
|
+
return escapeWithQuotes(text, '\'');
|
|
38
|
+
}
|
|
39
|
+
function formatObject(value, indent = ' ') {
|
|
40
|
+
if (typeof value === 'string')
|
|
41
|
+
return quote(value);
|
|
42
|
+
if (Array.isArray(value))
|
|
43
|
+
return `[${value.map(o => formatObject(o)).join(', ')}]`;
|
|
44
|
+
if (typeof value === 'object') {
|
|
45
|
+
const keys = Object.keys(value).filter(key => value[key] !== undefined).sort();
|
|
46
|
+
if (!keys.length)
|
|
47
|
+
return '{}';
|
|
48
|
+
const tokens = [];
|
|
49
|
+
for (const key of keys)
|
|
50
|
+
tokens.push(`${key}: ${formatObject(value[key])}`);
|
|
51
|
+
return `{\n${indent}${tokens.join(`,\n${indent}`)}\n}`;
|
|
52
|
+
}
|
|
53
|
+
return String(value);
|
|
54
|
+
}
|
package/lib/program.js
CHANGED
|
@@ -33,6 +33,7 @@ commander_1.program
|
|
|
33
33
|
.version('Version ' + packageJSON.version)
|
|
34
34
|
.name(packageJSON.name)
|
|
35
35
|
.option('--browser <browser>', 'Browser or chrome channel to use, possible values: chrome, firefox, webkit, msedge.')
|
|
36
|
+
.option('--caps <caps>', 'Comma-separated list of capabilities to enable, possible values: tabs, pdf, history, wait, files, install. Default is all.')
|
|
36
37
|
.option('--cdp-endpoint <endpoint>', 'CDP endpoint to connect to.')
|
|
37
38
|
.option('--executable-path <path>', 'Path to the browser executable.')
|
|
38
39
|
.option('--headless', 'Run browser in headless mode, headed by default')
|
|
@@ -68,7 +69,7 @@ commander_1.program
|
|
|
68
69
|
channel = 'chrome';
|
|
69
70
|
}
|
|
70
71
|
const launchOptions = {
|
|
71
|
-
headless: !!options.headless,
|
|
72
|
+
headless: !!(options.headless ?? (os_1.default.platform() === 'linux' && !process.env.DISPLAY)),
|
|
72
73
|
channel,
|
|
73
74
|
executablePath: options.executablePath,
|
|
74
75
|
};
|
|
@@ -79,6 +80,7 @@ commander_1.program
|
|
|
79
80
|
launchOptions,
|
|
80
81
|
vision: !!options.vision,
|
|
81
82
|
cdpEndpoint: options.cdpEndpoint,
|
|
83
|
+
capabilities: options.caps?.split(',').map((c) => c.trim()),
|
|
82
84
|
}));
|
|
83
85
|
setupExitWatchdog(serverList);
|
|
84
86
|
if (options.port) {
|
|
@@ -90,11 +92,14 @@ commander_1.program
|
|
|
90
92
|
}
|
|
91
93
|
});
|
|
92
94
|
function setupExitWatchdog(serverList) {
|
|
93
|
-
|
|
95
|
+
const handleExit = async () => {
|
|
94
96
|
setTimeout(() => process.exit(0), 15000);
|
|
95
97
|
await serverList.closeAll();
|
|
96
98
|
process.exit(0);
|
|
97
|
-
}
|
|
99
|
+
};
|
|
100
|
+
process.stdin.on('close', handleExit);
|
|
101
|
+
process.on('SIGINT', handleExit);
|
|
102
|
+
process.on('SIGTERM', handleExit);
|
|
98
103
|
}
|
|
99
104
|
commander_1.program.parse(process.argv);
|
|
100
105
|
async function createUserDataDir(browserName) {
|
package/lib/resources/console.js
CHANGED
|
@@ -23,7 +23,7 @@ exports.console = {
|
|
|
23
23
|
mimeType: 'text/plain',
|
|
24
24
|
},
|
|
25
25
|
read: async (context, uri) => {
|
|
26
|
-
const messages = await context.console();
|
|
26
|
+
const messages = await context.currentTab().console();
|
|
27
27
|
const log = messages.map(message => `[${message.type().toUpperCase()}] ${message.text()}`).join('\n');
|
|
28
28
|
return [{
|
|
29
29
|
uri,
|
package/lib/tools/common.js
CHANGED
|
@@ -14,70 +14,14 @@
|
|
|
14
14
|
* See the License for the specific language governing permissions and
|
|
15
15
|
* limitations under the License.
|
|
16
16
|
*/
|
|
17
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
18
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
19
|
-
};
|
|
20
17
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
21
|
-
exports.install = exports.chooseFile = exports.close = exports.pdf = exports.pressKey = exports.wait = exports.goForward = exports.goBack = exports.navigate = void 0;
|
|
22
|
-
const os_1 = __importDefault(require("os"));
|
|
23
|
-
const path_1 = __importDefault(require("path"));
|
|
24
18
|
const zod_1 = require("zod");
|
|
25
19
|
const zod_to_json_schema_1 = require("zod-to-json-schema");
|
|
26
|
-
const utils_1 = require("./utils");
|
|
27
|
-
const navigateSchema = zod_1.z.object({
|
|
28
|
-
url: zod_1.z.string().describe('The URL to navigate to'),
|
|
29
|
-
});
|
|
30
|
-
const navigate = snapshot => ({
|
|
31
|
-
schema: {
|
|
32
|
-
name: 'browser_navigate',
|
|
33
|
-
description: 'Navigate to a URL',
|
|
34
|
-
inputSchema: (0, zod_to_json_schema_1.zodToJsonSchema)(navigateSchema),
|
|
35
|
-
},
|
|
36
|
-
handle: async (context, params) => {
|
|
37
|
-
const validatedParams = navigateSchema.parse(params);
|
|
38
|
-
const page = await context.createPage();
|
|
39
|
-
await page.goto(validatedParams.url, { waitUntil: 'domcontentloaded' });
|
|
40
|
-
// Cap load event to 5 seconds, the page is operational at this point.
|
|
41
|
-
await page.waitForLoadState('load', { timeout: 5000 }).catch(() => { });
|
|
42
|
-
if (snapshot)
|
|
43
|
-
return (0, utils_1.captureAriaSnapshot)(context);
|
|
44
|
-
return {
|
|
45
|
-
content: [{
|
|
46
|
-
type: 'text',
|
|
47
|
-
text: `Navigated to ${validatedParams.url}`,
|
|
48
|
-
}],
|
|
49
|
-
};
|
|
50
|
-
},
|
|
51
|
-
});
|
|
52
|
-
exports.navigate = navigate;
|
|
53
|
-
const goBackSchema = zod_1.z.object({});
|
|
54
|
-
const goBack = snapshot => ({
|
|
55
|
-
schema: {
|
|
56
|
-
name: 'browser_go_back',
|
|
57
|
-
description: 'Go back to the previous page',
|
|
58
|
-
inputSchema: (0, zod_to_json_schema_1.zodToJsonSchema)(goBackSchema),
|
|
59
|
-
},
|
|
60
|
-
handle: async (context) => {
|
|
61
|
-
return await (0, utils_1.runAndWait)(context, 'Navigated back', async (page) => page.goBack(), snapshot);
|
|
62
|
-
},
|
|
63
|
-
});
|
|
64
|
-
exports.goBack = goBack;
|
|
65
|
-
const goForwardSchema = zod_1.z.object({});
|
|
66
|
-
const goForward = snapshot => ({
|
|
67
|
-
schema: {
|
|
68
|
-
name: 'browser_go_forward',
|
|
69
|
-
description: 'Go forward to the next page',
|
|
70
|
-
inputSchema: (0, zod_to_json_schema_1.zodToJsonSchema)(goForwardSchema),
|
|
71
|
-
},
|
|
72
|
-
handle: async (context) => {
|
|
73
|
-
return await (0, utils_1.runAndWait)(context, 'Navigated forward', async (page) => page.goForward(), snapshot);
|
|
74
|
-
},
|
|
75
|
-
});
|
|
76
|
-
exports.goForward = goForward;
|
|
77
20
|
const waitSchema = zod_1.z.object({
|
|
78
21
|
time: zod_1.z.number().describe('The time to wait in seconds'),
|
|
79
22
|
});
|
|
80
|
-
|
|
23
|
+
const wait = {
|
|
24
|
+
capability: 'wait',
|
|
81
25
|
schema: {
|
|
82
26
|
name: 'browser_wait',
|
|
83
27
|
description: 'Wait for a specified time in seconds',
|
|
@@ -94,43 +38,9 @@ exports.wait = {
|
|
|
94
38
|
};
|
|
95
39
|
},
|
|
96
40
|
};
|
|
97
|
-
const pressKeySchema = zod_1.z.object({
|
|
98
|
-
key: zod_1.z.string().describe('Name of the key to press or a character to generate, such as `ArrowLeft` or `a`'),
|
|
99
|
-
});
|
|
100
|
-
exports.pressKey = {
|
|
101
|
-
schema: {
|
|
102
|
-
name: 'browser_press_key',
|
|
103
|
-
description: 'Press a key on the keyboard',
|
|
104
|
-
inputSchema: (0, zod_to_json_schema_1.zodToJsonSchema)(pressKeySchema),
|
|
105
|
-
},
|
|
106
|
-
handle: async (context, params) => {
|
|
107
|
-
const validatedParams = pressKeySchema.parse(params);
|
|
108
|
-
return await (0, utils_1.runAndWait)(context, `Pressed key ${validatedParams.key}`, async (page) => {
|
|
109
|
-
await page.keyboard.press(validatedParams.key);
|
|
110
|
-
});
|
|
111
|
-
},
|
|
112
|
-
};
|
|
113
|
-
const pdfSchema = zod_1.z.object({});
|
|
114
|
-
exports.pdf = {
|
|
115
|
-
schema: {
|
|
116
|
-
name: 'browser_save_as_pdf',
|
|
117
|
-
description: 'Save page as PDF',
|
|
118
|
-
inputSchema: (0, zod_to_json_schema_1.zodToJsonSchema)(pdfSchema),
|
|
119
|
-
},
|
|
120
|
-
handle: async (context) => {
|
|
121
|
-
const page = context.existingPage();
|
|
122
|
-
const fileName = path_1.default.join(os_1.default.tmpdir(), (0, utils_1.sanitizeForFilePath)(`page-${new Date().toISOString()}`)) + '.pdf';
|
|
123
|
-
await page.pdf({ path: fileName });
|
|
124
|
-
return {
|
|
125
|
-
content: [{
|
|
126
|
-
type: 'text',
|
|
127
|
-
text: `Saved as ${fileName}`,
|
|
128
|
-
}],
|
|
129
|
-
};
|
|
130
|
-
},
|
|
131
|
-
};
|
|
132
41
|
const closeSchema = zod_1.z.object({});
|
|
133
|
-
|
|
42
|
+
const close = {
|
|
43
|
+
capability: 'core',
|
|
134
44
|
schema: {
|
|
135
45
|
name: 'browser_close',
|
|
136
46
|
description: 'Close the page',
|
|
@@ -146,36 +56,34 @@ exports.close = {
|
|
|
146
56
|
};
|
|
147
57
|
},
|
|
148
58
|
};
|
|
149
|
-
const
|
|
150
|
-
|
|
59
|
+
const resizeSchema = zod_1.z.object({
|
|
60
|
+
width: zod_1.z.number().describe('Width of the browser window'),
|
|
61
|
+
height: zod_1.z.number().describe('Height of the browser window'),
|
|
151
62
|
});
|
|
152
|
-
const
|
|
63
|
+
const resize = captureSnapshot => ({
|
|
64
|
+
capability: 'core',
|
|
153
65
|
schema: {
|
|
154
|
-
name: '
|
|
155
|
-
description: '
|
|
156
|
-
inputSchema: (0, zod_to_json_schema_1.zodToJsonSchema)(
|
|
66
|
+
name: 'browser_resize',
|
|
67
|
+
description: 'Resize the browser window',
|
|
68
|
+
inputSchema: (0, zod_to_json_schema_1.zodToJsonSchema)(resizeSchema),
|
|
157
69
|
},
|
|
158
70
|
handle: async (context, params) => {
|
|
159
|
-
const validatedParams =
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
71
|
+
const validatedParams = resizeSchema.parse(params);
|
|
72
|
+
const tab = context.currentTab();
|
|
73
|
+
return await tab.run(async (tab) => {
|
|
74
|
+
await tab.page.setViewportSize({ width: validatedParams.width, height: validatedParams.height });
|
|
75
|
+
const code = [
|
|
76
|
+
`// Resize browser window to ${validatedParams.width}x${validatedParams.height}`,
|
|
77
|
+
`await page.setViewportSize({ width: ${validatedParams.width}, height: ${validatedParams.height} });`
|
|
78
|
+
];
|
|
79
|
+
return { code };
|
|
80
|
+
}, {
|
|
81
|
+
captureSnapshot,
|
|
82
|
+
});
|
|
163
83
|
},
|
|
164
84
|
});
|
|
165
|
-
exports.
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
inputSchema: (0, zod_to_json_schema_1.zodToJsonSchema)(zod_1.z.object({})),
|
|
171
|
-
},
|
|
172
|
-
handle: async (context) => {
|
|
173
|
-
const channel = await context.install();
|
|
174
|
-
return {
|
|
175
|
-
content: [{
|
|
176
|
-
type: 'text',
|
|
177
|
-
text: `Browser ${channel} installed`,
|
|
178
|
-
}],
|
|
179
|
-
};
|
|
180
|
-
},
|
|
181
|
-
};
|
|
85
|
+
exports.default = (captureSnapshot) => [
|
|
86
|
+
close,
|
|
87
|
+
wait,
|
|
88
|
+
resize(captureSnapshot)
|
|
89
|
+
];
|
|
@@ -0,0 +1,47 @@
|
|
|
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 uploadFileSchema = zod_1.z.object({
|
|
21
|
+
paths: zod_1.z.array(zod_1.z.string()).describe('The absolute paths to the files to upload. Can be a single file or multiple files.'),
|
|
22
|
+
});
|
|
23
|
+
const uploadFile = captureSnapshot => ({
|
|
24
|
+
capability: 'files',
|
|
25
|
+
schema: {
|
|
26
|
+
name: 'browser_file_upload',
|
|
27
|
+
description: 'Upload one or multiple files',
|
|
28
|
+
inputSchema: (0, zod_to_json_schema_1.zodToJsonSchema)(uploadFileSchema),
|
|
29
|
+
},
|
|
30
|
+
handle: async (context, params) => {
|
|
31
|
+
const validatedParams = uploadFileSchema.parse(params);
|
|
32
|
+
const tab = context.currentTab();
|
|
33
|
+
return await tab.runAndWait(async () => {
|
|
34
|
+
await tab.submitFileChooser(validatedParams.paths);
|
|
35
|
+
const code = [
|
|
36
|
+
`// <internal code to chose files ${validatedParams.paths.join(', ')}`,
|
|
37
|
+
];
|
|
38
|
+
return { code };
|
|
39
|
+
}, {
|
|
40
|
+
captureSnapshot,
|
|
41
|
+
noClearFileChooser: true,
|
|
42
|
+
});
|
|
43
|
+
},
|
|
44
|
+
});
|
|
45
|
+
exports.default = (captureSnapshot) => [
|
|
46
|
+
uploadFile(captureSnapshot),
|
|
47
|
+
];
|
|
@@ -0,0 +1,59 @@
|
|
|
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 child_process_1 = require("child_process");
|
|
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 install = {
|
|
26
|
+
capability: 'install',
|
|
27
|
+
schema: {
|
|
28
|
+
name: 'browser_install',
|
|
29
|
+
description: 'Install the browser specified in the config. Call this if you get an error about the browser not being installed.',
|
|
30
|
+
inputSchema: (0, zod_to_json_schema_1.zodToJsonSchema)(zod_1.z.object({})),
|
|
31
|
+
},
|
|
32
|
+
handle: async (context) => {
|
|
33
|
+
const channel = context.options.launchOptions?.channel ?? context.options.browserName ?? 'chrome';
|
|
34
|
+
const cli = path_1.default.join(require.resolve('playwright/package.json'), '..', 'cli.js');
|
|
35
|
+
const child = (0, child_process_1.fork)(cli, ['install', channel], {
|
|
36
|
+
stdio: 'pipe',
|
|
37
|
+
});
|
|
38
|
+
const output = [];
|
|
39
|
+
child.stdout?.on('data', data => output.push(data.toString()));
|
|
40
|
+
child.stderr?.on('data', data => output.push(data.toString()));
|
|
41
|
+
await new Promise((resolve, reject) => {
|
|
42
|
+
child.on('close', code => {
|
|
43
|
+
if (code === 0)
|
|
44
|
+
resolve();
|
|
45
|
+
else
|
|
46
|
+
reject(new Error(`Failed to install browser: ${output.join('')}`));
|
|
47
|
+
});
|
|
48
|
+
});
|
|
49
|
+
return {
|
|
50
|
+
content: [{
|
|
51
|
+
type: 'text',
|
|
52
|
+
text: `Browser ${channel} installed`,
|
|
53
|
+
}],
|
|
54
|
+
};
|
|
55
|
+
},
|
|
56
|
+
};
|
|
57
|
+
exports.default = [
|
|
58
|
+
install,
|
|
59
|
+
];
|
|
@@ -0,0 +1,49 @@
|
|
|
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 zod_1 = require("zod");
|
|
22
|
+
const zod_to_json_schema_1 = __importDefault(require("zod-to-json-schema"));
|
|
23
|
+
const pressKeySchema = zod_1.z.object({
|
|
24
|
+
key: zod_1.z.string().describe('Name of the key to press or a character to generate, such as `ArrowLeft` or `a`'),
|
|
25
|
+
});
|
|
26
|
+
const pressKey = captureSnapshot => ({
|
|
27
|
+
capability: 'core',
|
|
28
|
+
schema: {
|
|
29
|
+
name: 'browser_press_key',
|
|
30
|
+
description: 'Press a key on the keyboard',
|
|
31
|
+
inputSchema: (0, zod_to_json_schema_1.default)(pressKeySchema),
|
|
32
|
+
},
|
|
33
|
+
handle: async (context, params) => {
|
|
34
|
+
const validatedParams = pressKeySchema.parse(params);
|
|
35
|
+
return await context.currentTab().runAndWait(async (tab) => {
|
|
36
|
+
await tab.page.keyboard.press(validatedParams.key);
|
|
37
|
+
const code = [
|
|
38
|
+
`// Press ${validatedParams.key}`,
|
|
39
|
+
`await page.keyboard.press('${validatedParams.key}');`,
|
|
40
|
+
];
|
|
41
|
+
return { code };
|
|
42
|
+
}, {
|
|
43
|
+
captureSnapshot,
|
|
44
|
+
});
|
|
45
|
+
},
|
|
46
|
+
});
|
|
47
|
+
exports.default = (captureSnapshot) => [
|
|
48
|
+
pressKey(captureSnapshot),
|
|
49
|
+
];
|
|
@@ -0,0 +1,91 @@
|
|
|
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 navigateSchema = zod_1.z.object({
|
|
21
|
+
url: zod_1.z.string().describe('The URL to navigate to'),
|
|
22
|
+
});
|
|
23
|
+
const navigate = captureSnapshot => ({
|
|
24
|
+
capability: 'core',
|
|
25
|
+
schema: {
|
|
26
|
+
name: 'browser_navigate',
|
|
27
|
+
description: 'Navigate to a URL',
|
|
28
|
+
inputSchema: (0, zod_to_json_schema_1.zodToJsonSchema)(navigateSchema),
|
|
29
|
+
},
|
|
30
|
+
handle: async (context, params) => {
|
|
31
|
+
const validatedParams = navigateSchema.parse(params);
|
|
32
|
+
const currentTab = await context.ensureTab();
|
|
33
|
+
return await currentTab.run(async (tab) => {
|
|
34
|
+
await tab.navigate(validatedParams.url);
|
|
35
|
+
const code = [
|
|
36
|
+
`// Navigate to ${validatedParams.url}`,
|
|
37
|
+
`await page.goto('${validatedParams.url}');`,
|
|
38
|
+
];
|
|
39
|
+
return { code };
|
|
40
|
+
}, {
|
|
41
|
+
captureSnapshot,
|
|
42
|
+
});
|
|
43
|
+
},
|
|
44
|
+
});
|
|
45
|
+
const goBackSchema = zod_1.z.object({});
|
|
46
|
+
const goBack = snapshot => ({
|
|
47
|
+
capability: 'history',
|
|
48
|
+
schema: {
|
|
49
|
+
name: 'browser_navigate_back',
|
|
50
|
+
description: 'Go back to the previous page',
|
|
51
|
+
inputSchema: (0, zod_to_json_schema_1.zodToJsonSchema)(goBackSchema),
|
|
52
|
+
},
|
|
53
|
+
handle: async (context) => {
|
|
54
|
+
return await context.currentTab().runAndWait(async (tab) => {
|
|
55
|
+
await tab.page.goBack();
|
|
56
|
+
const code = [
|
|
57
|
+
`// Navigate back`,
|
|
58
|
+
`await page.goBack();`,
|
|
59
|
+
];
|
|
60
|
+
return { code };
|
|
61
|
+
}, {
|
|
62
|
+
captureSnapshot: snapshot,
|
|
63
|
+
});
|
|
64
|
+
},
|
|
65
|
+
});
|
|
66
|
+
const goForwardSchema = zod_1.z.object({});
|
|
67
|
+
const goForward = snapshot => ({
|
|
68
|
+
capability: 'history',
|
|
69
|
+
schema: {
|
|
70
|
+
name: 'browser_navigate_forward',
|
|
71
|
+
description: 'Go forward to the next page',
|
|
72
|
+
inputSchema: (0, zod_to_json_schema_1.zodToJsonSchema)(goForwardSchema),
|
|
73
|
+
},
|
|
74
|
+
handle: async (context) => {
|
|
75
|
+
return await context.currentTab().runAndWait(async (tab) => {
|
|
76
|
+
await tab.page.goForward();
|
|
77
|
+
const code = [
|
|
78
|
+
`// Navigate forward`,
|
|
79
|
+
`await page.goForward();`,
|
|
80
|
+
];
|
|
81
|
+
return { code };
|
|
82
|
+
}, {
|
|
83
|
+
captureSnapshot: snapshot,
|
|
84
|
+
});
|
|
85
|
+
},
|
|
86
|
+
});
|
|
87
|
+
exports.default = (captureSnapshot) => [
|
|
88
|
+
navigate(captureSnapshot),
|
|
89
|
+
goBack(captureSnapshot),
|
|
90
|
+
goForward(captureSnapshot),
|
|
91
|
+
];
|