@piwitests/reporter 0.5.0 → 0.7.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.
Files changed (76) hide show
  1. package/README.md +37 -26
  2. package/dist/global-setup-module.js +2 -2
  3. package/dist/index.d.ts +13 -4
  4. package/dist/index.js +19 -5
  5. package/dist/internal/capture/attachments.d.ts +19 -0
  6. package/dist/internal/capture/attachments.js +22 -0
  7. package/dist/internal/capture/capture-fixtures.d.ts +41 -0
  8. package/dist/internal/capture/capture-fixtures.js +562 -0
  9. package/dist/{locator-healing.d.ts → internal/capture/locator-healing.d.ts} +47 -1
  10. package/dist/{locator-healing.js → internal/capture/locator-healing.js} +113 -29
  11. package/dist/internal/collect/error-text.d.ts +18 -0
  12. package/dist/internal/collect/error-text.js +79 -0
  13. package/dist/{metadata-collector.d.ts → internal/collect/metadata-collector.d.ts} +2 -2
  14. package/dist/{metadata-collector.js → internal/collect/metadata-collector.js} +12 -9
  15. package/dist/{skip-classify.d.ts → internal/collect/skip-classify.d.ts} +1 -1
  16. package/dist/{step-analyzer.d.ts → internal/collect/step-analyzer.d.ts} +6 -0
  17. package/dist/{step-analyzer.js → internal/collect/step-analyzer.js} +7 -2
  18. package/dist/internal/config/env.d.ts +45 -0
  19. package/dist/{config.js → internal/config/env.js} +50 -35
  20. package/dist/internal/files/compression.js +69 -0
  21. package/dist/{file-handler.d.ts → internal/files/file-handler.d.ts} +4 -4
  22. package/dist/{file-handler.js → internal/files/file-handler.js} +61 -29
  23. package/dist/{crash-recovery.d.ts → internal/streaming/crash-recovery.d.ts} +5 -5
  24. package/dist/{crash-recovery.js → internal/streaming/crash-recovery.js} +9 -8
  25. package/dist/{stream-buffer.d.ts → internal/streaming/stream-buffer.d.ts} +2 -2
  26. package/dist/{stream-buffer.js → internal/streaming/stream-buffer.js} +5 -5
  27. package/dist/{stream-manager.d.ts → internal/streaming/stream-manager.d.ts} +8 -8
  28. package/dist/{stream-manager.js → internal/streaming/stream-manager.js} +16 -13
  29. package/dist/{run-submitter.d.ts → internal/submit/run-submitter.d.ts} +7 -7
  30. package/dist/{run-submitter.js → internal/submit/run-submitter.js} +11 -9
  31. package/dist/{serializer.d.ts → internal/submit/serializer.d.ts} +1 -1
  32. package/dist/{uploader.d.ts → internal/submit/uploader.d.ts} +8 -8
  33. package/dist/{uploader.js → internal/submit/uploader.js} +45 -10
  34. package/dist/internal/support/ci.d.ts +2 -0
  35. package/dist/internal/support/ci.js +32 -0
  36. package/dist/internal/support/cli-filters.d.ts +1 -0
  37. package/dist/internal/support/cli-filters.js +51 -0
  38. package/dist/internal/support/errors.d.ts +8 -0
  39. package/dist/internal/support/errors.js +15 -0
  40. package/dist/internal/support/instance-id.d.ts +4 -0
  41. package/dist/internal/support/instance-id.js +48 -0
  42. package/dist/internal/support/limiter.d.ts +2 -0
  43. package/dist/internal/support/limiter.js +27 -0
  44. package/dist/internal/support/setup-file.d.ts +13 -0
  45. package/dist/internal/support/setup-file.js +61 -0
  46. package/dist/internal/support/source-snippet.d.ts +12 -0
  47. package/dist/internal/support/source-snippet.js +97 -0
  48. package/dist/internal/support/worker-index.d.ts +7 -0
  49. package/dist/internal/support/worker-index.js +14 -0
  50. package/dist/{http-client.d.ts → internal/transport/http-client.d.ts} +11 -1
  51. package/dist/{http-client.js → internal/transport/http-client.js} +57 -10
  52. package/dist/{config-wrapper.d.ts → public/config-wrapper.d.ts} +1 -1
  53. package/dist/{config-wrapper.js → public/config-wrapper.js} +4 -4
  54. package/dist/public/global-setup.d.ts +13 -0
  55. package/dist/public/global-setup.js +146 -0
  56. package/dist/{config.d.ts → public/options.d.ts} +7 -45
  57. package/dist/public/options.js +2 -0
  58. package/dist/{reporter.d.ts → public/reporter.d.ts} +1 -1
  59. package/dist/{reporter.js → public/reporter.js} +44 -54
  60. package/dist/types/collected.d.ts +96 -0
  61. package/dist/types/collected.js +10 -0
  62. package/dist/types/wire.d.ts +174 -0
  63. package/dist/types/wire.js +14 -0
  64. package/dist/types.d.ts +8 -253
  65. package/dist/types.js +23 -10
  66. package/package.json +1 -6
  67. package/dist/compression.js +0 -39
  68. package/dist/fixtures.d.ts +0 -25
  69. package/dist/fixtures.js +0 -336
  70. package/dist/helpers.d.ts +0 -44
  71. package/dist/helpers.js +0 -312
  72. /package/dist/{skip-classify.js → internal/collect/skip-classify.js} +0 -0
  73. /package/dist/{compression.d.ts → internal/files/compression.d.ts} +0 -0
  74. /package/dist/{serializer.js → internal/submit/serializer.js} +0 -0
  75. /package/dist/{logger.d.ts → internal/support/logger.d.ts} +0 -0
  76. /package/dist/{logger.js → internal/support/logger.js} +0 -0
@@ -0,0 +1,562 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.dashboardFixtures = void 0;
4
+ exports.ariaSnapshotBestEffort = ariaSnapshotBestEffort;
5
+ exports.extendDashboardFixtures = extendDashboardFixtures;
6
+ const node_zlib_1 = require("node:zlib");
7
+ const locator_healing_js_1 = require("./locator-healing.js");
8
+ const attachments_js_1 = require("./attachments.js");
9
+ function createSink() {
10
+ return {
11
+ networkRequests: [],
12
+ consoleEntries: [],
13
+ pendingHandlers: [],
14
+ capturedLocators: [],
15
+ capturePromises: [],
16
+ failedLocators: [],
17
+ lastActivePage: null,
18
+ };
19
+ }
20
+ /**
21
+ * The capture sink for the test currently running in this worker. Playwright
22
+ * runs a worker's tests sequentially, so a single "current" sink is unambiguous.
23
+ * It is null between tests and during `beforeAll`/`afterAll` — activity outside a
24
+ * test (auth setup, teardown) is intentionally not captured.
25
+ */
26
+ let currentSink = null;
27
+ // Idempotency guards: a page/context/browser can be reached through several
28
+ // paths (browser patch, context patch, the `page` fixture, popup events), and
29
+ // must be wrapped exactly once.
30
+ const INSTRUMENTED_PAGES = new WeakSet();
31
+ const INSTRUMENTED_CONTEXTS = new WeakSet();
32
+ const PATCHED_BROWSERS = new WeakSet();
33
+ // O(1) membership for the proxy `get` trap below, which runs on every property
34
+ // access of every wrapped locator. The source arrays from locator-healing are
35
+ // turned into Sets once here.
36
+ const CHAIN_METHOD_SET = new Set(locator_healing_js_1.CHAIN_METHODS);
37
+ const ACTION_METHOD_SET = new Set(locator_healing_js_1.ACTION_METHODS);
38
+ const FORM_FIELD_TAGS = new Set(['input', 'select', 'textarea']);
39
+ // CAPTURED_ATTRIBUTES is passed verbatim into evaluate() on every action — copy
40
+ // it once instead of spreading a fresh array per call.
41
+ const CAPTURED_ATTRS_ARG = [...locator_healing_js_1.CAPTURED_ATTRIBUTES];
42
+ /**
43
+ * ARIA snapshot that tolerates every Playwright version the reporter supports,
44
+ * returning null instead of throwing so a capture can never fail the test. The
45
+ * options validator runs client-side in the user's installed Playwright, so an
46
+ * unsupported key surfaces as a rejected promise (not a synchronous throw):
47
+ * - ≥ 1.59: `mode: 'ai'` yields the ref-annotated AI snapshot (the ideal);
48
+ * `ref` is unknown and silently stripped.
49
+ * - 1.52: `mode: 'ai'` fails validation (only 'raw'/'regex' are accepted) and
50
+ * rejects; the `{ ref: true }` fallback then yields a ref-annotated snapshot.
51
+ * - 1.53–1.58: neither `ref` nor `mode` exists — unknown keys are stripped
52
+ * server-side, so the first call already returns a plain flat snapshot.
53
+ * - < 1.49: `locator.ariaSnapshot` does not exist — returns null up front.
54
+ */
55
+ async function ariaSnapshotBestEffort(target, timeout) {
56
+ // Playwright < 1.49 predates locator.ariaSnapshot entirely.
57
+ if (typeof target.ariaSnapshot !== 'function')
58
+ return null;
59
+ try {
60
+ return await target.ariaSnapshot({
61
+ ...(timeout != null ? { timeout } : {}),
62
+ mode: 'ai',
63
+ });
64
+ }
65
+ catch {
66
+ // 1.52 rejects `mode: 'ai'`; retry with the `ref` flag that release accepts.
67
+ try {
68
+ return await target.ariaSnapshot({
69
+ ...(timeout != null ? { timeout } : {}),
70
+ ref: true,
71
+ });
72
+ }
73
+ catch {
74
+ return null;
75
+ }
76
+ }
77
+ }
78
+ // Chain methods that take args and define a new locator scope (not just narrow).
79
+ // Origin method/args update to the chain call, e.g. .locator('.item') → locator('.item').
80
+ // Positional/filter chains that narrow but don't change locator identity.
81
+ // Origin stays from the page-level call, e.g. .first(), .nth(2), .filter(...).
82
+ function wrapLocator(page, locator, originMethod, originArgs) {
83
+ return new Proxy(locator, {
84
+ get(target, prop) {
85
+ const original = Reflect.get(target, prop);
86
+ if (typeof original !== 'function')
87
+ return original;
88
+ const fn = original;
89
+ if (CHAIN_METHOD_SET.has(prop)) {
90
+ return (...args) => {
91
+ const next = fn.apply(target, args);
92
+ if (locator_healing_js_1.LOCATOR_CREATING_CHAINS.has(prop)) {
93
+ return wrapLocator(page, next, String(prop), args);
94
+ }
95
+ return wrapLocator(page, next, originMethod, originArgs);
96
+ };
97
+ }
98
+ if (!ACTION_METHOD_SET.has(prop))
99
+ return original;
100
+ return async (...callArgs) => {
101
+ // Bind to the sink active when the action starts so the async element
102
+ // capture below writes back to the right test even across a boundary.
103
+ const sink = currentSink;
104
+ // Action outside a tracked test (e.g. during beforeAll) — run untouched.
105
+ if (!sink)
106
+ return fn.apply(target, callArgs);
107
+ sink.lastActivePage = page;
108
+ const seq = sink.capturedLocators.length;
109
+ // Capture the test call-site now (sync) so the snapshot's location
110
+ // matches the error stack's first user frame — independent of
111
+ // pw:api step ordering, worker interleaving, or concurrent actions.
112
+ const callerLocation = (0, locator_healing_js_1.captureCallerLocation)();
113
+ // Built once, shared by the placeholder and the resolved snapshot below.
114
+ const used = {
115
+ method: originMethod,
116
+ args: originArgs,
117
+ raw: `${originMethod}(${JSON.stringify(originArgs)})`,
118
+ };
119
+ // Push a placeholder immediately — DOM capture runs async below
120
+ sink.capturedLocators.push({
121
+ location: callerLocation,
122
+ used,
123
+ element: null,
124
+ alternatives: [],
125
+ });
126
+ // The placeholder is already pushed. If the action throws, record the
127
+ // failed locator (so teardown can suggest a fresh one for the element's
128
+ // current identity) and re-throw so the test still fails.
129
+ let result;
130
+ try {
131
+ result = await fn.apply(target, callArgs);
132
+ }
133
+ catch (error) {
134
+ sink.failedLocators.push({ method: originMethod, args: originArgs });
135
+ throw error;
136
+ }
137
+ // Fire-and-forget: capture element data without blocking the test.
138
+ // evaluate() can hang when page navigates (element detaches), so
139
+ // race it against a 500ms deadline and never throw.
140
+ const resolveAttrs = (async () => {
141
+ let deadline;
142
+ try {
143
+ // `el` is browser-context (no DOM lib in this Node package), so it
144
+ // stays `any`; the callback's return type pins `attrs` to CapturedAttrs.
145
+ const attrs = await Promise.race([
146
+ target.evaluate((el, keep) => {
147
+ const attrMap = {};
148
+ for (const key of keep) {
149
+ const v = el.getAttribute(key) ?? el[key];
150
+ attrMap[key] = typeof v === 'string' ? v.slice(0, 200) : v ? String(v).slice(0, 200) : null;
151
+ }
152
+ const r = el.getBoundingClientRect();
153
+ // Uniqueness probe: how many elements each candidate selector
154
+ // matches. A count > 1 marks the alternative as ambiguous
155
+ // (strict-mode violation) so generateAlternatives drops it.
156
+ // All DOM/CSS access goes through `el` (no DOM lib here).
157
+ const selectorCounts = {};
158
+ try {
159
+ const doc = el.ownerDocument;
160
+ const cssEsc = (s) => doc.defaultView.CSS.escape(s);
161
+ const count = (sel) => {
162
+ try {
163
+ return doc.querySelectorAll(sel).length;
164
+ }
165
+ catch {
166
+ return undefined;
167
+ }
168
+ };
169
+ if (attrMap['data-testid']) {
170
+ selectorCounts.testId = count(`[data-testid=${JSON.stringify(attrMap['data-testid'])}]`);
171
+ }
172
+ if (attrMap['id'])
173
+ selectorCounts.id = count(`#${cssEsc(attrMap['id'])}`);
174
+ if (attrMap['name'])
175
+ selectorCounts.name = count(`[name=${JSON.stringify(attrMap['name'])}]`);
176
+ const classList = (attrMap['class'] || '')
177
+ .split(/\s+/)
178
+ .filter((c) => c.length > 1)
179
+ .slice(0, 10);
180
+ if (classList.length > 0) {
181
+ const classCounts = {};
182
+ for (const cls of classList) {
183
+ const n = count(`.${cssEsc(cls)}`);
184
+ if (n !== undefined)
185
+ classCounts[cls] = n;
186
+ }
187
+ selectorCounts.classes = classCounts;
188
+ }
189
+ }
190
+ catch {
191
+ // Uniqueness probing is best-effort — never fail the capture.
192
+ }
193
+ return {
194
+ tagName: el.tagName?.toLowerCase?.() ?? 'unknown',
195
+ attributes: attrMap,
196
+ // Collapse whitespace so multi-line text can't produce a
197
+ // getByText suggestion with literal newlines in it.
198
+ textContent: (el.textContent || '').replace(/\s+/g, ' ').trim().slice(0, 80),
199
+ center: {
200
+ x: Math.round(r.x + r.width / 2),
201
+ y: Math.round(r.y + r.height / 2),
202
+ },
203
+ hasLabel: !!(el.labels && el.labels.length > 0),
204
+ selectorCounts,
205
+ };
206
+ }, CAPTURED_ATTRS_ARG),
207
+ new Promise((_, reject) => {
208
+ deadline = setTimeout(() => reject(new Error('locator capture timeout')), 500);
209
+ }),
210
+ ]);
211
+ // The browser-computed accessible name only feeds role-based and
212
+ // form-field alternatives, so only pay for the extra ARIA
213
+ // snapshot when the element actually has a role or is a field.
214
+ const role = (0, locator_healing_js_1.resolveAriaRole)({ ...attrs, accessibleName: null });
215
+ const isFormField = FORM_FIELD_TAGS.has(attrs.tagName);
216
+ // Bound with a timeout: without it, ariaSnapshot waits up to the
217
+ // test timeout when the page is mid-navigation, which hangs the
218
+ // teardown that drains these capture promises.
219
+ // ariaSnapshotBestEffort adapts the options to the installed
220
+ // Playwright version and never throws (see its doc comment).
221
+ const aria = role || isFormField ? await ariaSnapshotBestEffort(target, 500) : null;
222
+ const accessibleName = (0, locator_healing_js_1.extractAccessibleName)(aria) || (0, locator_healing_js_1.approximateAccessibleName)({ ...attrs, accessibleName: null });
223
+ sink.capturedLocators[seq] = {
224
+ location: callerLocation,
225
+ used,
226
+ // hasLabel/selectorCounts inform alternative generation only —
227
+ // keep the stored element to the wire shape.
228
+ element: {
229
+ tagName: attrs.tagName,
230
+ attributes: attrs.attributes,
231
+ textContent: attrs.textContent,
232
+ accessibleName,
233
+ center: attrs.center,
234
+ },
235
+ alternatives: (0, locator_healing_js_1.generateAlternatives)({ ...attrs, accessibleName }),
236
+ };
237
+ }
238
+ catch {
239
+ // element detached or timeout — keep the placeholder
240
+ }
241
+ finally {
242
+ clearTimeout(deadline);
243
+ }
244
+ })();
245
+ sink.capturePromises.push(resolveAttrs);
246
+ return result;
247
+ };
248
+ },
249
+ });
250
+ }
251
+ /**
252
+ * Instrument a single page: wrap its locator-building methods for healing
253
+ * capture and attach console/network listeners. Idempotent — safe to call on a
254
+ * page already reached through another path (browser patch, `page` fixture).
255
+ */
256
+ function instrumentPage(page) {
257
+ if (!page || INSTRUMENTED_PAGES.has(page))
258
+ return;
259
+ INSTRUMENTED_PAGES.add(page);
260
+ // Opt-out: skipped when PIWI_CAPTURE_LOCATORS=false (set automatically when
261
+ // the reporter's collectPerformanceMetrics / captureLocators is disabled),
262
+ // so the per-action DOM read + ARIA snapshot cost is never paid when unused.
263
+ const captureLocators = process.env.PIWI_CAPTURE_LOCATORS !== 'false';
264
+ if (captureLocators) {
265
+ // Locator factories are accessed by dynamic name, so a string-indexed view
266
+ // of the page is the cleanest local escape hatch from the static surface.
267
+ const factories = page;
268
+ for (const method of locator_healing_js_1.LOCATOR_METHODS) {
269
+ const original = factories[method].bind(page);
270
+ factories[method] = (...args) => {
271
+ // Touching a page's locator factory marks it the active page even for
272
+ // assertion-only tests that never call an action method.
273
+ if (currentSink)
274
+ currentSink.lastActivePage = page;
275
+ return wrapLocator(page, original(...args), method, args);
276
+ };
277
+ }
278
+ }
279
+ page.on('console', (msg) => {
280
+ const sink = currentSink;
281
+ if (!sink)
282
+ return;
283
+ const type = msg.type();
284
+ if (['warning', 'error', 'assert'].includes(type)) {
285
+ const location = msg.location();
286
+ sink.consoleEntries.push({
287
+ type,
288
+ text: msg.text(),
289
+ timestamp: Date.now(),
290
+ location: location ? `${location.url}:${location.lineNumber}:${location.columnNumber}` : null,
291
+ });
292
+ }
293
+ });
294
+ page.on('requestfinished', (request) => {
295
+ const sink = currentSink;
296
+ if (!sink)
297
+ return;
298
+ sink.lastActivePage = page;
299
+ const p = (async () => {
300
+ try {
301
+ const url = request.url();
302
+ if (url.startsWith('data:') || url.startsWith('blob:'))
303
+ return;
304
+ const timing = request.timing();
305
+ const response = await request.response();
306
+ const resourceType = request.resourceType();
307
+ // Only keep API/document requests; skip static assets (scripts, styles, fonts, images, media)
308
+ if (!['fetch', 'xhr', 'document', 'other'].includes(resourceType))
309
+ return;
310
+ const entry = {
311
+ method: request.method(),
312
+ url,
313
+ status: response ? response.status() : 0,
314
+ duration: timing.responseEnd > 0 ? Math.round(timing.responseEnd - timing.requestStart) : 0,
315
+ startTime: timing.startTime,
316
+ resourceType,
317
+ };
318
+ if (response) {
319
+ const headers = response.headers();
320
+ // Response content type (without charset/boundary params) — relevant
321
+ // per-request metadata for distinguishing API/JSON vs document/HTML calls.
322
+ const contentType = headers['content-type'];
323
+ if (contentType)
324
+ entry.contentType = contentType.split(';')[0].trim();
325
+ const logHeader = headers['x-piwi-logs'];
326
+ if (logHeader) {
327
+ try {
328
+ entry.serverLogs = JSON.parse((0, node_zlib_1.gunzipSync)(Buffer.from(logHeader, 'base64')).toString('utf-8'));
329
+ }
330
+ catch {
331
+ /* ignore malformed header */
332
+ }
333
+ }
334
+ }
335
+ sink.networkRequests.push(entry);
336
+ }
337
+ catch {
338
+ /* ignore */
339
+ }
340
+ })();
341
+ sink.pendingHandlers.push(p);
342
+ });
343
+ }
344
+ /**
345
+ * Instrument a browser context so every page it opens — via `newPage()` or as a
346
+ * popup/`window.open` — is captured. Idempotent.
347
+ */
348
+ function instrumentContext(context) {
349
+ if (!context || INSTRUMENTED_CONTEXTS.has(context))
350
+ return;
351
+ INSTRUMENTED_CONTEXTS.add(context);
352
+ const originalNewPage = context.newPage.bind(context);
353
+ context.newPage = async (...args) => {
354
+ const page = await originalNewPage(...args);
355
+ instrumentPage(page);
356
+ return page;
357
+ };
358
+ // Popups and pages the context opens on its own (idempotent with the above).
359
+ context.on('page', (page) => instrumentPage(page));
360
+ }
361
+ /**
362
+ * Patch a browser so any page or context created directly from it is
363
+ * instrumented. This is what makes capture work for suites that build their own
364
+ * pages from the worker-scoped `browser` (e.g. `browser.newPage()` /
365
+ * `browser.newContext().newPage()`) instead of using the `page` fixture.
366
+ * Idempotent.
367
+ */
368
+ function patchBrowser(browser) {
369
+ if (!browser || PATCHED_BROWSERS.has(browser))
370
+ return;
371
+ PATCHED_BROWSERS.add(browser);
372
+ const originalNewPage = browser.newPage.bind(browser);
373
+ browser.newPage = async (...args) => {
374
+ const page = await originalNewPage(...args);
375
+ instrumentPage(page);
376
+ return page;
377
+ };
378
+ const originalNewContext = browser.newContext.bind(browser);
379
+ browser.newContext = async (...args) => {
380
+ const context = await originalNewContext(...args);
381
+ instrumentContext(context);
382
+ return context;
383
+ };
384
+ }
385
+ /**
386
+ * Drain in-flight capture work and attach the collected `piwi-*` data
387
+ * to the test. Mirrors the per-test teardown the `page` fixture used to do, but
388
+ * sourced from the sink so it works regardless of how the test's pages were made.
389
+ */
390
+ async function flushSink(sink, testInfo) {
391
+ // Wait for all in-flight requestfinished handlers before snapshotting
392
+ // networkRequests — the last request (often the one that failed the test)
393
+ // races with teardown and its serverLogs would otherwise be lost.
394
+ await Promise.allSettled(sink.pendingHandlers);
395
+ // ── Attach locator snapshots ──────────────────────────────────────────
396
+ // Cap the drain so a stuck capture (e.g. a navigation in flight) can never
397
+ // hang teardown past the test timeout; per-action evaluate/ariaSnapshot are
398
+ // already bounded, this is a backstop.
399
+ let drainDeadline;
400
+ await Promise.race([
401
+ Promise.allSettled(sink.capturePromises),
402
+ new Promise((resolve) => {
403
+ drainDeadline = setTimeout(resolve, 2000);
404
+ }),
405
+ ]);
406
+ clearTimeout(drainDeadline);
407
+ if (sink.capturedLocators.length > 0) {
408
+ await testInfo.attach(attachments_js_1.ATTACHMENT_NAMES.locators, {
409
+ contentType: 'application/json',
410
+ // Repeated call sites (loops) keep only their latest capture — the
411
+ // server stores one row per location anyway, so shipping every
412
+ // iteration is pure payload bloat.
413
+ body: Buffer.from(JSON.stringify((0, locator_healing_js_1.dedupeSnapshotsByLocation)(sink.capturedLocators))),
414
+ });
415
+ }
416
+ const page = sink.lastActivePage;
417
+ if (page && testInfo.status !== 'passed' && testInfo.status !== 'skipped') {
418
+ try {
419
+ const snapshot = await ariaSnapshotBestEffort(page.locator(':root'));
420
+ if (snapshot) {
421
+ await testInfo.attach(attachments_js_1.ATTACHMENT_NAMES.ariaSnapshot, {
422
+ contentType: 'text/plain',
423
+ body: snapshot,
424
+ });
425
+ // Suggest a fresh locator for the failed action from the current page.
426
+ // When the element was renamed/moved, the pre-captured alternatives
427
+ // describe the old element, so this points at where it went now — as a
428
+ // Playwright annotation (shown in the report + trace) and an attachment
429
+ // (shown in the trace viewer). It does NOT change the locator.
430
+ const failed = sink.failedLocators[sink.failedLocators.length - 1];
431
+ const suggestion = failed ? (0, locator_healing_js_1.suggestLocatorsFromAria)(failed, snapshot) : null;
432
+ if (suggestion) {
433
+ testInfo.annotations.push({
434
+ type: attachments_js_1.LOCATOR_SUGGESTION_ANNOTATION,
435
+ description: `${suggestion.failing} matched nothing on the failing page — the element may have been renamed or moved. Suggested: ${suggestion.suggestions.join(' | ')}`,
436
+ });
437
+ await testInfo.attach(attachments_js_1.ATTACHMENT_NAMES.locatorSuggestion, {
438
+ contentType: 'application/json',
439
+ body: Buffer.from(JSON.stringify(suggestion)),
440
+ });
441
+ }
442
+ }
443
+ }
444
+ catch {
445
+ /* ignore */
446
+ }
447
+ }
448
+ if (sink.consoleEntries.length > 0) {
449
+ await testInfo.attach(attachments_js_1.ATTACHMENT_NAMES.console, {
450
+ contentType: 'application/json',
451
+ body: Buffer.from(JSON.stringify(sink.consoleEntries)),
452
+ });
453
+ }
454
+ if (sink.networkRequests.length > 0) {
455
+ await testInfo.attach(attachments_js_1.ATTACHMENT_NAMES.network, {
456
+ contentType: 'application/json',
457
+ body: Buffer.from(JSON.stringify(sink.networkRequests)),
458
+ });
459
+ }
460
+ if (page) {
461
+ try {
462
+ // Runs in the browser, so the perf-entry reads stay `any` (no DOM lib);
463
+ // the callback return type pins `webVitals` to WebVitals.
464
+ const webVitals = await page.evaluate(() => {
465
+ const navEntries = performance.getEntriesByType('navigation');
466
+ const paintEntries = performance.getEntriesByType('paint');
467
+ const nav = navEntries[0];
468
+ const navigation = nav
469
+ ? {
470
+ url: nav.name,
471
+ ttfb: Math.round(nav.responseStart - nav.fetchStart),
472
+ domInteractive: Math.round(nav.domInteractive - nav.fetchStart),
473
+ domContentLoaded: Math.round(nav.domContentLoadedEventEnd - nav.fetchStart),
474
+ loadComplete: Math.round(nav.loadEventEnd - nav.fetchStart),
475
+ transferSize: nav.transferSize || 0,
476
+ encodedBodySize: nav.encodedBodySize || 0,
477
+ decodedBodySize: nav.decodedBodySize || 0,
478
+ }
479
+ : null;
480
+ const paint = {};
481
+ for (const entry of paintEntries) {
482
+ const key = entry.name.replace(/-([a-z])/g, (_, l) => l.toUpperCase());
483
+ paint[key] = Math.round(entry.startTime);
484
+ }
485
+ if (!navigation && Object.keys(paint).length === 0)
486
+ return null;
487
+ return { navigation, paint };
488
+ });
489
+ if (webVitals) {
490
+ await testInfo.attach(attachments_js_1.ATTACHMENT_NAMES.webVitals, {
491
+ contentType: 'application/json',
492
+ body: Buffer.from(JSON.stringify(webVitals)),
493
+ });
494
+ }
495
+ }
496
+ catch {
497
+ /* ignore */
498
+ }
499
+ }
500
+ }
501
+ /**
502
+ * Playwright fixtures that collect network requests, console entries,
503
+ * web vitals, ARIA snapshots, and locator interaction data during a test.
504
+ *
505
+ * Capture is wired at the `browser` level, so it works whether a test uses the
506
+ * standard `page` fixture or builds its own pages from `browser` /
507
+ * `browser.newContext()`. Collected data is attached as `piwi-*`
508
+ * test-info attachments which the Piwi Dashboard reporter parses on `onTestEnd`.
509
+ */
510
+ exports.dashboardFixtures = {
511
+ // Worker-scoped: patch the shared browser so every page/context created from
512
+ // it — including by user fixtures that take `browser` directly — is captured.
513
+ browser: [
514
+ async ({ browser }, use) => {
515
+ patchBrowser(browser);
516
+ await use(browser);
517
+ },
518
+ { scope: 'worker' },
519
+ ],
520
+ // Safety net for the standard `page` fixture, in case its page is created
521
+ // through an internal path the browser patch doesn't see. Idempotent, so it
522
+ // never double-wraps a page the browser patch already instrumented.
523
+ page: async ({ page }, use) => {
524
+ instrumentPage(page);
525
+ await use(page);
526
+ },
527
+ // Auto, test-scoped: open a capture sink for the running test and flush it
528
+ // (attach the collected data) at teardown. Runs for every test without being
529
+ // requested, so suites that never destructure `page` are still captured.
530
+ piwiDashboardCapture: [
531
+ async ({}, use, testInfo) => {
532
+ const sink = createSink();
533
+ currentSink = sink;
534
+ try {
535
+ await use();
536
+ }
537
+ finally {
538
+ currentSink = null;
539
+ await flushSink(sink, testInfo);
540
+ }
541
+ },
542
+ { auto: true },
543
+ ],
544
+ };
545
+ /**
546
+ * Extend a Playwright `test` object with Piwi Dashboard fixtures.
547
+ *
548
+ * Use this instead of importing `@playwright/test` directly from this package
549
+ * to avoid the "Requiring @playwright/test second time" error caused by
550
+ * duplicate module resolution.
551
+ *
552
+ * @example
553
+ * ```ts
554
+ * import { test as base } from '@playwright/test';
555
+ * import { extendDashboardFixtures } from '@piwitests/reporter';
556
+ *
557
+ * export const test = extendDashboardFixtures(base);
558
+ * ```
559
+ */
560
+ function extendDashboardFixtures(test) {
561
+ return test.extend(exports.dashboardFixtures);
562
+ }
@@ -11,6 +11,18 @@ export interface RankedLocator {
11
11
  /** 0-100 stability score. data-testid=100, semantic CSS=35-40, hash-suffixed=10. */
12
12
  score: number;
13
13
  }
14
+ /**
15
+ * Match counts for candidate selectors, probed with `querySelectorAll` against
16
+ * the live page at capture time. A count > 1 means the selector is ambiguous
17
+ * (would be a strict-mode violation) and the alternative is suppressed; a
18
+ * missing count means the probe didn't run and the alternative is kept.
19
+ */
20
+ export interface SelectorCounts {
21
+ testId?: number;
22
+ id?: number;
23
+ name?: number;
24
+ classes?: Record<string, number>;
25
+ }
14
26
  export interface ElementAttributes {
15
27
  tagName: string;
16
28
  attributes: Record<string, string | null>;
@@ -20,10 +32,18 @@ export interface ElementAttributes {
20
32
  x: number;
21
33
  y: number;
22
34
  } | null;
35
+ /**
36
+ * True when the element has an associated `<label>` (`el.labels`). Gates the
37
+ * `getByLabel` alternative — an accessible name approximated from
38
+ * placeholder/title would produce a `getByLabel` that matches nothing.
39
+ * Undefined on payloads from older capture probes (legacy behavior applies).
40
+ */
41
+ hasLabel?: boolean;
42
+ /** Live-page uniqueness probe results for candidate selectors. */
43
+ selectorCounts?: SelectorCounts;
23
44
  }
24
45
  export interface LocatorSnapshot {
25
46
  location: string | null;
26
- stepIndex: number;
27
47
  used: {
28
48
  method: string;
29
49
  args: unknown[];
@@ -41,6 +61,15 @@ export interface LocatorSnapshot {
41
61
  } | null;
42
62
  alternatives: RankedLocator[];
43
63
  }
64
+ /**
65
+ * Drop repeated captures of the same call site before attaching: a loop (or a
66
+ * page-object method called repeatedly) produces one snapshot per action, but
67
+ * the server keeps only the latest per location anyway. Keeps the last
68
+ * element-bearing snapshot per location — falling back to the last placeholder
69
+ * when no capture resolved, so the location still counts as "seen this run"
70
+ * for the server's stale-location purge. Entries with no location pass through.
71
+ */
72
+ export declare function dedupeSnapshotsByLocation(snaps: LocatorSnapshot[]): LocatorSnapshot[];
44
73
  /**
45
74
  * Page-level locator-building methods wrapped by the capture proxy. Imported by
46
75
  * both `reporter/src/fixtures.ts` and the dogfooding `application/tests/fixtures.ts`
@@ -123,6 +152,23 @@ export interface LocatorSuggestion {
123
152
  /** Fresh locator suggestions for the element's current identity, best first. */
124
153
  suggestions: string[];
125
154
  }
155
+ /**
156
+ * Parse `ariaSnapshot()` lines into role/name pairs (mirrors the server-side
157
+ * `parseAriaCandidates` in application/shared/locator-fingerprint.ts).
158
+ * Exported so the dashboard's drift-guard unit test can compare the two.
159
+ */
160
+ export declare function parseAriaRoleName(ariaSnapshot: string): Array<{
161
+ role: string;
162
+ name: string | null;
163
+ }>;
164
+ /**
165
+ * Token-set (Dice) similarity, 0-1, case- and punctuation-insensitive.
166
+ * Duplicated from `textSimilarity` in application/shared/locator-fingerprint.ts —
167
+ * this package publishes standalone to npm and can't import monorepo-relative
168
+ * shared/ code, so keep the two implementations in sync by hand. Exported so
169
+ * the dashboard's drift-guard unit test can compare the two.
170
+ */
171
+ export declare function nameSimilarity(a: string | null, b: string | null): number;
126
172
  /**
127
173
  * Best-effort runtime suggestion for a locator that matched nothing: find the
128
174
  * element on the *current* page that the failed locator most likely targeted