@playwright/mcp 0.0.30 → 0.0.31
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 +160 -321
- package/config.d.ts +3 -17
- package/lib/browserContextFactory.js +3 -35
- package/lib/config.js +62 -6
- package/lib/connection.js +2 -3
- package/lib/context.js +39 -29
- package/lib/log.js +21 -0
- package/lib/pageSnapshot.js +1 -1
- package/lib/program.js +10 -9
- package/lib/tab.js +37 -3
- package/lib/tools/common.js +4 -4
- package/lib/tools/console.js +1 -1
- package/lib/tools/dialogs.js +4 -4
- package/lib/tools/evaluate.js +62 -0
- package/lib/tools/files.js +5 -5
- package/lib/tools/install.js +1 -1
- package/lib/tools/keyboard.js +50 -4
- package/lib/tools/{vision.js → mouse.js} +14 -81
- package/lib/tools/navigate.js +12 -12
- package/lib/tools/screenshot.js +1 -1
- package/lib/tools/snapshot.js +7 -47
- package/lib/tools/tabs.js +14 -14
- package/lib/tools/utils.js +5 -4
- package/lib/tools/wait.js +6 -6
- package/lib/tools.js +12 -26
- package/package.json +4 -3
- package/lib/browserServer.js +0 -151
- package/lib/tools/testing.js +0 -60
package/lib/browserServer.js
DELETED
|
@@ -1,151 +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
|
-
/* eslint-disable no-console */
|
|
17
|
-
import net from 'net';
|
|
18
|
-
import { program } from 'commander';
|
|
19
|
-
import playwright from 'playwright';
|
|
20
|
-
import { HttpServer } from './httpServer.js';
|
|
21
|
-
import { packageJSON } from './package.js';
|
|
22
|
-
class BrowserServer {
|
|
23
|
-
_server = new HttpServer();
|
|
24
|
-
_entries = [];
|
|
25
|
-
constructor() {
|
|
26
|
-
this._setupExitHandler();
|
|
27
|
-
}
|
|
28
|
-
async start(port) {
|
|
29
|
-
await this._server.start({ port });
|
|
30
|
-
this._server.routePath('/json/list', (req, res) => {
|
|
31
|
-
this._handleJsonList(res);
|
|
32
|
-
});
|
|
33
|
-
this._server.routePath('/json/launch', async (req, res) => {
|
|
34
|
-
void this._handleLaunchBrowser(req, res).catch(e => console.error(e));
|
|
35
|
-
});
|
|
36
|
-
this._setEntries([]);
|
|
37
|
-
}
|
|
38
|
-
_handleJsonList(res) {
|
|
39
|
-
const list = this._entries.map(browser => browser.info);
|
|
40
|
-
res.end(JSON.stringify(list));
|
|
41
|
-
}
|
|
42
|
-
async _handleLaunchBrowser(req, res) {
|
|
43
|
-
const request = await readBody(req);
|
|
44
|
-
let info = this._entries.map(entry => entry.info).find(info => info.userDataDir === request.userDataDir);
|
|
45
|
-
if (!info || info.error)
|
|
46
|
-
info = await this._newBrowser(request);
|
|
47
|
-
res.end(JSON.stringify(info));
|
|
48
|
-
}
|
|
49
|
-
async _newBrowser(request) {
|
|
50
|
-
const cdpPort = await findFreePort();
|
|
51
|
-
request.launchOptions.cdpPort = cdpPort;
|
|
52
|
-
const info = {
|
|
53
|
-
browserType: request.browserType,
|
|
54
|
-
userDataDir: request.userDataDir,
|
|
55
|
-
cdpPort,
|
|
56
|
-
launchOptions: request.launchOptions,
|
|
57
|
-
contextOptions: request.contextOptions,
|
|
58
|
-
};
|
|
59
|
-
const browserType = playwright[request.browserType];
|
|
60
|
-
const { browser, error } = await browserType.launchPersistentContext(request.userDataDir, {
|
|
61
|
-
...request.launchOptions,
|
|
62
|
-
...request.contextOptions,
|
|
63
|
-
handleSIGINT: false,
|
|
64
|
-
handleSIGTERM: false,
|
|
65
|
-
}).then(context => {
|
|
66
|
-
return { browser: context.browser(), error: undefined };
|
|
67
|
-
}).catch(error => {
|
|
68
|
-
return { browser: undefined, error: error.message };
|
|
69
|
-
});
|
|
70
|
-
this._setEntries([...this._entries, {
|
|
71
|
-
browser,
|
|
72
|
-
info: {
|
|
73
|
-
browserType: request.browserType,
|
|
74
|
-
userDataDir: request.userDataDir,
|
|
75
|
-
cdpPort,
|
|
76
|
-
launchOptions: request.launchOptions,
|
|
77
|
-
contextOptions: request.contextOptions,
|
|
78
|
-
error,
|
|
79
|
-
},
|
|
80
|
-
}]);
|
|
81
|
-
browser?.on('disconnected', () => {
|
|
82
|
-
this._setEntries(this._entries.filter(entry => entry.browser !== browser));
|
|
83
|
-
});
|
|
84
|
-
return info;
|
|
85
|
-
}
|
|
86
|
-
_updateReport() {
|
|
87
|
-
// Clear the current line and move cursor to top of screen
|
|
88
|
-
process.stdout.write('\x1b[2J\x1b[H');
|
|
89
|
-
process.stdout.write(`Playwright Browser Server v${packageJSON.version}\n`);
|
|
90
|
-
process.stdout.write(`Listening on ${this._server.urlPrefix('human-readable')}\n\n`);
|
|
91
|
-
if (this._entries.length === 0) {
|
|
92
|
-
process.stdout.write('No browsers currently running\n');
|
|
93
|
-
return;
|
|
94
|
-
}
|
|
95
|
-
process.stdout.write('Running browsers:\n');
|
|
96
|
-
for (const entry of this._entries) {
|
|
97
|
-
const status = entry.browser ? 'running' : 'error';
|
|
98
|
-
const statusColor = entry.browser ? '\x1b[32m' : '\x1b[31m'; // green for running, red for error
|
|
99
|
-
process.stdout.write(`${statusColor}${entry.info.browserType}\x1b[0m (${entry.info.userDataDir}) - ${statusColor}${status}\x1b[0m\n`);
|
|
100
|
-
if (entry.info.error)
|
|
101
|
-
process.stdout.write(` Error: ${entry.info.error}\n`);
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
_setEntries(entries) {
|
|
105
|
-
this._entries = entries;
|
|
106
|
-
this._updateReport();
|
|
107
|
-
}
|
|
108
|
-
_setupExitHandler() {
|
|
109
|
-
let isExiting = false;
|
|
110
|
-
const handleExit = async () => {
|
|
111
|
-
if (isExiting)
|
|
112
|
-
return;
|
|
113
|
-
isExiting = true;
|
|
114
|
-
setTimeout(() => process.exit(0), 15000);
|
|
115
|
-
for (const entry of this._entries)
|
|
116
|
-
await entry.browser?.close().catch(() => { });
|
|
117
|
-
process.exit(0);
|
|
118
|
-
};
|
|
119
|
-
process.stdin.on('close', handleExit);
|
|
120
|
-
process.on('SIGINT', handleExit);
|
|
121
|
-
process.on('SIGTERM', handleExit);
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
program
|
|
125
|
-
.name('browser-agent')
|
|
126
|
-
.option('-p, --port <port>', 'Port to listen on', '9224')
|
|
127
|
-
.action(async (options) => {
|
|
128
|
-
await main(options);
|
|
129
|
-
});
|
|
130
|
-
void program.parseAsync(process.argv);
|
|
131
|
-
async function main(options) {
|
|
132
|
-
const server = new BrowserServer();
|
|
133
|
-
await server.start(+options.port);
|
|
134
|
-
}
|
|
135
|
-
function readBody(req) {
|
|
136
|
-
return new Promise((resolve, reject) => {
|
|
137
|
-
const chunks = [];
|
|
138
|
-
req.on('data', (chunk) => chunks.push(chunk));
|
|
139
|
-
req.on('end', () => resolve(JSON.parse(Buffer.concat(chunks).toString())));
|
|
140
|
-
});
|
|
141
|
-
}
|
|
142
|
-
async function findFreePort() {
|
|
143
|
-
return new Promise((resolve, reject) => {
|
|
144
|
-
const server = net.createServer();
|
|
145
|
-
server.listen(0, () => {
|
|
146
|
-
const { port } = server.address();
|
|
147
|
-
server.close(() => resolve(port));
|
|
148
|
-
});
|
|
149
|
-
server.on('error', reject);
|
|
150
|
-
});
|
|
151
|
-
}
|
package/lib/tools/testing.js
DELETED
|
@@ -1,60 +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 generateTestSchema = z.object({
|
|
19
|
-
name: z.string().describe('The name of the test'),
|
|
20
|
-
description: z.string().describe('The description of the test'),
|
|
21
|
-
steps: z.array(z.string()).describe('The steps of the test'),
|
|
22
|
-
});
|
|
23
|
-
const generateTest = defineTool({
|
|
24
|
-
capability: 'testing',
|
|
25
|
-
schema: {
|
|
26
|
-
name: 'browser_generate_playwright_test',
|
|
27
|
-
title: 'Generate a Playwright test',
|
|
28
|
-
description: 'Generate a Playwright test for given scenario',
|
|
29
|
-
inputSchema: generateTestSchema,
|
|
30
|
-
type: 'readOnly',
|
|
31
|
-
},
|
|
32
|
-
handle: async (context, params) => {
|
|
33
|
-
return {
|
|
34
|
-
resultOverride: {
|
|
35
|
-
content: [{
|
|
36
|
-
type: 'text',
|
|
37
|
-
text: instructions(params),
|
|
38
|
-
}],
|
|
39
|
-
},
|
|
40
|
-
code: [],
|
|
41
|
-
captureSnapshot: false,
|
|
42
|
-
waitForNetwork: false,
|
|
43
|
-
};
|
|
44
|
-
},
|
|
45
|
-
});
|
|
46
|
-
const instructions = (params) => [
|
|
47
|
-
`## Instructions`,
|
|
48
|
-
`- You are a playwright test generator.`,
|
|
49
|
-
`- You are given a scenario and you need to generate a playwright test for it.`,
|
|
50
|
-
'- DO NOT generate test code based on the scenario alone. DO run steps one by one using the tools provided instead.',
|
|
51
|
-
'- Only after all steps are completed, emit a Playwright TypeScript test that uses @playwright/test based on message history',
|
|
52
|
-
'- Save generated test file in the tests directory',
|
|
53
|
-
`Test name: ${params.name}`,
|
|
54
|
-
`Description: ${params.description}`,
|
|
55
|
-
`Steps:`,
|
|
56
|
-
...params.steps.map((step, index) => `- ${index + 1}. ${step}`),
|
|
57
|
-
].join('\n');
|
|
58
|
-
export default [
|
|
59
|
-
generateTest,
|
|
60
|
-
];
|