mcp-browser-dev-tools 0.2.2 → 0.3.0
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 +17 -15
- package/package.json +8 -2
- package/src/action-changes.mjs +340 -0
- package/src/browser-adapter.mjs +40 -4
- package/src/cdp-client.mjs +923 -43
- package/src/config.mjs +9 -0
- package/src/firefox-bidi-client.mjs +757 -47
- package/src/json-schema.mjs +14 -1
- package/src/mcp-server.mjs +59 -8
- package/src/network-control.mjs +156 -0
- package/src/page-context.mjs +306 -2
- package/src/page-script.mjs +1175 -13
- package/src/run-steps.mjs +75 -23
- package/src/run-tabs.mjs +11 -16
- package/src/screenshot-output.mjs +92 -1
- package/src/session-events.mjs +57 -0
- package/src/tool-schemas.mjs +88 -83
- package/src/tool-sets.mjs +73 -0
- package/src/tools/action-tools.mjs +217 -77
- package/src/tools/browser-tools.mjs +12 -19
- package/src/tools/inspection-tools.mjs +88 -54
- package/src/tools/more-tools.mjs +108 -0
- package/src/tools/performance-tools.mjs +116 -0
- package/src/tools/state-tools.mjs +62 -16
- package/src/upload-files.mjs +57 -0
package/README.md
CHANGED
|
@@ -36,12 +36,13 @@ Then ask the agent for browser work, for example "open example.com and check the
|
|
|
36
36
|
|
|
37
37
|

|
|
38
38
|
|
|
39
|
-
Your agent gets
|
|
39
|
+
Your agent gets 44 browser tools over MCP: open or attach to tabs, click, type, drag, upload files, and navigate, read the console and network, block or mock requests, take screenshots, and save and restore sessions. At the center is `run_steps`: the agent sends a whole task, with branches and waits in it, and the server carries it out in the browser and replies once. `run_tabs` does the same in several tabs at the same time.
|
|
40
40
|
|
|
41
41
|
## Why This One
|
|
42
42
|
|
|
43
43
|
- **Fewer round trips.** Every model turn costs thinking time and tokens, so the agent does more with each call. In our benchmark, batching halved the turns Sonnet 5.5 needed to sign up through a form.
|
|
44
|
-
- **
|
|
44
|
+
- **Actions say what they did.** A click, a typed value, or a whole batch comes back with what changed on the page once it settles: controls that appeared, disappeared, or changed, new text such as an error or a toast, console errors, and after a navigation the new page's controls. The agent can confirm the result without another look at the page.
|
|
45
|
+
- **Many pages at once.** Research over several pages happens side by side: `run_tabs` opens them together and `read_text` brings back what they say. In our benchmark, reading five slow pages took Sonnet 5.5 8.5 s and 4 turns instead of 28 s and 6.
|
|
45
46
|
- **Calm on real pages.** Waits end on whichever outcome shows up, actions wait out a covering banner or a button that isn't enabled yet, and if a step fails the reply lists the page's controls, so the next try is an informed one.
|
|
46
47
|
- **Right where you work.** It attaches to the browser you already use, with its logins, tabs, and extensions, and speaks to Chrome, Edge, and Firefox with the same tools.
|
|
47
48
|
- **A debugger at hand.** Console messages with stack traces, network requests and HAR export, cookies and storage, and a one-call debug report.
|
|
@@ -91,7 +92,7 @@ A `run_steps` call that dismisses a cookie banner if one is showing, fills in an
|
|
|
91
92
|
}
|
|
92
93
|
```
|
|
93
94
|
|
|
94
|
-
The result has each step's outcome: which branch the `if` took, which `anyOf` alternative matched, and the message element with its text. Locators can be CSS, `text=`, `role=` with an accessible name, or `name=`. The full semantics of `run_steps`, `if`, `repeat`, and conditions are in [Tools](docs/tools.md#batched-steps).
|
|
95
|
+
The result has each step's outcome: which branch the `if` took, which `anyOf` alternative matched, and the message element with its text. Because the batch has actions, the result also has `changes`: what the batch changed on the page, as described in [Tools](docs/tools.md#what-an-action-changed). Locators can be CSS, `text=`, `role=` with an accessible name, or `name=`. The full semantics of `run_steps`, `if`, `repeat`, and conditions are in [Tools](docs/tools.md#batched-steps).
|
|
95
96
|
|
|
96
97
|
## Many Pages At Once
|
|
97
98
|
|
|
@@ -129,26 +130,27 @@ The result lists every tab in the order given, each with its steps' results: her
|
|
|
129
130
|
|
|
130
131
|
## Measured
|
|
131
132
|
|
|
132
|
-
|
|
133
|
+
Median of 3 runs on the current build, from the benchmark in [PERFORMANCE.md](PERFORMANCE.md). "One action per call" disallows `run_steps` and `run_tabs`.
|
|
133
134
|
|
|
134
|
-
| Task | Sonnet 5.5, one action per call | Sonnet 5.5 with
|
|
135
|
-
| ----------------------------------- | ------------------------------- |
|
|
136
|
-
| Sign up through a form | 10
|
|
137
|
-
| Pay, retrying declined attempts |
|
|
138
|
-
| Sign in, then change settings | 16
|
|
139
|
-
| Read five pages from a results list |
|
|
135
|
+
| Task | Sonnet 5.5, one action per call | Sonnet 5.5 with batching | Opus 5.5 with batching |
|
|
136
|
+
| ----------------------------------- | ------------------------------- | ------------------------ | ---------------------- |
|
|
137
|
+
| Sign up through a form | 15.0 s · 9 turns | 10.2 s · 4 turns | 11.5 s · 4 turns |
|
|
138
|
+
| Pay, retrying declined attempts | 20.6 s · 8 turns | 19.1 s · 8 turns | 18.9 s · 9 turns |
|
|
139
|
+
| Sign in, then change settings | 16.2 s · 11 turns | 17.5 s · 7 turns | 18.2 s · 8 turns |
|
|
140
|
+
| Read five pages from a results list | 27.7 s · 6 turns | 8.5 s · 4 turns | 11.8 s · 5 turns |
|
|
140
141
|
|
|
141
|
-
|
|
142
|
+
Every form run started with `get_snapshot` and acted by ref, without being told to. Each payment click waits for the page to settle and reports the decline or the receipt, so the agents could retry from each reply. The last row uses `run_tabs`. Seven models (Claude Fable 5.1, Opus 5.5, Sonnet 5.5, and Haiku 4.5; GPT-6-Astra, GPT-6.1 Sol, and GPT-6-Luna) ran the same four tasks with the default tool set: all 96 runs passed, and PERFORMANCE.md has the time, cost, and turns for each, the charts, and how the runs were set up.
|
|
142
143
|
|
|
143
144
|
## Tools
|
|
144
145
|
|
|
145
|
-
|
|
146
|
+
44 tools, described in [docs/tools.md](docs/tools.md). The server lists the core set by default, since every model turn pays for each tool listed, and `more_tools` lists, describes, and calls the rest; `MCP_BROWSER_TOOLS=all`, or a list of groups such as `network,state`, lists them too ([Tool Sets](docs/tools.md#tool-sets)).
|
|
146
147
|
|
|
147
148
|
- **Browser and tabs:** `browser_status`, `ensure_browser`, `launch_browser`, `list_tabs`, `new_tab`, `close_tab`, `attach_tab`, `detach_tab`, `list_sessions`
|
|
148
|
-
- **Act:** `navigate`, `reload`, `click`, `hover`, `type`, `select`, `press_key`, `scroll`, `set_viewport`
|
|
149
|
+
- **Act:** `navigate`, `reload`, `click`, `hover`, `drag`, `type`, `select`, `press_key`, `upload_file`, `scroll`, `set_viewport`
|
|
149
150
|
- **Wait and batch:** `wait_for`, `run_steps`, `run_tabs`
|
|
150
|
-
- **Inspect:** `get_page_state`, `get_document`, `inspect_element`, `read_text`, `take_screenshot`, `evaluate_js`
|
|
151
|
-
- **Console and network:** `get_console_messages`, `get_network_requests`, `get_events`, `get_har`
|
|
151
|
+
- **Inspect:** `get_page_state`, `get_snapshot`, `get_document`, `inspect_element`, `read_text`, `take_screenshot`, `evaluate_js`
|
|
152
|
+
- **Console and network:** `get_console_messages`, `get_network_requests`, `get_events`, `get_har`, `set_network`
|
|
153
|
+
- **Performance:** `get_performance`, `record_trace`
|
|
152
154
|
- **Session state:** `get_cookies`, `get_storage`, `capture_session_snapshot`, `restore_session_snapshot`, `compare_page_state`, `compare_selector`, `capture_debug_report`
|
|
153
155
|
|
|
154
156
|
## Configuration
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mcp-browser-dev-tools",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "Browser MCP server for AI agents: batch actions, waits, and checks into one call on Chrome, Edge, or Firefox (CDP and WebDriver BiDi)",
|
|
5
5
|
"author": "Hinaser <rindo.hinase@gmail.com>",
|
|
6
6
|
"license": "MIT",
|
|
@@ -45,6 +45,7 @@
|
|
|
45
45
|
"src/tcp-relay.mjs",
|
|
46
46
|
"src/top-level-await.mjs",
|
|
47
47
|
"src/wait-for.mjs",
|
|
48
|
+
"src/action-changes.mjs",
|
|
48
49
|
"src/action-retry.mjs",
|
|
49
50
|
"src/browser-requests.mjs",
|
|
50
51
|
"src/json-schema.mjs",
|
|
@@ -53,9 +54,14 @@
|
|
|
53
54
|
"src/screenshot-output.mjs",
|
|
54
55
|
"src/tool-results.mjs",
|
|
55
56
|
"src/tool-schemas.mjs",
|
|
57
|
+
"src/tool-sets.mjs",
|
|
58
|
+
"src/upload-files.mjs",
|
|
59
|
+
"src/network-control.mjs",
|
|
56
60
|
"src/tools/action-tools.mjs",
|
|
57
61
|
"src/tools/browser-tools.mjs",
|
|
58
62
|
"src/tools/inspection-tools.mjs",
|
|
63
|
+
"src/tools/more-tools.mjs",
|
|
64
|
+
"src/tools/performance-tools.mjs",
|
|
59
65
|
"src/tools/state-tools.mjs",
|
|
60
66
|
"README.md"
|
|
61
67
|
],
|
|
@@ -91,7 +97,7 @@
|
|
|
91
97
|
"prepare": "husky",
|
|
92
98
|
"pack:check": "node scripts/pack-check.mjs",
|
|
93
99
|
"bench": "node bench/run.mjs",
|
|
94
|
-
"bench:
|
|
100
|
+
"bench:report": "node bench/report.mjs",
|
|
95
101
|
"prepublishOnly": "node --run check",
|
|
96
102
|
"test": "node --test"
|
|
97
103
|
},
|
|
@@ -0,0 +1,340 @@
|
|
|
1
|
+
import { prefixFrameReport } from "./page-context.mjs";
|
|
2
|
+
|
|
3
|
+
// Actions (click, type, select, press_key, hover, and run_steps batches that
|
|
4
|
+
// contain them) report what they changed, so an agent can confirm the result
|
|
5
|
+
// without reading the page again. The page records a baseline of its visible
|
|
6
|
+
// headings and controls before the action; afterwards the server waits for
|
|
7
|
+
// the page to settle and asks for the difference.
|
|
8
|
+
|
|
9
|
+
// The page has settled when its DOM has been quiet this long, and the
|
|
10
|
+
// requests and short timers the action started have finished.
|
|
11
|
+
export const CHANGE_QUIET_MS = 150;
|
|
12
|
+
// Most time to wait for that on the same document.
|
|
13
|
+
export const CHANGE_SETTLE_MS = 2000;
|
|
14
|
+
// Most time to wait while a navigation the action started loads.
|
|
15
|
+
export const CHANGE_NAVIGATION_MS = 10_000;
|
|
16
|
+
const CHANGE_POLL_MS = 50;
|
|
17
|
+
const NEW_PAGE_NODES = 40;
|
|
18
|
+
const EVENT_SCAN_LIMIT = 1000;
|
|
19
|
+
const MESSAGE_LIMIT = 5;
|
|
20
|
+
const MESSAGE_CHARS = 200;
|
|
21
|
+
export const CHANGE_REPORTING_TOOLS = new Set([
|
|
22
|
+
"click",
|
|
23
|
+
"drag",
|
|
24
|
+
"hover",
|
|
25
|
+
"type",
|
|
26
|
+
"select",
|
|
27
|
+
"press_key",
|
|
28
|
+
"upload_file",
|
|
29
|
+
]);
|
|
30
|
+
|
|
31
|
+
function sleep(ms) {
|
|
32
|
+
return new Promise((resolve) => {
|
|
33
|
+
setTimeout(resolve, ms);
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function clip(text, limit = MESSAGE_CHARS) {
|
|
38
|
+
const value = String(text ?? "")
|
|
39
|
+
.replace(/\s+/g, " ")
|
|
40
|
+
.trim();
|
|
41
|
+
return value.length > limit ? `${value.slice(0, limit)}...` : value;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function since(events, startedAt) {
|
|
45
|
+
return events.filter((event) => (event?.capturedAt ?? "") >= startedAt);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// The unfinished requests the action started, from the session's in-flight
|
|
49
|
+
// list, which holds only the http(s) document, fetch, and XHR requests a
|
|
50
|
+
// page's result depends on. A pending document request means a navigation
|
|
51
|
+
// is under way.
|
|
52
|
+
export function settleRequests(requests, startedAt) {
|
|
53
|
+
return requests
|
|
54
|
+
.filter((request) => (request.startedAt ?? "") >= startedAt)
|
|
55
|
+
.map((request) => ({
|
|
56
|
+
url: request.url,
|
|
57
|
+
document:
|
|
58
|
+
String(request.resourceType ?? "").toLowerCase() === "document" ||
|
|
59
|
+
Boolean(request.navigation),
|
|
60
|
+
}));
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function errorText(event) {
|
|
64
|
+
if (event.kind === "exception") {
|
|
65
|
+
const description =
|
|
66
|
+
typeof event.exception === "string" ? event.exception : null;
|
|
67
|
+
return description ? description.split("\n")[0] : event.text;
|
|
68
|
+
}
|
|
69
|
+
return event.text;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// Repeats (a reconnecting EventSource, a retried request) appear once.
|
|
73
|
+
function capMessages(messages) {
|
|
74
|
+
const unique = Array.from(new Set(messages.map((message) => clip(message))));
|
|
75
|
+
return {
|
|
76
|
+
items: unique.slice(0, MESSAGE_LIMIT),
|
|
77
|
+
more: Math.max(0, unique.length - MESSAGE_LIMIT),
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// A missing favicon is logged as an error on most pages a navigation opens.
|
|
82
|
+
function isFaviconError(event) {
|
|
83
|
+
return /\/favicon\.ico(?:[?#]|$)/i.test(event.url ?? "");
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// Console errors, uncaught exceptions, and dialogs since startedAt.
|
|
87
|
+
export function eventChanges(events, startedAt) {
|
|
88
|
+
const recent = since(events, startedAt);
|
|
89
|
+
const errors = recent
|
|
90
|
+
.filter(
|
|
91
|
+
(event) =>
|
|
92
|
+
event.kind === "exception" ||
|
|
93
|
+
((event.kind === "console" || event.kind === "log") &&
|
|
94
|
+
event.level === "error" &&
|
|
95
|
+
!isFaviconError(event)),
|
|
96
|
+
)
|
|
97
|
+
.map(errorText);
|
|
98
|
+
const dialogs = recent
|
|
99
|
+
.filter((event) => event.kind === "dialog" && event.phase === "opened")
|
|
100
|
+
.map((event) => `${event.dialogType ?? "dialog"}: ${event.message ?? ""}`);
|
|
101
|
+
return { errors: capMessages(errors), dialogs: capMessages(dialogs) };
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function addList(changes, key, list) {
|
|
105
|
+
if (list?.items?.length > 0 || list?.lines?.length > 0) {
|
|
106
|
+
changes[key] = list.items ?? list.lines;
|
|
107
|
+
}
|
|
108
|
+
if (list?.more > 0) {
|
|
109
|
+
changes[`${key}More`] = list.more;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// Shapes the page's report, the events, and anything still loading into the
|
|
114
|
+
// changes field. Lists that are empty are left out; { none: true } says the
|
|
115
|
+
// action changed nothing the report covers.
|
|
116
|
+
export function formatChanges({ report, events, pending }) {
|
|
117
|
+
const changes = {};
|
|
118
|
+
if (report?.newDocument) {
|
|
119
|
+
changes.navigated = true;
|
|
120
|
+
changes.url = report.url;
|
|
121
|
+
changes.title = report.title;
|
|
122
|
+
changes.nodes = report.nodes;
|
|
123
|
+
if (report.more > 0) {
|
|
124
|
+
changes.nodesMore = report.more;
|
|
125
|
+
}
|
|
126
|
+
} else if (report && !report.lost) {
|
|
127
|
+
if (report.urlChanged) {
|
|
128
|
+
changes.url = report.url;
|
|
129
|
+
}
|
|
130
|
+
if (report.titleChanged) {
|
|
131
|
+
changes.title = report.title;
|
|
132
|
+
}
|
|
133
|
+
addList(changes, "added", report.added);
|
|
134
|
+
addList(changes, "removed", report.removed);
|
|
135
|
+
addList(changes, "updated", report.updated);
|
|
136
|
+
addList(changes, "text", report.text);
|
|
137
|
+
} else {
|
|
138
|
+
changes.unavailable = true;
|
|
139
|
+
}
|
|
140
|
+
addList(changes, "consoleErrors", events.errors);
|
|
141
|
+
addList(changes, "dialogs", events.dialogs);
|
|
142
|
+
if (pending.length > 0) {
|
|
143
|
+
changes.stillLoading = pending.slice(0, 3).map((request) => request.url);
|
|
144
|
+
}
|
|
145
|
+
return Object.keys(changes).length > 0 ? changes : { none: true };
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
export class ChangeTracker {
|
|
149
|
+
constructor(browserAdapter, options = {}) {
|
|
150
|
+
this.browserAdapter = browserAdapter;
|
|
151
|
+
// The page drops baselines this server no longer tracks (an action
|
|
152
|
+
// that ended without a report) when the next one is taken, so the
|
|
153
|
+
// tracker names itself and its baselines in progress for each session.
|
|
154
|
+
this.owner = crypto.randomUUID();
|
|
155
|
+
this.active = new Map();
|
|
156
|
+
this.quietMs = options.quietMs ?? CHANGE_QUIET_MS;
|
|
157
|
+
this.settleMs = options.settleMs ?? CHANGE_SETTLE_MS;
|
|
158
|
+
this.navigationMs = options.navigationMs ?? CHANGE_NAVIGATION_MS;
|
|
159
|
+
this.pollMs = options.pollMs ?? CHANGE_POLL_MS;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
activeIds(sessionId) {
|
|
163
|
+
if (!this.active.has(sessionId)) {
|
|
164
|
+
this.active.set(sessionId, new Set());
|
|
165
|
+
}
|
|
166
|
+
return this.active.get(sessionId);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
finish(baseline) {
|
|
170
|
+
const ids = this.active.get(baseline.sessionId);
|
|
171
|
+
ids?.delete(baseline.changeId);
|
|
172
|
+
if (ids?.size === 0) {
|
|
173
|
+
this.active.delete(baseline.sessionId);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
// Records the baseline. Returns null when the page cannot take one (it is
|
|
178
|
+
// mid-navigation, or blocks scripts), and the action then reports nothing.
|
|
179
|
+
// With frameKey, the changes are those inside that frame (an action on
|
|
180
|
+
// an element named by a frame ref).
|
|
181
|
+
async begin(sessionId, frameKey = null) {
|
|
182
|
+
const changeId = crypto.randomUUID();
|
|
183
|
+
const startedAt = new Date().toISOString();
|
|
184
|
+
const active = this.activeIds(sessionId);
|
|
185
|
+
active.add(changeId);
|
|
186
|
+
try {
|
|
187
|
+
const { documentId } = await this.browserAdapter.trackChanges(
|
|
188
|
+
sessionId,
|
|
189
|
+
"baseline",
|
|
190
|
+
{
|
|
191
|
+
changeId,
|
|
192
|
+
owner: this.owner,
|
|
193
|
+
active: Array.from(active),
|
|
194
|
+
...(frameKey ? { frameKey } : {}),
|
|
195
|
+
},
|
|
196
|
+
);
|
|
197
|
+
return { sessionId, changeId, documentId, startedAt, frameKey };
|
|
198
|
+
} catch {
|
|
199
|
+
this.finish({ sessionId, changeId });
|
|
200
|
+
return null;
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
// Ends tracking without a report, for an action that failed.
|
|
205
|
+
async stop(baseline) {
|
|
206
|
+
const { sessionId, changeId, documentId, frameKey } = baseline;
|
|
207
|
+
this.finish(baseline);
|
|
208
|
+
await this.browserAdapter
|
|
209
|
+
.trackChanges(sessionId, "stop", {
|
|
210
|
+
changeId,
|
|
211
|
+
documentId,
|
|
212
|
+
...(frameKey ? { frameKey } : {}),
|
|
213
|
+
})
|
|
214
|
+
.catch(() => {});
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
async pending(sessionId, startedAt) {
|
|
218
|
+
try {
|
|
219
|
+
return settleRequests(
|
|
220
|
+
(await this.browserAdapter.pendingRequests(sessionId)) ?? [],
|
|
221
|
+
startedAt,
|
|
222
|
+
);
|
|
223
|
+
} catch {
|
|
224
|
+
return [];
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
async events(sessionId) {
|
|
229
|
+
try {
|
|
230
|
+
return (
|
|
231
|
+
(await this.browserAdapter.getEvents(sessionId, EVENT_SCAN_LIMIT)) ?? []
|
|
232
|
+
);
|
|
233
|
+
} catch {
|
|
234
|
+
return [];
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
// Waits until the DOM is quiet and the action's requests are done, or a
|
|
239
|
+
// navigation it started has loaded, within the settle and navigation
|
|
240
|
+
// limits; then asks the page for the report.
|
|
241
|
+
async settle(baseline) {
|
|
242
|
+
const { sessionId, changeId, documentId, startedAt, frameKey } = baseline;
|
|
243
|
+
const ids = {
|
|
244
|
+
changeId,
|
|
245
|
+
documentId,
|
|
246
|
+
owner: this.owner,
|
|
247
|
+
...(frameKey ? { frameKey } : {}),
|
|
248
|
+
};
|
|
249
|
+
const actionEndedAt = Date.now();
|
|
250
|
+
let pending;
|
|
251
|
+
let timedOut = false;
|
|
252
|
+
while (true) {
|
|
253
|
+
const elapsed = Date.now() - actionEndedAt;
|
|
254
|
+
pending = await this.pending(sessionId, startedAt);
|
|
255
|
+
let status;
|
|
256
|
+
try {
|
|
257
|
+
status = await this.browserAdapter.trackChanges(
|
|
258
|
+
sessionId,
|
|
259
|
+
"status",
|
|
260
|
+
ids,
|
|
261
|
+
);
|
|
262
|
+
} catch {
|
|
263
|
+
// The old document is going away and the new one is not ready.
|
|
264
|
+
status = { document: "unavailable" };
|
|
265
|
+
}
|
|
266
|
+
if (status.lost) {
|
|
267
|
+
break;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
// A new document has settled like the old one would: its DOM quiet
|
|
271
|
+
// and its own fetches done.
|
|
272
|
+
const loadingDocument = pending.some((request) => request.document);
|
|
273
|
+
const quiet =
|
|
274
|
+
elapsed >= this.quietMs &&
|
|
275
|
+
status.quietMs >= this.quietMs &&
|
|
276
|
+
!(status.timers > 0) &&
|
|
277
|
+
pending.length === 0;
|
|
278
|
+
if (
|
|
279
|
+
status.document === "new" &&
|
|
280
|
+
status.readyState !== "loading" &&
|
|
281
|
+
quiet
|
|
282
|
+
) {
|
|
283
|
+
break;
|
|
284
|
+
}
|
|
285
|
+
const navigating = status.document !== "same" || loadingDocument;
|
|
286
|
+
if (!navigating && quiet) {
|
|
287
|
+
break;
|
|
288
|
+
}
|
|
289
|
+
if (elapsed >= (navigating ? this.navigationMs : this.settleMs)) {
|
|
290
|
+
timedOut = true;
|
|
291
|
+
break;
|
|
292
|
+
}
|
|
293
|
+
await sleep(this.pollMs);
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
let report;
|
|
297
|
+
try {
|
|
298
|
+
report = await this.browserAdapter.trackChanges(sessionId, "report", {
|
|
299
|
+
...ids,
|
|
300
|
+
limit: NEW_PAGE_NODES,
|
|
301
|
+
});
|
|
302
|
+
} catch {
|
|
303
|
+
report = null;
|
|
304
|
+
} finally {
|
|
305
|
+
this.finish(baseline);
|
|
306
|
+
}
|
|
307
|
+
if (frameKey) {
|
|
308
|
+
report = prefixFrameReport(report, frameKey);
|
|
309
|
+
}
|
|
310
|
+
return formatChanges({
|
|
311
|
+
report,
|
|
312
|
+
events: eventChanges(await this.events(sessionId), startedAt),
|
|
313
|
+
pending: timedOut ? pending : [],
|
|
314
|
+
});
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
// Runs action between a baseline and a report, and adds the report to its
|
|
318
|
+
// result as changes. An action that throws, or whose element was not
|
|
319
|
+
// found, ends tracking and returns as it would without it.
|
|
320
|
+
async around(sessionId, action, frameKey = null) {
|
|
321
|
+
const baseline = await this.begin(sessionId, frameKey);
|
|
322
|
+
let result;
|
|
323
|
+
try {
|
|
324
|
+
result = await action();
|
|
325
|
+
} catch (error) {
|
|
326
|
+
if (baseline) {
|
|
327
|
+
await this.stop(baseline);
|
|
328
|
+
}
|
|
329
|
+
throw error;
|
|
330
|
+
}
|
|
331
|
+
if (!baseline) {
|
|
332
|
+
return result;
|
|
333
|
+
}
|
|
334
|
+
if (!result || typeof result !== "object" || result.found === false) {
|
|
335
|
+
await this.stop(baseline);
|
|
336
|
+
return result;
|
|
337
|
+
}
|
|
338
|
+
return { ...result, changes: await this.settle(baseline) };
|
|
339
|
+
}
|
|
340
|
+
}
|
package/src/browser-adapter.mjs
CHANGED
|
@@ -256,12 +256,36 @@ export class MultiBrowserAdapter {
|
|
|
256
256
|
return this.delegateSession(sessionId, "reload", [options]);
|
|
257
257
|
}
|
|
258
258
|
|
|
259
|
-
click(sessionId,
|
|
260
|
-
return this.delegateSession(sessionId, "click", [
|
|
259
|
+
click(sessionId, target, options) {
|
|
260
|
+
return this.delegateSession(sessionId, "click", [target, options]);
|
|
261
261
|
}
|
|
262
262
|
|
|
263
|
-
hover(sessionId,
|
|
264
|
-
return this.delegateSession(sessionId, "hover", [
|
|
263
|
+
hover(sessionId, target) {
|
|
264
|
+
return this.delegateSession(sessionId, "hover", [target]);
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
drag(sessionId, from, to, options) {
|
|
268
|
+
return this.delegateSession(sessionId, "drag", [from, to, options]);
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
uploadFiles(sessionId, selector, files) {
|
|
272
|
+
return this.delegateSession(sessionId, "uploadFiles", [selector, files]);
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
setNetwork(sessionId, options) {
|
|
276
|
+
return this.delegateSession(sessionId, "setNetwork", [options]);
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
getPerformance(sessionId) {
|
|
280
|
+
return this.delegateSession(sessionId, "getPerformance");
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
startTrace(sessionId, options) {
|
|
284
|
+
return this.delegateSession(sessionId, "startTrace", [options]);
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
stopTrace(sessionId) {
|
|
288
|
+
return this.delegateSession(sessionId, "stopTrace");
|
|
265
289
|
}
|
|
266
290
|
|
|
267
291
|
type(sessionId, selector, text, options) {
|
|
@@ -308,6 +332,14 @@ export class MultiBrowserAdapter {
|
|
|
308
332
|
return this.delegateSession(sessionId, "snapshotControls");
|
|
309
333
|
}
|
|
310
334
|
|
|
335
|
+
snapshotPage(sessionId, options) {
|
|
336
|
+
return this.delegateSession(sessionId, "snapshotPage", [options]);
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
trackChanges(sessionId, phase, options) {
|
|
340
|
+
return this.delegateSession(sessionId, "trackChanges", [phase, options]);
|
|
341
|
+
}
|
|
342
|
+
|
|
311
343
|
inspectElement(sessionId, selector, options) {
|
|
312
344
|
return this.delegateSession(sessionId, "inspectElement", [
|
|
313
345
|
selector,
|
|
@@ -323,6 +355,10 @@ export class MultiBrowserAdapter {
|
|
|
323
355
|
return this.delegateSession(sessionId, "getEvents", [limit]);
|
|
324
356
|
}
|
|
325
357
|
|
|
358
|
+
pendingRequests(sessionId) {
|
|
359
|
+
return this.delegateSession(sessionId, "pendingRequests");
|
|
360
|
+
}
|
|
361
|
+
|
|
326
362
|
async closeAll() {
|
|
327
363
|
await Promise.all(
|
|
328
364
|
Object.values(this.adapters).map((adapter) => adapter.closeAll()),
|