mcp-browser-dev-tools 0.1.0 → 0.2.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 +90 -355
- package/package.json +16 -2
- package/src/action-retry.mjs +50 -0
- package/src/browser-adapter.mjs +15 -4
- package/src/browser-requests.mjs +15 -0
- package/src/cdp-client.mjs +104 -10
- package/src/firefox-bidi-client.mjs +26 -3
- package/src/json-schema.mjs +99 -0
- package/src/mcp-server.mjs +34 -2013
- package/src/page-context.mjs +25 -1034
- package/src/page-script.mjs +1372 -0
- package/src/run-steps.mjs +510 -0
- package/src/run-tabs.mjs +291 -0
- package/src/screenshot-output.mjs +141 -0
- package/src/tool-results.mjs +34 -0
- package/src/tool-schemas.mjs +431 -0
- package/src/tools/action-tools.mjs +318 -0
- package/src/tools/browser-tools.mjs +221 -0
- package/src/tools/inspection-tools.mjs +287 -0
- package/src/tools/state-tools.mjs +327 -0
- package/src/top-level-await.mjs +260 -0
- package/src/wait-for.mjs +381 -71
package/src/wait-for.mjs
CHANGED
|
@@ -59,8 +59,24 @@ function elementText(result) {
|
|
|
59
59
|
return readElementText(result).text;
|
|
60
60
|
}
|
|
61
61
|
|
|
62
|
-
function
|
|
63
|
-
|
|
62
|
+
function hasTextCondition(options) {
|
|
63
|
+
return (
|
|
64
|
+
options.textEquals !== null ||
|
|
65
|
+
options.textIncludes !== null ||
|
|
66
|
+
options.textExcludes !== null
|
|
67
|
+
);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function textChecksFor(condition) {
|
|
71
|
+
return {
|
|
72
|
+
textEquals: condition.textEquals,
|
|
73
|
+
textIncludes: condition.textIncludes,
|
|
74
|
+
textExcludes: condition.textExcludes,
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function textMatches(result, options, checkIndex) {
|
|
79
|
+
if (!hasTextCondition(options)) {
|
|
64
80
|
return true;
|
|
65
81
|
}
|
|
66
82
|
|
|
@@ -68,15 +84,102 @@ function textMatches(result, options) {
|
|
|
68
84
|
return false;
|
|
69
85
|
}
|
|
70
86
|
|
|
71
|
-
//
|
|
87
|
+
// The page compares the full text when it supports textChecks.
|
|
88
|
+
if (Array.isArray(result.textMatches)) {
|
|
89
|
+
return result.textMatches[checkIndex] === true;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// Clipped text is only a prefix, so it can never prove equality or that a
|
|
93
|
+
// string is absent.
|
|
72
94
|
const { text, clipped } = readElementText(result);
|
|
73
95
|
return (
|
|
74
96
|
(options.textEquals === null ||
|
|
75
97
|
(!clipped && text === options.textEquals)) &&
|
|
76
|
-
(options.textIncludes === null || text.includes(options.textIncludes))
|
|
98
|
+
(options.textIncludes === null || text.includes(options.textIncludes)) &&
|
|
99
|
+
(options.textExcludes === null ||
|
|
100
|
+
(!clipped && !text.includes(options.textExcludes)))
|
|
77
101
|
);
|
|
78
102
|
}
|
|
79
103
|
|
|
104
|
+
// Expression conditions evaluate in the page's main world and hold when the
|
|
105
|
+
// result, awaited if it is a promise, is truthy.
|
|
106
|
+
export const DEFAULT_EXPRESSION_TIMEOUT_MS = 5_000;
|
|
107
|
+
const EXPRESSION_TIMEOUT = "EXPRESSION_TIMEOUT";
|
|
108
|
+
|
|
109
|
+
// The page gives up on its own after timeoutMs, so a promise that never
|
|
110
|
+
// settles does not leave the evaluation pending in the browser. Node waits a
|
|
111
|
+
// little longer as a backstop, since hidden tabs can run timers late.
|
|
112
|
+
const EXPRESSION_PAGE_TIMEOUT = "__mcpExpressionTimeout";
|
|
113
|
+
const EXPRESSION_BACKSTOP_GRACE_MS = 2_000;
|
|
114
|
+
|
|
115
|
+
// The expression is passed in as a function defined at the top level, so it
|
|
116
|
+
// sees only page globals, never the wrapper's own variables. Only the wrapper
|
|
117
|
+
// can produce the timeout marker, since the expression's result is converted
|
|
118
|
+
// to a boolean.
|
|
119
|
+
function conditionExpression(expression, timeoutMs) {
|
|
120
|
+
return `(async (check, timeoutMs) => {
|
|
121
|
+
const timedOut = {};
|
|
122
|
+
const value = await Promise.race([
|
|
123
|
+
check(),
|
|
124
|
+
new Promise((resolve) => {
|
|
125
|
+
setTimeout(() => resolve(timedOut), timeoutMs);
|
|
126
|
+
}),
|
|
127
|
+
]);
|
|
128
|
+
return value === timedOut ? ${JSON.stringify(EXPRESSION_PAGE_TIMEOUT)} : Boolean(value);
|
|
129
|
+
})(async () => (
|
|
130
|
+
${expression}
|
|
131
|
+
), ${timeoutMs})`;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
function exceptionMessage(details) {
|
|
135
|
+
const description = details?.exception?.description;
|
|
136
|
+
if (typeof description === "string" && description.trim()) {
|
|
137
|
+
return description.split("\n")[0];
|
|
138
|
+
}
|
|
139
|
+
if (typeof details?.text === "string" && details.text.trim()) {
|
|
140
|
+
return details.text;
|
|
141
|
+
}
|
|
142
|
+
const value = details?.exception?.value;
|
|
143
|
+
return value === undefined ? "unknown error" : String(value);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
async function evaluateCondition(evaluate, expression, timeoutMs) {
|
|
147
|
+
if (typeof evaluate !== "function") {
|
|
148
|
+
throw new Error("expression conditions are not available here");
|
|
149
|
+
}
|
|
150
|
+
const unsettled = () => {
|
|
151
|
+
const error = new Error(
|
|
152
|
+
`expression ${JSON.stringify(expression)} did not settle within ${timeoutMs}ms`,
|
|
153
|
+
);
|
|
154
|
+
error.code = EXPRESSION_TIMEOUT;
|
|
155
|
+
return error;
|
|
156
|
+
};
|
|
157
|
+
let timer;
|
|
158
|
+
const backstop = new Promise((_, reject) => {
|
|
159
|
+
timer = setTimeout(
|
|
160
|
+
() => reject(unsettled()),
|
|
161
|
+
timeoutMs + EXPRESSION_BACKSTOP_GRACE_MS,
|
|
162
|
+
);
|
|
163
|
+
});
|
|
164
|
+
try {
|
|
165
|
+
const result = await Promise.race([
|
|
166
|
+
evaluate(conditionExpression(expression, timeoutMs)),
|
|
167
|
+
backstop,
|
|
168
|
+
]);
|
|
169
|
+
if (result?.exceptionDetails) {
|
|
170
|
+
throw new Error(
|
|
171
|
+
`expression ${JSON.stringify(expression)} threw: ${exceptionMessage(result.exceptionDetails)}`,
|
|
172
|
+
);
|
|
173
|
+
}
|
|
174
|
+
if (result?.result === EXPRESSION_PAGE_TIMEOUT) {
|
|
175
|
+
throw unsettled();
|
|
176
|
+
}
|
|
177
|
+
return result?.result === true;
|
|
178
|
+
} finally {
|
|
179
|
+
clearTimeout(timer);
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
80
183
|
function readyStateMatches(current, expected) {
|
|
81
184
|
const currentRank = READY_STATE_ORDER[current] ?? -1;
|
|
82
185
|
const expectedRank = READY_STATE_ORDER[expected] ?? -1;
|
|
@@ -99,7 +202,7 @@ function selectorMatches(result, state) {
|
|
|
99
202
|
return result.node?.visible === true;
|
|
100
203
|
}
|
|
101
204
|
|
|
102
|
-
function
|
|
205
|
+
function describeWanted(options) {
|
|
103
206
|
const conditions = [];
|
|
104
207
|
if (options.selector) {
|
|
105
208
|
conditions.push(
|
|
@@ -121,7 +224,18 @@ function buildTimeoutMessage(options, lastPage, lastElement) {
|
|
|
121
224
|
if (options.textIncludes !== null) {
|
|
122
225
|
conditions.push(`text to include ${JSON.stringify(options.textIncludes)}`);
|
|
123
226
|
}
|
|
227
|
+
if (options.textExcludes !== null) {
|
|
228
|
+
conditions.push(`text to exclude ${JSON.stringify(options.textExcludes)}`);
|
|
229
|
+
}
|
|
230
|
+
if (options.expression) {
|
|
231
|
+
conditions.push(
|
|
232
|
+
`expression ${JSON.stringify(options.expression)} to be truthy`,
|
|
233
|
+
);
|
|
234
|
+
}
|
|
235
|
+
return conditions.join(", ");
|
|
236
|
+
}
|
|
124
237
|
|
|
238
|
+
function describeObserved(options, lastPage, lastElement, lastExpression) {
|
|
125
239
|
const observed = [];
|
|
126
240
|
if (lastPage?.url) {
|
|
127
241
|
observed.push(`last url=${JSON.stringify(lastPage.url)}`);
|
|
@@ -134,45 +248,86 @@ function buildTimeoutMessage(options, lastPage, lastElement) {
|
|
|
134
248
|
`last selector found=${Boolean(lastElement.found)} visible=${lastElement.node?.visible === true}`,
|
|
135
249
|
);
|
|
136
250
|
}
|
|
137
|
-
if (
|
|
138
|
-
(options.textEquals !== null || options.textIncludes !== null) &&
|
|
139
|
-
lastElement?.found
|
|
140
|
-
) {
|
|
251
|
+
if (hasTextCondition(options) && lastElement?.found) {
|
|
141
252
|
observed.push(`last text=${JSON.stringify(elementText(lastElement))}`);
|
|
142
253
|
}
|
|
254
|
+
if (options.expression && typeof lastExpression === "boolean") {
|
|
255
|
+
observed.push(`last expression=${lastExpression}`);
|
|
256
|
+
}
|
|
257
|
+
return observed;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
function buildTimeoutMessage(normalized, lastCheck) {
|
|
261
|
+
const prefix = `Timed out after ${normalized.timeoutMs}ms waiting for `;
|
|
262
|
+
if (!normalized.anyOf) {
|
|
263
|
+
const observed = describeObserved(
|
|
264
|
+
normalized,
|
|
265
|
+
lastCheck?.page,
|
|
266
|
+
lastCheck?.element,
|
|
267
|
+
lastCheck?.expression,
|
|
268
|
+
);
|
|
269
|
+
return `${prefix}${describeWanted(normalized)}${
|
|
270
|
+
observed.length > 0 ? `; ${observed.join("; ")}` : ""
|
|
271
|
+
}`;
|
|
272
|
+
}
|
|
143
273
|
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
274
|
+
const alternatives = normalized.anyOf.map((condition, index) => {
|
|
275
|
+
const check = lastCheck?.alternatives?.[index];
|
|
276
|
+
const observed = describeObserved(
|
|
277
|
+
condition,
|
|
278
|
+
check?.page,
|
|
279
|
+
check?.element,
|
|
280
|
+
check?.expression,
|
|
281
|
+
);
|
|
282
|
+
return `anyOf[${index}] ${describeWanted(condition)}${
|
|
283
|
+
observed.length > 0 ? ` (${observed.join("; ")})` : ""
|
|
284
|
+
}`;
|
|
285
|
+
});
|
|
286
|
+
return `${prefix}any of: ${alternatives.join("; or ")}`;
|
|
147
287
|
}
|
|
148
288
|
|
|
149
|
-
|
|
289
|
+
const CONDITION_KEYS = [
|
|
290
|
+
"selector",
|
|
291
|
+
"state",
|
|
292
|
+
"url",
|
|
293
|
+
"urlIncludes",
|
|
294
|
+
"readyState",
|
|
295
|
+
"textEquals",
|
|
296
|
+
"textIncludes",
|
|
297
|
+
"textExcludes",
|
|
298
|
+
"expression",
|
|
299
|
+
];
|
|
300
|
+
|
|
301
|
+
function normalizeOptionalText(value) {
|
|
302
|
+
return typeof value === "string" ? normalizeText(value) : null;
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
function normalizeCondition(options, label) {
|
|
306
|
+
if (options.anyOf !== undefined) {
|
|
307
|
+
throw new Error(`${label} cannot nest anyOf`);
|
|
308
|
+
}
|
|
309
|
+
|
|
150
310
|
const normalized = {
|
|
151
311
|
selector: normalizeOptionalString(options.selector),
|
|
152
312
|
state: normalizeSelectorState(options.state),
|
|
153
313
|
url: normalizeOptionalString(options.url),
|
|
154
314
|
urlIncludes: normalizeOptionalString(options.urlIncludes),
|
|
155
315
|
readyState: normalizeReadyState(options.readyState),
|
|
156
|
-
textEquals:
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
textIncludes:
|
|
161
|
-
typeof options.textIncludes === "string"
|
|
162
|
-
? normalizeText(options.textIncludes)
|
|
163
|
-
: null,
|
|
164
|
-
timeoutMs: normalizeInteger(options.timeoutMs, 10_000),
|
|
165
|
-
pollIntervalMs: normalizeInteger(options.pollIntervalMs, 100),
|
|
316
|
+
textEquals: normalizeOptionalText(options.textEquals),
|
|
317
|
+
textIncludes: normalizeOptionalText(options.textIncludes),
|
|
318
|
+
textExcludes: normalizeOptionalText(options.textExcludes),
|
|
319
|
+
expression: normalizeOptionalString(options.expression),
|
|
166
320
|
};
|
|
167
321
|
|
|
168
322
|
if (
|
|
169
323
|
!normalized.selector &&
|
|
170
324
|
!normalized.url &&
|
|
171
325
|
!normalized.urlIncludes &&
|
|
172
|
-
!normalized.readyState
|
|
326
|
+
!normalized.readyState &&
|
|
327
|
+
!normalized.expression
|
|
173
328
|
) {
|
|
174
329
|
throw new Error(
|
|
175
|
-
`${label} requires at least one of selector, url, urlIncludes, or
|
|
330
|
+
`${label} requires at least one of selector, url, urlIncludes, readyState, or expression`,
|
|
176
331
|
);
|
|
177
332
|
}
|
|
178
333
|
|
|
@@ -180,105 +335,260 @@ export function normalizeWaitForOptions(options = {}, label = "wait_for") {
|
|
|
180
335
|
throw new Error(`${label} state requires selector`);
|
|
181
336
|
}
|
|
182
337
|
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
338
|
+
if (hasTextCondition(normalized) && !normalized.selector) {
|
|
339
|
+
throw new Error(
|
|
340
|
+
`${label} textEquals, textIncludes, and textExcludes require selector`,
|
|
341
|
+
);
|
|
187
342
|
}
|
|
188
343
|
|
|
189
|
-
if (
|
|
344
|
+
if (hasTextCondition(normalized) && normalized.state === "hidden") {
|
|
190
345
|
throw new Error(
|
|
191
|
-
`${label} textEquals and
|
|
346
|
+
`${label} textEquals, textIncludes, and textExcludes cannot be combined with state hidden`,
|
|
192
347
|
);
|
|
193
348
|
}
|
|
194
349
|
|
|
195
350
|
return normalized;
|
|
196
351
|
}
|
|
197
352
|
|
|
198
|
-
//
|
|
199
|
-
export
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
if (normalized.url || normalized.urlIncludes || normalized.readyState) {
|
|
207
|
-
page = await getPageState();
|
|
353
|
+
// Returns either one condition or { anyOf: [conditions] }, plus timing.
|
|
354
|
+
export function normalizeWaitForOptions(options = {}, label = "wait_for") {
|
|
355
|
+
const timing = {
|
|
356
|
+
timeoutMs: normalizeInteger(options.timeoutMs, 10_000),
|
|
357
|
+
pollIntervalMs: normalizeInteger(options.pollIntervalMs, 100),
|
|
358
|
+
};
|
|
359
|
+
if (options.anyOf === undefined) {
|
|
360
|
+
return { ...normalizeCondition(options, label), ...timing };
|
|
208
361
|
}
|
|
209
362
|
|
|
210
|
-
if (
|
|
211
|
-
|
|
363
|
+
if (!Array.isArray(options.anyOf) || options.anyOf.length === 0) {
|
|
364
|
+
throw new Error(`${label} anyOf must list at least one condition`);
|
|
365
|
+
}
|
|
366
|
+
const mixed = CONDITION_KEYS.filter((key) => options[key] !== undefined);
|
|
367
|
+
if (mixed.length > 0) {
|
|
368
|
+
throw new Error(
|
|
369
|
+
`${label} anyOf cannot be combined with ${mixed.join(", ")}; put them inside each anyOf condition`,
|
|
370
|
+
);
|
|
371
|
+
}
|
|
372
|
+
return {
|
|
373
|
+
anyOf: options.anyOf.map((condition, index) =>
|
|
374
|
+
normalizeCondition(condition ?? {}, `${label}.anyOf[${index}]`),
|
|
375
|
+
),
|
|
376
|
+
...timing,
|
|
377
|
+
};
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
async function checkOneCondition(
|
|
381
|
+
condition,
|
|
382
|
+
readPage,
|
|
383
|
+
readElement,
|
|
384
|
+
readExpression,
|
|
385
|
+
) {
|
|
386
|
+
let checkIndex = -1;
|
|
387
|
+
const page =
|
|
388
|
+
condition.url || condition.urlIncludes || condition.readyState
|
|
389
|
+
? await readPage()
|
|
390
|
+
: null;
|
|
391
|
+
let element = null;
|
|
392
|
+
if (condition.selector) {
|
|
393
|
+
let textChecks;
|
|
394
|
+
({ element, textChecks } = await readElement(condition.selector));
|
|
395
|
+
checkIndex = textChecks.indexOf(JSON.stringify(textChecksFor(condition)));
|
|
212
396
|
if (element?.error) {
|
|
213
397
|
throw new Error(element.error);
|
|
214
398
|
}
|
|
215
399
|
}
|
|
216
400
|
|
|
217
401
|
const matched =
|
|
218
|
-
(
|
|
219
|
-
? selectorMatches(element,
|
|
220
|
-
textMatches(element,
|
|
402
|
+
(condition.selector
|
|
403
|
+
? selectorMatches(element, condition.state) &&
|
|
404
|
+
textMatches(element, condition, checkIndex)
|
|
221
405
|
: true) &&
|
|
222
|
-
(
|
|
223
|
-
(
|
|
224
|
-
? page?.url?.includes(
|
|
406
|
+
(condition.url ? page?.url === condition.url : true) &&
|
|
407
|
+
(condition.urlIncludes
|
|
408
|
+
? page?.url?.includes(condition.urlIncludes) === true
|
|
225
409
|
: true) &&
|
|
226
|
-
(
|
|
227
|
-
? readyStateMatches(page?.readyState,
|
|
410
|
+
(condition.readyState
|
|
411
|
+
? readyStateMatches(page?.readyState, condition.readyState)
|
|
228
412
|
: true);
|
|
413
|
+
const expression = condition.expression
|
|
414
|
+
? await readExpression(condition.expression)
|
|
415
|
+
: null;
|
|
229
416
|
|
|
230
417
|
return {
|
|
231
|
-
matched,
|
|
418
|
+
matched: matched && (condition.expression ? expression : true),
|
|
232
419
|
page,
|
|
233
420
|
element,
|
|
234
421
|
text: element?.found ? elementText(element) : null,
|
|
422
|
+
expression,
|
|
423
|
+
};
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
// Checks normalized conditions once against the current page. For anyOf, the
|
|
427
|
+
// alternatives are checked in order until one holds, reading the page state
|
|
428
|
+
// and each selector at most once. Expressions must settle by deadline.
|
|
429
|
+
export async function checkPageCondition({
|
|
430
|
+
getPageState,
|
|
431
|
+
inspectElement,
|
|
432
|
+
evaluate,
|
|
433
|
+
normalized,
|
|
434
|
+
deadline = Date.now() + DEFAULT_EXPRESSION_TIMEOUT_MS,
|
|
435
|
+
}) {
|
|
436
|
+
let pagePromise = null;
|
|
437
|
+
const readPage = () => {
|
|
438
|
+
pagePromise ??= getPageState();
|
|
439
|
+
return pagePromise;
|
|
440
|
+
};
|
|
441
|
+
const expressions = new Map();
|
|
442
|
+
const readExpression = (expression) => {
|
|
443
|
+
if (!expressions.has(expression)) {
|
|
444
|
+
expressions.set(
|
|
445
|
+
expression,
|
|
446
|
+
evaluateCondition(
|
|
447
|
+
evaluate,
|
|
448
|
+
expression,
|
|
449
|
+
Math.max(1, deadline - Date.now()),
|
|
450
|
+
),
|
|
451
|
+
);
|
|
452
|
+
}
|
|
453
|
+
return expressions.get(expression);
|
|
454
|
+
};
|
|
455
|
+
// Gather every text check per selector first, so one inspection per
|
|
456
|
+
// selector answers all of them.
|
|
457
|
+
const conditions = normalized.anyOf ?? [normalized];
|
|
458
|
+
const textChecks = new Map();
|
|
459
|
+
for (const condition of conditions) {
|
|
460
|
+
if (!condition.selector || !hasTextCondition(condition)) {
|
|
461
|
+
continue;
|
|
462
|
+
}
|
|
463
|
+
const checks = textChecks.get(condition.selector) ?? [];
|
|
464
|
+
const key = JSON.stringify(textChecksFor(condition));
|
|
465
|
+
if (!checks.includes(key)) {
|
|
466
|
+
checks.push(key);
|
|
467
|
+
}
|
|
468
|
+
textChecks.set(condition.selector, checks);
|
|
469
|
+
}
|
|
470
|
+
const elements = new Map();
|
|
471
|
+
const readElement = (selector) => {
|
|
472
|
+
if (!elements.has(selector)) {
|
|
473
|
+
const checks = textChecks.get(selector) ?? [];
|
|
474
|
+
elements.set(
|
|
475
|
+
selector,
|
|
476
|
+
(checks.length > 0
|
|
477
|
+
? inspectElement(selector, {
|
|
478
|
+
textChecks: checks.map((key) => JSON.parse(key)),
|
|
479
|
+
})
|
|
480
|
+
: inspectElement(selector)
|
|
481
|
+
).then((element) => ({ element, textChecks: checks })),
|
|
482
|
+
);
|
|
483
|
+
}
|
|
484
|
+
return elements.get(selector);
|
|
485
|
+
};
|
|
486
|
+
|
|
487
|
+
if (!normalized.anyOf) {
|
|
488
|
+
return checkOneCondition(normalized, readPage, readElement, readExpression);
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
const alternatives = [];
|
|
492
|
+
for (const condition of normalized.anyOf) {
|
|
493
|
+
const check = await checkOneCondition(
|
|
494
|
+
condition,
|
|
495
|
+
readPage,
|
|
496
|
+
readElement,
|
|
497
|
+
readExpression,
|
|
498
|
+
);
|
|
499
|
+
alternatives.push(check);
|
|
500
|
+
if (check.matched) {
|
|
501
|
+
return {
|
|
502
|
+
...check,
|
|
503
|
+
matchedIndex: alternatives.length - 1,
|
|
504
|
+
alternatives,
|
|
505
|
+
};
|
|
506
|
+
}
|
|
507
|
+
}
|
|
508
|
+
return {
|
|
509
|
+
matched: false,
|
|
510
|
+
page: null,
|
|
511
|
+
element: null,
|
|
512
|
+
text: null,
|
|
513
|
+
expression: null,
|
|
514
|
+
matchedIndex: null,
|
|
515
|
+
alternatives,
|
|
516
|
+
};
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
function describeCondition(condition) {
|
|
520
|
+
return {
|
|
521
|
+
selector: condition.selector,
|
|
522
|
+
state: condition.selector ? condition.state : null,
|
|
523
|
+
url: condition.url,
|
|
524
|
+
urlIncludes: condition.urlIncludes,
|
|
525
|
+
readyState: condition.readyState,
|
|
526
|
+
textEquals: condition.textEquals,
|
|
527
|
+
textIncludes: condition.textIncludes,
|
|
528
|
+
textExcludes: condition.textExcludes,
|
|
529
|
+
...(condition.expression ? { expression: condition.expression } : {}),
|
|
235
530
|
};
|
|
236
531
|
}
|
|
237
532
|
|
|
238
533
|
export async function waitForPageCondition({
|
|
239
534
|
getPageState,
|
|
240
535
|
inspectElement,
|
|
536
|
+
evaluate,
|
|
241
537
|
options,
|
|
242
538
|
}) {
|
|
243
539
|
const normalized = normalizeWaitForOptions(options);
|
|
244
540
|
const startedAt = Date.now();
|
|
245
541
|
let attempts = 0;
|
|
542
|
+
let lastCheck = null;
|
|
246
543
|
|
|
247
544
|
while (true) {
|
|
248
545
|
attempts += 1;
|
|
249
546
|
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
547
|
+
let check;
|
|
548
|
+
try {
|
|
549
|
+
check = await checkPageCondition({
|
|
550
|
+
getPageState,
|
|
551
|
+
inspectElement,
|
|
552
|
+
evaluate,
|
|
553
|
+
normalized,
|
|
554
|
+
deadline: startedAt + normalized.timeoutMs,
|
|
555
|
+
});
|
|
556
|
+
} catch (error) {
|
|
557
|
+
// An expression cut off by the deadline after earlier polls completed
|
|
558
|
+
// is an ordinary timeout; report what those polls observed.
|
|
559
|
+
if (error.code === EXPRESSION_TIMEOUT && lastCheck) {
|
|
560
|
+
throw new Error(buildTimeoutMessage(normalized, lastCheck), {
|
|
561
|
+
cause: error,
|
|
562
|
+
});
|
|
563
|
+
}
|
|
564
|
+
throw error;
|
|
565
|
+
}
|
|
566
|
+
lastCheck = check;
|
|
257
567
|
|
|
258
568
|
if (check.matched) {
|
|
569
|
+
const matchedCondition = normalized.anyOf
|
|
570
|
+
? normalized.anyOf[check.matchedIndex]
|
|
571
|
+
: normalized;
|
|
572
|
+
const lastPage = check.page;
|
|
573
|
+
const lastElement = check.element;
|
|
259
574
|
const browserFamily =
|
|
260
575
|
lastPage?.browserFamily ?? lastElement?.browserFamily ?? null;
|
|
261
576
|
|
|
262
577
|
return {
|
|
263
578
|
browserFamily,
|
|
264
579
|
matched: true,
|
|
580
|
+
...(normalized.anyOf ? { matchedIndex: check.matchedIndex } : {}),
|
|
265
581
|
waitedMs: Date.now() - startedAt,
|
|
266
582
|
attempts,
|
|
267
583
|
timeoutMs: normalized.timeoutMs,
|
|
268
584
|
pollIntervalMs: normalized.pollIntervalMs,
|
|
269
|
-
condition:
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
url: normalized.url,
|
|
273
|
-
urlIncludes: normalized.urlIncludes,
|
|
274
|
-
readyState: normalized.readyState,
|
|
275
|
-
textEquals: normalized.textEquals,
|
|
276
|
-
textIncludes: normalized.textIncludes,
|
|
277
|
-
},
|
|
585
|
+
condition: normalized.anyOf
|
|
586
|
+
? { anyOf: normalized.anyOf.map(describeCondition) }
|
|
587
|
+
: describeCondition(normalized),
|
|
278
588
|
page: lastPage,
|
|
279
|
-
element:
|
|
589
|
+
element: matchedCondition.selector
|
|
280
590
|
? {
|
|
281
|
-
selector:
|
|
591
|
+
selector: matchedCondition.selector,
|
|
282
592
|
found: Boolean(lastElement?.found),
|
|
283
593
|
locator:
|
|
284
594
|
lastElement?.locator ?? lastElement?.node?.locator ?? null,
|
|
@@ -289,7 +599,7 @@ export async function waitForPageCondition({
|
|
|
289
599
|
}
|
|
290
600
|
|
|
291
601
|
if (Date.now() - startedAt >= normalized.timeoutMs) {
|
|
292
|
-
throw new Error(buildTimeoutMessage(normalized,
|
|
602
|
+
throw new Error(buildTimeoutMessage(normalized, check));
|
|
293
603
|
}
|
|
294
604
|
|
|
295
605
|
await sleep(normalized.pollIntervalMs);
|