mcp-browser-dev-tools 0.0.1-beta.1 → 0.0.1-beta.10
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 +234 -13
- package/package.json +4 -1
- package/src/browser-adapter.mjs +275 -0
- package/src/browser-launcher.mjs +35 -0
- package/src/cdp-client.mjs +524 -85
- package/src/cli.mjs +212 -9
- package/src/config.mjs +24 -9
- package/src/doctor.mjs +25 -5
- package/src/firefox-bidi-client.mjs +509 -100
- package/src/index.mjs +13 -1
- package/src/json-rpc-stdio.mjs +130 -21
- package/src/mcp-server.mjs +360 -5
- package/src/page-context.mjs +734 -0
- package/src/session-events.mjs +16 -2
- package/src/tcp-relay.mjs +194 -0
package/src/cdp-client.mjs
CHANGED
|
@@ -2,6 +2,7 @@ import {
|
|
|
2
2
|
filterConsoleMessages,
|
|
3
3
|
summarizeNetworkRequests,
|
|
4
4
|
} from "./session-events.mjs";
|
|
5
|
+
import { buildPageContextExpression } from "./page-context.mjs";
|
|
5
6
|
|
|
6
7
|
function toErrorMessage(error) {
|
|
7
8
|
return error instanceof Error ? error.message : String(error);
|
|
@@ -18,6 +19,10 @@ function normalizeTarget(target) {
|
|
|
18
19
|
};
|
|
19
20
|
}
|
|
20
21
|
|
|
22
|
+
function cdpBrowserFamily(config) {
|
|
23
|
+
return config.browserFamily === "edge" ? "edge" : "chromium";
|
|
24
|
+
}
|
|
25
|
+
|
|
21
26
|
function summarizeRemoteObject(object) {
|
|
22
27
|
if (!object) {
|
|
23
28
|
return null;
|
|
@@ -42,26 +47,100 @@ function summarizeRemoteObject(object) {
|
|
|
42
47
|
};
|
|
43
48
|
}
|
|
44
49
|
|
|
45
|
-
function
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
50
|
+
function summarizeCallFrames(callFrames = []) {
|
|
51
|
+
return callFrames.map((frame) => ({
|
|
52
|
+
functionName: frame.functionName || null,
|
|
53
|
+
url: frame.url || null,
|
|
54
|
+
lineNumber: frame.lineNumber ?? null,
|
|
55
|
+
columnNumber: frame.columnNumber ?? null,
|
|
56
|
+
}));
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function firstCallFrame(stackTrace) {
|
|
60
|
+
const frame = stackTrace?.callFrames?.[0];
|
|
61
|
+
if (!frame) {
|
|
62
|
+
return null;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
return {
|
|
66
|
+
functionName: frame.functionName || null,
|
|
67
|
+
url: frame.url || null,
|
|
68
|
+
lineNumber: frame.lineNumber ?? null,
|
|
69
|
+
columnNumber: frame.columnNumber ?? null,
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function normalizeConsoleText(values = []) {
|
|
74
|
+
const text = values
|
|
75
|
+
.filter((value) => value !== null && value !== undefined)
|
|
76
|
+
.map((value) =>
|
|
77
|
+
typeof value === "string" ? value : JSON.stringify(value, null, 2),
|
|
78
|
+
)
|
|
79
|
+
.join(" ");
|
|
80
|
+
|
|
81
|
+
return text || null;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function screenshotMimeType(format) {
|
|
85
|
+
if (format === "jpeg") {
|
|
86
|
+
return "image/jpeg";
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
if (format === "webp") {
|
|
90
|
+
return "image/webp";
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
return "image/png";
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function formatScreenshotResult(data, format, extras = {}) {
|
|
97
|
+
return {
|
|
98
|
+
format,
|
|
99
|
+
mimeType: screenshotMimeType(format),
|
|
100
|
+
encoding: "base64",
|
|
101
|
+
byteLength: Buffer.byteLength(data, "base64"),
|
|
102
|
+
data,
|
|
103
|
+
...extras,
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function normalizeWaitUntil(value) {
|
|
108
|
+
if (value === "none" || value === "interactive") {
|
|
109
|
+
return value;
|
|
49
110
|
}
|
|
50
111
|
|
|
51
|
-
return
|
|
112
|
+
return "complete";
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function cdpLifecycleEventFor(waitUntil) {
|
|
116
|
+
if (waitUntil === "interactive") {
|
|
117
|
+
return "Page.domContentEventFired";
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
if (waitUntil === "none") {
|
|
121
|
+
return null;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
return "Page.loadEventFired";
|
|
52
125
|
}
|
|
53
126
|
|
|
54
127
|
function normalizeEvent(method, params) {
|
|
55
128
|
if (method === "Runtime.consoleAPICalled") {
|
|
129
|
+
const values = (params.args || []).map(summarizeRemoteObject);
|
|
130
|
+
const source = firstCallFrame(params.stackTrace);
|
|
56
131
|
return {
|
|
57
132
|
kind: "console",
|
|
58
133
|
level: params.type,
|
|
59
134
|
timestamp: params.timestamp,
|
|
60
|
-
|
|
135
|
+
text: normalizeConsoleText(values),
|
|
136
|
+
values,
|
|
137
|
+
source,
|
|
138
|
+
stackTrace: summarizeCallFrames(params.stackTrace?.callFrames),
|
|
61
139
|
};
|
|
62
140
|
}
|
|
63
141
|
|
|
64
142
|
if (method === "Runtime.exceptionThrown") {
|
|
143
|
+
const exception = params.exceptionDetails?.exception ?? null;
|
|
65
144
|
return {
|
|
66
145
|
kind: "exception",
|
|
67
146
|
timestamp: params.timestamp,
|
|
@@ -69,16 +148,28 @@ function normalizeEvent(method, params) {
|
|
|
69
148
|
url: params.exceptionDetails?.url,
|
|
70
149
|
lineNumber: params.exceptionDetails?.lineNumber,
|
|
71
150
|
columnNumber: params.exceptionDetails?.columnNumber,
|
|
151
|
+
stackTrace: summarizeCallFrames(
|
|
152
|
+
params.exceptionDetails?.stackTrace?.callFrames,
|
|
153
|
+
),
|
|
154
|
+
exception:
|
|
155
|
+
exception && typeof exception === "object"
|
|
156
|
+
? summarizeRemoteObject(exception)
|
|
157
|
+
: null,
|
|
72
158
|
};
|
|
73
159
|
}
|
|
74
160
|
|
|
75
161
|
if (method === "Log.entryAdded") {
|
|
162
|
+
const source = firstCallFrame(params.entry?.stackTrace);
|
|
76
163
|
return {
|
|
77
164
|
kind: "log",
|
|
78
165
|
level: params.entry?.level,
|
|
79
166
|
text: params.entry?.text,
|
|
80
167
|
url: params.entry?.url,
|
|
81
168
|
timestamp: params.entry?.timestamp,
|
|
169
|
+
lineNumber: source?.lineNumber ?? null,
|
|
170
|
+
columnNumber: source?.columnNumber ?? null,
|
|
171
|
+
source,
|
|
172
|
+
stackTrace: summarizeCallFrames(params.entry?.stackTrace?.callFrames),
|
|
82
173
|
};
|
|
83
174
|
}
|
|
84
175
|
|
|
@@ -92,6 +183,7 @@ function normalizeEvent(method, params) {
|
|
|
92
183
|
method: params.request?.method,
|
|
93
184
|
url: params.request?.url,
|
|
94
185
|
resourceType: params.type,
|
|
186
|
+
source: "protocol",
|
|
95
187
|
};
|
|
96
188
|
}
|
|
97
189
|
|
|
@@ -107,6 +199,7 @@ function normalizeEvent(method, params) {
|
|
|
107
199
|
status: params.response?.status,
|
|
108
200
|
statusText: params.response?.statusText,
|
|
109
201
|
mimeType: params.response?.mimeType,
|
|
202
|
+
source: "protocol",
|
|
110
203
|
};
|
|
111
204
|
}
|
|
112
205
|
|
|
@@ -118,6 +211,8 @@ function normalizeEvent(method, params) {
|
|
|
118
211
|
failed: false,
|
|
119
212
|
timestamp: params.timestamp,
|
|
120
213
|
requestId: params.requestId,
|
|
214
|
+
encodedBodySize: params.encodedDataLength ?? null,
|
|
215
|
+
source: "protocol",
|
|
121
216
|
};
|
|
122
217
|
}
|
|
123
218
|
|
|
@@ -131,6 +226,33 @@ function normalizeEvent(method, params) {
|
|
|
131
226
|
requestId: params.requestId,
|
|
132
227
|
errorText: params.errorText,
|
|
133
228
|
canceled: params.canceled ?? false,
|
|
229
|
+
source: "protocol",
|
|
230
|
+
};
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
if (
|
|
234
|
+
method === "Page.frameNavigated" ||
|
|
235
|
+
method === "Page.navigatedWithinDocument"
|
|
236
|
+
) {
|
|
237
|
+
return {
|
|
238
|
+
kind: "page",
|
|
239
|
+
phase: "navigated",
|
|
240
|
+
timestamp: new Date().toISOString(),
|
|
241
|
+
url: params.frame?.url ?? params.url ?? null,
|
|
242
|
+
frameId: params.frame?.id ?? params.frameId ?? null,
|
|
243
|
+
loaderId: params.frame?.loaderId ?? params.loaderId ?? null,
|
|
244
|
+
};
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
if (
|
|
248
|
+
method === "Page.loadEventFired" ||
|
|
249
|
+
method === "Page.domContentEventFired" ||
|
|
250
|
+
method === "Page.frameStoppedLoading"
|
|
251
|
+
) {
|
|
252
|
+
return {
|
|
253
|
+
kind: "page",
|
|
254
|
+
phase: method,
|
|
255
|
+
timestamp: params.timestamp ?? new Date().toISOString(),
|
|
134
256
|
};
|
|
135
257
|
}
|
|
136
258
|
|
|
@@ -185,6 +307,10 @@ export class CdpSession {
|
|
|
185
307
|
this.connectedAt = new Date().toISOString();
|
|
186
308
|
this.websocket = null;
|
|
187
309
|
this.closed = false;
|
|
310
|
+
this.eventWaiters = new Set();
|
|
311
|
+
this.lastNavigationAt = null;
|
|
312
|
+
this.lastReloadAt = null;
|
|
313
|
+
this.viewportOverride = null;
|
|
188
314
|
}
|
|
189
315
|
|
|
190
316
|
markClosed() {
|
|
@@ -194,6 +320,7 @@ export class CdpSession {
|
|
|
194
320
|
|
|
195
321
|
this.closed = true;
|
|
196
322
|
this.rejectPending(new Error("CDP session closed"));
|
|
323
|
+
this.rejectEventWaiters(new Error("CDP session closed"));
|
|
197
324
|
this.onClosed?.(this);
|
|
198
325
|
}
|
|
199
326
|
|
|
@@ -258,6 +385,8 @@ export class CdpSession {
|
|
|
258
385
|
this.send("Network.enable"),
|
|
259
386
|
]);
|
|
260
387
|
|
|
388
|
+
await this.seedBufferedState();
|
|
389
|
+
|
|
261
390
|
return this.getSummary();
|
|
262
391
|
}
|
|
263
392
|
|
|
@@ -291,17 +420,96 @@ export class CdpSession {
|
|
|
291
420
|
}
|
|
292
421
|
|
|
293
422
|
bufferEvent(method, params) {
|
|
294
|
-
|
|
423
|
+
const capturedAt = new Date().toISOString();
|
|
424
|
+
if (
|
|
425
|
+
method === "Page.frameNavigated" &&
|
|
426
|
+
params.frame?.url &&
|
|
427
|
+
!params.frame?.parentId
|
|
428
|
+
) {
|
|
429
|
+
this.target.url = params.frame.url;
|
|
430
|
+
this.lastNavigationAt = capturedAt;
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
if (method === "Page.navigatedWithinDocument" && params.url) {
|
|
434
|
+
this.target.url = params.url;
|
|
435
|
+
this.lastNavigationAt = capturedAt;
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
if (method === "Page.loadEventFired") {
|
|
439
|
+
this.lastNavigationAt = capturedAt;
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
this.pushEvent({
|
|
295
443
|
method,
|
|
296
|
-
capturedAt
|
|
444
|
+
capturedAt,
|
|
297
445
|
...normalizeEvent(method, params),
|
|
298
446
|
});
|
|
447
|
+
this.resolveEventWaiters(method, params);
|
|
448
|
+
}
|
|
299
449
|
|
|
450
|
+
pushEvent(event) {
|
|
451
|
+
this.bufferedEvents.push(event);
|
|
300
452
|
if (this.bufferedEvents.length > this.eventBufferSize) {
|
|
301
453
|
this.bufferedEvents.shift();
|
|
302
454
|
}
|
|
303
455
|
}
|
|
304
456
|
|
|
457
|
+
resolveEventWaiters(method, params) {
|
|
458
|
+
for (const waiter of Array.from(this.eventWaiters)) {
|
|
459
|
+
if (waiter.method !== method) {
|
|
460
|
+
continue;
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
try {
|
|
464
|
+
if (!waiter.predicate(params)) {
|
|
465
|
+
continue;
|
|
466
|
+
}
|
|
467
|
+
} catch {
|
|
468
|
+
continue;
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
clearTimeout(waiter.timer);
|
|
472
|
+
this.eventWaiters.delete(waiter);
|
|
473
|
+
waiter.resolve(params);
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
rejectEventWaiters(error) {
|
|
478
|
+
for (const waiter of this.eventWaiters) {
|
|
479
|
+
clearTimeout(waiter.timer);
|
|
480
|
+
waiter.reject(error);
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
this.eventWaiters.clear();
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
createEventWaiter(method, predicate = () => true, timeoutMs = 10_000) {
|
|
487
|
+
let waiter = null;
|
|
488
|
+
const promise = new Promise((resolve, reject) => {
|
|
489
|
+
const timer = setTimeout(() => {
|
|
490
|
+
this.eventWaiters.delete(waiter);
|
|
491
|
+
reject(new Error(`Timed out waiting for ${method}`));
|
|
492
|
+
}, timeoutMs);
|
|
493
|
+
|
|
494
|
+
waiter = { method, predicate, resolve, reject, timer };
|
|
495
|
+
this.eventWaiters.add(waiter);
|
|
496
|
+
});
|
|
497
|
+
|
|
498
|
+
return {
|
|
499
|
+
promise,
|
|
500
|
+
cancel: (error = new Error(`Stopped waiting for ${method}`)) => {
|
|
501
|
+
if (waiter && this.eventWaiters.delete(waiter)) {
|
|
502
|
+
clearTimeout(waiter.timer);
|
|
503
|
+
waiter.reject(error);
|
|
504
|
+
}
|
|
505
|
+
},
|
|
506
|
+
};
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
waitForEvent(method, predicate = () => true, timeoutMs = 10_000) {
|
|
510
|
+
return this.createEventWaiter(method, predicate, timeoutMs).promise;
|
|
511
|
+
}
|
|
512
|
+
|
|
305
513
|
rejectPending(error) {
|
|
306
514
|
for (const pending of this.pending.values()) {
|
|
307
515
|
pending.reject(error);
|
|
@@ -328,13 +536,18 @@ export class CdpSession {
|
|
|
328
536
|
});
|
|
329
537
|
}
|
|
330
538
|
|
|
331
|
-
async
|
|
332
|
-
|
|
539
|
+
async evaluateRuntime(expression, options = {}) {
|
|
540
|
+
return this.send("Runtime.evaluate", {
|
|
333
541
|
expression,
|
|
334
542
|
awaitPromise: options.awaitPromise ?? true,
|
|
335
543
|
returnByValue: options.returnByValue ?? true,
|
|
336
|
-
replMode: true,
|
|
544
|
+
replMode: options.replMode ?? true,
|
|
545
|
+
userGesture: options.userGesture ?? false,
|
|
337
546
|
});
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
async evaluate(expression, options = {}) {
|
|
550
|
+
const result = await this.evaluateRuntime(expression, options);
|
|
338
551
|
|
|
339
552
|
return {
|
|
340
553
|
result: summarizeRemoteObject(result.result),
|
|
@@ -349,8 +562,88 @@ export class CdpSession {
|
|
|
349
562
|
});
|
|
350
563
|
}
|
|
351
564
|
|
|
352
|
-
async
|
|
353
|
-
|
|
565
|
+
async runPageAction(payload, options = {}) {
|
|
566
|
+
const result = await this.evaluateRuntime(
|
|
567
|
+
buildPageContextExpression(
|
|
568
|
+
{
|
|
569
|
+
browserFamily: cdpBrowserFamily(this.config),
|
|
570
|
+
...payload,
|
|
571
|
+
},
|
|
572
|
+
{ serialize: true },
|
|
573
|
+
),
|
|
574
|
+
{
|
|
575
|
+
awaitPromise: true,
|
|
576
|
+
replMode: false,
|
|
577
|
+
userGesture: options.userGesture ?? false,
|
|
578
|
+
},
|
|
579
|
+
);
|
|
580
|
+
|
|
581
|
+
if (result.exceptionDetails) {
|
|
582
|
+
throw new Error(result.exceptionDetails.text || "Page action failed");
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
const value = result.result?.value ?? summarizeRemoteObject(result.result);
|
|
586
|
+
return typeof value === "string" ? JSON.parse(value) : value;
|
|
587
|
+
}
|
|
588
|
+
|
|
589
|
+
async getPageState() {
|
|
590
|
+
const result = await this.runPageAction({
|
|
591
|
+
action: "page_state",
|
|
592
|
+
});
|
|
593
|
+
|
|
594
|
+
return {
|
|
595
|
+
browserFamily: cdpBrowserFamily(this.config),
|
|
596
|
+
...result.page,
|
|
597
|
+
lastNavigationAt: this.lastNavigationAt,
|
|
598
|
+
lastReloadAt: this.lastReloadAt,
|
|
599
|
+
viewportOverride: this.viewportOverride,
|
|
600
|
+
};
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
async takeScreenshot(format = "png", options = {}) {
|
|
604
|
+
if (options.selector) {
|
|
605
|
+
const inspected = await this.runPageAction({
|
|
606
|
+
action: "inspect",
|
|
607
|
+
selector: options.selector,
|
|
608
|
+
scrollIntoView: true,
|
|
609
|
+
});
|
|
610
|
+
|
|
611
|
+
if (!inspected.found) {
|
|
612
|
+
return {
|
|
613
|
+
browserFamily: cdpBrowserFamily(this.config),
|
|
614
|
+
format,
|
|
615
|
+
scope: "element",
|
|
616
|
+
selector: options.selector,
|
|
617
|
+
found: false,
|
|
618
|
+
};
|
|
619
|
+
}
|
|
620
|
+
|
|
621
|
+
const clip = {
|
|
622
|
+
x: Math.max(inspected.node.box?.x ?? 0, 0),
|
|
623
|
+
y: Math.max(inspected.node.box?.y ?? 0, 0),
|
|
624
|
+
width: Math.max(inspected.node.box?.width ?? 0, 1),
|
|
625
|
+
height: Math.max(inspected.node.box?.height ?? 0, 1),
|
|
626
|
+
scale: 1,
|
|
627
|
+
};
|
|
628
|
+
|
|
629
|
+
const screenshot = await this.send("Page.captureScreenshot", {
|
|
630
|
+
format,
|
|
631
|
+
clip,
|
|
632
|
+
});
|
|
633
|
+
|
|
634
|
+
return formatScreenshotResult(screenshot.data, format, {
|
|
635
|
+
browserFamily: cdpBrowserFamily(this.config),
|
|
636
|
+
scope: "element",
|
|
637
|
+
selector: options.selector,
|
|
638
|
+
clip,
|
|
639
|
+
});
|
|
640
|
+
}
|
|
641
|
+
|
|
642
|
+
const screenshot = await this.send("Page.captureScreenshot", { format });
|
|
643
|
+
return formatScreenshotResult(screenshot.data, format, {
|
|
644
|
+
browserFamily: cdpBrowserFamily(this.config),
|
|
645
|
+
scope: "page",
|
|
646
|
+
});
|
|
354
647
|
}
|
|
355
648
|
|
|
356
649
|
getConsoleMessages(limit = 50) {
|
|
@@ -362,93 +655,196 @@ export class CdpSession {
|
|
|
362
655
|
}
|
|
363
656
|
|
|
364
657
|
async inspectElement(selector) {
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
if (!rootNodeId) {
|
|
368
|
-
throw new Error("Unable to resolve the DOM root node");
|
|
369
|
-
}
|
|
370
|
-
|
|
371
|
-
const { nodeId } = await this.send("DOM.querySelector", {
|
|
372
|
-
nodeId: rootNodeId,
|
|
658
|
+
return this.runPageAction({
|
|
659
|
+
action: "inspect",
|
|
373
660
|
selector,
|
|
374
661
|
});
|
|
662
|
+
}
|
|
375
663
|
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
664
|
+
async click(selector) {
|
|
665
|
+
return this.runPageAction(
|
|
666
|
+
{
|
|
667
|
+
action: "click",
|
|
379
668
|
selector,
|
|
380
|
-
|
|
381
|
-
}
|
|
382
|
-
|
|
669
|
+
},
|
|
670
|
+
{ userGesture: true },
|
|
671
|
+
);
|
|
672
|
+
}
|
|
383
673
|
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
this.send("DOM.getAttributes", { nodeId }),
|
|
392
|
-
this.send("DOM.getOuterHTML", { nodeId }),
|
|
393
|
-
this.readNodeText(nodeId),
|
|
394
|
-
],
|
|
674
|
+
async hover(selector) {
|
|
675
|
+
return this.runPageAction(
|
|
676
|
+
{
|
|
677
|
+
action: "hover",
|
|
678
|
+
selector,
|
|
679
|
+
},
|
|
680
|
+
{ userGesture: true },
|
|
395
681
|
);
|
|
682
|
+
}
|
|
396
683
|
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
nodeName: description.node?.nodeName ?? null,
|
|
405
|
-
localName: description.node?.localName ?? null,
|
|
406
|
-
nodeType: description.node?.nodeType ?? null,
|
|
407
|
-
childNodeCount: description.node?.childNodeCount ?? 0,
|
|
408
|
-
attributes: toAttributeObject(attributes.attributes),
|
|
409
|
-
textContent: textContent.textContent,
|
|
410
|
-
innerText: textContent.innerText,
|
|
411
|
-
outerHTML: outerHtml.outerHTML ?? null,
|
|
684
|
+
async type(selector, text, options = {}) {
|
|
685
|
+
return this.runPageAction(
|
|
686
|
+
{
|
|
687
|
+
action: "type",
|
|
688
|
+
selector,
|
|
689
|
+
text,
|
|
690
|
+
clear: options.clear,
|
|
412
691
|
},
|
|
413
|
-
|
|
692
|
+
{ userGesture: true },
|
|
693
|
+
);
|
|
694
|
+
}
|
|
695
|
+
|
|
696
|
+
async select(selector, options = {}) {
|
|
697
|
+
return this.runPageAction(
|
|
698
|
+
{
|
|
699
|
+
action: "select",
|
|
700
|
+
selector,
|
|
701
|
+
value: options.value,
|
|
702
|
+
label: options.label,
|
|
703
|
+
},
|
|
704
|
+
{ userGesture: true },
|
|
705
|
+
);
|
|
706
|
+
}
|
|
707
|
+
|
|
708
|
+
async pressKey(key, selector = null) {
|
|
709
|
+
return this.runPageAction(
|
|
710
|
+
{
|
|
711
|
+
action: "press_key",
|
|
712
|
+
key,
|
|
713
|
+
selector,
|
|
714
|
+
},
|
|
715
|
+
{ userGesture: true },
|
|
716
|
+
);
|
|
717
|
+
}
|
|
718
|
+
|
|
719
|
+
async scroll(options = {}) {
|
|
720
|
+
return this.runPageAction(
|
|
721
|
+
{
|
|
722
|
+
action: "scroll",
|
|
723
|
+
selector: options.selector,
|
|
724
|
+
deltaX: options.deltaX,
|
|
725
|
+
deltaY: options.deltaY,
|
|
726
|
+
block: options.block,
|
|
727
|
+
},
|
|
728
|
+
{ userGesture: true },
|
|
729
|
+
);
|
|
414
730
|
}
|
|
415
731
|
|
|
416
|
-
async
|
|
417
|
-
|
|
732
|
+
async navigate(url, options = {}) {
|
|
733
|
+
const waitUntil = normalizeWaitUntil(options.waitUntil);
|
|
734
|
+
const initiatedAt = new Date().toISOString();
|
|
735
|
+
const lifecycleEvent = cdpLifecycleEventFor(waitUntil);
|
|
736
|
+
const lifecycleWaiter = lifecycleEvent
|
|
737
|
+
? this.createEventWaiter(lifecycleEvent, () => true, options.timeoutMs)
|
|
738
|
+
: null;
|
|
418
739
|
|
|
419
740
|
try {
|
|
420
|
-
const
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
return {
|
|
424
|
-
textContent: null,
|
|
425
|
-
innerText: null,
|
|
426
|
-
};
|
|
741
|
+
const result = await this.send("Page.navigate", { url });
|
|
742
|
+
if (result.errorText) {
|
|
743
|
+
throw new Error(result.errorText);
|
|
427
744
|
}
|
|
428
745
|
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
returnByValue: true,
|
|
434
|
-
silent: true,
|
|
435
|
-
});
|
|
746
|
+
this.target.url = url;
|
|
747
|
+
if (lifecycleWaiter) {
|
|
748
|
+
await lifecycleWaiter.promise;
|
|
749
|
+
}
|
|
436
750
|
|
|
437
|
-
|
|
438
|
-
result.result?.value ?? {
|
|
439
|
-
textContent: null,
|
|
440
|
-
innerText: null,
|
|
441
|
-
}
|
|
442
|
-
);
|
|
443
|
-
} catch {
|
|
751
|
+
const page = lifecycleWaiter ? await this.getPageState() : null;
|
|
444
752
|
return {
|
|
445
|
-
|
|
446
|
-
|
|
753
|
+
browserFamily: cdpBrowserFamily(this.config),
|
|
754
|
+
url,
|
|
755
|
+
frameId: result.frameId ?? null,
|
|
756
|
+
loaderId: result.loaderId ?? null,
|
|
757
|
+
waitUntil,
|
|
758
|
+
initiatedAt,
|
|
759
|
+
page,
|
|
447
760
|
};
|
|
448
|
-
}
|
|
449
|
-
|
|
450
|
-
|
|
761
|
+
} catch (error) {
|
|
762
|
+
lifecycleWaiter?.cancel(error);
|
|
763
|
+
throw error;
|
|
764
|
+
}
|
|
765
|
+
}
|
|
766
|
+
|
|
767
|
+
async reload(options = {}) {
|
|
768
|
+
const waitUntil = normalizeWaitUntil(options.waitUntil);
|
|
769
|
+
const reloadedAt = new Date().toISOString();
|
|
770
|
+
const lifecycleEvent = cdpLifecycleEventFor(waitUntil);
|
|
771
|
+
const lifecycleWaiter = lifecycleEvent
|
|
772
|
+
? this.createEventWaiter(lifecycleEvent, () => true, options.timeoutMs)
|
|
773
|
+
: null;
|
|
774
|
+
|
|
775
|
+
try {
|
|
776
|
+
await this.send("Page.reload", {
|
|
777
|
+
ignoreCache: options.ignoreCache ?? false,
|
|
778
|
+
});
|
|
779
|
+
|
|
780
|
+
if (lifecycleWaiter) {
|
|
781
|
+
await lifecycleWaiter.promise;
|
|
451
782
|
}
|
|
783
|
+
} catch (error) {
|
|
784
|
+
lifecycleWaiter?.cancel(error);
|
|
785
|
+
throw error;
|
|
786
|
+
}
|
|
787
|
+
|
|
788
|
+
this.lastReloadAt = reloadedAt;
|
|
789
|
+
const page = lifecycleWaiter ? await this.getPageState() : null;
|
|
790
|
+
return {
|
|
791
|
+
browserFamily: cdpBrowserFamily(this.config),
|
|
792
|
+
url: this.target.url,
|
|
793
|
+
waitUntil,
|
|
794
|
+
ignoreCache: options.ignoreCache ?? false,
|
|
795
|
+
reloadedAt,
|
|
796
|
+
page,
|
|
797
|
+
};
|
|
798
|
+
}
|
|
799
|
+
|
|
800
|
+
async setViewport(options) {
|
|
801
|
+
const viewport = {
|
|
802
|
+
width: options.width,
|
|
803
|
+
height: options.height,
|
|
804
|
+
deviceScaleFactor:
|
|
805
|
+
typeof options.deviceScaleFactor === "number" &&
|
|
806
|
+
options.deviceScaleFactor > 0
|
|
807
|
+
? options.deviceScaleFactor
|
|
808
|
+
: 1,
|
|
809
|
+
mobile: options.mobile ?? false,
|
|
810
|
+
};
|
|
811
|
+
|
|
812
|
+
await this.send("Emulation.setDeviceMetricsOverride", viewport);
|
|
813
|
+
this.viewportOverride = {
|
|
814
|
+
...viewport,
|
|
815
|
+
appliedAt: new Date().toISOString(),
|
|
816
|
+
};
|
|
817
|
+
|
|
818
|
+
return {
|
|
819
|
+
browserFamily: cdpBrowserFamily(this.config),
|
|
820
|
+
applied: true,
|
|
821
|
+
viewport: this.viewportOverride,
|
|
822
|
+
page: await this.getPageState(),
|
|
823
|
+
};
|
|
824
|
+
}
|
|
825
|
+
|
|
826
|
+
async seedBufferedState() {
|
|
827
|
+
try {
|
|
828
|
+
const snapshot = await this.runPageAction({
|
|
829
|
+
action: "network_snapshot",
|
|
830
|
+
});
|
|
831
|
+
|
|
832
|
+
for (const entry of snapshot?.entries ?? []) {
|
|
833
|
+
this.pushEvent({
|
|
834
|
+
method: "Network.snapshotCaptured",
|
|
835
|
+
capturedAt: new Date().toISOString(),
|
|
836
|
+
kind: "network",
|
|
837
|
+
phase: "snapshot",
|
|
838
|
+
completed: true,
|
|
839
|
+
finished: true,
|
|
840
|
+
failed: false,
|
|
841
|
+
canceled: false,
|
|
842
|
+
source: "performance",
|
|
843
|
+
...entry,
|
|
844
|
+
});
|
|
845
|
+
}
|
|
846
|
+
} catch {
|
|
847
|
+
// Ignore snapshot failures so attach still succeeds against restrictive pages.
|
|
452
848
|
}
|
|
453
849
|
}
|
|
454
850
|
|
|
@@ -463,9 +859,12 @@ export class CdpSession {
|
|
|
463
859
|
targetId: this.target.targetId,
|
|
464
860
|
title: this.target.title,
|
|
465
861
|
url: this.target.url,
|
|
466
|
-
browserFamily:
|
|
862
|
+
browserFamily: cdpBrowserFamily(this.config),
|
|
467
863
|
connectedAt: this.connectedAt,
|
|
468
864
|
bufferedEvents: this.bufferedEvents.length,
|
|
865
|
+
lastNavigationAt: this.lastNavigationAt,
|
|
866
|
+
lastReloadAt: this.lastReloadAt,
|
|
867
|
+
viewportOverride: this.viewportOverride,
|
|
469
868
|
};
|
|
470
869
|
}
|
|
471
870
|
|
|
@@ -570,8 +969,48 @@ export class CdpSessionManager {
|
|
|
570
969
|
return this.getSession(sessionId).getDocument(depth);
|
|
571
970
|
}
|
|
572
971
|
|
|
573
|
-
async
|
|
574
|
-
return this.getSession(sessionId).
|
|
972
|
+
async getPageState(sessionId) {
|
|
973
|
+
return this.getSession(sessionId).getPageState();
|
|
974
|
+
}
|
|
975
|
+
|
|
976
|
+
async navigate(sessionId, url, options) {
|
|
977
|
+
return this.getSession(sessionId).navigate(url, options);
|
|
978
|
+
}
|
|
979
|
+
|
|
980
|
+
async reload(sessionId, options) {
|
|
981
|
+
return this.getSession(sessionId).reload(options);
|
|
982
|
+
}
|
|
983
|
+
|
|
984
|
+
async click(sessionId, selector) {
|
|
985
|
+
return this.getSession(sessionId).click(selector);
|
|
986
|
+
}
|
|
987
|
+
|
|
988
|
+
async hover(sessionId, selector) {
|
|
989
|
+
return this.getSession(sessionId).hover(selector);
|
|
990
|
+
}
|
|
991
|
+
|
|
992
|
+
async type(sessionId, selector, text, options) {
|
|
993
|
+
return this.getSession(sessionId).type(selector, text, options);
|
|
994
|
+
}
|
|
995
|
+
|
|
996
|
+
async select(sessionId, selector, options) {
|
|
997
|
+
return this.getSession(sessionId).select(selector, options);
|
|
998
|
+
}
|
|
999
|
+
|
|
1000
|
+
async pressKey(sessionId, key, selector) {
|
|
1001
|
+
return this.getSession(sessionId).pressKey(key, selector);
|
|
1002
|
+
}
|
|
1003
|
+
|
|
1004
|
+
async scroll(sessionId, options) {
|
|
1005
|
+
return this.getSession(sessionId).scroll(options);
|
|
1006
|
+
}
|
|
1007
|
+
|
|
1008
|
+
async setViewport(sessionId, options) {
|
|
1009
|
+
return this.getSession(sessionId).setViewport(options);
|
|
1010
|
+
}
|
|
1011
|
+
|
|
1012
|
+
async takeScreenshot(sessionId, format, options = {}) {
|
|
1013
|
+
return this.getSession(sessionId).takeScreenshot(format, options);
|
|
575
1014
|
}
|
|
576
1015
|
|
|
577
1016
|
getConsoleMessages(sessionId, limit) {
|