@toolstackhq/cdpwright 1.0.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.
@@ -0,0 +1,1572 @@
1
+ // src/assert/AssertionError.ts
2
+ var AssertionError = class extends Error {
3
+ selector;
4
+ timeoutMs;
5
+ lastState;
6
+ constructor(message, options = {}) {
7
+ super(message);
8
+ this.name = "AssertionError";
9
+ this.selector = options.selector;
10
+ this.timeoutMs = options.timeoutMs;
11
+ this.lastState = options.lastState;
12
+ }
13
+ };
14
+
15
+ // src/core/Waiter.ts
16
+ async function waitFor(predicate, options = {}) {
17
+ const timeoutMs = options.timeoutMs ?? 3e4;
18
+ const intervalMs = options.intervalMs ?? 100;
19
+ const start = Date.now();
20
+ let lastError;
21
+ while (Date.now() - start < timeoutMs) {
22
+ try {
23
+ const result = await predicate();
24
+ if (result) {
25
+ return result;
26
+ }
27
+ } catch (err) {
28
+ lastError = err;
29
+ }
30
+ await new Promise((resolve) => setTimeout(resolve, intervalMs));
31
+ }
32
+ const description = options.description ? ` (${options.description})` : "";
33
+ const error = new Error(`Timeout after ${timeoutMs}ms${description}`);
34
+ error.cause = lastError;
35
+ throw error;
36
+ }
37
+
38
+ // src/core/Page.ts
39
+ import fs2 from "fs";
40
+ import path2 from "path";
41
+
42
+ // src/core/Frame.ts
43
+ import fs from "fs";
44
+ import path from "path";
45
+
46
+ // src/core/Selectors.ts
47
+ function isXPathSelector(input) {
48
+ if (input.startsWith("/")) return true;
49
+ if (input.startsWith("./")) return true;
50
+ if (input.startsWith(".//")) return true;
51
+ if (input.startsWith("..")) return true;
52
+ if (input.startsWith("(")) {
53
+ const trimmed = input.trimStart();
54
+ if (trimmed.startsWith("(")) {
55
+ const inner = trimmed.slice(1).trimStart();
56
+ return inner.startsWith("/") || inner.startsWith(".");
57
+ }
58
+ }
59
+ return false;
60
+ }
61
+ function parseSelector(input) {
62
+ const value = input.trim();
63
+ const pierceShadowDom = value.includes(">>>");
64
+ return {
65
+ type: isXPathSelector(value) ? "xpath" : "css",
66
+ value,
67
+ pierceShadowDom
68
+ };
69
+ }
70
+
71
+ // src/core/ShadowDom.ts
72
+ function getElementCtor(root) {
73
+ if (typeof Element !== "undefined") return Element;
74
+ const doc = root.ownerDocument;
75
+ const view = (doc || root).defaultView;
76
+ return view?.Element ?? null;
77
+ }
78
+ function isElementNode(node, ElementCtor) {
79
+ return node instanceof ElementCtor;
80
+ }
81
+ function nodeChildren(node) {
82
+ if (!("children" in node)) {
83
+ return [];
84
+ }
85
+ return Array.from(node.children);
86
+ }
87
+ function querySelectorDeep(root, selector) {
88
+ const ElementCtor = getElementCtor(root);
89
+ if (!ElementCtor) return null;
90
+ const elementCtor = ElementCtor;
91
+ function walk(node, sel, results2) {
92
+ if (isElementNode(node, elementCtor) && node.matches(sel)) {
93
+ results2.push(node);
94
+ }
95
+ if (isElementNode(node, elementCtor) && node.shadowRoot) {
96
+ walk(node.shadowRoot, sel, results2);
97
+ }
98
+ for (const child of nodeChildren(node)) {
99
+ walk(child, sel, results2);
100
+ }
101
+ }
102
+ function findAll(rootNode, sel) {
103
+ const results2 = [];
104
+ walk(rootNode, sel, results2);
105
+ return results2;
106
+ }
107
+ if (selector.includes(">>>")) {
108
+ const parts = selector.split(">>>").map((p) => p.trim()).filter(Boolean);
109
+ let scope = [root];
110
+ for (const part of parts) {
111
+ const matches = [];
112
+ for (const item of scope) {
113
+ matches.push(...findAll(item, part));
114
+ }
115
+ if (matches.length === 0) return null;
116
+ scope = matches;
117
+ }
118
+ return scope[0] ?? null;
119
+ }
120
+ const results = findAll(root, selector);
121
+ return results[0] ?? null;
122
+ }
123
+ function querySelectorAllDeep(root, selector) {
124
+ const ElementCtor = getElementCtor(root);
125
+ if (!ElementCtor) return [];
126
+ const elementCtor = ElementCtor;
127
+ function walk(node, sel, results) {
128
+ if (isElementNode(node, elementCtor) && node.matches(sel)) {
129
+ results.push(node);
130
+ }
131
+ if (isElementNode(node, elementCtor) && node.shadowRoot) {
132
+ walk(node.shadowRoot, sel, results);
133
+ }
134
+ for (const child of nodeChildren(node)) {
135
+ walk(child, sel, results);
136
+ }
137
+ }
138
+ function findAll(rootNode, sel) {
139
+ const results = [];
140
+ walk(rootNode, sel, results);
141
+ return results;
142
+ }
143
+ if (selector.includes(">>>")) {
144
+ const parts = selector.split(">>>").map((p) => p.trim()).filter(Boolean);
145
+ let scope = [root];
146
+ for (const part of parts) {
147
+ const matches = [];
148
+ for (const item of scope) {
149
+ matches.push(...findAll(item, part));
150
+ }
151
+ scope = matches;
152
+ if (scope.length === 0) return [];
153
+ }
154
+ return scope.filter((el) => el instanceof Element);
155
+ }
156
+ return findAll(root, selector);
157
+ }
158
+ function serializeShadowDomHelpers() {
159
+ return {
160
+ querySelectorDeep: querySelectorDeep.toString(),
161
+ querySelectorAllDeep: querySelectorAllDeep.toString()
162
+ };
163
+ }
164
+
165
+ // src/core/Locator.ts
166
+ var Locator = class {
167
+ frame;
168
+ selector;
169
+ options;
170
+ constructor(frame, selector, options = {}) {
171
+ this.frame = frame;
172
+ this.selector = selector;
173
+ this.options = options;
174
+ }
175
+ async click(options = {}) {
176
+ return this.frame.click(this.selector, { ...this.options, ...options });
177
+ }
178
+ async dblclick(options = {}) {
179
+ return this.frame.dblclick(this.selector, { ...this.options, ...options });
180
+ }
181
+ async type(text, options = {}) {
182
+ return this.frame.type(this.selector, text, { ...this.options, ...options });
183
+ }
184
+ async exists() {
185
+ return this.frame.exists(this.selector, this.options);
186
+ }
187
+ async text() {
188
+ return this.frame.text(this.selector, this.options);
189
+ }
190
+ };
191
+
192
+ // src/core/Frame.ts
193
+ var Frame = class {
194
+ id;
195
+ name;
196
+ url;
197
+ parentId;
198
+ session;
199
+ logger;
200
+ events;
201
+ contextId;
202
+ defaultTimeout = 3e4;
203
+ constructor(id, session, logger, events) {
204
+ this.id = id;
205
+ this.session = session;
206
+ this.logger = logger;
207
+ this.events = events;
208
+ }
209
+ setExecutionContext(contextId) {
210
+ this.contextId = contextId;
211
+ }
212
+ getExecutionContext() {
213
+ return this.contextId;
214
+ }
215
+ setMeta(meta) {
216
+ this.name = meta.name;
217
+ this.url = meta.url;
218
+ this.parentId = meta.parentId;
219
+ }
220
+ async evaluate(fnOrString, ...args) {
221
+ return this.evaluateInContext(fnOrString, args);
222
+ }
223
+ async query(selector, options = {}) {
224
+ return this.querySelectorInternal(selector, options, false);
225
+ }
226
+ async queryAll(selector, options = {}) {
227
+ return this.querySelectorAllInternal(selector, options, false);
228
+ }
229
+ async queryXPath(selector, options = {}) {
230
+ return this.querySelectorInternal(selector, options, true);
231
+ }
232
+ async queryAllXPath(selector, options = {}) {
233
+ return this.querySelectorAllInternal(selector, options, true);
234
+ }
235
+ locator(selector, options = {}) {
236
+ return new Locator(this, selector, options);
237
+ }
238
+ async click(selector, options = {}) {
239
+ await this.performClick(selector, options, false);
240
+ }
241
+ async dblclick(selector, options = {}) {
242
+ await this.performClick(selector, options, true);
243
+ }
244
+ async type(selector, text, options = {}) {
245
+ const start = Date.now();
246
+ const parsed = parseSelector(selector);
247
+ const pierce = Boolean(parsed.pierceShadowDom);
248
+ this.events.emit("action:start", { name: "type", selector, frameId: this.id, sensitive: options.sensitive });
249
+ await waitFor(async () => {
250
+ const box = await this.resolveElementBox(selector, options);
251
+ if (!box || !box.visible) {
252
+ return false;
253
+ }
254
+ return true;
255
+ }, { timeoutMs: options.timeoutMs ?? this.defaultTimeout, description: `type ${selector}` });
256
+ const helpers = serializeShadowDomHelpers();
257
+ const focusExpression = `(function() {
258
+ const querySelectorDeep = ${helpers.querySelectorDeep};
259
+ const root = document;
260
+ const selector = ${JSON.stringify(selector)};
261
+ const el = ${pierce ? "querySelectorDeep(root, selector)" : "root.querySelector(selector)"};
262
+ if (!el) {
263
+ return;
264
+ }
265
+ el.focus();
266
+ })()`;
267
+ const focusParams = {
268
+ expression: focusExpression,
269
+ returnByValue: true
270
+ };
271
+ if (this.contextId) {
272
+ focusParams.contextId = this.contextId;
273
+ }
274
+ await this.session.send("Runtime.evaluate", focusParams);
275
+ await this.session.send("Input.insertText", { text });
276
+ const duration = Date.now() - start;
277
+ this.events.emit("action:end", { name: "type", selector, frameId: this.id, durationMs: duration, sensitive: options.sensitive });
278
+ this.logger.debug("Type", selector, `${duration}ms`);
279
+ }
280
+ async typeSecure(selector, text, options = {}) {
281
+ return this.type(selector, text, { ...options, sensitive: true });
282
+ }
283
+ async fillInput(selector, value, options = {}) {
284
+ const start = Date.now();
285
+ this.events.emit("action:start", { name: "fillInput", selector, frameId: this.id });
286
+ await waitFor(async () => {
287
+ const expression = `(function() {
288
+ const selector = ${JSON.stringify(selector)};
289
+ const findDeep = (sel) => {
290
+ if (sel.includes(">>>")) {
291
+ const parts = sel.split(">>>").map((s) => s.trim()).filter(Boolean);
292
+ let scope = [document];
293
+ for (const part of parts) {
294
+ const next = [];
295
+ for (const node of scope) {
296
+ const roots = [node];
297
+ if (node instanceof Element && node.shadowRoot) roots.push(node.shadowRoot);
298
+ for (const root of roots) {
299
+ next.push(...root.querySelectorAll(part));
300
+ }
301
+ }
302
+ if (!next.length) return null;
303
+ scope = next;
304
+ }
305
+ return scope[0] || null;
306
+ }
307
+ return document.querySelector(sel);
308
+ };
309
+ const el = findDeep(selector);
310
+ if (!el) return false;
311
+ if (!(el instanceof HTMLInputElement || el instanceof HTMLTextAreaElement || el instanceof HTMLSelectElement)) {
312
+ return false;
313
+ }
314
+ el.value = ${JSON.stringify(value)};
315
+ el.dispatchEvent(new Event("input", { bubbles: true }));
316
+ el.dispatchEvent(new Event("change", { bubbles: true }));
317
+ return true;
318
+ })()`;
319
+ const params = {
320
+ expression,
321
+ returnByValue: true
322
+ };
323
+ if (this.contextId) {
324
+ params.contextId = this.contextId;
325
+ }
326
+ const result = await this.session.send("Runtime.evaluate", params);
327
+ return Boolean(result.result?.value);
328
+ }, { timeoutMs: options.timeoutMs ?? this.defaultTimeout, description: `fillInput ${selector}` });
329
+ const duration = Date.now() - start;
330
+ this.events.emit("action:end", { name: "fillInput", selector, frameId: this.id, durationMs: duration });
331
+ this.logger.debug("FillInput", selector, `${duration}ms`);
332
+ }
333
+ async findLocators(options = {}) {
334
+ const start = Date.now();
335
+ this.events.emit("action:start", { name: "findLocators", frameId: this.id });
336
+ const artifactsDir = path.resolve(process.cwd(), "artifacts");
337
+ try {
338
+ fs.mkdirSync(artifactsDir, { recursive: true });
339
+ } catch {
340
+ }
341
+ const resolveOut = (name) => {
342
+ if (!name) return null;
343
+ const base = path.basename(name);
344
+ return path.join(artifactsDir, base);
345
+ };
346
+ const outputJson = resolveOut(options.outputJson || options.outputPath);
347
+ const outputHtml = resolveOut(options.outputHtml);
348
+ const expression = `(function() {
349
+ const highlight = ${options.highlight !== false};
350
+ const previous = Array.from(document.querySelectorAll(".__cdpwright-locator-overlay"));
351
+ previous.forEach((el) => el.remove());
352
+
353
+ const cssEscape = (value) => {
354
+ if (typeof CSS !== "undefined" && CSS.escape) return CSS.escape(value);
355
+ return value.replace(/[^a-zA-Z0-9_-]/g, (c) => "\\\\" + c.charCodeAt(0).toString(16) + " ");
356
+ };
357
+
358
+ const visible = (el) => {
359
+ const rect = el.getBoundingClientRect();
360
+ const style = window.getComputedStyle(el);
361
+ return rect.width > 0 && rect.height > 0 && style.visibility !== "hidden" && style.display !== "none" && Number(style.opacity || "1") > 0;
362
+ };
363
+
364
+ const siblingsIndex = (el) => {
365
+ if (!el.parentElement) return 1;
366
+ const sibs = Array.from(el.parentElement.children).filter((n) => n.tagName === el.tagName);
367
+ return sibs.indexOf(el) + 1;
368
+ };
369
+
370
+ const xpathFor = (el) => {
371
+ const parts = [];
372
+ let node = el;
373
+ while (node && node.nodeType === 1 && node !== document.documentElement) {
374
+ const idx = siblingsIndex(node);
375
+ parts.unshift(node.tagName.toLowerCase() + "[" + idx + "]");
376
+ node = node.parentElement;
377
+ }
378
+ return "//" + parts.join("/");
379
+ };
380
+
381
+ const buildLocator = (el) => {
382
+ const testid = el.getAttribute("data-testid");
383
+ const id = el.id;
384
+ const name = el.getAttribute("name");
385
+ const aria = el.getAttribute("aria-label");
386
+ const labelledBy = el.getAttribute("aria-labelledby");
387
+ const placeholder = el.getAttribute("placeholder");
388
+ const role = el.getAttribute("role");
389
+ const labelText = (() => {
390
+ if (labelledBy) {
391
+ const ref = document.getElementById(labelledBy);
392
+ if (ref) return ref.textContent?.trim() || "";
393
+ }
394
+ if (el instanceof HTMLInputElement || el instanceof HTMLTextAreaElement || el instanceof HTMLSelectElement) {
395
+ const idRef = el.id && document.querySelector("label[for='" + el.id.replace(/'/g, "\\\\'") + "']");
396
+ if (idRef) return idRef.textContent?.trim() || "";
397
+ const wrap = el.closest("label");
398
+ if (wrap) return wrap.textContent?.trim() || "";
399
+ }
400
+ return "";
401
+ })();
402
+
403
+ let css = "";
404
+ let quality = "low";
405
+ let reason = "fallback";
406
+ if (testid) {
407
+ css = "[data-testid="" + testid.replace(/"/g, '\\\\"') + ""]";
408
+ quality = "high";
409
+ reason = "data-testid";
410
+ } else if (id) {
411
+ css = "#" + cssEscape(id);
412
+ quality = "high";
413
+ reason = "id";
414
+ } else if (name) {
415
+ css = "[name="" + name.replace(/"/g, '\\\\"') + ""]";
416
+ quality = "ok";
417
+ reason = "name";
418
+ } else if (aria) {
419
+ css = "[aria-label="" + aria.replace(/"/g, '\\\\"') + ""]";
420
+ quality = "ok";
421
+ reason = "aria-label";
422
+ } else if (placeholder) {
423
+ css = "[placeholder="" + placeholder.replace(/"/g, '\\\\"') + ""]";
424
+ quality = "low";
425
+ reason = "placeholder";
426
+ } else if (labelText) {
427
+ css = el.tagName.toLowerCase() + "[aria-label="" + labelText.replace(/"/g, '\\\\"') + ""]";
428
+ quality = "low";
429
+ reason = "label text";
430
+ } else {
431
+ const nth = siblingsIndex(el);
432
+ css = el.tagName.toLowerCase() + ":nth-of-type(" + nth + ")";
433
+ quality = "low";
434
+ reason = "nth-of-type";
435
+ }
436
+
437
+ const tag = el.tagName.toLowerCase();
438
+ const type = el.getAttribute("type") || "";
439
+ const text = (el.textContent || "").trim().slice(0, 80);
440
+
441
+ return {
442
+ name: labelText || aria || placeholder || name || testid || id || tag,
443
+ css,
444
+ xpath: xpathFor(el),
445
+ quality,
446
+ reason,
447
+ visible: true,
448
+ tag,
449
+ type,
450
+ role,
451
+ text,
452
+ id,
453
+ nameAttr: name,
454
+ dataTestid: testid
455
+ };
456
+ };
457
+
458
+ const preferredSelectors = ["input", "select", "textarea", "button", "a[href]", "[role='button']", "[contenteditable='true']", "h1", "h2", "h3", "h4", "h5", "h6", "label", "legend", "fieldset"];
459
+ let nodes = Array.from(document.querySelectorAll(preferredSelectors.join(", ")));
460
+ if (nodes.length === 0) {
461
+ nodes = Array.from(document.querySelectorAll("*")).filter((el) => {
462
+ if (!(el instanceof HTMLElement)) return false;
463
+ const tag = el.tagName.toLowerCase();
464
+ if (preferredSelectors.includes(tag)) return true;
465
+ if (tag === "a" && el.hasAttribute("href")) return true;
466
+ if (el.getAttribute("role") === "button") return true;
467
+ if (el.hasAttribute("contenteditable")) return true;
468
+ return false;
469
+ });
470
+ }
471
+ const results = [];
472
+ nodes.forEach((el) => {
473
+ const isVisible = visible(el);
474
+ const locator = buildLocator(el);
475
+ locator.visible = isVisible;
476
+ results.push(locator);
477
+ });
478
+
479
+ if (highlight) {
480
+ results.forEach((loc, index) => {
481
+ const el = nodes[index];
482
+ if (!el) return;
483
+ const rect = el.getBoundingClientRect();
484
+ const overlay = document.createElement("div");
485
+ overlay.className = "__cdpwright-locator-overlay";
486
+ overlay.style.position = "absolute";
487
+ overlay.style.left = rect.x + window.scrollX + "px";
488
+ overlay.style.top = rect.y + window.scrollY + "px";
489
+ overlay.style.width = rect.width + "px";
490
+ overlay.style.height = rect.height + "px";
491
+ overlay.style.border = "2px solid #e67e22";
492
+ overlay.style.borderRadius = "6px";
493
+ overlay.style.pointerEvents = "none";
494
+ overlay.style.zIndex = "99999";
495
+ const badge = document.createElement("div");
496
+ badge.textContent = String(index);
497
+ badge.style.position = "absolute";
498
+ badge.style.top = "-10px";
499
+ badge.style.left = "-10px";
500
+ badge.style.background = "#e67e22";
501
+ badge.style.color = "#fff";
502
+ badge.style.fontSize = "12px";
503
+ badge.style.padding = "2px 6px";
504
+ badge.style.borderRadius = "10px";
505
+ overlay.appendChild(badge);
506
+ document.body.appendChild(overlay);
507
+ });
508
+ }
509
+
510
+ if (console && console.table) {
511
+ console.table(results.map((r, idx) => ({ idx, name: r.name, css: r.css, quality: r.quality, reason: r.reason, tag: r.tag, type: r.type })));
512
+ }
513
+ return results;
514
+ })()`;
515
+ const params = {
516
+ expression,
517
+ returnByValue: true
518
+ };
519
+ if (this.contextId) {
520
+ params.contextId = this.contextId;
521
+ }
522
+ let result = await this.session.send("Runtime.evaluate", params);
523
+ const duration = Date.now() - start;
524
+ this.events.emit("action:end", { name: "findLocators", frameId: this.id, durationMs: duration });
525
+ const value = result.result?.value ?? [];
526
+ if (Array.isArray(value) && value.length > 0) {
527
+ this.logger.info("FindLocators", `${value.length} candidates`, value.slice(0, 5).map((v) => v.css || v.name || v.tag));
528
+ if (outputJson) {
529
+ try {
530
+ fs.writeFileSync(outputJson, JSON.stringify(value, null, 2), "utf-8");
531
+ this.logger.info("FindLocators", `written to ${outputJson}`);
532
+ } catch (err) {
533
+ this.logger.warn("FindLocators write failed", err);
534
+ }
535
+ }
536
+ if (outputHtml) {
537
+ this.writeLocatorHtml(outputHtml, value);
538
+ }
539
+ return value;
540
+ }
541
+ result = await this.session.send("Runtime.evaluate", {
542
+ expression: `(function() {
543
+ const allowed = ["input","select","textarea","button","a","label","legend","fieldset","h1","h2","h3","h4","h5","h6"];
544
+ const skip = ["html","head","body","meta","link","script","style"];
545
+ return Array.from(document.querySelectorAll("*"))
546
+ .filter((el) => {
547
+ if (!(el instanceof HTMLElement)) return false;
548
+ const tag = el.tagName.toLowerCase();
549
+ if (skip.includes(tag)) return false;
550
+ if (tag === "a" && el.hasAttribute("href")) return true;
551
+ if (el.getAttribute("role") === "button") return true;
552
+ if (el.hasAttribute("contenteditable")) return true;
553
+ return allowed.includes(tag);
554
+ })
555
+ .slice(0, 200)
556
+ .map((el, idx) => ({
557
+ name: el.getAttribute("aria-label") || el.getAttribute("name") || el.getAttribute("data-testid") || el.id || el.tagName.toLowerCase() + "-" + idx,
558
+ css: el.id ? "#" + el.id : el.getAttribute("data-testid") ? "[data-testid=\\"" + el.getAttribute("data-testid") + "\\"]" : el.tagName.toLowerCase(),
559
+ xpath: "",
560
+ quality: "low",
561
+ reason: "fallback",
562
+ visible: true,
563
+ tag: el.tagName.toLowerCase(),
564
+ type: el.getAttribute("type") || "",
565
+ role: el.getAttribute("role") || ""
566
+ }));
567
+ })()`,
568
+ returnByValue: true
569
+ });
570
+ const fallback = result.result?.value ?? [];
571
+ this.logger.info("FindLocators", `${fallback.length} candidates`, fallback.slice(0, 5).map((v) => v.css || v.name || v.tag));
572
+ if (outputJson) {
573
+ try {
574
+ fs.writeFileSync(outputJson, JSON.stringify(fallback, null, 2), "utf-8");
575
+ this.logger.info("FindLocators", `written to ${outputJson}`);
576
+ } catch (err) {
577
+ this.logger.warn("FindLocators write failed", err);
578
+ }
579
+ }
580
+ if (outputHtml) {
581
+ this.writeLocatorHtml(outputHtml, fallback);
582
+ }
583
+ return fallback;
584
+ }
585
+ writeLocatorHtml(filePath, data) {
586
+ const html = `<!doctype html>
587
+ <html>
588
+ <head>
589
+ <meta charset="utf-8" />
590
+ <title>Locators</title>
591
+ <style>
592
+ body { font-family: "Segoe UI", system-ui, -apple-system, sans-serif; padding: 16px; background: #f8f9fa; color: #222; }
593
+ h1 { font-size: 20px; margin-bottom: 12px; }
594
+ table { border-collapse: collapse; width: 100%; background: #fff; box-shadow: 0 2px 6px rgba(0,0,0,0.08); }
595
+ th, td { border: 1px solid #e5e7eb; padding: 8px; font-size: 13px; text-align: left; }
596
+ th { background: linear-gradient(180deg, #f6f7fb, #edf0f7); font-weight: 600; }
597
+ tr:nth-child(even) { background: #fafbfc; }
598
+ .copy { color: #2563eb; text-decoration: underline dotted; cursor: pointer; font-size: 12px; background: none; border: none; padding: 0; opacity: 0; transition: opacity 0.15s ease; }
599
+ tr:hover .copy { opacity: 1; }
600
+ .copy:hover { color: #1d4ed8; }
601
+ .loc-value { margin-left: 6px; font-family: ui-monospace, SFMono-Regular, SFMono, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace; }
602
+ </style>
603
+ </head>
604
+ <body>
605
+ <h1>Locator candidates</h1>
606
+ <table>
607
+ <thead>
608
+ <tr><th>#</th><th>Name</th><th>CSS</th><th>XPath</th><th>Quality</th><th>Reason</th><th>Visible</th></tr>
609
+ </thead>
610
+ <tbody id="rows"></tbody>
611
+ </table>
612
+ <script>
613
+ const data = ${JSON.stringify(data)};
614
+ const rows = document.getElementById("rows");
615
+ data.forEach((loc, idx) => {
616
+ const tr = document.createElement("tr");
617
+ const cells = [
618
+ idx,
619
+ loc.name || "",
620
+ loc.css || "",
621
+ loc.xpath || "",
622
+ loc.quality || "",
623
+ loc.reason || "",
624
+ String(loc.visible)
625
+ ];
626
+ cells.forEach((val, i) => {
627
+ const td = document.createElement("td");
628
+ if (i === 2 || i === 3) {
629
+ const link = document.createElement("button");
630
+ link.className = "copy";
631
+ link.textContent = "copy";
632
+ link.addEventListener("click", async (e) => {
633
+ e.preventDefault();
634
+ try { await navigator.clipboard.writeText(val); link.textContent = "copied"; setTimeout(() => link.textContent = "copy", 1000); }
635
+ catch { link.textContent = "error"; }
636
+ });
637
+ const span = document.createElement("span");
638
+ span.className = "loc-value";
639
+ span.textContent = val;
640
+ td.appendChild(link);
641
+ td.appendChild(span);
642
+ } else {
643
+ td.textContent = val;
644
+ }
645
+ tr.appendChild(td);
646
+ });
647
+ rows.appendChild(tr);
648
+ });
649
+ </script>
650
+ </body>
651
+ </html>`;
652
+ try {
653
+ fs.writeFileSync(filePath, html, "utf-8");
654
+ this.logger.info("FindLocators", `HTML written to ${filePath}`);
655
+ } catch (err) {
656
+ this.logger.warn("FindLocators HTML write failed", err);
657
+ }
658
+ }
659
+ async exists(selector, options = {}) {
660
+ const handle = await this.query(selector, options);
661
+ if (handle) {
662
+ await this.releaseObject(handle.objectId);
663
+ return true;
664
+ }
665
+ return false;
666
+ }
667
+ async isVisible(selector, options = {}) {
668
+ const box = await this.resolveElementBox(selector, options);
669
+ return Boolean(box && box.visible);
670
+ }
671
+ async text(selector, options = {}) {
672
+ return this.evalOnSelector(selector, options, false, `
673
+ if (!el) {
674
+ return null;
675
+ }
676
+ return el.textContent || "";
677
+ `);
678
+ }
679
+ async textSecure(selector, options = {}) {
680
+ const start = Date.now();
681
+ this.events.emit("action:start", { name: "text", selector, frameId: this.id, sensitive: true });
682
+ const result = await this.text(selector, options);
683
+ const duration = Date.now() - start;
684
+ this.events.emit("action:end", { name: "text", selector, frameId: this.id, durationMs: duration, sensitive: true });
685
+ return result;
686
+ }
687
+ async selectOption(selector, value) {
688
+ await this.evaluate(
689
+ (sel, val) => {
690
+ const el = document.querySelector(sel);
691
+ if (!(el instanceof HTMLSelectElement)) return false;
692
+ el.value = val;
693
+ el.dispatchEvent(new Event("input", { bubbles: true }));
694
+ el.dispatchEvent(new Event("change", { bubbles: true }));
695
+ return true;
696
+ },
697
+ selector,
698
+ value
699
+ );
700
+ }
701
+ async setFileInput(selector, name, contents, options = {}) {
702
+ await this.evaluate(
703
+ (sel, fileName, text, mime) => {
704
+ const input = document.querySelector(sel);
705
+ if (!(input instanceof HTMLInputElement)) return false;
706
+ const file = new File([text], fileName, { type: mime || "text/plain" });
707
+ const data = new DataTransfer();
708
+ data.items.add(file);
709
+ input.files = data.files;
710
+ input.dispatchEvent(new Event("input", { bubbles: true }));
711
+ input.dispatchEvent(new Event("change", { bubbles: true }));
712
+ return true;
713
+ },
714
+ selector,
715
+ name,
716
+ contents,
717
+ options.mimeType || "text/plain"
718
+ );
719
+ }
720
+ async attribute(selector, name, options = {}) {
721
+ return this.evalOnSelector(selector, options, false, `
722
+ if (!el || !(el instanceof Element)) {
723
+ return null;
724
+ }
725
+ return el.getAttribute(${JSON.stringify(name)});
726
+ `);
727
+ }
728
+ async value(selector, options = {}) {
729
+ return this.evalOnSelector(selector, options, false, `
730
+ if (!el) {
731
+ return null;
732
+ }
733
+ if ("value" in el) {
734
+ return el.value ?? "";
735
+ }
736
+ return el.getAttribute("value");
737
+ `);
738
+ }
739
+ async valueSecure(selector, options = {}) {
740
+ const start = Date.now();
741
+ this.events.emit("action:start", { name: "value", selector, frameId: this.id, sensitive: true });
742
+ const result = await this.value(selector, options);
743
+ const duration = Date.now() - start;
744
+ this.events.emit("action:end", { name: "value", selector, frameId: this.id, durationMs: duration, sensitive: true });
745
+ return result;
746
+ }
747
+ async isEnabled(selector, options = {}) {
748
+ return this.evalOnSelector(selector, options, false, `
749
+ if (!el) {
750
+ return null;
751
+ }
752
+ const disabled = Boolean(el.disabled) || el.hasAttribute("disabled");
753
+ const ariaDisabled = el.getAttribute && el.getAttribute("aria-disabled") === "true";
754
+ return !(disabled || ariaDisabled);
755
+ `);
756
+ }
757
+ async isChecked(selector, options = {}) {
758
+ return this.evalOnSelector(selector, options, false, `
759
+ if (!el) {
760
+ return null;
761
+ }
762
+ const aria = el.getAttribute && el.getAttribute("aria-checked");
763
+ if (aria === "true") {
764
+ return true;
765
+ }
766
+ if (aria === "false") {
767
+ return false;
768
+ }
769
+ if ("checked" in el) {
770
+ return Boolean(el.checked);
771
+ }
772
+ return null;
773
+ `);
774
+ }
775
+ async count(selector, options = {}) {
776
+ const parsed = parseSelector(selector);
777
+ const pierce = Boolean(parsed.pierceShadowDom);
778
+ const helpers = serializeShadowDomHelpers();
779
+ const expression = parsed.type === "xpath" ? `(function() {
780
+ const result = document.evaluate(${JSON.stringify(parsed.value)}, document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
781
+ return result.snapshotLength;
782
+ })()` : `(function() {
783
+ const querySelectorAllDeep = ${helpers.querySelectorAllDeep};
784
+ const root = document;
785
+ const selector = ${JSON.stringify(parsed.value)};
786
+ const nodes = ${pierce ? "querySelectorAllDeep(root, selector)" : "root.querySelectorAll(selector)"};
787
+ return nodes.length;
788
+ })()`;
789
+ const params = {
790
+ expression,
791
+ returnByValue: true
792
+ };
793
+ if (this.contextId) {
794
+ params.contextId = this.contextId;
795
+ }
796
+ const result = await this.session.send("Runtime.evaluate", params);
797
+ return result.result.value ?? 0;
798
+ }
799
+ async classes(selector, options = {}) {
800
+ return this.evalOnSelector(selector, options, false, `
801
+ if (!el) {
802
+ return null;
803
+ }
804
+ if (!el.classList) {
805
+ return [];
806
+ }
807
+ return Array.from(el.classList);
808
+ `);
809
+ }
810
+ async css(selector, property, options = {}) {
811
+ return this.evalOnSelector(selector, options, false, `
812
+ if (!el) {
813
+ return null;
814
+ }
815
+ const style = window.getComputedStyle(el);
816
+ return style.getPropertyValue(${JSON.stringify(property)}) || "";
817
+ `);
818
+ }
819
+ async hasFocus(selector, options = {}) {
820
+ return this.evalOnSelector(selector, options, false, `
821
+ if (!el) {
822
+ return null;
823
+ }
824
+ return document.activeElement === el;
825
+ `);
826
+ }
827
+ async isInViewport(selector, options = {}, fully = false) {
828
+ return this.evalOnSelector(selector, options, false, `
829
+ if (!el) {
830
+ return null;
831
+ }
832
+ const rect = el.getBoundingClientRect();
833
+ const viewWidth = window.innerWidth || document.documentElement.clientWidth;
834
+ const viewHeight = window.innerHeight || document.documentElement.clientHeight;
835
+ if (${fully ? "true" : "false"}) {
836
+ return rect.top >= 0 && rect.left >= 0 && rect.bottom <= viewHeight && rect.right <= viewWidth;
837
+ }
838
+ return rect.bottom > 0 && rect.right > 0 && rect.top < viewHeight && rect.left < viewWidth;
839
+ `);
840
+ }
841
+ async isEditable(selector, options = {}) {
842
+ return this.evalOnSelector(selector, options, false, `
843
+ if (!el) {
844
+ return null;
845
+ }
846
+ const disabled = Boolean(el.disabled) || el.hasAttribute("disabled");
847
+ const readOnly = Boolean(el.readOnly) || el.hasAttribute("readonly");
848
+ const ariaDisabled = el.getAttribute && el.getAttribute("aria-disabled") === "true";
849
+ return !(disabled || readOnly || ariaDisabled);
850
+ `);
851
+ }
852
+ async performClick(selector, options, isDouble) {
853
+ const start = Date.now();
854
+ const actionName = isDouble ? "dblclick" : "click";
855
+ this.events.emit("action:start", { name: actionName, selector, frameId: this.id });
856
+ const box = await waitFor(async () => {
857
+ const result = await this.resolveElementBox(selector, options);
858
+ if (!result || !result.visible) {
859
+ return null;
860
+ }
861
+ return result;
862
+ }, { timeoutMs: options.timeoutMs ?? this.defaultTimeout, description: `${actionName} ${selector}` });
863
+ const centerX = box.x + box.width / 2;
864
+ const centerY = box.y + box.height / 2;
865
+ await this.session.send("Input.dispatchMouseEvent", { type: "mouseMoved", x: centerX, y: centerY });
866
+ await this.session.send("Input.dispatchMouseEvent", { type: "mousePressed", x: centerX, y: centerY, button: "left", clickCount: 1, buttons: 1 });
867
+ await this.session.send("Input.dispatchMouseEvent", { type: "mouseReleased", x: centerX, y: centerY, button: "left", clickCount: 1, buttons: 0 });
868
+ if (isDouble) {
869
+ await this.session.send("Input.dispatchMouseEvent", { type: "mouseMoved", x: centerX, y: centerY });
870
+ await this.session.send("Input.dispatchMouseEvent", { type: "mousePressed", x: centerX, y: centerY, button: "left", clickCount: 2, buttons: 1 });
871
+ await this.session.send("Input.dispatchMouseEvent", { type: "mouseReleased", x: centerX, y: centerY, button: "left", clickCount: 2, buttons: 0 });
872
+ }
873
+ const duration = Date.now() - start;
874
+ this.events.emit("action:end", { name: actionName, selector, frameId: this.id, durationMs: duration });
875
+ this.logger.debug("Click", selector, `${duration}ms`);
876
+ }
877
+ async resolveElementBox(selector, options) {
878
+ const parsed = parseSelector(selector);
879
+ const pierce = Boolean(parsed.pierceShadowDom);
880
+ const helpers = serializeShadowDomHelpers();
881
+ const expression = parsed.type === "xpath" ? `(function() {
882
+ const result = document.evaluate(${JSON.stringify(parsed.value)}, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
883
+ if (!result || !(result instanceof Element)) {
884
+ return null;
885
+ }
886
+ result.scrollIntoView({ block: 'center', inline: 'center' });
887
+ const rect = result.getBoundingClientRect();
888
+ const style = window.getComputedStyle(result);
889
+ return { x: rect.x, y: rect.y, width: rect.width, height: rect.height, visible: rect.width > 0 && rect.height > 0 && style.visibility !== 'hidden' && style.display !== 'none' && Number(style.opacity || '1') > 0 };
890
+ })()` : `(function() {
891
+ const querySelectorDeep = ${helpers.querySelectorDeep};
892
+ const root = document;
893
+ const selector = ${JSON.stringify(parsed.value)};
894
+ const el = ${pierce ? "querySelectorDeep(root, selector)" : "root.querySelector(selector)"};
895
+ if (!el) {
896
+ return null;
897
+ }
898
+ el.scrollIntoView({ block: 'center', inline: 'center' });
899
+ const rect = el.getBoundingClientRect();
900
+ const style = window.getComputedStyle(el);
901
+ return { x: rect.x, y: rect.y, width: rect.width, height: rect.height, visible: rect.width > 0 && rect.height > 0 && style.visibility !== 'hidden' && style.display !== 'none' && Number(style.opacity || '1') > 0 };
902
+ })()`;
903
+ const boxParams = {
904
+ expression,
905
+ returnByValue: true
906
+ };
907
+ if (this.contextId) {
908
+ boxParams.contextId = this.contextId;
909
+ }
910
+ const result = await this.session.send("Runtime.evaluate", boxParams);
911
+ return result?.result?.value ?? null;
912
+ }
913
+ async querySelectorInternal(selector, options, forceXPath) {
914
+ const parsed = forceXPath ? { type: "xpath", value: selector.trim(), pierceShadowDom: void 0 } : parseSelector(selector);
915
+ const pierce = Boolean(parsed.pierceShadowDom);
916
+ const helpers = serializeShadowDomHelpers();
917
+ const expression = parsed.type === "xpath" ? `(function() {
918
+ const result = document.evaluate(${JSON.stringify(parsed.value)}, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
919
+ return result || null;
920
+ })()` : `(function() {
921
+ const querySelectorDeep = ${helpers.querySelectorDeep};
922
+ const root = document;
923
+ const selector = ${JSON.stringify(parsed.value)};
924
+ return ${pierce ? "querySelectorDeep(root, selector)" : "root.querySelector(selector)"};
925
+ })()`;
926
+ const queryParams = {
927
+ expression,
928
+ returnByValue: false
929
+ };
930
+ if (this.contextId) {
931
+ queryParams.contextId = this.contextId;
932
+ }
933
+ const response = await this.session.send("Runtime.evaluate", queryParams);
934
+ if (response.result?.subtype === "null" || !response.result?.objectId) {
935
+ return null;
936
+ }
937
+ return { objectId: response.result.objectId, contextId: this.contextId ?? 0 };
938
+ }
939
+ async querySelectorAllInternal(selector, options, forceXPath) {
940
+ const parsed = forceXPath ? { type: "xpath", value: selector.trim(), pierceShadowDom: void 0 } : parseSelector(selector);
941
+ const pierce = Boolean(parsed.pierceShadowDom);
942
+ const helpers = serializeShadowDomHelpers();
943
+ const expression = parsed.type === "xpath" ? `(function() {
944
+ const result = document.evaluate(${JSON.stringify(parsed.value)}, document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
945
+ const nodes = [];
946
+ for (let i = 0; i < result.snapshotLength; i += 1) {
947
+ nodes.push(result.snapshotItem(i));
948
+ }
949
+ return nodes;
950
+ })()` : `(function() {
951
+ const querySelectorAllDeep = ${helpers.querySelectorAllDeep};
952
+ const root = document;
953
+ const selector = ${JSON.stringify(parsed.value)};
954
+ return ${pierce ? "querySelectorAllDeep(root, selector)" : "Array.from(root.querySelectorAll(selector))"};
955
+ })()`;
956
+ const listParams = {
957
+ expression,
958
+ returnByValue: false
959
+ };
960
+ if (this.contextId) {
961
+ listParams.contextId = this.contextId;
962
+ }
963
+ const response = await this.session.send("Runtime.evaluate", listParams);
964
+ if (!response.result?.objectId) {
965
+ return [];
966
+ }
967
+ const properties = await this.session.send("Runtime.getProperties", {
968
+ objectId: response.result.objectId,
969
+ ownProperties: true
970
+ });
971
+ const handles = [];
972
+ for (const prop of properties.result) {
973
+ if (prop.name && !/^\d+$/.test(prop.name)) {
974
+ continue;
975
+ }
976
+ const objectId = prop.value?.objectId;
977
+ if (objectId) {
978
+ handles.push({ objectId, contextId: this.contextId ?? 0 });
979
+ }
980
+ }
981
+ await this.releaseObject(response.result.objectId);
982
+ return handles;
983
+ }
984
+ async evaluateInContext(fnOrString, args) {
985
+ if (typeof fnOrString === "string") {
986
+ const params2 = {
987
+ expression: fnOrString,
988
+ returnByValue: true,
989
+ awaitPromise: true
990
+ };
991
+ if (this.contextId) {
992
+ params2.contextId = this.contextId;
993
+ }
994
+ const result2 = await this.session.send("Runtime.evaluate", params2);
995
+ return result2.result.value;
996
+ }
997
+ const serializedArgs = args.map((arg) => serializeArgument(arg)).join(", ");
998
+ const expression = `(${fnOrString.toString()})(${serializedArgs})`;
999
+ const params = {
1000
+ expression,
1001
+ returnByValue: true,
1002
+ awaitPromise: true
1003
+ };
1004
+ if (this.contextId) {
1005
+ params.contextId = this.contextId;
1006
+ }
1007
+ const result = await this.session.send("Runtime.evaluate", params);
1008
+ return result.result.value;
1009
+ }
1010
+ async releaseObject(objectId) {
1011
+ try {
1012
+ await this.session.send("Runtime.releaseObject", { objectId });
1013
+ } catch {
1014
+ }
1015
+ }
1016
+ buildElementExpression(selector, options, forceXPath, body) {
1017
+ const parsed = forceXPath ? { type: "xpath", value: selector.trim(), pierceShadowDom: void 0 } : parseSelector(selector);
1018
+ const pierce = options.pierceShadowDom ?? Boolean(parsed.pierceShadowDom);
1019
+ const helpers = serializeShadowDomHelpers();
1020
+ if (parsed.type === "xpath") {
1021
+ return `(function() {
1022
+ const el = document.evaluate(${JSON.stringify(parsed.value)}, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
1023
+ ${body}
1024
+ })()`;
1025
+ }
1026
+ return `(function() {
1027
+ const querySelectorDeep = ${helpers.querySelectorDeep};
1028
+ const root = document;
1029
+ const selector = ${JSON.stringify(parsed.value)};
1030
+ const el = ${pierce ? "querySelectorDeep(root, selector)" : "root.querySelector(selector)"};
1031
+ ${body}
1032
+ })()`;
1033
+ }
1034
+ async evalOnSelector(selector, options, forceXPath, body) {
1035
+ const expression = this.buildElementExpression(selector, options, forceXPath, body);
1036
+ const params = {
1037
+ expression,
1038
+ returnByValue: true
1039
+ };
1040
+ if (this.contextId) {
1041
+ params.contextId = this.contextId;
1042
+ }
1043
+ const result = await this.session.send("Runtime.evaluate", params);
1044
+ return result.result.value;
1045
+ }
1046
+ };
1047
+ function serializeArgument(value) {
1048
+ if (value === void 0) {
1049
+ return "undefined";
1050
+ }
1051
+ return JSON.stringify(value);
1052
+ }
1053
+
1054
+ // src/core/UrlGuard.ts
1055
+ function ensureAllowedUrl(url, options = {}) {
1056
+ let parsed;
1057
+ try {
1058
+ parsed = new URL(url);
1059
+ } catch {
1060
+ throw new Error(`Invalid URL: ${url}`);
1061
+ }
1062
+ if (parsed.protocol === "http:" || parsed.protocol === "https:") {
1063
+ return;
1064
+ }
1065
+ if (parsed.protocol === "file:" && options.allowFileUrl) {
1066
+ return;
1067
+ }
1068
+ throw new Error(`URL protocol not allowed: ${parsed.protocol}`);
1069
+ }
1070
+
1071
+ // src/core/Page.ts
1072
+ var Page = class {
1073
+ session;
1074
+ logger;
1075
+ events;
1076
+ framesById = /* @__PURE__ */ new Map();
1077
+ mainFrameId;
1078
+ lifecycleEvents = /* @__PURE__ */ new Map();
1079
+ defaultTimeout = 3e4;
1080
+ constructor(session, logger, events) {
1081
+ this.session = session;
1082
+ this.logger = logger;
1083
+ this.events = events;
1084
+ }
1085
+ async initialize() {
1086
+ await this.session.send("Page.enable");
1087
+ await this.session.send("DOM.enable");
1088
+ await this.session.send("Runtime.enable");
1089
+ await this.session.send("Network.enable");
1090
+ await this.session.send("Page.setLifecycleEventsEnabled", { enabled: true });
1091
+ this.session.on("Page.frameAttached", (params) => this.onFrameAttached(params));
1092
+ this.session.on("Page.frameNavigated", (params) => this.onFrameNavigated(params));
1093
+ this.session.on("Page.frameDetached", (params) => this.onFrameDetached(params));
1094
+ this.session.on("Runtime.executionContextCreated", (params) => this.onExecutionContextCreated(params));
1095
+ this.session.on("Runtime.executionContextDestroyed", (params) => this.onExecutionContextDestroyed(params));
1096
+ this.session.on("Runtime.executionContextsCleared", () => this.onExecutionContextsCleared());
1097
+ this.session.on("Page.lifecycleEvent", (params) => this.onLifecycleEvent(params));
1098
+ const tree = await this.session.send("Page.getFrameTree");
1099
+ this.buildFrameTree(tree.frameTree);
1100
+ }
1101
+ frames() {
1102
+ return Array.from(this.framesById.values());
1103
+ }
1104
+ mainFrame() {
1105
+ if (!this.mainFrameId) {
1106
+ throw new Error("Main frame not initialized");
1107
+ }
1108
+ const frame = this.framesById.get(this.mainFrameId);
1109
+ if (!frame) {
1110
+ throw new Error("Main frame missing");
1111
+ }
1112
+ return frame;
1113
+ }
1114
+ frame(options) {
1115
+ for (const frame of this.framesById.values()) {
1116
+ if (options.name && frame.name !== options.name) continue;
1117
+ if (options.urlIncludes && !frame.url?.includes(options.urlIncludes)) continue;
1118
+ if (options.predicate && !options.predicate(frame)) continue;
1119
+ return frame;
1120
+ }
1121
+ return null;
1122
+ }
1123
+ locator(selector) {
1124
+ return new Locator(this.mainFrame(), selector);
1125
+ }
1126
+ async goto(url, options = {}) {
1127
+ ensureAllowedUrl(url, { allowFileUrl: options.allowFileUrl });
1128
+ const waitUntil = options.waitUntil ?? "load";
1129
+ const lifecycleName = waitUntil === "domcontentloaded" ? "DOMContentLoaded" : waitUntil === "load" ? "load" : null;
1130
+ if (!lifecycleName) {
1131
+ throw new Error(`Invalid waitUntil "${waitUntil}". Use "load" or "domcontentloaded".`);
1132
+ }
1133
+ const timeoutMs = options.timeoutMs ?? this.defaultTimeout;
1134
+ this.events.emit("action:start", { name: "goto", selector: url, frameId: this.mainFrameId });
1135
+ const start = Date.now();
1136
+ this.lifecycleEvents.clear();
1137
+ const navigation = await this.session.send("Page.navigate", { url });
1138
+ if (navigation?.errorText) {
1139
+ throw new Error(`Navigation failed: ${navigation.errorText}`);
1140
+ }
1141
+ await this.waitForLifecycle(this.mainFrameId, lifecycleName, timeoutMs);
1142
+ const duration = Date.now() - start;
1143
+ this.events.emit("action:end", { name: "goto", selector: url, frameId: this.mainFrameId, durationMs: duration });
1144
+ this.logger.debug("Goto", url, `${duration}ms`);
1145
+ }
1146
+ async query(selector) {
1147
+ return this.mainFrame().query(selector);
1148
+ }
1149
+ async queryAll(selector) {
1150
+ return this.mainFrame().queryAll(selector);
1151
+ }
1152
+ async queryXPath(selector) {
1153
+ return this.mainFrame().queryXPath(selector);
1154
+ }
1155
+ async queryAllXPath(selector) {
1156
+ return this.mainFrame().queryAllXPath(selector);
1157
+ }
1158
+ async click(selector, options) {
1159
+ return this.mainFrame().click(selector, options);
1160
+ }
1161
+ async dblclick(selector, options) {
1162
+ return this.mainFrame().dblclick(selector, options);
1163
+ }
1164
+ async type(selector, text, options) {
1165
+ return this.mainFrame().type(selector, text, options);
1166
+ }
1167
+ async typeSecure(selector, text, options) {
1168
+ return this.mainFrame().typeSecure(selector, text, options);
1169
+ }
1170
+ async fillInput(selector, value, options = {}) {
1171
+ return this.mainFrame().fillInput(selector, value, options);
1172
+ }
1173
+ async evaluate(fnOrString, ...args) {
1174
+ return this.mainFrame().evaluate(fnOrString, ...args);
1175
+ }
1176
+ async textSecure(selector) {
1177
+ return this.mainFrame().textSecure(selector);
1178
+ }
1179
+ async valueSecure(selector) {
1180
+ return this.mainFrame().valueSecure(selector);
1181
+ }
1182
+ async selectOption(selector, value) {
1183
+ return this.mainFrame().selectOption(selector, value);
1184
+ }
1185
+ async setFileInput(selector, name, contents, options = {}) {
1186
+ return this.mainFrame().setFileInput(selector, name, contents, options);
1187
+ }
1188
+ async findLocators(options = {}) {
1189
+ return this.mainFrame().findLocators(options);
1190
+ }
1191
+ async screenshot(options = {}) {
1192
+ const start = Date.now();
1193
+ this.events.emit("action:start", { name: "screenshot", frameId: this.mainFrameId });
1194
+ const result = await this.session.send("Page.captureScreenshot", {
1195
+ format: options.format ?? "png",
1196
+ quality: options.quality,
1197
+ fromSurface: true
1198
+ });
1199
+ const buffer = Buffer.from(result.data, "base64");
1200
+ if (options.path) {
1201
+ const resolved = path2.resolve(options.path);
1202
+ fs2.writeFileSync(resolved, buffer);
1203
+ }
1204
+ const duration = Date.now() - start;
1205
+ this.events.emit("action:end", { name: "screenshot", frameId: this.mainFrameId, durationMs: duration });
1206
+ return buffer;
1207
+ }
1208
+ async screenshotBase64(options = {}) {
1209
+ const start = Date.now();
1210
+ this.events.emit("action:start", { name: "screenshotBase64", frameId: this.mainFrameId });
1211
+ const result = await this.session.send("Page.captureScreenshot", {
1212
+ format: options.format ?? "png",
1213
+ quality: options.quality,
1214
+ fromSurface: true
1215
+ });
1216
+ const duration = Date.now() - start;
1217
+ this.events.emit("action:end", { name: "screenshotBase64", frameId: this.mainFrameId, durationMs: duration });
1218
+ return result.data;
1219
+ }
1220
+ getEvents() {
1221
+ return this.events;
1222
+ }
1223
+ getDefaultTimeout() {
1224
+ return this.defaultTimeout;
1225
+ }
1226
+ buildFrameTree(tree) {
1227
+ const frame = this.ensureFrame(tree.frame.id);
1228
+ frame.setMeta({ name: tree.frame.name, url: tree.frame.url, parentId: tree.frame.parentId });
1229
+ if (!tree.frame.parentId) {
1230
+ this.mainFrameId = tree.frame.id;
1231
+ }
1232
+ if (tree.childFrames) {
1233
+ for (const child of tree.childFrames) {
1234
+ this.buildFrameTree(child);
1235
+ }
1236
+ }
1237
+ }
1238
+ ensureFrame(id) {
1239
+ let frame = this.framesById.get(id);
1240
+ if (!frame) {
1241
+ frame = new Frame(id, this.session, this.logger, this.events);
1242
+ this.framesById.set(id, frame);
1243
+ }
1244
+ return frame;
1245
+ }
1246
+ onFrameAttached(params) {
1247
+ const frame = this.ensureFrame(params.frameId);
1248
+ frame.setMeta({ parentId: params.parentFrameId });
1249
+ }
1250
+ onFrameNavigated(params) {
1251
+ const frame = this.ensureFrame(params.frame.id);
1252
+ frame.setMeta({ name: params.frame.name, url: params.frame.url, parentId: params.frame.parentId });
1253
+ if (!params.frame.parentId) {
1254
+ this.mainFrameId = params.frame.id;
1255
+ }
1256
+ }
1257
+ onFrameDetached(params) {
1258
+ this.framesById.delete(params.frameId);
1259
+ }
1260
+ onExecutionContextCreated(params) {
1261
+ const frameId = params.context.auxData?.frameId;
1262
+ if (!frameId) {
1263
+ return;
1264
+ }
1265
+ const frame = this.ensureFrame(frameId);
1266
+ frame.setExecutionContext(params.context.id);
1267
+ }
1268
+ onExecutionContextDestroyed(params) {
1269
+ for (const frame of this.framesById.values()) {
1270
+ if (frame.getExecutionContext() === params.executionContextId) {
1271
+ frame.setExecutionContext(void 0);
1272
+ }
1273
+ }
1274
+ }
1275
+ onExecutionContextsCleared() {
1276
+ for (const frame of this.framesById.values()) {
1277
+ frame.setExecutionContext(void 0);
1278
+ }
1279
+ }
1280
+ onLifecycleEvent(params) {
1281
+ if (!this.lifecycleEvents.has(params.frameId)) {
1282
+ this.lifecycleEvents.set(params.frameId, /* @__PURE__ */ new Set());
1283
+ }
1284
+ this.lifecycleEvents.get(params.frameId).add(params.name);
1285
+ }
1286
+ async waitForLifecycle(frameId, eventName, timeoutMs) {
1287
+ if (!frameId) {
1288
+ throw new Error("Missing frame id for lifecycle wait");
1289
+ }
1290
+ const start = Date.now();
1291
+ while (Date.now() - start < timeoutMs) {
1292
+ const events = this.lifecycleEvents.get(frameId);
1293
+ if (events && events.has(eventName)) {
1294
+ return;
1295
+ }
1296
+ await new Promise((resolve) => setTimeout(resolve, 100));
1297
+ }
1298
+ throw new Error(`Timeout waiting for lifecycle event: ${eventName}`);
1299
+ }
1300
+ };
1301
+
1302
+ // src/assert/expect.ts
1303
+ var ElementExpectation = class _ElementExpectation {
1304
+ frame;
1305
+ selector;
1306
+ options;
1307
+ negate;
1308
+ events;
1309
+ constructor(frame, selector, options, negate, events) {
1310
+ this.frame = frame;
1311
+ this.selector = selector;
1312
+ this.options = options;
1313
+ this.negate = negate;
1314
+ this.events = events;
1315
+ }
1316
+ get not() {
1317
+ return new _ElementExpectation(this.frame, this.selector, this.options, !this.negate, this.events);
1318
+ }
1319
+ async toExist() {
1320
+ return this.assert(async () => {
1321
+ const exists = await this.frame.exists(this.selector, this.options);
1322
+ return this.negate ? !exists : exists;
1323
+ }, this.negate ? "Expected element not to exist" : "Expected element to exist");
1324
+ }
1325
+ async toBeVisible() {
1326
+ return this.assert(async () => {
1327
+ const visible = await this.frame.isVisible(this.selector, this.options);
1328
+ return this.negate ? !visible : visible;
1329
+ }, this.negate ? "Expected element not to be visible" : "Expected element to be visible");
1330
+ }
1331
+ async toBeHidden() {
1332
+ return this.assert(async () => {
1333
+ const visible = await this.frame.isVisible(this.selector, this.options);
1334
+ return this.negate ? visible : !visible;
1335
+ }, this.negate ? "Expected element not to be hidden" : "Expected element to be hidden");
1336
+ }
1337
+ async toBeEnabled() {
1338
+ return this.assert(async () => {
1339
+ const enabled = await this.frame.isEnabled(this.selector, this.options);
1340
+ if (enabled == null) {
1341
+ return this.negate ? true : false;
1342
+ }
1343
+ return this.negate ? !enabled : enabled;
1344
+ }, this.negate ? "Expected element not to be enabled" : "Expected element to be enabled");
1345
+ }
1346
+ async toBeDisabled() {
1347
+ return this.assert(async () => {
1348
+ const enabled = await this.frame.isEnabled(this.selector, this.options);
1349
+ if (enabled == null) {
1350
+ return this.negate ? true : false;
1351
+ }
1352
+ const disabled = !enabled;
1353
+ return this.negate ? !disabled : disabled;
1354
+ }, this.negate ? "Expected element not to be disabled" : "Expected element to be disabled");
1355
+ }
1356
+ async toBeChecked() {
1357
+ return this.assert(async () => {
1358
+ const checked = await this.frame.isChecked(this.selector, this.options);
1359
+ if (checked == null) {
1360
+ return this.negate ? true : false;
1361
+ }
1362
+ return this.negate ? !checked : checked;
1363
+ }, this.negate ? "Expected element not to be checked" : "Expected element to be checked");
1364
+ }
1365
+ async toBeUnchecked() {
1366
+ return this.assert(async () => {
1367
+ const checked = await this.frame.isChecked(this.selector, this.options);
1368
+ if (checked == null) {
1369
+ return this.negate ? true : false;
1370
+ }
1371
+ const unchecked = !checked;
1372
+ return this.negate ? !unchecked : unchecked;
1373
+ }, this.negate ? "Expected element not to be unchecked" : "Expected element to be unchecked");
1374
+ }
1375
+ async toHaveText(textOrRegex) {
1376
+ const expected = textOrRegex;
1377
+ return this.assert(async () => {
1378
+ const text = await this.frame.text(this.selector, this.options);
1379
+ if (text == null) {
1380
+ return this.negate ? true : false;
1381
+ }
1382
+ const matches = expected instanceof RegExp ? new RegExp(expected.source, expected.flags.replace("g", "")).test(text) : text.includes(expected);
1383
+ return this.negate ? !matches : matches;
1384
+ }, this.negate ? "Expected element text not to match" : "Expected element text to match", { expected });
1385
+ }
1386
+ async toHaveExactText(textOrRegex) {
1387
+ const expected = textOrRegex;
1388
+ return this.assert(async () => {
1389
+ const text = await this.frame.text(this.selector, this.options);
1390
+ if (text == null) {
1391
+ return this.negate ? true : false;
1392
+ }
1393
+ const matches = expected instanceof RegExp ? new RegExp(expected.source, expected.flags.replace("g", "")).test(text) : text === expected;
1394
+ return this.negate ? !matches : matches;
1395
+ }, this.negate ? "Expected element text not to match exactly" : "Expected element text to match exactly", { expected });
1396
+ }
1397
+ async toContainText(textOrRegex) {
1398
+ const expected = textOrRegex;
1399
+ return this.assert(async () => {
1400
+ const text = await this.frame.text(this.selector, this.options);
1401
+ if (text == null) {
1402
+ return this.negate ? true : false;
1403
+ }
1404
+ const matches = expected instanceof RegExp ? new RegExp(expected.source, expected.flags.replace("g", "")).test(text) : text.includes(expected);
1405
+ return this.negate ? !matches : matches;
1406
+ }, this.negate ? "Expected element text not to contain" : "Expected element text to contain", { expected });
1407
+ }
1408
+ async toHaveValue(valueOrRegex) {
1409
+ const expected = valueOrRegex;
1410
+ return this.assert(async () => {
1411
+ const value = await this.frame.value(this.selector, this.options);
1412
+ if (value == null) {
1413
+ return this.negate ? true : false;
1414
+ }
1415
+ const matches = expected instanceof RegExp ? new RegExp(expected.source, expected.flags.replace("g", "")).test(value) : value === expected;
1416
+ return this.negate ? !matches : matches;
1417
+ }, this.negate ? "Expected element value not to match" : "Expected element value to match", { expected });
1418
+ }
1419
+ async toHaveAttribute(name, valueOrRegex) {
1420
+ const expected = valueOrRegex;
1421
+ return this.assert(async () => {
1422
+ const value = await this.frame.attribute(this.selector, name, this.options);
1423
+ if (expected === void 0) {
1424
+ const exists = value != null;
1425
+ return this.negate ? !exists : exists;
1426
+ }
1427
+ if (value == null) {
1428
+ return this.negate ? true : false;
1429
+ }
1430
+ const matches = expected instanceof RegExp ? new RegExp(expected.source, expected.flags.replace("g", "")).test(value) : value === expected;
1431
+ return this.negate ? !matches : matches;
1432
+ }, this.negate ? "Expected element attribute not to match" : "Expected element attribute to match", { expected, name });
1433
+ }
1434
+ async toHaveId(idOrRegex) {
1435
+ return this.toHaveAttribute("id", idOrRegex);
1436
+ }
1437
+ async toHaveName(nameOrRegex) {
1438
+ return this.toHaveAttribute("name", nameOrRegex);
1439
+ }
1440
+ async toHaveCount(expected) {
1441
+ return this.assert(async () => {
1442
+ const count = await this.frame.count(this.selector, this.options);
1443
+ const matches = count === expected;
1444
+ return this.negate ? !matches : matches;
1445
+ }, this.negate ? "Expected element count not to match" : "Expected element count to match", { expected });
1446
+ }
1447
+ async toHaveClass(nameOrRegex) {
1448
+ const expected = nameOrRegex;
1449
+ return this.assert(async () => {
1450
+ const classes = await this.frame.classes(this.selector, this.options);
1451
+ if (classes == null) {
1452
+ return this.negate ? true : false;
1453
+ }
1454
+ const matches = expected instanceof RegExp ? classes.some((value) => new RegExp(expected.source, expected.flags.replace("g", "")).test(value)) : classes.includes(expected);
1455
+ return this.negate ? !matches : matches;
1456
+ }, this.negate ? "Expected element class not to match" : "Expected element class to match", { expected });
1457
+ }
1458
+ async toHaveClasses(expected) {
1459
+ return this.assert(async () => {
1460
+ const classes = await this.frame.classes(this.selector, this.options);
1461
+ if (classes == null) {
1462
+ return this.negate ? true : false;
1463
+ }
1464
+ const matches = expected.every((value) => classes.includes(value));
1465
+ return this.negate ? !matches : matches;
1466
+ }, this.negate ? "Expected element classes not to match" : "Expected element classes to match", { expected });
1467
+ }
1468
+ async toHaveCss(property, valueOrRegex) {
1469
+ const expected = valueOrRegex;
1470
+ return this.assert(async () => {
1471
+ const value = await this.frame.css(this.selector, property, this.options);
1472
+ if (value == null) {
1473
+ return this.negate ? true : false;
1474
+ }
1475
+ const actual = value.trim();
1476
+ const matches = expected instanceof RegExp ? new RegExp(expected.source, expected.flags.replace("g", "")).test(actual) : actual === expected;
1477
+ return this.negate ? !matches : matches;
1478
+ }, this.negate ? "Expected element css not to match" : "Expected element css to match", { expected, property });
1479
+ }
1480
+ async toHaveFocus() {
1481
+ return this.assert(async () => {
1482
+ const focused = await this.frame.hasFocus(this.selector, this.options);
1483
+ if (focused == null) {
1484
+ return this.negate ? true : false;
1485
+ }
1486
+ return this.negate ? !focused : focused;
1487
+ }, this.negate ? "Expected element not to have focus" : "Expected element to have focus");
1488
+ }
1489
+ async toBeInViewport(options = {}) {
1490
+ return this.assert(async () => {
1491
+ const inViewport = await this.frame.isInViewport(this.selector, this.options, Boolean(options.fully));
1492
+ if (inViewport == null) {
1493
+ return this.negate ? true : false;
1494
+ }
1495
+ return this.negate ? !inViewport : inViewport;
1496
+ }, this.negate ? "Expected element not to be in viewport" : "Expected element to be in viewport");
1497
+ }
1498
+ async toBeEditable() {
1499
+ return this.assert(async () => {
1500
+ const editable = await this.frame.isEditable(this.selector, this.options);
1501
+ if (editable == null) {
1502
+ return this.negate ? true : false;
1503
+ }
1504
+ return this.negate ? !editable : editable;
1505
+ }, this.negate ? "Expected element not to be editable" : "Expected element to be editable");
1506
+ }
1507
+ async assert(predicate, message, details = {}) {
1508
+ const timeoutMs = this.options.timeoutMs ?? 3e4;
1509
+ const start = Date.now();
1510
+ this.events.emit("assertion:start", { name: message, selector: this.selector, frameId: this.frame.id });
1511
+ let lastState;
1512
+ try {
1513
+ await waitFor(async () => {
1514
+ const result = await predicate();
1515
+ lastState = result;
1516
+ return result;
1517
+ }, { timeoutMs, description: message });
1518
+ } catch {
1519
+ const duration2 = Date.now() - start;
1520
+ this.events.emit("assertion:end", { name: message, selector: this.selector, frameId: this.frame.id, durationMs: duration2, status: "failed" });
1521
+ throw new AssertionError(message, { selector: this.selector, timeoutMs, lastState: { lastState, ...details } });
1522
+ }
1523
+ const duration = Date.now() - start;
1524
+ this.events.emit("assertion:end", { name: message, selector: this.selector, frameId: this.frame.id, durationMs: duration, status: "passed" });
1525
+ }
1526
+ };
1527
+ var ExpectFrame = class {
1528
+ frame;
1529
+ events;
1530
+ constructor(frame, events) {
1531
+ this.frame = frame;
1532
+ this.events = events;
1533
+ }
1534
+ element(selector, options = {}) {
1535
+ return new ElementExpectation(this.frame, selector, options, false, this.events);
1536
+ }
1537
+ };
1538
+ function expect(page) {
1539
+ return {
1540
+ element: (selector, options = {}) => new ElementExpectation(page.mainFrame(), selector, options, false, page.getEvents()),
1541
+ frame: (options) => {
1542
+ const frame = page.frame(options);
1543
+ if (!frame) {
1544
+ throw new AssertionError("Frame not found", { selector: JSON.stringify(options) });
1545
+ }
1546
+ return new ExpectFrame(frame, page.getEvents());
1547
+ }
1548
+ };
1549
+ }
1550
+ Page.prototype.expect = function(selector, options) {
1551
+ const builder = expect(this);
1552
+ if (selector) {
1553
+ return builder.element(selector, options);
1554
+ }
1555
+ return builder;
1556
+ };
1557
+ Object.defineProperty(Page.prototype, "find", {
1558
+ get: function() {
1559
+ return {
1560
+ locators: (options) => this.findLocators(options)
1561
+ };
1562
+ }
1563
+ });
1564
+
1565
+ export {
1566
+ Locator,
1567
+ Frame,
1568
+ Page,
1569
+ AssertionError,
1570
+ expect
1571
+ };
1572
+ //# sourceMappingURL=chunk-6BPF3IEU.js.map