@playwright/mcp 0.0.36-alpha-2025-09-04 → 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 +81 -46
- package/cli.js +7 -1
- package/config.d.ts +24 -0
- package/index.js +1 -1
- package/package.json +14 -39
- package/lib/browser/browserContextFactory.js +0 -251
- package/lib/browser/browserServerBackend.js +0 -79
- package/lib/browser/codegen.js +0 -54
- package/lib/browser/config.js +0 -263
- package/lib/browser/context.js +0 -223
- package/lib/browser/response.js +0 -165
- package/lib/browser/sessionLog.js +0 -126
- package/lib/browser/tab.js +0 -251
- package/lib/browser/tools/common.js +0 -57
- package/lib/browser/tools/console.js +0 -35
- package/lib/browser/tools/dialogs.js +0 -49
- package/lib/browser/tools/evaluate.js +0 -88
- package/lib/browser/tools/files.js +0 -46
- package/lib/browser/tools/form.js +0 -92
- package/lib/browser/tools/install.js +0 -57
- package/lib/browser/tools/keyboard.js +0 -113
- package/lib/browser/tools/mouse.js +0 -101
- package/lib/browser/tools/navigate.js +0 -56
- package/lib/browser/tools/network.js +0 -43
- package/lib/browser/tools/pdf.js +0 -76
- package/lib/browser/tools/screenshot.js +0 -115
- package/lib/browser/tools/snapshot.js +0 -175
- package/lib/browser/tools/tabs.js +0 -61
- package/lib/browser/tools/tool.js +0 -37
- package/lib/browser/tools/utils.js +0 -79
- package/lib/browser/tools/verify.js +0 -172
- package/lib/browser/tools/wait.js +0 -57
- package/lib/browser/tools.js +0 -61
- package/lib/extension/cdpRelay.js +0 -395
- package/lib/extension/extensionContextFactory.js +0 -93
- package/lib/extension/protocol.js +0 -21
- package/lib/index.js +0 -75
- package/lib/log.js +0 -28
- package/lib/package.js +0 -24
- package/lib/program.js +0 -161
- package/lib/sdk/bundle.js +0 -79
- package/lib/sdk/http.js +0 -175
- package/lib/sdk/inProcessTransport.js +0 -67
- package/lib/sdk/manualPromise.js +0 -113
- package/lib/sdk/mdb.js +0 -237
- package/lib/sdk/proxyBackend.js +0 -141
- package/lib/sdk/server.js +0 -164
- package/lib/sdk/tool.js +0 -36
- package/lib/vscode/host.js +0 -199
- package/lib/vscode/main.js +0 -97
package/lib/browser/response.js
DELETED
|
@@ -1,165 +0,0 @@
|
|
|
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.Response = void 0;
|
|
19
|
-
const tab_1 = require("./tab");
|
|
20
|
-
class Response {
|
|
21
|
-
constructor(context, toolName, toolArgs) {
|
|
22
|
-
this._result = [];
|
|
23
|
-
this._code = [];
|
|
24
|
-
this._images = [];
|
|
25
|
-
this._includeSnapshot = false;
|
|
26
|
-
this._includeTabs = false;
|
|
27
|
-
this._context = context;
|
|
28
|
-
this.toolName = toolName;
|
|
29
|
-
this.toolArgs = toolArgs;
|
|
30
|
-
}
|
|
31
|
-
addResult(result) {
|
|
32
|
-
this._result.push(result);
|
|
33
|
-
}
|
|
34
|
-
addError(error) {
|
|
35
|
-
this._result.push(error);
|
|
36
|
-
this._isError = true;
|
|
37
|
-
}
|
|
38
|
-
isError() {
|
|
39
|
-
return this._isError;
|
|
40
|
-
}
|
|
41
|
-
result() {
|
|
42
|
-
return this._result.join('\n');
|
|
43
|
-
}
|
|
44
|
-
addCode(code) {
|
|
45
|
-
this._code.push(code);
|
|
46
|
-
}
|
|
47
|
-
code() {
|
|
48
|
-
return this._code.join('\n');
|
|
49
|
-
}
|
|
50
|
-
addImage(image) {
|
|
51
|
-
this._images.push(image);
|
|
52
|
-
}
|
|
53
|
-
images() {
|
|
54
|
-
return this._images;
|
|
55
|
-
}
|
|
56
|
-
setIncludeSnapshot() {
|
|
57
|
-
this._includeSnapshot = true;
|
|
58
|
-
}
|
|
59
|
-
setIncludeTabs() {
|
|
60
|
-
this._includeTabs = true;
|
|
61
|
-
}
|
|
62
|
-
async finish() {
|
|
63
|
-
// All the async snapshotting post-action is happening here.
|
|
64
|
-
// Everything below should race against modal states.
|
|
65
|
-
if (this._includeSnapshot && this._context.currentTab())
|
|
66
|
-
this._tabSnapshot = await this._context.currentTabOrDie().captureSnapshot();
|
|
67
|
-
for (const tab of this._context.tabs())
|
|
68
|
-
await tab.updateTitle();
|
|
69
|
-
}
|
|
70
|
-
tabSnapshot() {
|
|
71
|
-
return this._tabSnapshot;
|
|
72
|
-
}
|
|
73
|
-
serialize() {
|
|
74
|
-
var _a;
|
|
75
|
-
const response = [];
|
|
76
|
-
// Start with command result.
|
|
77
|
-
if (this._result.length) {
|
|
78
|
-
response.push('### Result');
|
|
79
|
-
response.push(this._result.join('\n'));
|
|
80
|
-
response.push('');
|
|
81
|
-
}
|
|
82
|
-
// Add code if it exists.
|
|
83
|
-
if (this._code.length) {
|
|
84
|
-
response.push(`### Ran Playwright code
|
|
85
|
-
\`\`\`js
|
|
86
|
-
${this._code.join('\n')}
|
|
87
|
-
\`\`\``);
|
|
88
|
-
response.push('');
|
|
89
|
-
}
|
|
90
|
-
// List browser tabs.
|
|
91
|
-
if (this._includeSnapshot || this._includeTabs)
|
|
92
|
-
response.push(...renderTabsMarkdown(this._context.tabs(), this._includeTabs));
|
|
93
|
-
// Add snapshot if provided.
|
|
94
|
-
if ((_a = this._tabSnapshot) === null || _a === void 0 ? void 0 : _a.modalStates.length) {
|
|
95
|
-
response.push(...(0, tab_1.renderModalStates)(this._context, this._tabSnapshot.modalStates));
|
|
96
|
-
response.push('');
|
|
97
|
-
}
|
|
98
|
-
else if (this._tabSnapshot) {
|
|
99
|
-
response.push(renderTabSnapshot(this._tabSnapshot));
|
|
100
|
-
response.push('');
|
|
101
|
-
}
|
|
102
|
-
// Main response part
|
|
103
|
-
const content = [
|
|
104
|
-
{ type: 'text', text: response.join('\n') },
|
|
105
|
-
];
|
|
106
|
-
// Image attachments.
|
|
107
|
-
if (this._context.config.imageResponses !== 'omit') {
|
|
108
|
-
for (const image of this._images)
|
|
109
|
-
content.push({ type: 'image', data: image.data.toString('base64'), mimeType: image.contentType });
|
|
110
|
-
}
|
|
111
|
-
return { content, isError: this._isError };
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
exports.Response = Response;
|
|
115
|
-
function renderTabSnapshot(tabSnapshot) {
|
|
116
|
-
const lines = [];
|
|
117
|
-
if (tabSnapshot.consoleMessages.length) {
|
|
118
|
-
lines.push(`### New console messages`);
|
|
119
|
-
for (const message of tabSnapshot.consoleMessages)
|
|
120
|
-
lines.push(`- ${trim(message.toString(), 100)}`);
|
|
121
|
-
lines.push('');
|
|
122
|
-
}
|
|
123
|
-
if (tabSnapshot.downloads.length) {
|
|
124
|
-
lines.push(`### Downloads`);
|
|
125
|
-
for (const entry of tabSnapshot.downloads) {
|
|
126
|
-
if (entry.finished)
|
|
127
|
-
lines.push(`- Downloaded file ${entry.download.suggestedFilename()} to ${entry.outputFile}`);
|
|
128
|
-
else
|
|
129
|
-
lines.push(`- Downloading file ${entry.download.suggestedFilename()} ...`);
|
|
130
|
-
}
|
|
131
|
-
lines.push('');
|
|
132
|
-
}
|
|
133
|
-
lines.push(`### Page state`);
|
|
134
|
-
lines.push(`- Page URL: ${tabSnapshot.url}`);
|
|
135
|
-
lines.push(`- Page Title: ${tabSnapshot.title}`);
|
|
136
|
-
lines.push(`- Page Snapshot:`);
|
|
137
|
-
lines.push('```yaml');
|
|
138
|
-
lines.push(tabSnapshot.ariaSnapshot);
|
|
139
|
-
lines.push('```');
|
|
140
|
-
return lines.join('\n');
|
|
141
|
-
}
|
|
142
|
-
function renderTabsMarkdown(tabs, force = false) {
|
|
143
|
-
if (tabs.length === 1 && !force)
|
|
144
|
-
return [];
|
|
145
|
-
if (!tabs.length) {
|
|
146
|
-
return [
|
|
147
|
-
'### Open tabs',
|
|
148
|
-
'No open tabs. Use the "browser_navigate" tool to navigate to a page first.',
|
|
149
|
-
'',
|
|
150
|
-
];
|
|
151
|
-
}
|
|
152
|
-
const lines = ['### Open tabs'];
|
|
153
|
-
for (let i = 0; i < tabs.length; i++) {
|
|
154
|
-
const tab = tabs[i];
|
|
155
|
-
const current = tab.isCurrentTab() ? ' (current)' : '';
|
|
156
|
-
lines.push(`- ${i}:${current} [${tab.lastTitle()}] (${tab.page.url()})`);
|
|
157
|
-
}
|
|
158
|
-
lines.push('');
|
|
159
|
-
return lines;
|
|
160
|
-
}
|
|
161
|
-
function trim(text, maxLength) {
|
|
162
|
-
if (text.length <= maxLength)
|
|
163
|
-
return text;
|
|
164
|
-
return text.slice(0, maxLength) + '...';
|
|
165
|
-
}
|
|
@@ -1,126 +0,0 @@
|
|
|
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
|
-
exports.SessionLog = void 0;
|
|
22
|
-
const fs_1 = __importDefault(require("fs"));
|
|
23
|
-
const path_1 = __importDefault(require("path"));
|
|
24
|
-
const log_1 = require("../log");
|
|
25
|
-
const config_1 = require("./config");
|
|
26
|
-
class SessionLog {
|
|
27
|
-
constructor(sessionFolder) {
|
|
28
|
-
this._ordinal = 0;
|
|
29
|
-
this._pendingEntries = [];
|
|
30
|
-
this._sessionFileQueue = Promise.resolve();
|
|
31
|
-
this._folder = sessionFolder;
|
|
32
|
-
this._file = path_1.default.join(this._folder, 'session.md');
|
|
33
|
-
}
|
|
34
|
-
static async create(config, rootPath) {
|
|
35
|
-
const sessionFolder = await (0, config_1.outputFile)(config, rootPath, `session-${Date.now()}`);
|
|
36
|
-
await fs_1.default.promises.mkdir(sessionFolder, { recursive: true });
|
|
37
|
-
// eslint-disable-next-line no-console
|
|
38
|
-
console.error(`Session: ${sessionFolder}`);
|
|
39
|
-
return new SessionLog(sessionFolder);
|
|
40
|
-
}
|
|
41
|
-
logResponse(response) {
|
|
42
|
-
const entry = {
|
|
43
|
-
timestamp: performance.now(),
|
|
44
|
-
toolCall: {
|
|
45
|
-
toolName: response.toolName,
|
|
46
|
-
toolArgs: response.toolArgs,
|
|
47
|
-
result: response.result(),
|
|
48
|
-
isError: response.isError(),
|
|
49
|
-
},
|
|
50
|
-
code: response.code(),
|
|
51
|
-
tabSnapshot: response.tabSnapshot(),
|
|
52
|
-
};
|
|
53
|
-
this._appendEntry(entry);
|
|
54
|
-
}
|
|
55
|
-
logUserAction(action, tab, code, isUpdate) {
|
|
56
|
-
var _a, _b;
|
|
57
|
-
code = code.trim();
|
|
58
|
-
if (isUpdate) {
|
|
59
|
-
const lastEntry = this._pendingEntries[this._pendingEntries.length - 1];
|
|
60
|
-
if (((_a = lastEntry.userAction) === null || _a === void 0 ? void 0 : _a.name) === action.name) {
|
|
61
|
-
lastEntry.userAction = action;
|
|
62
|
-
lastEntry.code = code;
|
|
63
|
-
return;
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
if (action.name === 'navigate') {
|
|
67
|
-
// Already logged at this location.
|
|
68
|
-
const lastEntry = this._pendingEntries[this._pendingEntries.length - 1];
|
|
69
|
-
if (((_b = lastEntry === null || lastEntry === void 0 ? void 0 : lastEntry.tabSnapshot) === null || _b === void 0 ? void 0 : _b.url) === action.url)
|
|
70
|
-
return;
|
|
71
|
-
}
|
|
72
|
-
const entry = {
|
|
73
|
-
timestamp: performance.now(),
|
|
74
|
-
userAction: action,
|
|
75
|
-
code,
|
|
76
|
-
tabSnapshot: {
|
|
77
|
-
url: tab.page.url(),
|
|
78
|
-
title: '',
|
|
79
|
-
ariaSnapshot: action.ariaSnapshot || '',
|
|
80
|
-
modalStates: [],
|
|
81
|
-
consoleMessages: [],
|
|
82
|
-
downloads: [],
|
|
83
|
-
},
|
|
84
|
-
};
|
|
85
|
-
this._appendEntry(entry);
|
|
86
|
-
}
|
|
87
|
-
_appendEntry(entry) {
|
|
88
|
-
this._pendingEntries.push(entry);
|
|
89
|
-
if (this._flushEntriesTimeout)
|
|
90
|
-
clearTimeout(this._flushEntriesTimeout);
|
|
91
|
-
this._flushEntriesTimeout = setTimeout(() => this._flushEntries(), 1000);
|
|
92
|
-
}
|
|
93
|
-
async _flushEntries() {
|
|
94
|
-
clearTimeout(this._flushEntriesTimeout);
|
|
95
|
-
const entries = this._pendingEntries;
|
|
96
|
-
this._pendingEntries = [];
|
|
97
|
-
const lines = [''];
|
|
98
|
-
for (const entry of entries) {
|
|
99
|
-
const ordinal = (++this._ordinal).toString().padStart(3, '0');
|
|
100
|
-
if (entry.toolCall) {
|
|
101
|
-
lines.push(`### Tool call: ${entry.toolCall.toolName}`, `- Args`, '```json', JSON.stringify(entry.toolCall.toolArgs, null, 2), '```');
|
|
102
|
-
if (entry.toolCall.result) {
|
|
103
|
-
lines.push(entry.toolCall.isError ? `- Error` : `- Result`, '```', entry.toolCall.result, '```');
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
if (entry.userAction) {
|
|
107
|
-
const actionData = { ...entry.userAction };
|
|
108
|
-
delete actionData.ariaSnapshot;
|
|
109
|
-
delete actionData.selector;
|
|
110
|
-
delete actionData.signals;
|
|
111
|
-
lines.push(`### User action: ${entry.userAction.name}`, `- Args`, '```json', JSON.stringify(actionData, null, 2), '```');
|
|
112
|
-
}
|
|
113
|
-
if (entry.code) {
|
|
114
|
-
lines.push(`- Code`, '```js', entry.code, '```');
|
|
115
|
-
}
|
|
116
|
-
if (entry.tabSnapshot) {
|
|
117
|
-
const fileName = `${ordinal}.snapshot.yml`;
|
|
118
|
-
fs_1.default.promises.writeFile(path_1.default.join(this._folder, fileName), entry.tabSnapshot.ariaSnapshot).catch(log_1.logUnhandledError);
|
|
119
|
-
lines.push(`- Snapshot: ${fileName}`);
|
|
120
|
-
}
|
|
121
|
-
lines.push('', '');
|
|
122
|
-
}
|
|
123
|
-
this._sessionFileQueue = this._sessionFileQueue.then(() => fs_1.default.promises.appendFile(this._file, lines.join('\n')));
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
exports.SessionLog = SessionLog;
|
package/lib/browser/tab.js
DELETED
|
@@ -1,251 +0,0 @@
|
|
|
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.Tab = exports.TabEvents = void 0;
|
|
19
|
-
exports.renderModalStates = renderModalStates;
|
|
20
|
-
const events_1 = require("events");
|
|
21
|
-
const utils_1 = require("./tools/utils");
|
|
22
|
-
const log_1 = require("../log");
|
|
23
|
-
const manualPromise_1 = require("../sdk/manualPromise");
|
|
24
|
-
exports.TabEvents = {
|
|
25
|
-
modalState: 'modalState'
|
|
26
|
-
};
|
|
27
|
-
class Tab extends events_1.EventEmitter {
|
|
28
|
-
constructor(context, page, onPageClose) {
|
|
29
|
-
super();
|
|
30
|
-
this._lastTitle = 'about:blank';
|
|
31
|
-
this._consoleMessages = [];
|
|
32
|
-
this._recentConsoleMessages = [];
|
|
33
|
-
this._requests = new Map();
|
|
34
|
-
this._modalStates = [];
|
|
35
|
-
this._downloads = [];
|
|
36
|
-
this.context = context;
|
|
37
|
-
this.page = page;
|
|
38
|
-
this._onPageClose = onPageClose;
|
|
39
|
-
page.on('console', event => this._handleConsoleMessage(messageToConsoleMessage(event)));
|
|
40
|
-
page.on('pageerror', error => this._handleConsoleMessage(pageErrorToConsoleMessage(error)));
|
|
41
|
-
page.on('request', request => this._requests.set(request, null));
|
|
42
|
-
page.on('response', response => this._requests.set(response.request(), response));
|
|
43
|
-
page.on('close', () => this._onClose());
|
|
44
|
-
page.on('filechooser', chooser => {
|
|
45
|
-
this.setModalState({
|
|
46
|
-
type: 'fileChooser',
|
|
47
|
-
description: 'File chooser',
|
|
48
|
-
fileChooser: chooser,
|
|
49
|
-
});
|
|
50
|
-
});
|
|
51
|
-
page.on('dialog', dialog => this._dialogShown(dialog));
|
|
52
|
-
page.on('download', download => {
|
|
53
|
-
void this._downloadStarted(download);
|
|
54
|
-
});
|
|
55
|
-
page.setDefaultNavigationTimeout(60000);
|
|
56
|
-
page.setDefaultTimeout(5000);
|
|
57
|
-
page[tabSymbol] = this;
|
|
58
|
-
}
|
|
59
|
-
static forPage(page) {
|
|
60
|
-
return page[tabSymbol];
|
|
61
|
-
}
|
|
62
|
-
modalStates() {
|
|
63
|
-
return this._modalStates;
|
|
64
|
-
}
|
|
65
|
-
setModalState(modalState) {
|
|
66
|
-
this._modalStates.push(modalState);
|
|
67
|
-
this.emit(exports.TabEvents.modalState, modalState);
|
|
68
|
-
}
|
|
69
|
-
clearModalState(modalState) {
|
|
70
|
-
this._modalStates = this._modalStates.filter(state => state !== modalState);
|
|
71
|
-
}
|
|
72
|
-
modalStatesMarkdown() {
|
|
73
|
-
return renderModalStates(this.context, this.modalStates());
|
|
74
|
-
}
|
|
75
|
-
_dialogShown(dialog) {
|
|
76
|
-
this.setModalState({
|
|
77
|
-
type: 'dialog',
|
|
78
|
-
description: `"${dialog.type()}" dialog with message "${dialog.message()}"`,
|
|
79
|
-
dialog,
|
|
80
|
-
});
|
|
81
|
-
}
|
|
82
|
-
async _downloadStarted(download) {
|
|
83
|
-
const entry = {
|
|
84
|
-
download,
|
|
85
|
-
finished: false,
|
|
86
|
-
outputFile: await this.context.outputFile(download.suggestedFilename())
|
|
87
|
-
};
|
|
88
|
-
this._downloads.push(entry);
|
|
89
|
-
await download.saveAs(entry.outputFile);
|
|
90
|
-
entry.finished = true;
|
|
91
|
-
}
|
|
92
|
-
_clearCollectedArtifacts() {
|
|
93
|
-
this._consoleMessages.length = 0;
|
|
94
|
-
this._recentConsoleMessages.length = 0;
|
|
95
|
-
this._requests.clear();
|
|
96
|
-
}
|
|
97
|
-
_handleConsoleMessage(message) {
|
|
98
|
-
this._consoleMessages.push(message);
|
|
99
|
-
this._recentConsoleMessages.push(message);
|
|
100
|
-
}
|
|
101
|
-
_onClose() {
|
|
102
|
-
this._clearCollectedArtifacts();
|
|
103
|
-
this._onPageClose(this);
|
|
104
|
-
}
|
|
105
|
-
async updateTitle() {
|
|
106
|
-
await this._raceAgainstModalStates(async () => {
|
|
107
|
-
this._lastTitle = await (0, utils_1.callOnPageNoTrace)(this.page, page => page.title());
|
|
108
|
-
});
|
|
109
|
-
}
|
|
110
|
-
lastTitle() {
|
|
111
|
-
return this._lastTitle;
|
|
112
|
-
}
|
|
113
|
-
isCurrentTab() {
|
|
114
|
-
return this === this.context.currentTab();
|
|
115
|
-
}
|
|
116
|
-
async waitForLoadState(state, options) {
|
|
117
|
-
await (0, utils_1.callOnPageNoTrace)(this.page, page => page.waitForLoadState(state, options).catch(log_1.logUnhandledError));
|
|
118
|
-
}
|
|
119
|
-
async navigate(url) {
|
|
120
|
-
this._clearCollectedArtifacts();
|
|
121
|
-
const downloadEvent = (0, utils_1.callOnPageNoTrace)(this.page, page => page.waitForEvent('download').catch(log_1.logUnhandledError));
|
|
122
|
-
try {
|
|
123
|
-
await this.page.goto(url, { waitUntil: 'domcontentloaded' });
|
|
124
|
-
}
|
|
125
|
-
catch (_e) {
|
|
126
|
-
const e = _e;
|
|
127
|
-
const mightBeDownload = e.message.includes('net::ERR_ABORTED') // chromium
|
|
128
|
-
|| e.message.includes('Download is starting'); // firefox + webkit
|
|
129
|
-
if (!mightBeDownload)
|
|
130
|
-
throw e;
|
|
131
|
-
// on chromium, the download event is fired *after* page.goto rejects, so we wait a lil bit
|
|
132
|
-
const download = await Promise.race([
|
|
133
|
-
downloadEvent,
|
|
134
|
-
new Promise(resolve => setTimeout(resolve, 3000)),
|
|
135
|
-
]);
|
|
136
|
-
if (!download)
|
|
137
|
-
throw e;
|
|
138
|
-
// Make sure other "download" listeners are notified first.
|
|
139
|
-
await new Promise(resolve => setTimeout(resolve, 500));
|
|
140
|
-
return;
|
|
141
|
-
}
|
|
142
|
-
// Cap load event to 5 seconds, the page is operational at this point.
|
|
143
|
-
await this.waitForLoadState('load', { timeout: 5000 });
|
|
144
|
-
}
|
|
145
|
-
consoleMessages() {
|
|
146
|
-
return this._consoleMessages;
|
|
147
|
-
}
|
|
148
|
-
requests() {
|
|
149
|
-
return this._requests;
|
|
150
|
-
}
|
|
151
|
-
async captureSnapshot() {
|
|
152
|
-
let tabSnapshot;
|
|
153
|
-
const modalStates = await this._raceAgainstModalStates(async () => {
|
|
154
|
-
const snapshot = await this.page._snapshotForAI();
|
|
155
|
-
tabSnapshot = {
|
|
156
|
-
url: this.page.url(),
|
|
157
|
-
title: await this.page.title(),
|
|
158
|
-
ariaSnapshot: snapshot,
|
|
159
|
-
modalStates: [],
|
|
160
|
-
consoleMessages: [],
|
|
161
|
-
downloads: this._downloads,
|
|
162
|
-
};
|
|
163
|
-
});
|
|
164
|
-
if (tabSnapshot) {
|
|
165
|
-
// Assign console message late so that we did not lose any to modal state.
|
|
166
|
-
tabSnapshot.consoleMessages = this._recentConsoleMessages;
|
|
167
|
-
this._recentConsoleMessages = [];
|
|
168
|
-
}
|
|
169
|
-
return tabSnapshot !== null && tabSnapshot !== void 0 ? tabSnapshot : {
|
|
170
|
-
url: this.page.url(),
|
|
171
|
-
title: '',
|
|
172
|
-
ariaSnapshot: '',
|
|
173
|
-
modalStates,
|
|
174
|
-
consoleMessages: [],
|
|
175
|
-
downloads: [],
|
|
176
|
-
};
|
|
177
|
-
}
|
|
178
|
-
_javaScriptBlocked() {
|
|
179
|
-
return this._modalStates.some(state => state.type === 'dialog');
|
|
180
|
-
}
|
|
181
|
-
async _raceAgainstModalStates(action) {
|
|
182
|
-
if (this.modalStates().length)
|
|
183
|
-
return this.modalStates();
|
|
184
|
-
const promise = new manualPromise_1.ManualPromise();
|
|
185
|
-
const listener = (modalState) => promise.resolve([modalState]);
|
|
186
|
-
this.once(exports.TabEvents.modalState, listener);
|
|
187
|
-
return await Promise.race([
|
|
188
|
-
action().then(() => {
|
|
189
|
-
this.off(exports.TabEvents.modalState, listener);
|
|
190
|
-
return [];
|
|
191
|
-
}),
|
|
192
|
-
promise,
|
|
193
|
-
]);
|
|
194
|
-
}
|
|
195
|
-
async waitForCompletion(callback) {
|
|
196
|
-
await this._raceAgainstModalStates(() => (0, utils_1.waitForCompletion)(this, callback));
|
|
197
|
-
}
|
|
198
|
-
async refLocator(params) {
|
|
199
|
-
return (await this.refLocators([params]))[0];
|
|
200
|
-
}
|
|
201
|
-
async refLocators(params) {
|
|
202
|
-
const snapshot = await this.page._snapshotForAI();
|
|
203
|
-
return params.map(param => {
|
|
204
|
-
if (!snapshot.includes(`[ref=${param.ref}]`))
|
|
205
|
-
throw new Error(`Ref ${param.ref} not found in the current page snapshot. Try capturing new snapshot.`);
|
|
206
|
-
return this.page.locator(`aria-ref=${param.ref}`).describe(param.element);
|
|
207
|
-
});
|
|
208
|
-
}
|
|
209
|
-
async waitForTimeout(time) {
|
|
210
|
-
if (this._javaScriptBlocked()) {
|
|
211
|
-
await new Promise(f => setTimeout(f, time));
|
|
212
|
-
return;
|
|
213
|
-
}
|
|
214
|
-
await (0, utils_1.callOnPageNoTrace)(this.page, page => {
|
|
215
|
-
return page.evaluate(() => new Promise(f => setTimeout(f, 1000)));
|
|
216
|
-
});
|
|
217
|
-
}
|
|
218
|
-
}
|
|
219
|
-
exports.Tab = Tab;
|
|
220
|
-
function messageToConsoleMessage(message) {
|
|
221
|
-
return {
|
|
222
|
-
type: message.type(),
|
|
223
|
-
text: message.text(),
|
|
224
|
-
toString: () => `[${message.type().toUpperCase()}] ${message.text()} @ ${message.location().url}:${message.location().lineNumber}`,
|
|
225
|
-
};
|
|
226
|
-
}
|
|
227
|
-
function pageErrorToConsoleMessage(errorOrValue) {
|
|
228
|
-
if (errorOrValue instanceof Error) {
|
|
229
|
-
return {
|
|
230
|
-
type: undefined,
|
|
231
|
-
text: errorOrValue.message,
|
|
232
|
-
toString: () => errorOrValue.stack || errorOrValue.message,
|
|
233
|
-
};
|
|
234
|
-
}
|
|
235
|
-
return {
|
|
236
|
-
type: undefined,
|
|
237
|
-
text: String(errorOrValue),
|
|
238
|
-
toString: () => String(errorOrValue),
|
|
239
|
-
};
|
|
240
|
-
}
|
|
241
|
-
function renderModalStates(context, modalStates) {
|
|
242
|
-
const result = ['### Modal state'];
|
|
243
|
-
if (modalStates.length === 0)
|
|
244
|
-
result.push('- There is no modal state present');
|
|
245
|
-
for (const state of modalStates) {
|
|
246
|
-
const tool = context.tools.filter(tool => 'clearsModalState' in tool).find(tool => tool.clearsModalState === state.type);
|
|
247
|
-
result.push(`- [${state.description}]: can be handled by the "${tool === null || tool === void 0 ? void 0 : tool.schema.name}" tool`);
|
|
248
|
-
}
|
|
249
|
-
return result;
|
|
250
|
-
}
|
|
251
|
-
const tabSymbol = Symbol('tabSymbol');
|
|
@@ -1,57 +0,0 @@
|
|
|
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 bundle_1 = require("../../sdk/bundle");
|
|
19
|
-
const tool_1 = require("./tool");
|
|
20
|
-
const close = (0, tool_1.defineTool)({
|
|
21
|
-
capability: 'core',
|
|
22
|
-
schema: {
|
|
23
|
-
name: 'browser_close',
|
|
24
|
-
title: 'Close browser',
|
|
25
|
-
description: 'Close the page',
|
|
26
|
-
inputSchema: bundle_1.z.object({}),
|
|
27
|
-
type: 'readOnly',
|
|
28
|
-
},
|
|
29
|
-
handle: async (context, params, response) => {
|
|
30
|
-
await context.closeBrowserContext();
|
|
31
|
-
response.setIncludeTabs();
|
|
32
|
-
response.addCode(`await page.close()`);
|
|
33
|
-
},
|
|
34
|
-
});
|
|
35
|
-
const resize = (0, tool_1.defineTabTool)({
|
|
36
|
-
capability: 'core',
|
|
37
|
-
schema: {
|
|
38
|
-
name: 'browser_resize',
|
|
39
|
-
title: 'Resize browser window',
|
|
40
|
-
description: 'Resize the browser window',
|
|
41
|
-
inputSchema: bundle_1.z.object({
|
|
42
|
-
width: bundle_1.z.number().describe('Width of the browser window'),
|
|
43
|
-
height: bundle_1.z.number().describe('Height of the browser window'),
|
|
44
|
-
}),
|
|
45
|
-
type: 'readOnly',
|
|
46
|
-
},
|
|
47
|
-
handle: async (tab, params, response) => {
|
|
48
|
-
response.addCode(`await page.setViewportSize({ width: ${params.width}, height: ${params.height} });`);
|
|
49
|
-
await tab.waitForCompletion(async () => {
|
|
50
|
-
await tab.page.setViewportSize({ width: params.width, height: params.height });
|
|
51
|
-
});
|
|
52
|
-
},
|
|
53
|
-
});
|
|
54
|
-
exports.default = [
|
|
55
|
-
close,
|
|
56
|
-
resize
|
|
57
|
-
];
|
|
@@ -1,35 +0,0 @@
|
|
|
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 bundle_1 = require("../../sdk/bundle");
|
|
19
|
-
const tool_1 = require("./tool");
|
|
20
|
-
const console = (0, tool_1.defineTabTool)({
|
|
21
|
-
capability: 'core',
|
|
22
|
-
schema: {
|
|
23
|
-
name: 'browser_console_messages',
|
|
24
|
-
title: 'Get console messages',
|
|
25
|
-
description: 'Returns all console messages',
|
|
26
|
-
inputSchema: bundle_1.z.object({}),
|
|
27
|
-
type: 'readOnly',
|
|
28
|
-
},
|
|
29
|
-
handle: async (tab, params, response) => {
|
|
30
|
-
tab.consoleMessages().map(message => response.addResult(message.toString()));
|
|
31
|
-
},
|
|
32
|
-
});
|
|
33
|
-
exports.default = [
|
|
34
|
-
console,
|
|
35
|
-
];
|
|
@@ -1,49 +0,0 @@
|
|
|
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 bundle_1 = require("../../sdk/bundle");
|
|
19
|
-
const tool_1 = require("./tool");
|
|
20
|
-
const handleDialog = (0, tool_1.defineTabTool)({
|
|
21
|
-
capability: 'core',
|
|
22
|
-
schema: {
|
|
23
|
-
name: 'browser_handle_dialog',
|
|
24
|
-
title: 'Handle a dialog',
|
|
25
|
-
description: 'Handle a dialog',
|
|
26
|
-
inputSchema: bundle_1.z.object({
|
|
27
|
-
accept: bundle_1.z.boolean().describe('Whether to accept the dialog.'),
|
|
28
|
-
promptText: bundle_1.z.string().optional().describe('The text of the prompt in case of a prompt dialog.'),
|
|
29
|
-
}),
|
|
30
|
-
type: 'destructive',
|
|
31
|
-
},
|
|
32
|
-
handle: async (tab, params, response) => {
|
|
33
|
-
response.setIncludeSnapshot();
|
|
34
|
-
const dialogState = tab.modalStates().find(state => state.type === 'dialog');
|
|
35
|
-
if (!dialogState)
|
|
36
|
-
throw new Error('No dialog visible');
|
|
37
|
-
tab.clearModalState(dialogState);
|
|
38
|
-
await tab.waitForCompletion(async () => {
|
|
39
|
-
if (params.accept)
|
|
40
|
-
await dialogState.dialog.accept(params.promptText);
|
|
41
|
-
else
|
|
42
|
-
await dialogState.dialog.dismiss();
|
|
43
|
-
});
|
|
44
|
-
},
|
|
45
|
-
clearsModalState: 'dialog',
|
|
46
|
-
});
|
|
47
|
-
exports.default = [
|
|
48
|
-
handleDialog,
|
|
49
|
-
];
|