@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.
- package/README.md +82 -47
- package/cli.js +7 -1
- package/config.d.ts +24 -0
- package/index.d.ts +1 -1
- package/index.js +2 -2
- package/package.json +14 -40
- package/lib/browserContextFactory.js +0 -211
- package/lib/browserServerBackend.js +0 -77
- package/lib/config.js +0 -246
- package/lib/context.js +0 -226
- package/lib/extension/cdpRelay.js +0 -358
- package/lib/extension/extensionContextFactory.js +0 -56
- package/lib/extension/protocol.js +0 -18
- package/lib/index.js +0 -40
- package/lib/loop/loop.js +0 -69
- package/lib/loop/loopClaude.js +0 -152
- package/lib/loop/loopOpenAI.js +0 -141
- package/lib/loop/main.js +0 -60
- package/lib/loopTools/context.js +0 -67
- package/lib/loopTools/main.js +0 -54
- package/lib/loopTools/perform.js +0 -32
- package/lib/loopTools/snapshot.js +0 -29
- package/lib/loopTools/tool.js +0 -18
- package/lib/mcp/http.js +0 -135
- package/lib/mcp/inProcessTransport.js +0 -72
- package/lib/mcp/manualPromise.js +0 -111
- package/lib/mcp/mdb.js +0 -198
- package/lib/mcp/proxyBackend.js +0 -104
- package/lib/mcp/server.js +0 -123
- package/lib/mcp/tool.js +0 -32
- package/lib/program.js +0 -132
- package/lib/response.js +0 -165
- package/lib/sessionLog.js +0 -121
- package/lib/tab.js +0 -249
- package/lib/tools/common.js +0 -55
- package/lib/tools/console.js +0 -33
- package/lib/tools/dialogs.js +0 -47
- package/lib/tools/evaluate.js +0 -53
- package/lib/tools/files.js +0 -44
- package/lib/tools/form.js +0 -57
- package/lib/tools/install.js +0 -53
- package/lib/tools/keyboard.js +0 -78
- package/lib/tools/mouse.js +0 -99
- package/lib/tools/navigate.js +0 -54
- package/lib/tools/network.js +0 -41
- package/lib/tools/pdf.js +0 -40
- package/lib/tools/screenshot.js +0 -79
- package/lib/tools/snapshot.js +0 -139
- package/lib/tools/tabs.js +0 -59
- package/lib/tools/tool.js +0 -33
- package/lib/tools/utils.js +0 -74
- package/lib/tools/verify.js +0 -137
- package/lib/tools/wait.js +0 -55
- package/lib/tools.js +0 -54
- package/lib/utils/codegen.js +0 -49
- package/lib/utils/fileUtils.js +0 -36
- package/lib/utils/guid.js +0 -22
- package/lib/utils/log.js +0 -21
- package/lib/utils/package.js +0 -20
- package/lib/vscode/host.js +0 -128
- package/lib/vscode/main.js +0 -62
package/lib/tab.js
DELETED
|
@@ -1,249 +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 { EventEmitter } from 'events';
|
|
17
|
-
import { callOnPageNoTrace, waitForCompletion } from './tools/utils.js';
|
|
18
|
-
import { logUnhandledError } from './utils/log.js';
|
|
19
|
-
import { ManualPromise } from './mcp/manualPromise.js';
|
|
20
|
-
export const TabEvents = {
|
|
21
|
-
modalState: 'modalState'
|
|
22
|
-
};
|
|
23
|
-
export class Tab extends EventEmitter {
|
|
24
|
-
context;
|
|
25
|
-
page;
|
|
26
|
-
_lastTitle = 'about:blank';
|
|
27
|
-
_consoleMessages = [];
|
|
28
|
-
_recentConsoleMessages = [];
|
|
29
|
-
_requests = new Map();
|
|
30
|
-
_onPageClose;
|
|
31
|
-
_modalStates = [];
|
|
32
|
-
_downloads = [];
|
|
33
|
-
constructor(context, page, onPageClose) {
|
|
34
|
-
super();
|
|
35
|
-
this.context = context;
|
|
36
|
-
this.page = page;
|
|
37
|
-
this._onPageClose = onPageClose;
|
|
38
|
-
page.on('console', event => this._handleConsoleMessage(messageToConsoleMessage(event)));
|
|
39
|
-
page.on('pageerror', error => this._handleConsoleMessage(pageErrorToConsoleMessage(error)));
|
|
40
|
-
page.on('request', request => this._requests.set(request, null));
|
|
41
|
-
page.on('response', response => this._requests.set(response.request(), response));
|
|
42
|
-
page.on('close', () => this._onClose());
|
|
43
|
-
page.on('filechooser', chooser => {
|
|
44
|
-
this.setModalState({
|
|
45
|
-
type: 'fileChooser',
|
|
46
|
-
description: 'File chooser',
|
|
47
|
-
fileChooser: chooser,
|
|
48
|
-
});
|
|
49
|
-
});
|
|
50
|
-
page.on('dialog', dialog => this._dialogShown(dialog));
|
|
51
|
-
page.on('download', download => {
|
|
52
|
-
void this._downloadStarted(download);
|
|
53
|
-
});
|
|
54
|
-
page.setDefaultNavigationTimeout(60000);
|
|
55
|
-
page.setDefaultTimeout(5000);
|
|
56
|
-
page[tabSymbol] = this;
|
|
57
|
-
}
|
|
58
|
-
static forPage(page) {
|
|
59
|
-
return page[tabSymbol];
|
|
60
|
-
}
|
|
61
|
-
modalStates() {
|
|
62
|
-
return this._modalStates;
|
|
63
|
-
}
|
|
64
|
-
setModalState(modalState) {
|
|
65
|
-
this._modalStates.push(modalState);
|
|
66
|
-
this.emit(TabEvents.modalState, modalState);
|
|
67
|
-
}
|
|
68
|
-
clearModalState(modalState) {
|
|
69
|
-
this._modalStates = this._modalStates.filter(state => state !== modalState);
|
|
70
|
-
}
|
|
71
|
-
modalStatesMarkdown() {
|
|
72
|
-
return renderModalStates(this.context, this.modalStates());
|
|
73
|
-
}
|
|
74
|
-
_dialogShown(dialog) {
|
|
75
|
-
this.setModalState({
|
|
76
|
-
type: 'dialog',
|
|
77
|
-
description: `"${dialog.type()}" dialog with message "${dialog.message()}"`,
|
|
78
|
-
dialog,
|
|
79
|
-
});
|
|
80
|
-
}
|
|
81
|
-
async _downloadStarted(download) {
|
|
82
|
-
const entry = {
|
|
83
|
-
download,
|
|
84
|
-
finished: false,
|
|
85
|
-
outputFile: await this.context.outputFile(download.suggestedFilename())
|
|
86
|
-
};
|
|
87
|
-
this._downloads.push(entry);
|
|
88
|
-
await download.saveAs(entry.outputFile);
|
|
89
|
-
entry.finished = true;
|
|
90
|
-
}
|
|
91
|
-
_clearCollectedArtifacts() {
|
|
92
|
-
this._consoleMessages.length = 0;
|
|
93
|
-
this._recentConsoleMessages.length = 0;
|
|
94
|
-
this._requests.clear();
|
|
95
|
-
}
|
|
96
|
-
_handleConsoleMessage(message) {
|
|
97
|
-
this._consoleMessages.push(message);
|
|
98
|
-
this._recentConsoleMessages.push(message);
|
|
99
|
-
}
|
|
100
|
-
_onClose() {
|
|
101
|
-
this._clearCollectedArtifacts();
|
|
102
|
-
this._onPageClose(this);
|
|
103
|
-
}
|
|
104
|
-
async updateTitle() {
|
|
105
|
-
await this._raceAgainstModalStates(async () => {
|
|
106
|
-
this._lastTitle = await callOnPageNoTrace(this.page, page => page.title());
|
|
107
|
-
});
|
|
108
|
-
}
|
|
109
|
-
lastTitle() {
|
|
110
|
-
return this._lastTitle;
|
|
111
|
-
}
|
|
112
|
-
isCurrentTab() {
|
|
113
|
-
return this === this.context.currentTab();
|
|
114
|
-
}
|
|
115
|
-
async waitForLoadState(state, options) {
|
|
116
|
-
await callOnPageNoTrace(this.page, page => page.waitForLoadState(state, options).catch(logUnhandledError));
|
|
117
|
-
}
|
|
118
|
-
async navigate(url) {
|
|
119
|
-
this._clearCollectedArtifacts();
|
|
120
|
-
const downloadEvent = callOnPageNoTrace(this.page, page => page.waitForEvent('download').catch(logUnhandledError));
|
|
121
|
-
try {
|
|
122
|
-
await this.page.goto(url, { waitUntil: 'domcontentloaded' });
|
|
123
|
-
}
|
|
124
|
-
catch (_e) {
|
|
125
|
-
const e = _e;
|
|
126
|
-
const mightBeDownload = e.message.includes('net::ERR_ABORTED') // chromium
|
|
127
|
-
|| e.message.includes('Download is starting'); // firefox + webkit
|
|
128
|
-
if (!mightBeDownload)
|
|
129
|
-
throw e;
|
|
130
|
-
// on chromium, the download event is fired *after* page.goto rejects, so we wait a lil bit
|
|
131
|
-
const download = await Promise.race([
|
|
132
|
-
downloadEvent,
|
|
133
|
-
new Promise(resolve => setTimeout(resolve, 3000)),
|
|
134
|
-
]);
|
|
135
|
-
if (!download)
|
|
136
|
-
throw e;
|
|
137
|
-
// Make sure other "download" listeners are notified first.
|
|
138
|
-
await new Promise(resolve => setTimeout(resolve, 500));
|
|
139
|
-
return;
|
|
140
|
-
}
|
|
141
|
-
// Cap load event to 5 seconds, the page is operational at this point.
|
|
142
|
-
await this.waitForLoadState('load', { timeout: 5000 });
|
|
143
|
-
}
|
|
144
|
-
consoleMessages() {
|
|
145
|
-
return this._consoleMessages;
|
|
146
|
-
}
|
|
147
|
-
requests() {
|
|
148
|
-
return this._requests;
|
|
149
|
-
}
|
|
150
|
-
async captureSnapshot() {
|
|
151
|
-
let tabSnapshot;
|
|
152
|
-
const modalStates = await this._raceAgainstModalStates(async () => {
|
|
153
|
-
const snapshot = await this.page._snapshotForAI();
|
|
154
|
-
tabSnapshot = {
|
|
155
|
-
url: this.page.url(),
|
|
156
|
-
title: await this.page.title(),
|
|
157
|
-
ariaSnapshot: snapshot,
|
|
158
|
-
modalStates: [],
|
|
159
|
-
consoleMessages: [],
|
|
160
|
-
downloads: this._downloads,
|
|
161
|
-
};
|
|
162
|
-
});
|
|
163
|
-
if (tabSnapshot) {
|
|
164
|
-
// Assign console message late so that we did not lose any to modal state.
|
|
165
|
-
tabSnapshot.consoleMessages = this._recentConsoleMessages;
|
|
166
|
-
this._recentConsoleMessages = [];
|
|
167
|
-
}
|
|
168
|
-
return tabSnapshot ?? {
|
|
169
|
-
url: this.page.url(),
|
|
170
|
-
title: '',
|
|
171
|
-
ariaSnapshot: '',
|
|
172
|
-
modalStates,
|
|
173
|
-
consoleMessages: [],
|
|
174
|
-
downloads: [],
|
|
175
|
-
};
|
|
176
|
-
}
|
|
177
|
-
_javaScriptBlocked() {
|
|
178
|
-
return this._modalStates.some(state => state.type === 'dialog');
|
|
179
|
-
}
|
|
180
|
-
async _raceAgainstModalStates(action) {
|
|
181
|
-
if (this.modalStates().length)
|
|
182
|
-
return this.modalStates();
|
|
183
|
-
const promise = new ManualPromise();
|
|
184
|
-
const listener = (modalState) => promise.resolve([modalState]);
|
|
185
|
-
this.once(TabEvents.modalState, listener);
|
|
186
|
-
return await Promise.race([
|
|
187
|
-
action().then(() => {
|
|
188
|
-
this.off(TabEvents.modalState, listener);
|
|
189
|
-
return [];
|
|
190
|
-
}),
|
|
191
|
-
promise,
|
|
192
|
-
]);
|
|
193
|
-
}
|
|
194
|
-
async waitForCompletion(callback) {
|
|
195
|
-
await this._raceAgainstModalStates(() => waitForCompletion(this, callback));
|
|
196
|
-
}
|
|
197
|
-
async refLocator(params) {
|
|
198
|
-
return (await this.refLocators([params]))[0];
|
|
199
|
-
}
|
|
200
|
-
async refLocators(params) {
|
|
201
|
-
const snapshot = await this.page._snapshotForAI();
|
|
202
|
-
return params.map(param => {
|
|
203
|
-
if (!snapshot.includes(`[ref=${param.ref}]`))
|
|
204
|
-
throw new Error(`Ref ${param.ref} not found in the current page snapshot. Try capturing new snapshot.`);
|
|
205
|
-
return this.page.locator(`aria-ref=${param.ref}`).describe(param.element);
|
|
206
|
-
});
|
|
207
|
-
}
|
|
208
|
-
async waitForTimeout(time) {
|
|
209
|
-
if (this._javaScriptBlocked()) {
|
|
210
|
-
await new Promise(f => setTimeout(f, time));
|
|
211
|
-
return;
|
|
212
|
-
}
|
|
213
|
-
await callOnPageNoTrace(this.page, page => {
|
|
214
|
-
return page.evaluate(() => new Promise(f => setTimeout(f, 1000)));
|
|
215
|
-
});
|
|
216
|
-
}
|
|
217
|
-
}
|
|
218
|
-
function messageToConsoleMessage(message) {
|
|
219
|
-
return {
|
|
220
|
-
type: message.type(),
|
|
221
|
-
text: message.text(),
|
|
222
|
-
toString: () => `[${message.type().toUpperCase()}] ${message.text()} @ ${message.location().url}:${message.location().lineNumber}`,
|
|
223
|
-
};
|
|
224
|
-
}
|
|
225
|
-
function pageErrorToConsoleMessage(errorOrValue) {
|
|
226
|
-
if (errorOrValue instanceof Error) {
|
|
227
|
-
return {
|
|
228
|
-
type: undefined,
|
|
229
|
-
text: errorOrValue.message,
|
|
230
|
-
toString: () => errorOrValue.stack || errorOrValue.message,
|
|
231
|
-
};
|
|
232
|
-
}
|
|
233
|
-
return {
|
|
234
|
-
type: undefined,
|
|
235
|
-
text: String(errorOrValue),
|
|
236
|
-
toString: () => String(errorOrValue),
|
|
237
|
-
};
|
|
238
|
-
}
|
|
239
|
-
export function renderModalStates(context, modalStates) {
|
|
240
|
-
const result = ['### Modal state'];
|
|
241
|
-
if (modalStates.length === 0)
|
|
242
|
-
result.push('- There is no modal state present');
|
|
243
|
-
for (const state of modalStates) {
|
|
244
|
-
const tool = context.tools.filter(tool => 'clearsModalState' in tool).find(tool => tool.clearsModalState === state.type);
|
|
245
|
-
result.push(`- [${state.description}]: can be handled by the "${tool?.schema.name}" tool`);
|
|
246
|
-
}
|
|
247
|
-
return result;
|
|
248
|
-
}
|
|
249
|
-
const tabSymbol = Symbol('tabSymbol');
|
package/lib/tools/common.js
DELETED
|
@@ -1,55 +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
|
-
const close = defineTool({
|
|
19
|
-
capability: 'core',
|
|
20
|
-
schema: {
|
|
21
|
-
name: 'browser_close',
|
|
22
|
-
title: 'Close browser',
|
|
23
|
-
description: 'Close the page',
|
|
24
|
-
inputSchema: z.object({}),
|
|
25
|
-
type: 'readOnly',
|
|
26
|
-
},
|
|
27
|
-
handle: async (context, params, response) => {
|
|
28
|
-
await context.closeBrowserContext();
|
|
29
|
-
response.setIncludeTabs();
|
|
30
|
-
response.addCode(`await page.close()`);
|
|
31
|
-
},
|
|
32
|
-
});
|
|
33
|
-
const resize = defineTabTool({
|
|
34
|
-
capability: 'core',
|
|
35
|
-
schema: {
|
|
36
|
-
name: 'browser_resize',
|
|
37
|
-
title: 'Resize browser window',
|
|
38
|
-
description: 'Resize the browser window',
|
|
39
|
-
inputSchema: z.object({
|
|
40
|
-
width: z.number().describe('Width of the browser window'),
|
|
41
|
-
height: z.number().describe('Height of the browser window'),
|
|
42
|
-
}),
|
|
43
|
-
type: 'readOnly',
|
|
44
|
-
},
|
|
45
|
-
handle: async (tab, params, response) => {
|
|
46
|
-
response.addCode(`await page.setViewportSize({ width: ${params.width}, height: ${params.height} });`);
|
|
47
|
-
await tab.waitForCompletion(async () => {
|
|
48
|
-
await tab.page.setViewportSize({ width: params.width, height: params.height });
|
|
49
|
-
});
|
|
50
|
-
},
|
|
51
|
-
});
|
|
52
|
-
export default [
|
|
53
|
-
close,
|
|
54
|
-
resize
|
|
55
|
-
];
|
package/lib/tools/console.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
|
-
import { z } from 'zod';
|
|
17
|
-
import { defineTabTool } from './tool.js';
|
|
18
|
-
const console = defineTabTool({
|
|
19
|
-
capability: 'core',
|
|
20
|
-
schema: {
|
|
21
|
-
name: 'browser_console_messages',
|
|
22
|
-
title: 'Get console messages',
|
|
23
|
-
description: 'Returns all console messages',
|
|
24
|
-
inputSchema: z.object({}),
|
|
25
|
-
type: 'readOnly',
|
|
26
|
-
},
|
|
27
|
-
handle: async (tab, params, response) => {
|
|
28
|
-
tab.consoleMessages().map(message => response.addResult(message.toString()));
|
|
29
|
-
},
|
|
30
|
-
});
|
|
31
|
-
export default [
|
|
32
|
-
console,
|
|
33
|
-
];
|
package/lib/tools/dialogs.js
DELETED
|
@@ -1,47 +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 handleDialog = defineTabTool({
|
|
19
|
-
capability: 'core',
|
|
20
|
-
schema: {
|
|
21
|
-
name: 'browser_handle_dialog',
|
|
22
|
-
title: 'Handle a dialog',
|
|
23
|
-
description: 'Handle a dialog',
|
|
24
|
-
inputSchema: z.object({
|
|
25
|
-
accept: z.boolean().describe('Whether to accept the dialog.'),
|
|
26
|
-
promptText: z.string().optional().describe('The text of the prompt in case of a prompt dialog.'),
|
|
27
|
-
}),
|
|
28
|
-
type: 'destructive',
|
|
29
|
-
},
|
|
30
|
-
handle: async (tab, params, response) => {
|
|
31
|
-
response.setIncludeSnapshot();
|
|
32
|
-
const dialogState = tab.modalStates().find(state => state.type === 'dialog');
|
|
33
|
-
if (!dialogState)
|
|
34
|
-
throw new Error('No dialog visible');
|
|
35
|
-
tab.clearModalState(dialogState);
|
|
36
|
-
await tab.waitForCompletion(async () => {
|
|
37
|
-
if (params.accept)
|
|
38
|
-
await dialogState.dialog.accept(params.promptText);
|
|
39
|
-
else
|
|
40
|
-
await dialogState.dialog.dismiss();
|
|
41
|
-
});
|
|
42
|
-
},
|
|
43
|
-
clearsModalState: 'dialog',
|
|
44
|
-
});
|
|
45
|
-
export default [
|
|
46
|
-
handleDialog,
|
|
47
|
-
];
|
package/lib/tools/evaluate.js
DELETED
|
@@ -1,53 +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 evaluateSchema = z.object({
|
|
21
|
-
function: z.string().describe('() => { /* code */ } or (element) => { /* code */ } when element is provided'),
|
|
22
|
-
element: z.string().optional().describe('Human-readable element description used to obtain permission to interact with the element'),
|
|
23
|
-
ref: z.string().optional().describe('Exact target element reference from the page snapshot'),
|
|
24
|
-
});
|
|
25
|
-
const evaluate = defineTabTool({
|
|
26
|
-
capability: 'core',
|
|
27
|
-
schema: {
|
|
28
|
-
name: 'browser_evaluate',
|
|
29
|
-
title: 'Evaluate JavaScript',
|
|
30
|
-
description: 'Evaluate JavaScript expression on page or element',
|
|
31
|
-
inputSchema: evaluateSchema,
|
|
32
|
-
type: 'destructive',
|
|
33
|
-
},
|
|
34
|
-
handle: async (tab, params, response) => {
|
|
35
|
-
response.setIncludeSnapshot();
|
|
36
|
-
let locator;
|
|
37
|
-
if (params.ref && params.element) {
|
|
38
|
-
locator = await tab.refLocator({ ref: params.ref, element: params.element });
|
|
39
|
-
response.addCode(`await page.${await generateLocator(locator)}.evaluate(${javascript.quote(params.function)});`);
|
|
40
|
-
}
|
|
41
|
-
else {
|
|
42
|
-
response.addCode(`await page.evaluate(${javascript.quote(params.function)});`);
|
|
43
|
-
}
|
|
44
|
-
await tab.waitForCompletion(async () => {
|
|
45
|
-
const receiver = locator ?? tab.page;
|
|
46
|
-
const result = await receiver._evaluateFunction(params.function);
|
|
47
|
-
response.addResult(JSON.stringify(result, null, 2) || 'undefined');
|
|
48
|
-
});
|
|
49
|
-
},
|
|
50
|
-
});
|
|
51
|
-
export default [
|
|
52
|
-
evaluate,
|
|
53
|
-
];
|
package/lib/tools/files.js
DELETED
|
@@ -1,44 +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 uploadFile = defineTabTool({
|
|
19
|
-
capability: 'core',
|
|
20
|
-
schema: {
|
|
21
|
-
name: 'browser_file_upload',
|
|
22
|
-
title: 'Upload files',
|
|
23
|
-
description: 'Upload one or multiple files',
|
|
24
|
-
inputSchema: z.object({
|
|
25
|
-
paths: z.array(z.string()).describe('The absolute paths to the files to upload. Can be a single file or multiple files.'),
|
|
26
|
-
}),
|
|
27
|
-
type: 'destructive',
|
|
28
|
-
},
|
|
29
|
-
handle: async (tab, params, response) => {
|
|
30
|
-
response.setIncludeSnapshot();
|
|
31
|
-
const modalState = tab.modalStates().find(state => state.type === 'fileChooser');
|
|
32
|
-
if (!modalState)
|
|
33
|
-
throw new Error('No file chooser visible');
|
|
34
|
-
response.addCode(`await fileChooser.setFiles(${JSON.stringify(params.paths)})`);
|
|
35
|
-
tab.clearModalState(modalState);
|
|
36
|
-
await tab.waitForCompletion(async () => {
|
|
37
|
-
await modalState.fileChooser.setFiles(params.paths);
|
|
38
|
-
});
|
|
39
|
-
},
|
|
40
|
-
clearsModalState: 'fileChooser',
|
|
41
|
-
});
|
|
42
|
-
export default [
|
|
43
|
-
uploadFile,
|
|
44
|
-
];
|
package/lib/tools/form.js
DELETED
|
@@ -1,57 +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 { generateLocator } from './utils.js';
|
|
19
|
-
import * as javascript from '../utils/codegen.js';
|
|
20
|
-
const fillForm = defineTabTool({
|
|
21
|
-
capability: 'core',
|
|
22
|
-
schema: {
|
|
23
|
-
name: 'browser_fill_form',
|
|
24
|
-
title: 'Fill form',
|
|
25
|
-
description: 'Fill multiple form fields',
|
|
26
|
-
inputSchema: z.object({
|
|
27
|
-
fields: z.array(z.object({
|
|
28
|
-
name: z.string().describe('Human-readable field name'),
|
|
29
|
-
type: z.enum(['textbox', 'checkbox', 'radio', 'combobox', 'slider']).describe('Type of the field'),
|
|
30
|
-
ref: z.string().describe('Exact target field reference from the page snapshot'),
|
|
31
|
-
value: z.string().describe('Value to fill in the field. If the field is a checkbox, the value should be `true` or `false`. If the field is a combobox, the value should be the text of the option.'),
|
|
32
|
-
})).describe('Fields to fill in'),
|
|
33
|
-
}),
|
|
34
|
-
type: 'destructive',
|
|
35
|
-
},
|
|
36
|
-
handle: async (tab, params, response) => {
|
|
37
|
-
for (const field of params.fields) {
|
|
38
|
-
const locator = await tab.refLocator({ element: field.name, ref: field.ref });
|
|
39
|
-
const locatorSource = `await page.${await generateLocator(locator)}`;
|
|
40
|
-
if (field.type === 'textbox' || field.type === 'slider') {
|
|
41
|
-
await locator.fill(field.value);
|
|
42
|
-
response.addCode(`${locatorSource}.fill(${javascript.quote(field.value)});`);
|
|
43
|
-
}
|
|
44
|
-
else if (field.type === 'checkbox' || field.type === 'radio') {
|
|
45
|
-
await locator.setChecked(field.value === 'true');
|
|
46
|
-
response.addCode(`${locatorSource}.setChecked(${javascript.quote(field.value)});`);
|
|
47
|
-
}
|
|
48
|
-
else if (field.type === 'combobox') {
|
|
49
|
-
await locator.selectOption({ label: field.value });
|
|
50
|
-
response.addCode(`${locatorSource}.selectOption(${javascript.quote(field.value)});`);
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
},
|
|
54
|
-
});
|
|
55
|
-
export default [
|
|
56
|
-
fillForm,
|
|
57
|
-
];
|
package/lib/tools/install.js
DELETED
|
@@ -1,53 +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 { fork } from 'child_process';
|
|
17
|
-
import path from 'path';
|
|
18
|
-
import { fileURLToPath } from 'url';
|
|
19
|
-
import { z } from 'zod';
|
|
20
|
-
import { defineTool } from './tool.js';
|
|
21
|
-
const install = defineTool({
|
|
22
|
-
capability: 'core-install',
|
|
23
|
-
schema: {
|
|
24
|
-
name: 'browser_install',
|
|
25
|
-
title: 'Install the browser specified in the config',
|
|
26
|
-
description: 'Install the browser specified in the config. Call this if you get an error about the browser not being installed.',
|
|
27
|
-
inputSchema: z.object({}),
|
|
28
|
-
type: 'destructive',
|
|
29
|
-
},
|
|
30
|
-
handle: async (context, params, response) => {
|
|
31
|
-
const channel = context.config.browser?.launchOptions?.channel ?? context.config.browser?.browserName ?? 'chrome';
|
|
32
|
-
const cliUrl = import.meta.resolve('playwright/package.json');
|
|
33
|
-
const cliPath = path.join(fileURLToPath(cliUrl), '..', 'cli.js');
|
|
34
|
-
const child = fork(cliPath, ['install', channel], {
|
|
35
|
-
stdio: 'pipe',
|
|
36
|
-
});
|
|
37
|
-
const output = [];
|
|
38
|
-
child.stdout?.on('data', data => output.push(data.toString()));
|
|
39
|
-
child.stderr?.on('data', data => output.push(data.toString()));
|
|
40
|
-
await new Promise((resolve, reject) => {
|
|
41
|
-
child.on('close', code => {
|
|
42
|
-
if (code === 0)
|
|
43
|
-
resolve();
|
|
44
|
-
else
|
|
45
|
-
reject(new Error(`Failed to install browser: ${output.join('')}`));
|
|
46
|
-
});
|
|
47
|
-
});
|
|
48
|
-
response.setIncludeTabs();
|
|
49
|
-
},
|
|
50
|
-
});
|
|
51
|
-
export default [
|
|
52
|
-
install,
|
|
53
|
-
];
|
package/lib/tools/keyboard.js
DELETED
|
@@ -1,78 +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 { elementSchema } from './snapshot.js';
|
|
19
|
-
import { generateLocator } from './utils.js';
|
|
20
|
-
import * as javascript from '../utils/codegen.js';
|
|
21
|
-
const pressKey = defineTabTool({
|
|
22
|
-
capability: 'core',
|
|
23
|
-
schema: {
|
|
24
|
-
name: 'browser_press_key',
|
|
25
|
-
title: 'Press a key',
|
|
26
|
-
description: 'Press a key on the keyboard',
|
|
27
|
-
inputSchema: z.object({
|
|
28
|
-
key: z.string().describe('Name of the key to press or a character to generate, such as `ArrowLeft` or `a`'),
|
|
29
|
-
}),
|
|
30
|
-
type: 'destructive',
|
|
31
|
-
},
|
|
32
|
-
handle: async (tab, params, response) => {
|
|
33
|
-
response.setIncludeSnapshot();
|
|
34
|
-
response.addCode(`// Press ${params.key}`);
|
|
35
|
-
response.addCode(`await page.keyboard.press('${params.key}');`);
|
|
36
|
-
await tab.waitForCompletion(async () => {
|
|
37
|
-
await tab.page.keyboard.press(params.key);
|
|
38
|
-
});
|
|
39
|
-
},
|
|
40
|
-
});
|
|
41
|
-
const typeSchema = elementSchema.extend({
|
|
42
|
-
text: z.string().describe('Text to type into the element'),
|
|
43
|
-
submit: z.boolean().optional().describe('Whether to submit entered text (press Enter after)'),
|
|
44
|
-
slowly: z.boolean().optional().describe('Whether to type one character at a time. Useful for triggering key handlers in the page. By default entire text is filled in at once.'),
|
|
45
|
-
});
|
|
46
|
-
const type = defineTabTool({
|
|
47
|
-
capability: 'core',
|
|
48
|
-
schema: {
|
|
49
|
-
name: 'browser_type',
|
|
50
|
-
title: 'Type text',
|
|
51
|
-
description: 'Type text into editable element',
|
|
52
|
-
inputSchema: typeSchema,
|
|
53
|
-
type: 'destructive',
|
|
54
|
-
},
|
|
55
|
-
handle: async (tab, params, response) => {
|
|
56
|
-
const locator = await tab.refLocator(params);
|
|
57
|
-
await tab.waitForCompletion(async () => {
|
|
58
|
-
if (params.slowly) {
|
|
59
|
-
response.setIncludeSnapshot();
|
|
60
|
-
response.addCode(`await page.${await generateLocator(locator)}.pressSequentially(${javascript.quote(params.text)});`);
|
|
61
|
-
await locator.pressSequentially(params.text);
|
|
62
|
-
}
|
|
63
|
-
else {
|
|
64
|
-
response.addCode(`await page.${await generateLocator(locator)}.fill(${javascript.quote(params.text)});`);
|
|
65
|
-
await locator.fill(params.text);
|
|
66
|
-
}
|
|
67
|
-
if (params.submit) {
|
|
68
|
-
response.setIncludeSnapshot();
|
|
69
|
-
response.addCode(`await page.${await generateLocator(locator)}.press('Enter');`);
|
|
70
|
-
await locator.press('Enter');
|
|
71
|
-
}
|
|
72
|
-
});
|
|
73
|
-
},
|
|
74
|
-
});
|
|
75
|
-
export default [
|
|
76
|
-
pressKey,
|
|
77
|
-
type,
|
|
78
|
-
];
|