From d27aa95b3df0b7149198510d8b7bf9f54cd9d38f Mon Sep 17 00:00:00 2001 From: Vishnu Vardhan Date: Thu, 2 Jul 2026 18:53:58 +0530 Subject: [PATCH 01/68] feat(core,backend): emit console/stdout/stderr events into trace.zip and round-trip them in the reader --- packages/backend/src/trace-reader-types.ts | 17 +++ packages/backend/src/trace-reader-utils.ts | 58 +++++++- packages/backend/src/trace-reader.ts | 26 ++-- packages/backend/tests/trace-reader.test.ts | 38 +++++- packages/core/src/index.ts | 2 + packages/core/src/trace-console.ts | 93 +++++++++++++ packages/core/src/trace-exporter.ts | 141 +++++--------------- packages/core/src/trace-snapshots.ts | 102 ++++++++++++++ packages/core/tests/trace-console.test.ts | 111 +++++++++++++++ 9 files changed, 465 insertions(+), 123 deletions(-) create mode 100644 packages/core/src/trace-console.ts create mode 100644 packages/core/src/trace-snapshots.ts create mode 100644 packages/core/tests/trace-console.test.ts diff --git a/packages/backend/src/trace-reader-types.ts b/packages/backend/src/trace-reader-types.ts index 63bc3139..3a9d5e77 100644 --- a/packages/backend/src/trace-reader-types.ts +++ b/packages/backend/src/trace-reader-types.ts @@ -24,6 +24,22 @@ export interface ScreencastFrameEvent { timestamp: number } +export interface ConsoleEvent { + type: 'console' + time: number + messageType: string + text: string + args?: { preview: string; value: unknown }[] +} + +export interface StdioEvent { + type: 'stdout' | 'stderr' + timestamp: number + text?: string + /** Extension field restoring the test-vs-terminal origin; absent in foreign zips. */ + source?: 'test' | 'terminal' +} + export interface ContextOptionsEvent { type: 'context-options' wallTime: number @@ -54,4 +70,5 @@ export interface CategorizedEvents { befores: Map afters: Map frameEvents: ScreencastFrameEvent[] + consoleEvents: (ConsoleEvent | StdioEvent)[] } diff --git a/packages/backend/src/trace-reader-utils.ts b/packages/backend/src/trace-reader-utils.ts index 16a0972e..b7162c12 100644 --- a/packages/backend/src/trace-reader-utils.ts +++ b/packages/backend/src/trace-reader-utils.ts @@ -3,13 +3,20 @@ import { TraceType, + type ConsoleLog, + type LogLevel, type Metadata, type NetworkRequest, type TracePlayerFrame, type Viewport } from '@wdio/devtools-shared' -import type { ContextOptionsEvent, HarSnapshot } from './trace-reader-types.js' +import type { + ConsoleEvent, + ContextOptionsEvent, + HarSnapshot, + StdioEvent +} from './trace-reader-types.js' export function parseNdjson(text: string): Record[] { return text @@ -44,9 +51,9 @@ export function paramsToArgs( return indexKeys.map((key) => params[key]) } -// Playwright/vibium-style action label, built from class.method + the most -// meaningful param (value for fill/type, url for navigate, nothing for click) — -// matching what `playwright show-trace` renders for the same trace. +// Trace-viewer action label, built from class.method + the most meaningful +// param (value for fill/type, url for navigate, nothing for click) — +// matching what standard trace viewers render for the same trace. export function actionLabel( cls: string, method: string, @@ -109,7 +116,7 @@ export function harToNetworkRequest( ): NetworkRequest { const started = Date.parse(snapshot.startedDateTime) const startTime = Number.isFinite(started) ? started : 0 - // A foreign trace.zip (the reader accepts any Vibium/Playwright zip) can carry + // A foreign trace.zip (the reader accepts any standard-format zip) can carry // a pending or failed request with no response or content; default those. const response = snapshot.response const content = response?.content @@ -137,6 +144,47 @@ export function harToNetworkRequest( } } +const LOG_LEVELS: ReadonlySet = new Set([ + 'trace', + 'debug', + 'log', + 'info', + 'warn', + 'error' +]) + +// Reverse of the writer's level mapping ('warn' → 'warning'); a foreign +// trace zip can carry levels outside our union, which default to 'log'. +function fromTraceLevel(messageType: string): LogLevel { + if (messageType === 'warning') { + return 'warn' + } + return LOG_LEVELS.has(messageType) ? (messageType as LogLevel) : 'log' +} + +export function buildConsoleLogs( + consoleEvents: (ConsoleEvent | StdioEvent)[], + wallTime: number +): ConsoleLog[] { + return consoleEvents.map((event) => { + if (event.type === 'console') { + return { + type: fromTraceLevel(event.messageType), + args: event.args?.map((arg) => arg.value) ?? [event.text], + timestamp: wallTime + event.time, + source: 'browser' as const + } + } + return { + type: event.type === 'stderr' ? ('error' as const) : ('log' as const), + args: [event.text ?? ''], + timestamp: wallTime + event.timestamp, + // Our zips carry the origin; foreign stdio events default to terminal. + source: event.source ?? ('terminal' as const) + } + }) +} + export function nearestFrame( frames: TracePlayerFrame[], timestamp: number diff --git a/packages/backend/src/trace-reader.ts b/packages/backend/src/trace-reader.ts index d8643f39..6146998f 100644 --- a/packages/backend/src/trace-reader.ts +++ b/packages/backend/src/trace-reader.ts @@ -1,10 +1,11 @@ // Reads a trace.zip produced by core/trace-exporter.ts back into a player // payload. The writer is the inverse: this reconstructs commands from // before/after events, the frame filmstrip from screencast-frame events + -// resources/*.jpeg, and network requests from the HAR resource-snapshot -// entries. Fields the zip never carried (console logs, mutations, sources, -// suites) come back empty. Constants, event types, and pure helpers live in the -// sibling trace-reader-{constants,types,utils}.ts files. +// resources/*.jpeg, console logs from console/stdout/stderr events, and +// network requests from the HAR resource-snapshot entries. Fields the zip +// never carried (mutations, sources, suites) come back empty. Constants, +// event types, and pure helpers live in the sibling +// trace-reader-{constants,types,utils}.ts files. import fs from 'node:fs/promises' import { unzipSync, strFromU8 } from 'fflate' @@ -20,12 +21,15 @@ import type { AfterEvent, BeforeEvent, CategorizedEvents, + ConsoleEvent, ContextOptionsEvent, HarSnapshot, - ScreencastFrameEvent + ScreencastFrameEvent, + StdioEvent } from './trace-reader-types.js' import { actionLabel, + buildConsoleLogs, buildMetadata, harToNetworkRequest, nearestFrame, @@ -39,6 +43,7 @@ function categorizeEvents( const befores = new Map() const afters = new Map() const frameEvents: ScreencastFrameEvent[] = [] + const consoleEvents: (ConsoleEvent | StdioEvent)[] = [] let ctx: ContextOptionsEvent | undefined for (const event of events) { switch (event.type) { @@ -58,9 +63,14 @@ function categorizeEvents( case 'screencast-frame': frameEvents.push(event as unknown as ScreencastFrameEvent) break + case 'console': + case 'stdout': + case 'stderr': + consoleEvents.push(event as unknown as ConsoleEvent | StdioEvent) + break } } - return { ctx, befores, afters, frameEvents } + return { ctx, befores, afters, frameEvents, consoleEvents } } function buildFrames( @@ -100,7 +110,7 @@ function buildCommands( command: REVERSE_ACTION_MAP[`${before.class}.${before.method}`] ?? before.method, args: paramsToArgs(before.params), - // Show the Playwright/vibium label (`Element.fill("x")`); the command + // Show the trace-style label (`Element.fill("x")`); the command // name above still drives the UI's category colour and icon. title: actionLabel(before.class, before.method, before.params), startTime: wallTime + before.startTime, @@ -143,7 +153,7 @@ export function parseTraceZip(zip: Uint8Array): TracePlayerData { const trace: TraceLog = { mutations: [], logs: [], - consoleLogs: [], + consoleLogs: buildConsoleLogs(categorized.consoleEvents, wallTime), networkRequests, metadata: buildMetadata(categorized.ctx), commands, diff --git a/packages/backend/tests/trace-reader.test.ts b/packages/backend/tests/trace-reader.test.ts index f2aa880e..8df8f9ff 100644 --- a/packages/backend/tests/trace-reader.test.ts +++ b/packages/backend/tests/trace-reader.test.ts @@ -73,7 +73,18 @@ function fixtureZip(): Uint8Array { width: 1024, height: 768, timestamp: 260 - } + }, + { + type: 'console', + time: 120, + pageId: 'page@abcd1234', + messageType: 'warning', + text: 'low disk', + args: [{ preview: 'low disk', value: 'low disk' }], + location: { url: '', lineNumber: 0, columnNumber: 0 } + }, + { type: 'stdout', timestamp: 130, text: 'spec started', source: 'test' }, + { type: 'stderr', timestamp: 140, text: 'worker warning' } ] const networkEntry = { type: 'resource-snapshot', @@ -159,9 +170,32 @@ describe('parseTraceZip', () => { (trace.metadata.capabilities as { browserName: string }).browserName ).toBe('chrome') expect(trace.metadata.sessionId).toBe('abcd1234') - expect(trace.consoleLogs).toEqual([]) expect(trace.mutations).toEqual([]) expect(trace.suites).toEqual([]) expect(trace.sources).toEqual({}) }) + + it('reconstructs console logs from console and stdio events', () => { + const { trace } = parseTraceZip(fixtureZip()) + expect(trace.consoleLogs).toEqual([ + { + type: 'warn', + args: ['low disk'], + timestamp: WALL_TIME + 120, + source: 'browser' + }, + { + type: 'log', + args: ['spec started'], + timestamp: WALL_TIME + 130, + source: 'test' + }, + { + type: 'error', + args: ['worker warning'], + timestamp: WALL_TIME + 140, + source: 'terminal' + } + ]) + }) }) diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index 21fcdf08..36b8bcd7 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -8,8 +8,10 @@ export * from './assert-patcher.js' export * from './element-snapshot.js' export * from './element-scripts.js' export * from './element-types.js' +export * from './trace-console.js' export * from './trace-exporter.js' export * from './trace-har.js' +export * from './trace-snapshots.js' export * from './trace-zip-writer.js' export * from './bidi.js' export * from './console.js' diff --git a/packages/core/src/trace-console.ts b/packages/core/src/trace-console.ts new file mode 100644 index 00000000..f9db30fd --- /dev/null +++ b/packages/core/src/trace-console.ts @@ -0,0 +1,93 @@ +// Maps captured ConsoleLog entries into trace-event vocabulary: browser +// console entries become `console` events; test/terminal output becomes +// `stdout`/`stderr` events (which carry no location semantics — matching +// what we capture for Node-side lines). + +import type { ConsoleLog, LogSource } from '@wdio/devtools-shared' + +export interface ConsoleEvent { + type: 'console' + time: number + pageId?: string + messageType: string + text: string + args?: { preview: string; value: unknown }[] + location: { url: string; lineNumber: number; columnNumber: number } +} + +export interface StdioEvent { + type: 'stdout' | 'stderr' + timestamp: number + text?: string + /** Extension field: preserves the test-vs-terminal origin the standard + * stdio vocabulary can't express. Foreign viewers ignore it. */ + source?: Extract +} + +// Keeps a pathological run (console.log in a tight loop) from producing a +// trace.trace the viewer can't open. +const MAX_CONSOLE_EVENTS = 10_000 + +/** The trace format's console vocabulary uses 'warning'; 'trace' has no + * equivalent — 'debug' is the nearest severity. */ +function toTraceLevel(level: ConsoleLog['type']): string { + if (level === 'warn') { + return 'warning' + } + if (level === 'trace') { + return 'debug' + } + return level +} + +function previewArg(arg: unknown): string { + if (typeof arg === 'string') { + return arg + } + try { + return JSON.stringify(arg) ?? String(arg) + } catch { + return String(arg) + } +} + +export function buildConsoleEvents( + logs: ConsoleLog[], + pageId: string, + wallTime: number +): (ConsoleEvent | StdioEvent)[] { + const capped = logs.slice(0, MAX_CONSOLE_EVENTS) + const events: (ConsoleEvent | StdioEvent)[] = capped.map((log) => { + const time = Math.max(0, log.timestamp - wallTime) + const text = log.args.map(previewArg).join(' ') + // Untagged entries predate source tagging; they came from the page. + if (log.source === 'browser' || log.source === undefined) { + return { + type: 'console', + time, + pageId, + messageType: toTraceLevel(log.type), + text, + args: log.args.map((arg) => ({ preview: previewArg(arg), value: arg })), + // Location isn't captured at the console patch site; the event + // shape requires the field, so it ships zeroed. + location: { url: '', lineNumber: 0, columnNumber: 0 } + } satisfies ConsoleEvent + } + return { + type: log.type === 'error' || log.type === 'warn' ? 'stderr' : 'stdout', + timestamp: time, + text, + source: log.source + } satisfies StdioEvent + }) + if (logs.length > MAX_CONSOLE_EVENTS) { + const last = capped[capped.length - 1] + events.push({ + type: 'stderr', + timestamp: last ? Math.max(0, last.timestamp - wallTime) : 0, + text: `[devtools] console truncated: dropped ${logs.length - MAX_CONSOLE_EVENTS} entries` + }) + } + return events +} diff --git a/packages/core/src/trace-exporter.ts b/packages/core/src/trace-exporter.ts index 24488442..8fd7d575 100644 --- a/packages/core/src/trace-exporter.ts +++ b/packages/core/src/trace-exporter.ts @@ -20,6 +20,16 @@ import { FILL_METHODS, type TraceAction } from './action-mapping.js' +import { + buildConsoleEvents, + type ConsoleEvent, + type StdioEvent +} from './trace-console.js' +import { + buildFilmstripEvents, + buildSnapshotResources, + type ScreencastFrameEvent +} from './trace-snapshots.js' import { networkRequestToHar } from './trace-har.js' import { buildTraceZip, type TraceZipResource } from './trace-zip-writer.js' @@ -52,7 +62,7 @@ interface BeforeEvent { pageId: string params: Record title: string - /** Playwright-compatible API name (e.g. 'page.goBack', 'element.click'). */ + /** Trace-viewer API name (e.g. 'page.goBack', 'element.click'). */ apiName: string /** CallId of the Tracing.tracingGroup that wraps this action (if any). */ parentId?: string @@ -67,22 +77,13 @@ interface AfterEvent { parentId?: string } -interface ScreencastFrameEvent { - type: 'screencast-frame' - pageId: string - sha1: string - elements?: string - snapshot?: string - width: number - height: number - timestamp: number -} - type TraceEvent = | ContextOptionsEvent | BeforeEvent | AfterEvent | ScreencastFrameEvent + | ConsoleEvent + | StdioEvent function shortId(sessionId?: string): string { return (sessionId ?? Math.random().toString(36).slice(2, 10)).slice(0, 8) @@ -294,63 +295,6 @@ function buildNetworkNdjson( return Buffer.from(lines.join('\n'), 'utf8') } -function buildSnapshotResources( - snapshots: ActionSnapshot[], - pageId: string -): TraceZipResource[] { - const out: TraceZipResource[] = [] - for (const snap of snapshots) { - const base = `${pageId}-${snap.timestamp}` - if (snap.screenshot) { - out.push({ - resourceName: `${base}.jpeg`, - data: Buffer.from(snap.screenshot, 'base64') - }) - } - if (snap.elements && snap.elements.length) { - out.push({ - resourceName: `${base}-elements.json`, - data: Buffer.from(JSON.stringify(snap.elements), 'utf8') - }) - } - if (snap.snapshotText) { - out.push({ - resourceName: `${base}-snapshot.txt`, - data: Buffer.from(snap.snapshotText, 'utf8') - }) - } - } - return out -} - -function buildScreencastFrames( - snapshots: ActionSnapshot[], - pageId: string, - wallTime: number, - viewport: { width: number; height: number } -): ScreencastFrameEvent[] { - return snapshots - .filter((s) => s.screenshot) - .map((s) => { - const base = `${pageId}-${s.timestamp}` - const frame: ScreencastFrameEvent = { - type: 'screencast-frame', - pageId, - sha1: `${base}.jpeg`, - width: viewport.width, - height: viewport.height, - timestamp: Math.max(0, s.timestamp - wallTime) - } - if (s.elements && s.elements.length) { - frame.elements = `${base}-elements.json` - } - if (s.snapshotText) { - frame.snapshot = `${base}-snapshot.txt` - } - return frame - }) -} - /** * Build a trace.zip buffer from the captured TraceLog. * Filters commands through ACTION_MAP and renames to trace vocabulary; @@ -368,13 +312,19 @@ function eventTime(e: TraceEvent): number { return e.endTime case 'screencast-frame': return e.timestamp + case 'console': + return e.time + case 'stdout': + case 'stderr': + return e.timestamp } } /** At the same timestamp T: an action's `after` ends first, then the - * snapshot captured at the action boundary, then the next action's `before`. - * Matches the viewer's expectation that the screencast frame shows the - * state between the previous action's completion and the next one's start. */ + * snapshot captured at the action boundary, then console output observed + * at the boundary, then the next action's `before`. Matches the viewer's + * expectation that the screencast frame shows the state between the + * previous action's completion and the next one's start. */ function eventOrder(e: TraceEvent): number { switch (e.type) { case 'context-options': @@ -383,8 +333,12 @@ function eventOrder(e: TraceEvent): number { return 1 case 'screencast-frame': return 2 - case 'before': + case 'console': + case 'stdout': + case 'stderr': return 3 + case 'before': + return 4 } } @@ -464,41 +418,12 @@ function buildTraceBundle( const viewport = trace.metadata.viewport ?? { width: 1280, height: 720 } const snapshots = trace.actionSnapshots ?? [] const ctxOptions = buildContextOptions(trace, contextId, wallTime) - const events: TraceEvent[] = [ctxOptions] - - // Emit initial screencast-frame (timestamp=0) using the first snapshot's - // resources so trace viewers show the page state before any interaction. - const firstSnap = snapshots.find((s) => s.screenshot) - if (firstSnap) { - const base = `${pageId}-${firstSnap.timestamp}` - const initFrame: ScreencastFrameEvent = { - type: 'screencast-frame', - pageId, - sha1: `${base}.jpeg`, - width: viewport.width, - height: viewport.height, - timestamp: 0 - } - if (firstSnap.elements && firstSnap.elements.length) { - initFrame.elements = `${base}-elements.json` - } - if (firstSnap.snapshotText) { - initFrame.snapshot = `${base}-snapshot.txt` - } - events.push(initFrame) - } - - events.push( - // Skip the first snapshot in buildScreencastFrames — it was already emitted - // as the initial t=0 frame above. - ...buildScreencastFrames( - firstSnap ? snapshots.filter((s) => s !== firstSnap) : snapshots, - pageId, - wallTime, - viewport - ), - ...buildActionEvents(trace.commands, pageId, wallTime, opts.testMetadata) - ) + const events: TraceEvent[] = [ + ctxOptions, + ...buildFilmstripEvents(snapshots, pageId, wallTime, viewport), + ...buildActionEvents(trace.commands, pageId, wallTime, opts.testMetadata), + ...buildConsoleEvents(trace.consoleLogs, pageId, wallTime) + ] events.sort(compareEvents) const ctxBName = ctxOptions.title return { diff --git a/packages/core/src/trace-snapshots.ts b/packages/core/src/trace-snapshots.ts new file mode 100644 index 00000000..0f139bdb --- /dev/null +++ b/packages/core/src/trace-snapshots.ts @@ -0,0 +1,102 @@ +// Per-action snapshot resources and the screencast-frame filmstrip derived +// from them. Split from trace-exporter.ts; the exporter composes these into +// the trace event stream. + +import type { ActionSnapshot } from '@wdio/devtools-shared' +import type { TraceZipResource } from './trace-zip-writer.js' + +export interface ScreencastFrameEvent { + type: 'screencast-frame' + pageId: string + sha1: string + elements?: string + snapshot?: string + width: number + height: number + timestamp: number +} + +export function buildSnapshotResources( + snapshots: ActionSnapshot[], + pageId: string +): TraceZipResource[] { + const out: TraceZipResource[] = [] + for (const snap of snapshots) { + const base = `${pageId}-${snap.timestamp}` + if (snap.screenshot) { + out.push({ + resourceName: `${base}.jpeg`, + data: Buffer.from(snap.screenshot, 'base64') + }) + } + if (snap.elements && snap.elements.length) { + out.push({ + resourceName: `${base}-elements.json`, + data: Buffer.from(JSON.stringify(snap.elements), 'utf8') + }) + } + if (snap.snapshotText) { + out.push({ + resourceName: `${base}-snapshot.txt`, + data: Buffer.from(snap.snapshotText, 'utf8') + }) + } + } + return out +} + +function frameForSnapshot( + snap: ActionSnapshot, + pageId: string, + timestamp: number, + viewport: { width: number; height: number } +): ScreencastFrameEvent { + const base = `${pageId}-${snap.timestamp}` + const frame: ScreencastFrameEvent = { + type: 'screencast-frame', + pageId, + sha1: `${base}.jpeg`, + width: viewport.width, + height: viewport.height, + timestamp + } + if (snap.elements && snap.elements.length) { + frame.elements = `${base}-elements.json` + } + if (snap.snapshotText) { + frame.snapshot = `${base}-snapshot.txt` + } + return frame +} + +/** + * Full filmstrip for the trace: the first snapshot is re-anchored to t=0 so + * viewers show the page state before any interaction; the rest keep their + * wall-time offsets. + */ +export function buildFilmstripEvents( + snapshots: ActionSnapshot[], + pageId: string, + wallTime: number, + viewport: { width: number; height: number } +): ScreencastFrameEvent[] { + const firstSnap = snapshots.find((s) => s.screenshot) + const events: ScreencastFrameEvent[] = [] + if (firstSnap) { + events.push(frameForSnapshot(firstSnap, pageId, 0, viewport)) + } + for (const snap of snapshots) { + if (snap === firstSnap || !snap.screenshot) { + continue + } + events.push( + frameForSnapshot( + snap, + pageId, + Math.max(0, snap.timestamp - wallTime), + viewport + ) + ) + } + return events +} diff --git a/packages/core/tests/trace-console.test.ts b/packages/core/tests/trace-console.test.ts new file mode 100644 index 00000000..5bb6ee59 --- /dev/null +++ b/packages/core/tests/trace-console.test.ts @@ -0,0 +1,111 @@ +import { describe, it, expect } from 'vitest' +import { buildConsoleEvents } from '@wdio/devtools-core' +import type { ConsoleLog } from '@wdio/devtools-shared' + +const WALL_TIME = 1000 +const PAGE_ID = 'page@abc123' + +function log(overrides: Partial = {}): ConsoleLog { + return { + type: 'log', + args: ['hello'], + timestamp: WALL_TIME + 50, + source: 'browser', + ...overrides + } +} + +describe('buildConsoleEvents', () => { + it('returns empty array for no logs', () => { + expect(buildConsoleEvents([], PAGE_ID, WALL_TIME)).toEqual([]) + }) + + it('maps browser logs to console events with monotonic offsets', () => { + const events = buildConsoleEvents([log()], PAGE_ID, WALL_TIME) + expect(events).toEqual([ + { + type: 'console', + time: 50, + pageId: PAGE_ID, + messageType: 'log', + text: 'hello', + args: [{ preview: 'hello', value: 'hello' }], + location: { url: '', lineNumber: 0, columnNumber: 0 } + } + ]) + }) + + it('treats untagged logs as browser console', () => { + const events = buildConsoleEvents( + [log({ source: undefined })], + PAGE_ID, + WALL_TIME + ) + expect(events[0]!.type).toBe('console') + }) + + it("maps 'warn' to 'warning' and 'trace' to 'debug'", () => { + const events = buildConsoleEvents( + [log({ type: 'warn' }), log({ type: 'trace' })], + PAGE_ID, + WALL_TIME + ) + expect( + events.map((e) => (e.type === 'console' ? e.messageType : '')) + ).toEqual(['warning', 'debug']) + }) + + it('previews non-string args as JSON and joins into text', () => { + const events = buildConsoleEvents( + [log({ args: ['count', { a: 1 }, 2] })], + PAGE_ID, + WALL_TIME + ) + const event = events[0]! + expect(event.type).toBe('console') + if (event.type === 'console') { + expect(event.text).toBe('count {"a":1} 2') + expect(event.args?.[1]).toEqual({ preview: '{"a":1}', value: { a: 1 } }) + } + }) + + it('routes test/terminal logs to stdout/stderr by level with source kept', () => { + const events = buildConsoleEvents( + [ + log({ source: 'test', type: 'log', args: ['out'] }), + log({ source: 'test', type: 'error', args: ['bad'] }), + log({ source: 'terminal', type: 'warn', args: ['careful'] }) + ], + PAGE_ID, + WALL_TIME + ) + expect(events).toEqual([ + { type: 'stdout', timestamp: 50, text: 'out', source: 'test' }, + { type: 'stderr', timestamp: 50, text: 'bad', source: 'test' }, + { type: 'stderr', timestamp: 50, text: 'careful', source: 'terminal' } + ]) + }) + + it('floors offsets at zero for logs before wallTime', () => { + const events = buildConsoleEvents( + [log({ timestamp: WALL_TIME - 500 })], + PAGE_ID, + WALL_TIME + ) + const event = events[0]! + expect(event.type === 'console' ? event.time : -1).toBe(0) + }) + + it('caps output and appends a truncation marker', () => { + const logs = Array.from({ length: 10_001 }, (_, i) => + log({ timestamp: WALL_TIME + i }) + ) + const events = buildConsoleEvents(logs, PAGE_ID, WALL_TIME) + expect(events).toHaveLength(10_001) + const last = events[events.length - 1]! + expect(last.type).toBe('stderr') + if (last.type === 'stderr') { + expect(last.text).toContain('dropped 1 entries') + } + }) +}) From 9eb326c9c6ae8d8088dd4c8b2e7e4f4c85eddd2c Mon Sep 17 00:00:00 2001 From: Vishnu Vardhan Date: Thu, 2 Jul 2026 19:56:46 +0530 Subject: [PATCH 02/68] =?UTF-8?q?feat(app):=20trace-player=20layout=20pari?= =?UTF-8?q?ty=20=E2=80=94=20full=20workbench=20with=20top-docked=20timelin?= =?UTF-8?q?e=20strip?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/app/src/app.ts | 7 +- .../browser/trace-player-controls.ts | 145 ++++++++ .../browser/trace-timeline-constants.ts | 26 +- .../browser/trace-timeline-styles.ts | 45 +-- .../src/components/browser/trace-timeline.ts | 313 +++++------------- packages/app/src/components/workbench.ts | 135 ++++++-- packages/app/src/controller/constants.ts | 9 + 7 files changed, 357 insertions(+), 323 deletions(-) create mode 100644 packages/app/src/components/browser/trace-player-controls.ts diff --git a/packages/app/src/app.ts b/packages/app/src/app.ts index d9a89e5d..d8076a4b 100644 --- a/packages/app/src/app.ts +++ b/packages/app/src/app.ts @@ -232,9 +232,10 @@ export class WebdriverIODevtoolsApplication extends Element { class="flex h-[calc(100%-40px)] w-full relative" > ${ - // Only render the test-suite sidebar (and its resize slider) when the - // trace came from a testrunner — the player (standalone) has no tree, - // so the slider would otherwise show a stray dragger on hover. + // Only render the test-suite sidebar (and its resize slider) for a + // live testrunner session. The player has no run/rerun affordances, + // so the tree is dead weight even for testrunner-captured zips. + !this.dataManager.playerMode && this.dataManager.traceType === TraceType.Testrunner ? html` { + this.playerState = (event as CustomEvent).detail + } + + #emit(name: string, detail?: unknown): void { + window.dispatchEvent(new CustomEvent(name, { detail })) + } + + #button( + title: string, + icon: TemplateResult, + onClick: () => void, + extra = '' + ): TemplateResult { + return html`` + } + + #renderSpeedSelect(speed: number): TemplateResult { + return html` + + ` + } + + render() { + const { currentMs, duration, playing, speed } = this.playerState + return html` +
+ ${formatTimecode(currentMs)} + / + ${formatTimecode(duration)} + + ${this.#button( + 'Restart', + html``, + () => this.#emit(PLAYER_RESTART_EVENT) + )} + ${this.#button( + 'Previous action', + html``, + () => this.#emit(KBD.step, { dir: -1 }) + )} + ${this.#button( + playing ? 'Pause' : 'Play', + playing + ? html`` + : html``, + () => this.#emit(KBD.togglePlay), + 'text-chartsBlue' + )} + ${this.#button( + 'Next action', + html``, + () => this.#emit(KBD.step, { dir: 1 }) + )} + ${this.#renderSpeedSelect(speed)} +
+ ` + } +} + +declare global { + interface HTMLElementTagNameMap { + [COMPONENT]: TracePlayerControls + } +} diff --git a/packages/app/src/components/browser/trace-timeline-constants.ts b/packages/app/src/components/browser/trace-timeline-constants.ts index d88f1c61..1e6a1dad 100644 --- a/packages/app/src/components/browser/trace-timeline-constants.ts +++ b/packages/app/src/components/browser/trace-timeline-constants.ts @@ -1,19 +1,15 @@ -import type { ActionCategory } from '../workbench/actionItems/category.js' - /** Playback speed multipliers offered in the timeline controls. */ export const SPEEDS = [0.5, 1, 2, 3, 5] -/** Width of the track-label gutter (px) — lanes start after it. */ -export const GUTTER = 80 - -/** Right breathing room (px) so end-of-timeline markers don't hug the edge. */ -export const INSET = 14 - -/** Tailwind background class per action category, for the timeline chips. */ -export const CATEGORY_BG: Record = { - navigation: 'bg-chartsBlue', - input: 'bg-chartsPurple', - assertion: 'bg-chartsGreen', - query: 'bg-chartsYellow', - other: 'bg-gray-500' +/** Window events linking the controls bar and the timeline strip — same + * decoupling pattern as the KBD events, so either side can be re-homed. */ +export const PLAYER_STATE_EVENT = 'trace-player:state' +export const PLAYER_RESTART_EVENT = 'trace-player:restart' +export const PLAYER_SPEED_EVENT = 'trace-player:speed' + +export interface PlayerState { + currentMs: number + duration: number + playing: boolean + speed: number } diff --git a/packages/app/src/components/browser/trace-timeline-styles.ts b/packages/app/src/components/browser/trace-timeline-styles.ts index f2e276ad..d65cbe24 100644 --- a/packages/app/src/components/browser/trace-timeline-styles.ts +++ b/packages/app/src/components/browser/trace-timeline-styles.ts @@ -1,7 +1,6 @@ import { css } from 'lit' -/** Styles for the trace-player timeline: host layout, hidden scrollbars, and - * the network-detail drawer. Detail-block styles come from networkStyles. */ +/** Styles for the trace-player timeline strip: host layout + hidden scrollbars. */ export const timelineStyles = css` :host { position: relative; @@ -19,46 +18,4 @@ export const timelineStyles = css` .no-scrollbar::-webkit-scrollbar { display: none; } - .net-drawer { - position: absolute; - left: 0; - right: 0; - bottom: 0; - max-height: 62%; - display: flex; - flex-direction: column; - background: var(--vscode-sideBar-background); - border-top: 1px solid var(--accent, #ff7a3c); - box-shadow: 0 -16px 40px -24px #000; - z-index: 30; - } - .net-drawer-head { - display: flex; - align-items: center; - gap: 8px; - padding: 6px 10px; - border-bottom: 1px solid var(--vscode-panel-border); - font-size: 12px; - } - .net-drawer-head .url { - font-family: monospace; - font-size: 11.5px; - overflow: hidden; - text-overflow: ellipsis; - white-space: nowrap; - opacity: 0.85; - } - .net-drawer-head .close { - margin-left: auto; - cursor: pointer; - padding: 2px 6px; - border-radius: 4px; - } - .net-drawer-head .close:hover { - background: var(--vscode-toolbar-hoverBackground); - } - .net-drawer-body { - overflow: auto; - padding: 4px 0; - } ` diff --git a/packages/app/src/components/browser/trace-timeline.ts b/packages/app/src/components/browser/trace-timeline.ts index c2a62d52..2abc3753 100644 --- a/packages/app/src/components/browser/trace-timeline.ts +++ b/packages/app/src/components/browser/trace-timeline.ts @@ -1,41 +1,30 @@ import { Element } from '@core/element' -import { html, nothing, type TemplateResult } from 'lit' +import { html, type TemplateResult } from 'lit' import { customElement, state, query } from 'lit/decorators.js' import { consume } from '@lit/context' import type { CommandLog, TracePlayerFrame } from '@wdio/devtools-shared' -import { - commandContext, - framesContext, - networkRequestContext -} from '../../controller/context.js' -import { commandCategory } from '../workbench/actionItems/category.js' +import { commandContext, framesContext } from '../../controller/context.js' import { activeTimestampAt } from '../workbench/active-entry.js' -import { networkStyles } from '../workbench/network/styles.js' -import { renderNetworkRequestDetail } from '../workbench/network/request-detail.js' import { KBD } from '../../controller/keyboard.js' import { - CATEGORY_BG, - GUTTER, - INSET, - SPEEDS + PLAYER_RESTART_EVENT, + PLAYER_SPEED_EVENT, + PLAYER_STATE_EVENT, + SPEEDS, + type PlayerState } from './trace-timeline-constants.js' import { formatTimecode, imageMime } from './trace-timeline-utils.js' import { timelineStyles } from './trace-timeline-styles.js' -import '~icons/mdi/play.js' -import '~icons/mdi/pause.js' -import '~icons/mdi/skip-previous.js' -import '~icons/mdi/skip-next.js' -import '~icons/mdi/restart.js' - const COMPONENT = 'wdio-devtools-trace-timeline' /** - * Trace-player timeline (replaces the workbench dock in `pnpm show-trace` - * mode). Owns the playback clock, the screenshot filmstrip, the per-track - * timeline (actions / network / console), and the playhead. Advancing the - * clock dispatches `show-command` so the reused browser pane swaps screenshots. + * Trace-player timeline strip, docked above the workbench in `pnpm show-trace` + * mode. Owns the playback clock, the screenshot filmstrip, and the playhead; + * the controls bar (trace-player-controls) and keyboard drive it via window + * events, and it broadcasts its state back the same way. Advancing the clock + * dispatches `show-command` so the reused browser pane and actions list follow. */ @customElement(COMPONENT) export class TraceTimeline extends Element { @@ -47,10 +36,6 @@ export class TraceTimeline extends Element { @state() frames: TracePlayerFrame[] = [] - @consume({ context: networkRequestContext, subscribe: true }) - @state() - networkRequests: NetworkRequest[] = [] - /** Playback position in ms relative to the recording start. */ @state() currentMs = 0 @state() playing = false @@ -61,14 +46,11 @@ export class TraceTimeline extends Element { #activeTimestamp?: number #started = false - @query('[data-lanes]') lanesEl?: HTMLElement + @query('[data-scrub]') scrubEl?: HTMLElement #dragging = false - /** Network request whose detail drawer is open, or undefined. */ - @state() selectedRequest?: NetworkRequest - - static styles = [...Element.styles, networkStyles, timelineStyles] + static styles = [...Element.styles, timelineStyles] connectedCallback(): void { super.connectedCallback() @@ -76,6 +58,8 @@ export class TraceTimeline extends Element { window.addEventListener(KBD.step, this.#onKbdStep) window.addEventListener(KBD.jump, this.#onKbdJump) window.addEventListener(KBD.speed, this.#onKbdSpeed) + window.addEventListener(PLAYER_RESTART_EVENT, this.#onRestartEvent) + window.addEventListener(PLAYER_SPEED_EVENT, this.#onSpeedEvent) } disconnectedCallback(): void { @@ -87,6 +71,13 @@ export class TraceTimeline extends Element { window.removeEventListener(KBD.step, this.#onKbdStep) window.removeEventListener(KBD.jump, this.#onKbdJump) window.removeEventListener(KBD.speed, this.#onKbdSpeed) + window.removeEventListener(PLAYER_RESTART_EVENT, this.#onRestartEvent) + window.removeEventListener(PLAYER_SPEED_EVENT, this.#onSpeedEvent) + } + + #onRestartEvent = (): void => this.#restart() + #onSpeedEvent = (event: Event): void => { + this.speed = (event as CustomEvent<{ value: number }>).detail.value } #onKbdTogglePlay = (): void => this.#togglePlay() @@ -152,6 +143,17 @@ export class TraceTimeline extends Element { if (!this.#started && this.commands.length) { this.#syncActiveCommand() } + // Mirror playback state to the controls bar on the tab-header line. + window.dispatchEvent( + new CustomEvent(PLAYER_STATE_EVENT, { + detail: { + currentMs: this.currentMs, + duration: this.#duration, + playing: this.playing, + speed: this.speed + } + }) + ) } #stopRaf(): void { @@ -243,23 +245,14 @@ export class TraceTimeline extends Element { } } - #onSpeedChange(event: Event): void { - this.speed = Number((event.target as HTMLSelectElement).value) - } - - // ─── scrubbing (free-flow playhead drag) ─────────────────────────────────── + // ─── scrubbing (drag anywhere on the strip) ─────────────────────────────── #fractionFromClientX(clientX: number): number { - const rect = this.lanesEl?.getBoundingClientRect() - if (!rect) { + const rect = this.scrubEl?.getBoundingClientRect() + if (!rect || rect.width <= 0) { return 0 } - const laneStart = rect.left + GUTTER - const laneWidth = rect.width - GUTTER - INSET - if (laneWidth <= 0) { - return 0 - } - return Math.min(1, Math.max(0, (clientX - laneStart) / laneWidth)) + return Math.min(1, Math.max(0, (clientX - rect.left) / rect.width)) } #onPointerDown = (event: PointerEvent): void => { @@ -290,72 +283,6 @@ export class TraceTimeline extends Element { // ─── render ─────────────────────────────────────────────────────────────── - #ctrlButton( - title: string, - icon: TemplateResult, - onClick: () => void, - extra = '' - ): TemplateResult { - return html`` - } - - #renderControls(): TemplateResult { - return html` -
- ${this.#ctrlButton( - 'Restart', - html``, - () => this.#restart() - )} - ${this.#ctrlButton( - 'Previous action', - html``, - () => this.#step(-1) - )} - ${this.#ctrlButton( - this.playing ? 'Pause' : 'Play', - this.playing - ? html`` - : html``, - () => this.#togglePlay(), - 'text-chartsBlue' - )} - ${this.#ctrlButton( - 'Next action', - html``, - () => this.#step(1) - )} - ${formatTimecode(this.currentMs)} - / - ${formatTimecode(this.#duration)} - -
- ` - } - /** Timestamp of the frame nearest the playhead — drives filmstrip highlight. */ get #activeFrameTimestamp(): number | undefined { const clock = this.#start + this.currentMs @@ -371,16 +298,10 @@ export class TraceTimeline extends Element { return best } - // CSS left for a marker inside a track body (which starts after the gutter), - // leaving INSET of right margin so end-of-timeline markers don't hug the edge. - #laneLeft(fraction: number): string { - return `calc(${fraction} * (100% - ${INSET}px))` - } - #renderFilmstrip(): TemplateResult { if (!this.frames.length) { return html`
No frames captured
` @@ -388,150 +309,64 @@ export class TraceTimeline extends Element { const activeFrame = this.#activeFrameTimestamp return html`
-
-
- ${this.frames.map( - (frame) => - html`` - )} -
-
- ` - } - - #renderTrack( - label: string, - body: TemplateResult | typeof nothing - ): TemplateResult { - return html` -
-
- ${label} -
-
${body}
+ ${this.frames.map( + (frame) => + html`` + )}
` } - #renderActionsTrack(): TemplateResult { - const body = html`${this.#sortedCommands.map((command) => { - const ts = command.timestamp ?? 0 - const fraction = this.#fraction(ts) - const active = ts === this.#activeTimestamp - const color = CATEGORY_BG[commandCategory(command.command)] - // Track chips stay compact with the short command name; the full - // Playwright label is the hover tooltip (and the left Actions list). - return html`` - })}` - return this.#renderTrack('Actions', body) - } - - #renderNetworkTrack(): TemplateResult { - if (!this.networkRequests.length) { - return this.#renderTrack('Network', nothing) - } - const body = html`${this.networkRequests.map((request) => { - const leftFr = this.#fraction(request.startTime) - const rawFr = Math.max(0.004, (request.time ?? 0) / this.#duration) - const widthFr = Math.min(rawFr, 1 - leftFr) - const selected = this.selectedRequest?.id === request.id - // stopPropagation so a click selects the request rather than scrubbing the - // playhead (the lanes container owns the pointerdown drag handler). - return html`
` - })}` - return this.#renderTrack('Network', body) - } - - #renderNetworkDrawer(): TemplateResult | typeof nothing { - const req = this.selectedRequest - if (!req) { - return nothing - } + // Slim full-width ruler under the controls: action ticks + drag-to-scrub. + #renderRuler(): TemplateResult { return html` -
-
- ${req.method} ${req.url} - -
-
${renderNetworkRequestDetail(req)}
+
+ ${this.#sortedCommands.map((command) => { + const ts = command.timestamp ?? 0 + return html`
` + })}
` } #renderPlayhead(): TemplateResult { const fraction = Math.min(1, Math.max(0, this.currentMs / this.#duration)) - // Anchored at the gutter and inset on the right so it tracks the same lane - // coordinates as the action/network markers. return html`
` } render() { return html` - ${this.#renderControls()} ${this.#renderFilmstrip()}
- ${this.#renderActionsTrack()} ${this.#renderNetworkTrack()} - ${this.#renderTrack('Console', nothing)} ${this.#renderPlayhead()} + ${this.#renderRuler()} ${this.#renderFilmstrip()} + ${this.#renderPlayhead()}
- ${this.#renderNetworkDrawer()} ` } } diff --git a/packages/app/src/components/workbench.ts b/packages/app/src/components/workbench.ts index 5b87cd9d..0d56d7d9 100644 --- a/packages/app/src/components/workbench.ts +++ b/packages/app/src/components/workbench.ts @@ -26,23 +26,36 @@ import './workbench/network.js' import './workbench/compare.js' import './browser/snapshot.js' import './browser/trace-timeline.js' +import './browser/trace-player-controls.js' import { + HEADER_HEIGHT, MIN_WORKBENCH_HEIGHT, MIN_METATAB_WIDTH, ACTIONS_DEFAULT_WIDTH, BROWSER_HEIGHT_RATIO, - RERENDER_TIMEOUT + PLAYER_CONTROLS_HEIGHT, + PLAYER_DOCK_DEFAULT_HEIGHT, + PLAYER_DOCK_MIN_HEIGHT, + RERENDER_TIMEOUT, + TRACE_TIMELINE_MIN_HEIGHT, + TRACE_TIMELINE_DEFAULT_HEIGHT } from '../controller/constants.js' const COMPONENT = 'wdio-devtools-workbench' + +/** Pixel value from a DragController position string (`flex-basis: 123px`). */ +function basisPx(position: string): number | undefined { + const value = parseFloat(position.split(':')[1] ?? '') + return Number.isFinite(value) ? value : undefined +} @customElement(COMPONENT) export class DevtoolsWorkbench extends Element { #toolbarCollapsed = localStorage.getItem('toolbar') === 'true' #workbenchSidebarCollapsed = localStorage.getItem('workbenchSidebar') === 'true' - // Trace-player mode (`pnpm show-trace`): hide the Metadata tab and swap the - // workbench tabs for the timeline player. + // Trace-player mode (`pnpm show-trace`): the full workbench renders as in + // live mode, plus the playback timeline docked above the browser pane. @property({ type: Boolean }) playerMode = false @@ -99,6 +112,33 @@ export class DevtoolsWorkbench extends Element { direction: Direction.horizontal }) + #dragTimeline = new DragController(this, { + localStorageKey: 'traceTimelineHeight', + minPosition: TRACE_TIMELINE_MIN_HEIGHT, + maxPosition: window.innerHeight * 0.4, + initialPosition: TRACE_TIMELINE_DEFAULT_HEIGHT, + getContainerEl: () => this.#getVerticalWindow(), + direction: Direction.vertical + }) + + // Player-mode browser-pane height. Separate controller + storage key so + // resizing the player never disturbs the live-mode split. + #dragVerticalPlayer = new DragController(this, { + localStorageKey: 'playerBrowserHeight', + minPosition: MIN_WORKBENCH_HEIGHT, + maxPosition: window.innerHeight * 0.8, + initialPosition: Math.max( + MIN_WORKBENCH_HEIGHT, + window.innerHeight - + HEADER_HEIGHT - + PLAYER_CONTROLS_HEIGHT - + TRACE_TIMELINE_DEFAULT_HEIGHT - + PLAYER_DOCK_DEFAULT_HEIGHT + ), + getContainerEl: () => this.#getVerticalWindow(), + direction: Direction.vertical + }) + async #getHorizontalWindow() { await this.updateComplete return this.horizontalResizerWindow as Element @@ -144,13 +184,43 @@ export class DevtoolsWorkbench extends Element { if (this.#toolbarCollapsed) { return '' } - const m = this.#dragVertical.getPosition().match(/(\d+(?:\.\d+)?)px/) - const raw = m ? parseFloat(m[1]) : window.innerHeight * BROWSER_HEIGHT_RATIO + if (this.playerMode) { + // Player proportions: the snapshot dominates — pane gets everything the + // timeline, controls bar, and a compact dock don't need, clamped in CSS + // so the dock never drops below its minimum inside the viewport. + const fallback = + window.innerHeight - + HEADER_HEIGHT - + PLAYER_CONTROLS_HEIGHT - + this.#timelinePaneHeight() - + PLAYER_DOCK_DEFAULT_HEIGHT + const raw = basisPx(this.#dragVerticalPlayer.getPosition()) ?? fallback + const paneHeight = Math.max(MIN_WORKBENCH_HEIGHT, raw) + const maxHeight = `calc(100vh - ${ + HEADER_HEIGHT + PLAYER_CONTROLS_HEIGHT + PLAYER_DOCK_MIN_HEIGHT + }px - ${this.#timelinePaneHeight()}px)` + return `flex:0 0 ${paneHeight}px; height:${paneHeight}px; max-height:${maxHeight}; min-height:0;` + } + const raw = + basisPx(this.#dragVertical.getPosition()) ?? + window.innerHeight * BROWSER_HEIGHT_RATIO const capped = Math.min(raw, window.innerHeight * 0.7) const paneHeight = Math.max(MIN_WORKBENCH_HEIGHT, capped) return `flex:0 0 ${paneHeight}px; height:${paneHeight}px; max-height:70vh; min-height:0;` } + #timelinePaneHeight(): number { + const raw = + basisPx(this.#dragTimeline.getPosition()) ?? TRACE_TIMELINE_DEFAULT_HEIGHT + const capped = Math.min(raw, window.innerHeight * 0.4) + return Math.max(TRACE_TIMELINE_MIN_HEIGHT, capped) + } + + #computeTimelinePaneStyle(): string { + const paneHeight = this.#timelinePaneHeight() + return `flex:0 0 ${paneHeight}px; height:${paneHeight}px; max-height:40vh; min-height:0;` + } + #computeSidebarStyle(): string { if (this.#workbenchSidebarCollapsed) { return 'width:0; flex:0 0 0; overflow:hidden;' @@ -173,11 +243,9 @@ export class DevtoolsWorkbench extends Element { - ${this.playerMode - ? nothing - : html` - - `} + + +
diff --git a/packages/app/src/components/browser/trace-timeline-constants.ts b/packages/app/src/components/browser/trace-timeline-constants.ts index 1e6a1dad..9f057994 100644 --- a/packages/app/src/components/browser/trace-timeline-constants.ts +++ b/packages/app/src/components/browser/trace-timeline-constants.ts @@ -1,8 +1,16 @@ /** Playback speed multipliers offered in the timeline controls. */ export const SPEEDS = [0.5, 1, 2, 3, 5] -/** Window events linking the controls bar and the timeline strip — same - * decoupling pattern as the KBD events, so either side can be re-homed. */ +/** Candidate ruler intervals (ms); tickStep picks the smallest fitting one. */ +export const TICK_STEPS = [ + 100, 250, 500, 1_000, 2_000, 5_000, 10_000, 15_000, 30_000, 60_000, 120_000, + 300_000, 600_000 +] + +/** Ruler divisions to aim for — keeps labels readable at any duration. */ +export const TICK_TARGET_DIVISIONS = 14 + +/** Window events linking the controls bar and the timeline strip (KBD-style). */ export const PLAYER_STATE_EVENT = 'trace-player:state' export const PLAYER_RESTART_EVENT = 'trace-player:restart' export const PLAYER_SPEED_EVENT = 'trace-player:speed' diff --git a/packages/app/src/components/browser/trace-timeline-styles.ts b/packages/app/src/components/browser/trace-timeline-styles.ts index d65cbe24..f1482adc 100644 --- a/packages/app/src/components/browser/trace-timeline-styles.ts +++ b/packages/app/src/components/browser/trace-timeline-styles.ts @@ -1,6 +1,6 @@ import { css } from 'lit' -/** Styles for the trace-player timeline strip: host layout + hidden scrollbars. */ +/** Host layout for the trace-player timeline strip. */ export const timelineStyles = css` :host { position: relative; @@ -11,11 +11,4 @@ export const timelineStyles = css` background-color: var(--vscode-editor-background); color: var(--vscode-foreground); } - .no-scrollbar { - scrollbar-width: none; - -ms-overflow-style: none; - } - .no-scrollbar::-webkit-scrollbar { - display: none; - } ` diff --git a/packages/app/src/components/browser/trace-timeline-utils.ts b/packages/app/src/components/browser/trace-timeline-utils.ts index 1083ee65..ef99a363 100644 --- a/packages/app/src/components/browser/trace-timeline-utils.ts +++ b/packages/app/src/components/browser/trace-timeline-utils.ts @@ -1,9 +1,37 @@ +import { + TICK_STEPS, + TICK_TARGET_DIVISIONS +} from './trace-timeline-constants.js' + /** Detect image mime from a base64 string's magic bytes — trace screenshots * may be PNG (polling capture) or JPEG (CDP), and the zip names both `.jpeg`. */ export function imageMime(base64: string): string { return base64.startsWith('/9j/') ? 'image/jpeg' : 'image/png' } +export function tickStep( + durationMs: number, + targetTicks = TICK_TARGET_DIVISIONS +): number { + const raw = durationMs / targetTicks + return ( + TICK_STEPS.find((step) => step >= raw) ?? TICK_STEPS[TICK_STEPS.length - 1] + ) +} + +/** Ruler tick label: `500ms`, `3.5s`, `1:15`. */ +export function formatTickLabel(ms: number): string { + if (ms < 1_000) { + return `${ms}ms` + } + if (ms < 60_000) { + return `${(ms / 1_000).toFixed(1)}s` + } + const minutes = Math.floor(ms / 60_000) + const seconds = Math.round((ms % 60_000) / 1_000) + return `${minutes}:${String(seconds).padStart(2, '0')}` +} + /** `m:ss.cc` timecode (e.g. 32_270ms → `0:32.27`). */ export function formatTimecode(ms: number): string { const safe = Number.isFinite(ms) && ms > 0 ? ms : 0 diff --git a/packages/app/src/components/browser/trace-timeline.ts b/packages/app/src/components/browser/trace-timeline.ts index 2abc3753..21f7ffce 100644 --- a/packages/app/src/components/browser/trace-timeline.ts +++ b/packages/app/src/components/browser/trace-timeline.ts @@ -14,18 +14,17 @@ import { SPEEDS, type PlayerState } from './trace-timeline-constants.js' -import { formatTimecode, imageMime } from './trace-timeline-utils.js' +import { + formatTickLabel, + formatTimecode, + imageMime, + tickStep +} from './trace-timeline-utils.js' import { timelineStyles } from './trace-timeline-styles.js' const COMPONENT = 'wdio-devtools-trace-timeline' -/** - * Trace-player timeline strip, docked above the workbench in `pnpm show-trace` - * mode. Owns the playback clock, the screenshot filmstrip, and the playhead; - * the controls bar (trace-player-controls) and keyboard drive it via window - * events, and it broadcasts its state back the same way. Advancing the clock - * dispatches `show-command` so the reused browser pane and actions list follow. - */ +/** Player timeline strip: owns the playback clock, filmstrip, and playhead; wired to the controls bar and keyboard via window events, and drives the workbench via `show-command`. */ @customElement(COMPONENT) export class TraceTimeline extends Element { @consume({ context: commandContext, subscribe: true }) @@ -298,7 +297,45 @@ export class TraceTimeline extends Element { return best } - #renderFilmstrip(): TemplateResult { + get #ticks(): number[] { + const step = tickStep(this.#duration) + const out: number[] = [] + for (let t = step; t < this.#duration; t += step) { + out.push(t) + } + return out + } + + // Faint vertical gridlines at each ruler tick, spanning the whole strip. + #renderGridlines(): TemplateResult { + return html`${this.#ticks.map( + (tick) => + html`
` + )}` + } + + // Ruler labels stay inside the strip via the bounded translateX trick. + #renderRulerLabels(): TemplateResult { + return html` +
+ ${this.#ticks.map((tick) => { + const fraction = tick / this.#duration + return html`${formatTickLabel(tick)}` + })} +
+ ` + } + + // Thumbnails sit at their wall-clock position along the axis. + #renderThumbTrack(): TemplateResult { if (!this.frames.length) { return html`
- ${this.frames.map( - (frame) => - html`` - )} +
+ ${this.frames.map((frame) => { + const fraction = this.#fraction(frame.timestamp) + const active = frame.timestamp === activeFrame + return html`` + })}
` } - // Slim full-width ruler under the controls: action ticks + drag-to-scrub. - #renderRuler(): TemplateResult { + // Bottom scrub bar: full-width line, action tick marks, draggable knob. + #renderScrubBar(): TemplateResult { + const fraction = Math.min(1, Math.max(0, this.currentMs / this.#duration)) return html` -
+
+
${this.#sortedCommands.map((command) => { - const ts = command.timestamp ?? 0 + const tickFraction = this.#fraction(command.timestamp ?? 0) return html`
` })} +
` } - #renderPlayhead(): TemplateResult { - const fraction = Math.min(1, Math.max(0, this.currentMs / this.#duration)) - return html`
` - } - render() { return html`
- ${this.#renderRuler()} ${this.#renderFilmstrip()} - ${this.#renderPlayhead()} + ${this.#renderGridlines()} ${this.#renderRulerLabels()} + ${this.#renderThumbTrack()} ${this.#renderScrubBar()}
` } diff --git a/packages/app/src/components/workbench.ts b/packages/app/src/components/workbench.ts index 0d56d7d9..0bb9f243 100644 --- a/packages/app/src/components/workbench.ts +++ b/packages/app/src/components/workbench.ts @@ -6,10 +6,11 @@ import { consume } from '@lit/context' import { DragController, Direction } from '../utils/DragController.js' import { consoleLogContext, + metadataContext, networkRequestContext, baselineContext } from '../controller/context.js' -import type { PreservedAttempt } from '@wdio/devtools-shared' +import type { Metadata, PreservedAttempt } from '@wdio/devtools-shared' import '~icons/mdi/arrow-collapse-down.js' import '~icons/mdi/arrow-collapse-up.js' @@ -28,6 +29,7 @@ import './browser/snapshot.js' import './browser/trace-timeline.js' import './browser/trace-player-controls.js' import { + BROWSER_BACKDROP_GRADIENT, HEADER_HEIGHT, MIN_WORKBENCH_HEIGHT, MIN_METATAB_WIDTH, @@ -36,6 +38,7 @@ import { PLAYER_CONTROLS_HEIGHT, PLAYER_DOCK_DEFAULT_HEIGHT, PLAYER_DOCK_MIN_HEIGHT, + PLAYER_SNAPSHOT_WIDTH_RATIO, RERENDER_TIMEOUT, TRACE_TIMELINE_MIN_HEIGHT, TRACE_TIMELINE_DEFAULT_HEIGHT @@ -54,8 +57,7 @@ export class DevtoolsWorkbench extends Element { #workbenchSidebarCollapsed = localStorage.getItem('workbenchSidebar') === 'true' - // Trace-player mode (`pnpm show-trace`): the full workbench renders as in - // live mode, plus the playback timeline docked above the browser pane. + // Trace-player mode: full workbench plus the timeline strip and controls bar. @property({ type: Boolean }) playerMode = false @@ -71,6 +73,10 @@ export class DevtoolsWorkbench extends Element { @state() baselines: Map | undefined = undefined + @consume({ context: metadataContext, subscribe: true }) + @state() + metadata: Metadata | undefined = undefined + static styles = [ ...Element.styles, css` @@ -115,18 +121,18 @@ export class DevtoolsWorkbench extends Element { #dragTimeline = new DragController(this, { localStorageKey: 'traceTimelineHeight', minPosition: TRACE_TIMELINE_MIN_HEIGHT, - maxPosition: window.innerHeight * 0.4, + maxPosition: () => window.innerHeight * 0.4, initialPosition: TRACE_TIMELINE_DEFAULT_HEIGHT, getContainerEl: () => this.#getVerticalWindow(), direction: Direction.vertical }) - // Player-mode browser-pane height. Separate controller + storage key so - // resizing the player never disturbs the live-mode split. + // Player-mode pane height; own storage key so it never disturbs the live split. + // The live max bound keeps the handle (and pane) inside the current budget. #dragVerticalPlayer = new DragController(this, { - localStorageKey: 'playerBrowserHeight', + localStorageKey: 'playerPaneHeight', minPosition: MIN_WORKBENCH_HEIGHT, - maxPosition: window.innerHeight * 0.8, + maxPosition: () => this.#playerPaneBudget(), initialPosition: Math.max( MIN_WORKBENCH_HEIGHT, window.innerHeight - @@ -139,6 +145,27 @@ export class DevtoolsWorkbench extends Element { direction: Direction.vertical }) + // Player snapshot keeps the recorded viewport's shape, slightly narrowed. + #playerAspectRatio(): string { + const viewport = this.metadata?.viewport + const width = Math.round( + (viewport?.width || 1280) * PLAYER_SNAPSHOT_WIDTH_RATIO + ) + return `${width} / ${viewport?.height || 800}` + } + + // Space left for the snapshot pane once the fixed rows and dock minimum eat theirs. + #playerPaneBudget(): number { + return Math.max( + MIN_WORKBENCH_HEIGHT, + window.innerHeight - + HEADER_HEIGHT - + PLAYER_CONTROLS_HEIGHT - + PLAYER_DOCK_MIN_HEIGHT - + this.#timelinePaneHeight() + ) + } + async #getHorizontalWindow() { await this.updateComplete return this.horizontalResizerWindow as Element @@ -185,21 +212,12 @@ export class DevtoolsWorkbench extends Element { return '' } if (this.playerMode) { - // Player proportions: the snapshot dominates — pane gets everything the - // timeline, controls bar, and a compact dock don't need, clamped in CSS - // so the dock never drops below its minimum inside the viewport. - const fallback = - window.innerHeight - - HEADER_HEIGHT - - PLAYER_CONTROLS_HEIGHT - - this.#timelinePaneHeight() - - PLAYER_DOCK_DEFAULT_HEIGHT - const raw = basisPx(this.#dragVerticalPlayer.getPosition()) ?? fallback - const paneHeight = Math.max(MIN_WORKBENCH_HEIGHT, raw) + // Snapshot pane dominates; the CSS clamp keeps the dock minimum in view. + // Literal getPosition() basis lets adjustPosition sync slider ↔ clamped height. const maxHeight = `calc(100vh - ${ HEADER_HEIGHT + PLAYER_CONTROLS_HEIGHT + PLAYER_DOCK_MIN_HEIGHT }px - ${this.#timelinePaneHeight()}px)` - return `flex:0 0 ${paneHeight}px; height:${paneHeight}px; max-height:${maxHeight}; min-height:0;` + return `flex-grow:0; flex-shrink:0; ${this.#dragVerticalPlayer.getPosition()}; max-height:${maxHeight}; min-height:${MIN_WORKBENCH_HEIGHT}px;` } const raw = basisPx(this.#dragVertical.getPosition()) ?? @@ -344,6 +362,31 @@ export class DevtoolsWorkbench extends Element { ` } + #renderBrowserPane() { + // Player: the boxed host goes transparent and the pane carries the shared + // backdrop, so the aspect box blends instead of showing a gradient seam. + const playerPaneExtra = this.playerMode + ? ` background:${BROWSER_BACKDROP_GRADIENT};` + : '' + return html` +
+ ${this.playerMode + ? html`
+ +
` + : html``} +
+ ` + } + // Full-width playback strip above the workbench row — player mode only. #renderTimelineStrip() { if (!this.playerMode) { @@ -388,12 +431,7 @@ export class DevtoolsWorkbench extends Element {
-
- -
+ ${this.#renderBrowserPane()} ${!this.#toolbarCollapsed ? (this.playerMode ? this.#dragVerticalPlayer diff --git a/packages/app/src/controller/constants.ts b/packages/app/src/controller/constants.ts index 105e74ad..6992ebf4 100644 --- a/packages/app/src/controller/constants.ts +++ b/packages/app/src/controller/constants.ts @@ -14,6 +14,11 @@ export const PLAYER_DOCK_MIN_HEIGHT = 140 export const PLAYER_DOCK_DEFAULT_HEIGHT = 220 /** Controls bar on the tab-header line in player mode (matches h-10 headers). */ export const PLAYER_CONTROLS_HEIGHT = 40 +/** Width factor on the player snapshot's aspect box — trims width, keeps height. */ +export const PLAYER_SNAPSHOT_WIDTH_RATIO = 0.9 +/** Backdrop behind the browser chrome — shared with the snapshot component styles. */ +export const BROWSER_BACKDROP_GRADIENT = + 'radial-gradient(120% 120% at 50% 0%, var(--vscode-editorWidget-background), var(--vscode-editor-background))' /** Fixed app-header height (see header.ts / app.ts `h-[calc(100%-40px)]`). */ export const HEADER_HEIGHT = 40 export const LOG_ICONS: Record = { diff --git a/packages/app/src/controller/keyboard.ts b/packages/app/src/controller/keyboard.ts index 87559253..f94ce52c 100644 --- a/packages/app/src/controller/keyboard.ts +++ b/packages/app/src/controller/keyboard.ts @@ -32,7 +32,7 @@ function isTyping(event: KeyboardEvent): boolean { ) } -function emit(name: string, detail?: unknown): void { +export function emit(name: string, detail?: unknown): void { window.dispatchEvent(new CustomEvent(name, { detail })) } diff --git a/packages/app/src/utils/DragController.ts b/packages/app/src/utils/DragController.ts index 593d452d..d675570f 100644 --- a/packages/app/src/utils/DragController.ts +++ b/packages/app/src/utils/DragController.ts @@ -14,15 +14,22 @@ export enum Direction { type DragControllerHost = HTMLElement & ReactiveControllerHost type AsyncGetElFn = () => Element | Promise +/** Bounds accept getters so panes with a layout-dependent budget clamp live. */ +type Bound = number | (() => number) + interface DragControllerOptions { initialPosition: number direction: Direction localStorageKey?: string - minPosition?: number - maxPosition?: number + minPosition?: Bound + maxPosition?: Bound getContainerEl: AsyncGetElFn } +function resolveBound(bound: Bound | undefined): number | undefined { + return typeof bound === 'function' ? bound() : bound +} + type State = 'dragging' | 'idle' const defaultOptions = { @@ -107,16 +114,18 @@ export class DragController implements ReactiveController { } #setPosition(x: number, y: number) { + const min = resolveBound(this.#options.minPosition) ?? 0 + const max = resolveBound(this.#options.maxPosition) if (this.#options.direction === Direction.horizontal) { - let nx = Math.max(x, this.#options.minPosition || 0) - if (this.#options.maxPosition !== undefined) { - nx = Math.min(nx, this.#options.maxPosition) + let nx = Math.max(x, min) + if (max !== undefined) { + nx = Math.min(nx, max) } this.#x = nx } else { - let ny = Math.max(y, this.#options.minPosition || 0) - if (this.#options.maxPosition !== undefined) { - ny = Math.min(ny, this.#options.maxPosition) + let ny = Math.max(y, min) + if (max !== undefined) { + ny = Math.min(ny, max) } this.#y = ny } diff --git a/packages/app/tests/trace-timeline-utils.test.ts b/packages/app/tests/trace-timeline-utils.test.ts new file mode 100644 index 00000000..e3197c7b --- /dev/null +++ b/packages/app/tests/trace-timeline-utils.test.ts @@ -0,0 +1,33 @@ +import { describe, it, expect } from 'vitest' +import { + formatTickLabel, + tickStep +} from '../src/components/browser/trace-timeline-utils.js' + +describe('tickStep', () => { + it('picks the smallest step yielding at most the target tick count', () => { + expect(tickStep(10_000)).toBe(1_000) + expect(tickStep(78_460)).toBe(10_000) + expect(tickStep(1_200)).toBe(100) + }) + + it('caps at the largest step for very long traces', () => { + expect(tickStep(3 * 60 * 60 * 1000)).toBe(600_000) + }) +}) + +describe('formatTickLabel', () => { + it('formats sub-second ticks as milliseconds', () => { + expect(formatTickLabel(500)).toBe('500ms') + }) + + it('formats seconds with one decimal', () => { + expect(formatTickLabel(1_000)).toBe('1.0s') + expect(formatTickLabel(3_500)).toBe('3.5s') + }) + + it('formats minutes as m:ss', () => { + expect(formatTickLabel(75_000)).toBe('1:15') + expect(formatTickLabel(60_000)).toBe('1:00') + }) +}) From 56af6567ac3ed97805c331be38b805bd71262b32 Mon Sep 17 00:00:00 2001 From: Vishnu Vardhan Date: Fri, 3 Jul 2026 17:35:17 +0530 Subject: [PATCH 04/68] refactor(shared,backend,core): derive LogLevel from runtime LOG_LEVELS; tighten comments --- packages/backend/src/trace-reader-constants.ts | 5 ++++- packages/backend/src/trace-reader-utils.ts | 15 +++------------ packages/core/src/trace-console.ts | 12 ++++-------- packages/core/src/trace-snapshots.ts | 6 +----- packages/shared/src/types.ts | 10 +++++++++- 5 files changed, 21 insertions(+), 27 deletions(-) diff --git a/packages/backend/src/trace-reader-constants.ts b/packages/backend/src/trace-reader-constants.ts index 6e738464..0572b51f 100644 --- a/packages/backend/src/trace-reader-constants.ts +++ b/packages/backend/src/trace-reader-constants.ts @@ -1,4 +1,7 @@ -import { ACTION_MAP } from '@wdio/devtools-shared' +import { ACTION_MAP, LOG_LEVELS } from '@wdio/devtools-shared' + +/** Runtime lookup for narrowing foreign trace levels to the shared union. */ +export const LOG_LEVEL_SET: ReadonlySet = new Set(LOG_LEVELS) // Inverse of ACTION_MAP, derived so it can never drift from the forward map. // The forward map is many-to-one (url/navigateTo/get all → Page.navigate); the diff --git a/packages/backend/src/trace-reader-utils.ts b/packages/backend/src/trace-reader-utils.ts index b7162c12..e1f8e3dd 100644 --- a/packages/backend/src/trace-reader-utils.ts +++ b/packages/backend/src/trace-reader-utils.ts @@ -11,6 +11,7 @@ import { type Viewport } from '@wdio/devtools-shared' +import { LOG_LEVEL_SET } from './trace-reader-constants.js' import type { ConsoleEvent, ContextOptionsEvent, @@ -144,22 +145,12 @@ export function harToNetworkRequest( } } -const LOG_LEVELS: ReadonlySet = new Set([ - 'trace', - 'debug', - 'log', - 'info', - 'warn', - 'error' -]) - -// Reverse of the writer's level mapping ('warn' → 'warning'); a foreign -// trace zip can carry levels outside our union, which default to 'log'. +// Reverse level mapping; foreign levels outside our union default to 'log'. function fromTraceLevel(messageType: string): LogLevel { if (messageType === 'warning') { return 'warn' } - return LOG_LEVELS.has(messageType) ? (messageType as LogLevel) : 'log' + return LOG_LEVEL_SET.has(messageType) ? (messageType as LogLevel) : 'log' } export function buildConsoleLogs( diff --git a/packages/core/src/trace-console.ts b/packages/core/src/trace-console.ts index f9db30fd..78deb7aa 100644 --- a/packages/core/src/trace-console.ts +++ b/packages/core/src/trace-console.ts @@ -19,17 +19,14 @@ export interface StdioEvent { type: 'stdout' | 'stderr' timestamp: number text?: string - /** Extension field: preserves the test-vs-terminal origin the standard - * stdio vocabulary can't express. Foreign viewers ignore it. */ + /** Extension field: test-vs-terminal origin; foreign viewers ignore it. */ source?: Extract } -// Keeps a pathological run (console.log in a tight loop) from producing a -// trace.trace the viewer can't open. +// Caps pathological runs (console.log in a loop) so the trace stays openable. const MAX_CONSOLE_EVENTS = 10_000 -/** The trace format's console vocabulary uses 'warning'; 'trace' has no - * equivalent — 'debug' is the nearest severity. */ +/** Trace vocabulary uses 'warning'; 'trace' maps to the nearest severity, 'debug'. */ function toTraceLevel(level: ConsoleLog['type']): string { if (level === 'warn') { return 'warning' @@ -69,8 +66,7 @@ export function buildConsoleEvents( messageType: toTraceLevel(log.type), text, args: log.args.map((arg) => ({ preview: previewArg(arg), value: arg })), - // Location isn't captured at the console patch site; the event - // shape requires the field, so it ships zeroed. + // Location isn't captured at the patch site; the required field ships zeroed. location: { url: '', lineNumber: 0, columnNumber: 0 } } satisfies ConsoleEvent } diff --git a/packages/core/src/trace-snapshots.ts b/packages/core/src/trace-snapshots.ts index 0f139bdb..9e98704d 100644 --- a/packages/core/src/trace-snapshots.ts +++ b/packages/core/src/trace-snapshots.ts @@ -69,11 +69,7 @@ function frameForSnapshot( return frame } -/** - * Full filmstrip for the trace: the first snapshot is re-anchored to t=0 so - * viewers show the page state before any interaction; the rest keep their - * wall-time offsets. - */ +/** Filmstrip events; the first snapshot is re-anchored to t=0 for pre-interaction state. */ export function buildFilmstripEvents( snapshots: ActionSnapshot[], pageId: string, diff --git a/packages/shared/src/types.ts b/packages/shared/src/types.ts index 23ad3c7f..2f209aed 100644 --- a/packages/shared/src/types.ts +++ b/packages/shared/src/types.ts @@ -4,7 +4,15 @@ // these shapes. The backend stores and forwards them. The app consumes them. // See ARCHITECTURE.md §2 and CLAUDE.md §2.1. -export type LogLevel = 'trace' | 'debug' | 'log' | 'info' | 'warn' | 'error' +export const LOG_LEVELS = [ + 'trace', + 'debug', + 'log', + 'info', + 'warn', + 'error' +] as const +export type LogLevel = (typeof LOG_LEVELS)[number] /** Where a captured ConsoleLog entry originated. */ export type LogSource = 'browser' | 'test' | 'terminal' From fe190388f4fa334ba842da3504eaf89f259e728a Mon Sep 17 00:00:00 2001 From: Vishnu Vardhan Date: Fri, 3 Jul 2026 17:35:44 +0530 Subject: [PATCH 05/68] fix(backend): skip tracing-group markers when reconstructing player commands --- packages/backend/src/trace-reader.ts | 5 +++++ packages/backend/tests/trace-reader.test.ts | 15 +++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/packages/backend/src/trace-reader.ts b/packages/backend/src/trace-reader.ts index 6146998f..3c6b84ea 100644 --- a/packages/backend/src/trace-reader.ts +++ b/packages/backend/src/trace-reader.ts @@ -103,6 +103,11 @@ function buildCommands( const commands: CommandLog[] = [] let maxOffset = 0 for (const [callId, before] of events.befores) { + // Group markers are structure, not actions — as command rows their end + // timestamp ties with the last action and steals the active highlight. + if (before.class === 'Tracing') { + continue + } const after = events.afters.get(callId) const endOffset = after?.endTime ?? before.startTime maxOffset = Math.max(maxOffset, endOffset) diff --git a/packages/backend/tests/trace-reader.test.ts b/packages/backend/tests/trace-reader.test.ts index 8df8f9ff..7d1f059e 100644 --- a/packages/backend/tests/trace-reader.test.ts +++ b/packages/backend/tests/trace-reader.test.ts @@ -36,6 +36,14 @@ function fixtureZip(): Uint8Array { params: { selector: '#name', value: 'vishnu' } }, { type: 'after', callId: 'call@2', endTime: 160 }, + { + type: 'before', + callId: 'call@0', + startTime: 0, + class: 'Tracing', + method: 'tracingGroup', + params: { name: 'my test' } + }, { type: 'before', callId: 'call@3', @@ -50,6 +58,7 @@ function fixtureZip(): Uint8Array { endTime: 260, error: { message: 'boom' } }, + { type: 'after', callId: 'call@0', endTime: 260 }, { type: 'screencast-frame', pageId: 'page@abcd1234', @@ -175,6 +184,12 @@ describe('parseTraceZip', () => { expect(trace.sources).toEqual({}) }) + it('skips tracing group markers so the last command stays the last action', () => { + const { trace } = parseTraceZip(fixtureZip()) + expect(trace.commands.some((c) => c.command === 'tracingGroup')).toBe(false) + expect(trace.commands[trace.commands.length - 1].command).toBe('click') + }) + it('reconstructs console logs from console and stdio events', () => { const { trace } = parseTraceZip(fixtureZip()) expect(trace.consoleLogs).toEqual([ From c7f94b46a33e16ab23baf7377edea5b8ded1f74f Mon Sep 17 00:00:00 2001 From: Vishnu Vardhan Date: Fri, 3 Jul 2026 19:23:03 +0530 Subject: [PATCH 06/68] feat(shared,backend,app): nested action tree with failure rollup in the trace player --- examples/wdio/wdio.conf.ts | 2 +- .../src/components/workbench/action-tree.ts | 72 +++ .../workbench/actionItems/command.ts | 9 +- .../workbench/actionItems/duration.ts | 16 +- .../components/workbench/actionItems/group.ts | 69 +++ .../components/workbench/actionItems/item.ts | 17 + .../app/src/components/workbench/actions.ts | 106 ++++- .../src/components/workbench/call-source.ts | 45 ++ .../app/src/components/workbench/source.ts | 66 ++- .../src/components/workbench/source/styles.ts | 13 + packages/app/src/controller/DataManager.ts | 92 ++-- packages/app/src/controller/context.ts | 7 + packages/app/tests/action-tree.test.ts | 95 ++++ packages/app/tests/call-source.test.ts | 93 +++- packages/app/tests/duration.test.ts | 9 + .../backend/src/trace-reader-constants.ts | 12 + packages/backend/src/trace-reader-groups.ts | 165 +++++++ packages/backend/src/trace-reader-types.ts | 21 +- packages/backend/src/trace-reader-utils.ts | 99 +++- packages/backend/src/trace-reader.ts | 258 ++++++++--- packages/backend/tests/trace-reader.test.ts | 435 +++++++++++++++++- packages/core/src/index.ts | 4 + packages/core/src/sha1.ts | 6 + packages/core/src/trace-action-events.ts | 195 ++++++++ packages/core/src/trace-exporter.ts | 186 ++------ packages/core/src/trace-frame-snapshots.ts | 154 +++++++ packages/core/src/trace-sources.ts | 65 +++ packages/core/tests/trace-exporter.test.ts | 181 +++++++- .../core/tests/trace-frame-snapshots.test.ts | 180 ++++++++ packages/core/tests/trace-sources.test.ts | 71 +++ packages/shared/src/trace-player.ts | 22 + 31 files changed, 2458 insertions(+), 307 deletions(-) create mode 100644 packages/app/src/components/workbench/action-tree.ts create mode 100644 packages/app/src/components/workbench/actionItems/group.ts create mode 100644 packages/app/tests/action-tree.test.ts create mode 100644 packages/backend/src/trace-reader-groups.ts create mode 100644 packages/core/src/sha1.ts create mode 100644 packages/core/src/trace-action-events.ts create mode 100644 packages/core/src/trace-frame-snapshots.ts create mode 100644 packages/core/src/trace-sources.ts create mode 100644 packages/core/tests/trace-frame-snapshots.test.ts create mode 100644 packages/core/tests/trace-sources.test.ts diff --git a/examples/wdio/wdio.conf.ts b/examples/wdio/wdio.conf.ts index 0f7b3470..b6305819 100644 --- a/examples/wdio/wdio.conf.ts +++ b/examples/wdio/wdio.conf.ts @@ -131,7 +131,7 @@ export const config: Options.Testrunner = { [ 'devtools', { - mode: 'live' as const, + mode: 'trace' as const, screencast: { enabled: true, pollIntervalMs: 200 } } ] diff --git a/packages/app/src/components/workbench/action-tree.ts b/packages/app/src/components/workbench/action-tree.ts new file mode 100644 index 00000000..723f83af --- /dev/null +++ b/packages/app/src/components/workbench/action-tree.ts @@ -0,0 +1,72 @@ +// Pure helpers behind the player's collapsible action tree: flattening the +// group tree into render rows and deciding which groups start expanded. + +import type { + TraceActionChild, + TraceActionGroupNode +} from '@wdio/devtools-shared' + +export interface GroupRow { + kind: 'group' + group: TraceActionGroupNode + depth: number + expanded: boolean +} + +export interface CommandRow { + kind: 'command' + commandIndex: number + depth: number +} + +export type ActionTreeRow = GroupRow | CommandRow + +/** Command indices anywhere under a group, nested groups included. */ +export function collectCommandIndices(group: TraceActionGroupNode): number[] { + const indices: number[] = [] + for (const child of group.children) { + if ('group' in child) { + indices.push(...collectCommandIndices(child.group)) + } else { + indices.push(child.commandIndex) + } + } + return indices +} + +/** Groups open by default when failed or when holding the active command. */ +export function defaultExpanded( + group: TraceActionGroupNode, + activeCommandIndex?: number +): boolean { + if (group.failed) { + return true + } + return ( + activeCommandIndex !== undefined && + collectCommandIndices(group).includes(activeCommandIndex) + ) +} + +/** Flatten the tree into render rows, descending only into expanded groups. */ +export function flattenActionTree( + children: TraceActionChild[], + isExpanded: (group: TraceActionGroupNode) => boolean, + depth = 0 +): ActionTreeRow[] { + const rows: ActionTreeRow[] = [] + for (const child of children) { + if ('group' in child) { + const expanded = isExpanded(child.group) + rows.push({ kind: 'group', group: child.group, depth, expanded }) + if (expanded) { + rows.push( + ...flattenActionTree(child.group.children, isExpanded, depth + 1) + ) + } + } else { + rows.push({ kind: 'command', commandIndex: child.commandIndex, depth }) + } + } + return rows +} diff --git a/packages/app/src/components/workbench/actionItems/command.ts b/packages/app/src/components/workbench/actionItems/command.ts index a587369e..1a596f56 100644 --- a/packages/app/src/components/workbench/actionItems/command.ts +++ b/packages/app/src/components/workbench/actionItems/command.ts @@ -33,6 +33,10 @@ export class CommandItem extends ActionItem { @property({ type: Object, attribute: true }) entry?: CommandLog + willUpdate(): void { + this.failed = Boolean(this.entry?.error) + } + #highlightLine() { const event = new CustomEvent('show-command', { detail: { @@ -85,7 +89,10 @@ export class CommandItem extends ActionItem { @click="${() => this.#highlightLine()}" > ${this.iconChip(this.#renderIcon(entry.command))} - ${entry.title ?? entry.command} ${this.renderTime()} diff --git a/packages/app/src/components/workbench/actionItems/duration.ts b/packages/app/src/components/workbench/actionItems/duration.ts index db4b7fbc..5d2df61f 100644 --- a/packages/app/src/components/workbench/actionItems/duration.ts +++ b/packages/app/src/components/workbench/actionItems/duration.ts @@ -3,17 +3,19 @@ export type DurationHeat = 'fast' | 'mid' | 'slow' const ONE_SECOND = 1000 const ONE_MINUTE = ONE_SECOND * 60 -/** Human-readable duration: `ms` under a second, `s` under a minute, `m s` above. */ +/** Human-readable duration: `ms` under a second, `s` under a minute, `m s` + * above. Rounds first — reconstructed traces carry fractional-ms clocks. */ export function formatDuration(ms: number): string { - if (ms > ONE_MINUTE) { - const minutes = Math.floor(ms / ONE_MINUTE) - const seconds = Math.floor((ms - minutes * ONE_MINUTE) / ONE_SECOND) + const rounded = Math.round(ms) + if (rounded > ONE_MINUTE) { + const minutes = Math.floor(rounded / ONE_MINUTE) + const seconds = Math.floor((rounded - minutes * ONE_MINUTE) / ONE_SECOND) return `${minutes}m ${seconds}s` } - if (ms > ONE_SECOND) { - return `${(ms / ONE_SECOND).toFixed(2)}s` + if (rounded > ONE_SECOND) { + return `${(rounded / ONE_SECOND).toFixed(2)}s` } - return `${ms}ms` + return `${rounded}ms` } /** Bucket a step duration so slow steps stand out: fast < 500ms ≤ mid < 2s ≤ slow. */ diff --git a/packages/app/src/components/workbench/actionItems/group.ts b/packages/app/src/components/workbench/actionItems/group.ts new file mode 100644 index 00000000..10780076 --- /dev/null +++ b/packages/app/src/components/workbench/actionItems/group.ts @@ -0,0 +1,69 @@ +import { html } from 'lit' +import { customElement, property } from 'lit/decorators.js' + +import type { TraceActionGroupNode } from '@wdio/devtools-shared' + +import { ActionItem } from './item.js' +import '~icons/mdi/chevron-right.js' + +const SOURCE_COMPONENT = 'wdio-devtools-group-item' + +/** Collapsible step/group row of the trace player's action tree. */ +@customElement(SOURCE_COMPONENT) +export class GroupItem extends ActionItem { + @property({ type: Object }) + group?: TraceActionGroupNode + + /** Whether the group's children are currently rendered below it. */ + @property({ type: Boolean, reflect: true }) + expanded = false + + willUpdate(): void { + this.failed = Boolean(this.group?.failed) + this.duration = this.group + ? this.group.endTime - this.group.startTime + : undefined + } + + #toggle() { + this.dispatchEvent( + new CustomEvent('group-toggle', { + detail: { callId: this.group?.callId, expanded: this.expanded }, + bubbles: true, + composed: true + }) + ) + } + + render() { + if (!this.group) { + return + } + return html` + + ` + } +} + +declare global { + interface HTMLElementTagNameMap { + [SOURCE_COMPONENT]: GroupItem + } +} diff --git a/packages/app/src/components/workbench/actionItems/item.ts b/packages/app/src/components/workbench/actionItems/item.ts index 931a2299..945a79db 100644 --- a/packages/app/src/components/workbench/actionItems/item.ts +++ b/packages/app/src/components/workbench/actionItems/item.ts @@ -29,6 +29,10 @@ export class ActionItem extends Element { @property({ type: Boolean, reflect: true }) active = false + /** Whether this row's action errored — drives the red row treatment. */ + @property({ type: Boolean, reflect: true }) + failed = false + static styles = [ ...Element.styles, css` @@ -67,6 +71,19 @@ export class ActionItem extends Element { :host([active]) .ic { border-color: var(--accent); } + :host([failed]) button { + background: color-mix( + in srgb, + var(--vscode-charts-red) 8%, + transparent + ); + box-shadow: inset 2px 0 0 var(--vscode-charts-red); + } + :host([failed][active]) button { + box-shadow: + inset 2px 0 0 var(--vscode-charts-red), + inset 0 0 0 1px var(--vscode-panel-border); + } ` ] diff --git a/packages/app/src/components/workbench/actions.ts b/packages/app/src/components/workbench/actions.ts index 86f4e856..842f4f56 100644 --- a/packages/app/src/components/workbench/actions.ts +++ b/packages/app/src/components/workbench/actions.ts @@ -1,21 +1,38 @@ import { Element } from '@core/element' -import { html, css } from 'lit' +import { html, css, nothing } from 'lit' import { customElement, state } from 'lit/decorators.js' import { consume } from '@lit/context' -import type { CommandLog } from '@wdio/devtools-shared' -import { mutationContext, commandContext } from '../../controller/context.js' +import type { + CommandLog, + TraceActionChild, + TraceActionGroupNode +} from '@wdio/devtools-shared' +import { + mutationContext, + commandContext, + actionGroupsContext +} from '../../controller/context.js' import '../placeholder.js' import './actionItems/command.js' +import './actionItems/group.js' import './actionItems/mutation.js' import { stepDurations } from './actionItems/duration.js' import { activeTimestampAt } from './active-entry.js' +import { + defaultExpanded, + flattenActionTree, + type ActionTreeRow +} from './action-tree.js' type TimelineEntry = TraceMutation | CommandLog const SOURCE_COMPONENT = 'wdio-devtools-actions' +/** Horizontal shift per tree depth level in the player's action tree. */ +const TREE_INDENT_PX = 14 + @customElement(SOURCE_COMPONENT) export class DevtoolsActions extends Element { static styles = [ @@ -47,6 +64,11 @@ export class DevtoolsActions extends Element { background: var(--vscode-panel-border); pointer-events: none; } + + /* Tree mode indents rows, so the straight rail no longer lines up. */ + .timeline.tree::before { + display: none; + } ` ] @@ -56,12 +78,32 @@ export class DevtoolsActions extends Element { @consume({ context: commandContext, subscribe: true }) commands: CommandLog[] = [] + @consume({ context: actionGroupsContext, subscribe: true }) + groups?: TraceActionChild[] + // The selected timeline row, tracked by object reference — timestamps aren't // unique (commands logged in the same millisecond would all match), so // reference identity is what highlights exactly one row. @state() private activeEntry?: TimelineEntry + // User chevron toggles, by group callId; unset groups follow the default + // (failed or containing the active command → open). + @state() + private expandOverrides: ReadonlyMap = new Map() + + #onGroupToggle = (event: Event) => { + const { callId, expanded } = ( + event as CustomEvent<{ callId?: string; expanded: boolean }> + ).detail + if (!callId) { + return + } + const next = new Map(this.expandOverrides) + next.set(callId, !expanded) + this.expandOverrides = next + } + #onShowCommand = (event: Event) => { const command = (event as CustomEvent<{ command?: CommandLog }>).detail ?.command @@ -146,7 +188,65 @@ export class DevtoolsActions extends Element { } } + // Player tree mode: group rows expand/collapse; leaf rows are the same + // command items as the flat list, indented under their group. + #renderTree(rootChildren: TraceActionChild[]) { + const commands = this.commands || [] + const activeIndex = + this.activeEntry && 'command' in this.activeEntry + ? commands.indexOf(this.activeEntry) + : -1 + const isExpanded = (group: TraceActionGroupNode) => + this.expandOverrides.get(group.callId) ?? + defaultExpanded(group, activeIndex >= 0 ? activeIndex : undefined) + const rows = flattenActionTree(rootChildren, isExpanded) + const baseline = commands[0]?.timestamp ?? 0 + const gaps = stepDurations(commands.map((command) => command.timestamp)) + return html`
+ ${rows.map((row) => this.#renderTreeRow(row, commands, baseline, gaps))} +
` + } + + #renderTreeRow( + row: ActionTreeRow, + commands: CommandLog[], + baseline: number, + gaps: Array + ) { + const indent = `padding-left: ${row.depth * TREE_INDENT_PX}px` + if (row.kind === 'group') { + return html` + + ` + } + const entry = commands[row.commandIndex] + if (!entry) { + return nothing + } + // Reconstructed zips carry the real invocation span; gap is the fallback. + const duration = + entry.startTime !== undefined + ? entry.timestamp - entry.startTime + : gaps[row.commandIndex] + return html` + + ` + } + render() { + if (this.groups?.length) { + return this.#renderTree(this.groups) + } const entries = this.#sortedEntries() if (!entries.length) { diff --git a/packages/app/src/components/workbench/call-source.ts b/packages/app/src/components/workbench/call-source.ts index 9349b184..3fa2d5ca 100644 --- a/packages/app/src/components/workbench/call-source.ts +++ b/packages/app/src/components/workbench/call-source.ts @@ -23,6 +23,51 @@ export function parseCallSource( } } +/** Path with any trailing `:line` / `:line:column` suffix stripped — some + * recorded traces glue the line onto the file, so lookups compare clean paths. */ +export function normalizeSourcePath(path: string): string { + const match = path.match(/:\d+:\d+$/) || path.match(/:\d+$/) + return match && match.index ? path.slice(0, match.index) : path +} + +/** Key in `sources` holding `file`'s content — exact match first, then a + * normalized-path match so suffixed keys and clean queries still pair up. */ +export function resolveSourceFile( + sources: Record, + file: string +): string | undefined { + if (file in sources) { + return file + } + const target = normalizeSourcePath(file) + return Object.keys(sources).find((key) => normalizeSourcePath(key) === target) +} + +/** Normalized display list: every captured source file plus any file referenced + * by a command call source, deduplicated by clean path. */ +export function listSourceFiles( + sources: Record, + callSources: (string | undefined)[] +): string[] { + const files: string[] = [] + const seen = new Set() + const add = (path: string) => { + const normalized = normalizeSourcePath(path) + if (!seen.has(normalized)) { + seen.add(normalized) + files.push(normalized) + } + } + Object.keys(sources).forEach(add) + for (const callSource of callSources) { + const parsed = callSource ? parseCallSource(callSource) : null + if (parsed) { + add(parsed.file) + } + } + return files +} + /** Last path segment, handling both POSIX (`/`) and Windows (`\`) separators. */ export function fileBasename(path: string): string { const segments = path.split(/[/\\]/) diff --git a/packages/app/src/components/workbench/source.ts b/packages/app/src/components/workbench/source.ts index 81a12d0b..fcdb6fe5 100644 --- a/packages/app/src/components/workbench/source.ts +++ b/packages/app/src/components/workbench/source.ts @@ -13,7 +13,14 @@ import type { CommandLog } from '@wdio/devtools-shared' import { sourceContext, commandContext } from '../../controller/context.js' import { commandCategory, type ActionCategory } from './actionItems/category.js' -import { parseCallSource, fileBasename, pathSegments } from './call-source.js' +import { + parseCallSource, + fileBasename, + pathSegments, + normalizeSourcePath, + resolveSourceFile, + listSourceFiles +} from './call-source.js' import { sourceStyles } from './source/styles.js' import '../placeholder.js' @@ -95,12 +102,26 @@ export class DevtoolsSource extends Element { return document.body.classList.contains('dark') } + /** Captured files plus files referenced by command call sources (clean paths). */ + get #fileList(): string[] { + return listSourceFiles( + this.sources || {}, + (this.commands || []).map((c) => c.callSource) + ) + } + /** File to show: an explicit selection/call-site, else the first available. */ get #effectiveFile(): string | undefined { - if (this.activeFile && this.sources?.[this.activeFile]) { + const files = this.#fileList + if (this.activeFile && files.includes(this.activeFile)) { return this.activeFile } - return Object.keys(this.sources || {})[0] + return files[0] + } + + #contentFor(file: string): string | undefined { + const key = resolveSourceFile(this.sources || {}, file) + return key !== undefined ? this.sources[key] : undefined } connectedCallback(): void { @@ -145,8 +166,7 @@ export class DevtoolsSource extends Element { super.disconnectedCallback() window.removeEventListener('app-source-highlight', this.#onHighlight) window.removeEventListener('app-source-track', this.#onTrack) - this.#editorView?.destroy() - this.#editorView = undefined + this.#unmountEditor() this.#tabObserver?.disconnect() this.#tabObserver = undefined this.#themeObserver?.disconnect() @@ -158,6 +178,10 @@ export class DevtoolsSource extends Element { if (!target) { return } + if (this.#contentFor(target) === undefined) { + this.#unmountEditor() + return + } this.#mountEditor(target) this.#refreshCallSite() } @@ -167,10 +191,11 @@ export class DevtoolsSource extends Element { if (!parsed) { return } - this.activeFile = parsed.file - this.callSiteFile = parsed.file + const file = normalizeSourcePath(parsed.file) + this.activeFile = file + this.callSiteFile = file this.callSiteLine = parsed.line - const cmd = this.#commandAt(parsed.file, parsed.line) + const cmd = this.#commandAt(file, parsed.line) this.callSiteCommand = cmd?.command this.callSiteCategory = cmd ? commandCategory(cmd.command) : 'other' if (activateTab) { @@ -184,7 +209,11 @@ export class DevtoolsSource extends Element { return false } const parsed = parseCallSource(c.callSource) - return parsed?.file === file && parsed.line === line + return ( + !!parsed && + normalizeSourcePath(parsed.file) === file && + parsed.line === line + ) }) } @@ -192,9 +221,15 @@ export class DevtoolsSource extends Element { this.activeFile = file } + #unmountEditor() { + this.#editorView?.destroy() + this.#editorView = undefined + this.#mountedFile = undefined + } + #mountEditor(filePath: string) { - const source = this.sources?.[filePath] - if (!source) { + const source = this.#contentFor(filePath) + if (source === undefined) { return } const container = @@ -256,7 +291,7 @@ export class DevtoolsSource extends Element { } #renderFileTabs(active: string) { - return Object.keys(this.sources || {}).map( + return this.#fileList.map( (file) => html` ` } @@ -197,6 +214,9 @@ export class DevtoolsTab extends Element { @property({ type: Number }) badge?: number + @property({ type: String }) + badgeTone?: BadgeTone + static styles = [ ...Element.styles, css` diff --git a/packages/app/src/components/workbench.ts b/packages/app/src/components/workbench.ts index 0bb9f243..a97b20c9 100644 --- a/packages/app/src/components/workbench.ts +++ b/packages/app/src/components/workbench.ts @@ -8,9 +8,17 @@ import { consoleLogContext, metadataContext, networkRequestContext, - baselineContext + baselineContext, + commandContext, + suiteContext } from '../controller/context.js' -import type { Metadata, PreservedAttempt } from '@wdio/devtools-shared' +import type { + CommandLog, + Metadata, + PreservedAttempt +} from '@wdio/devtools-shared' +import type { SuiteStatsFragment } from '../controller/types.js' +import { collectErrors } from './workbench/errors/collect.js' import '~icons/mdi/arrow-collapse-down.js' import '~icons/mdi/arrow-collapse-up.js' @@ -24,6 +32,7 @@ import './workbench/logs.js' import './workbench/console.js' import './workbench/metadata.js' import './workbench/network.js' +import './workbench/errors.js' import './workbench/compare.js' import './browser/snapshot.js' import './browser/trace-timeline.js' @@ -73,6 +82,14 @@ export class DevtoolsWorkbench extends Element { @state() baselines: Map | undefined = undefined + @consume({ context: commandContext, subscribe: true }) + @state() + commands: CommandLog[] | undefined = undefined + + @consume({ context: suiteContext, subscribe: true }) + @state() + suites: Record[] | undefined = undefined + @consume({ context: metadataContext, subscribe: true }) @state() metadata: Metadata | undefined = undefined @@ -317,6 +334,44 @@ export class DevtoolsWorkbench extends Element { ` } + #errorCount(): number { + return collectErrors(this.commands, this.suites).length + } + + // Dock tab list — extracted so #renderWorkbenchTabs stays under the size cap. + #renderDockTabItems() { + return html` + + + + + + + + + + + + + + + + ${this.#renderCompareTabIfAvailable()} + ` + } + #renderWorkbenchTabs() { return html` - - - - - - - - - - - - - ${this.#renderCompareTabIfAvailable()} + ${this.#renderDockTabItems()}
+ ` + } + + render() { + const errors = collectErrors(this.commands, this.suites) + if (!errors.length) { + return html` +
+
+
No errors
+
+ ` + } + return html`${errors.map((error) => this.#renderEntry(error))}` + } +} + +declare global { + interface HTMLElementTagNameMap { + [COMPONENT]: DevtoolsErrors + } +} diff --git a/packages/app/src/components/workbench/errors/collect.ts b/packages/app/src/components/workbench/errors/collect.ts new file mode 100644 index 00000000..ca7429e1 --- /dev/null +++ b/packages/app/src/components/workbench/errors/collect.ts @@ -0,0 +1,232 @@ +/** + * Pure error-collection for the workbench Errors tab. Merges failed commands + * (from `commandContext`) with failed tests (from `suiteContext`) into a single + * ordered, de-duplicated list. Kept framework-free and side-effect-free so the + * tab component only has to render what this returns. + */ + +import type { CommandLog } from '@wdio/devtools-shared' +import type { + SuiteStatsFragment, + TestStatsFragment +} from '../../../controller/types.js' +import { stripAnsi } from '../console-filter.js' + +/** One row in the Errors tab. */ +export interface CollectedError { + /** Failing action/step or test title — the row heading. */ + title: string + /** Error message shown message-first, monospace. */ + message: string + /** Optional stack, rendered under the message when present. */ + stack?: string + /** `file:line:col` source anchor for the "open source" link. */ + callSource?: string + /** The failing command, when the error came from one — lets the tab dispatch + * `show-command` to select and scroll to that action. */ + command?: CommandLog + /** Command timestamp; drives ordering and the `show-command` elapsed time. */ + timestamp?: number + /** Assertion expected value, rendered as a labelled row when present. */ + expected?: string + /** Assertion received/actual value, rendered as a labelled row when present. */ + actual?: string +} + +const ASSERTION_COMMAND_RE = /^(expect|assert|verify)\./ + +/** Display string for an expected/actual value that may already be serialized. */ +function displayValue(value: unknown): string | undefined { + if (value === undefined || value === null) { + return undefined + } + if (typeof value === 'string') { + return value + } + try { + return JSON.stringify(value) + } catch { + return String(value) + } +} + +/** Assertion commands carry `[actual, expected]` in args (see the exporter's + * Assert params + synthesizeExpectFailure). */ +function assertionValues(command: CommandLog): { + actual?: string + expected?: string +} { + if (!ASSERTION_COMMAND_RE.test(command.command) || command.args.length < 2) { + return {} + } + return { + actual: displayValue(command.args[0]), + expected: displayValue(command.args[1]) + } +} + +interface ReadableError { + message?: string + name?: string + stack?: string + expected?: unknown + actual?: unknown +} + +/** Split trailing `at …` stack-frame lines off the message body. */ +function splitStack(clean: string): { body: string; stack?: string } { + const lines = clean.split('\n') + const idx = lines.findIndex((line) => /^\s*at\s/.test(line)) + if (idx === -1) { + return { body: clean.trimEnd() } + } + return { + body: lines.slice(0, idx).join('\n').trimEnd(), + stack: lines.slice(idx).join('\n').trim() + } +} + +/** Trim each line and drop blanks — assertion libraries indent continuation + * lines, which would otherwise show as ragged whitespace. */ +function dedent(text: string): string { + return text + .split('\n') + .map((line) => line.trim()) + .filter(Boolean) + .join('\n') +} + +/** Pull `Expected:` / `Received:` values out of a matcher body and return the + * remaining headline. The labels may be indented (expect-webdriverio pads + * them), and `Received:` can span several lines up to the end of the body. */ +function extractDiff(body: string): { + headline: string + expected?: string + actual?: string +} { + const expected = body.match(/^[ \t]*Expected:[ \t]*(.*)$/m)?.[1]?.trim() + const receivedAt = body.search(/^[ \t]*Received:/m) + let actual: string | undefined + let headline = body + if (receivedAt !== -1) { + actual = dedent( + body.slice(receivedAt).replace(/^[ \t]*Received:[ \t]*/, '') + ) + headline = body.slice(0, receivedAt) + } + if (expected !== undefined) { + headline = headline.replace(/^[ \t]*Expected:[ \t]*.*$/m, '') + } + return { headline: dedent(headline), expected, actual } +} + +/** Clean, structured view of any error-ish value: ANSI stripped, stack split + * off the message, and assertion Expected/Received pulled into fields. */ +function readError(error: unknown): + | { + message: string + stack?: string + expected?: string + actual?: string + } + | undefined { + if (!error || typeof error !== 'object') { + return undefined + } + const e = error as ReadableError + const raw = e.message?.trim() || e.name?.trim() || '' + if (!raw && !e.stack) { + return undefined + } + const { body, stack } = splitStack(stripAnsi(raw)) + const diff = extractDiff(body) + return { + message: diff.headline || 'Error', + stack: e.stack ? stripAnsi(e.stack) : stack, + expected: diff.expected ?? displayValue(e.expected), + actual: diff.actual ?? displayValue(e.actual) + } +} + +/** Failed leaf tests across every suite map, deduped by uid (last wins, matching + * the sidebar's root-suite dedup so we read the freshest fragment). */ +function collectFailedTests( + suites: Record[] | undefined +): TestStatsFragment[] { + const byUid = new Map() + const visit = (suite: SuiteStatsFragment) => { + for (const test of suite.tests ?? []) { + if (test.state === 'failed') { + byUid.set(test.uid, test) + } + } + for (const child of suite.suites ?? []) { + visit(child) + } + } + for (const map of suites ?? []) { + for (const suite of Object.values(map)) { + visit(suite) + } + } + return [...byUid.values()] +} + +function commandErrors(commands: CommandLog[] | undefined): CollectedError[] { + return (commands ?? []) + .flatMap((command) => { + const read = readError(command.error) + if (!read) { + return [] + } + const values = assertionValues(command) + return [ + { + title: command.title ?? command.command, + message: read.message, + stack: read.stack, + callSource: command.callSource, + command, + timestamp: command.timestamp, + expected: values.expected ?? read.expected, + actual: values.actual ?? read.actual + } + ] + }) + .sort((a, b) => (a.timestamp ?? 0) - (b.timestamp ?? 0)) +} + +/** + * Build the Errors-tab list from the live/player contexts. + * + * Command failures come first (time-ordered) because they carry the clickable + * action; a failed test that only echoes a command's message is dropped so the + * same failure isn't listed twice (e.g. a Cucumber `Then` fails as both the + * assertion command and the scenario). + */ +export function collectErrors( + commands: CommandLog[] | undefined, + suites: Record[] | undefined +): CollectedError[] { + const fromCommands = commandErrors(commands) + const seenMessages = new Set(fromCommands.map((e) => e.message)) + + const fromTests = collectFailedTests(suites).flatMap((test) => { + const read = readError(test.error ?? test.errors?.[0]) + if (!read || seenMessages.has(read.message)) { + return [] + } + return [ + { + title: test.fullTitle || test.title || test.uid, + message: read.message, + stack: read.stack, + callSource: test.callSource, + expected: read.expected, + actual: read.actual + } + ] + }) + + return [...fromCommands, ...fromTests] +} diff --git a/packages/app/src/components/workbench/source.ts b/packages/app/src/components/workbench/source.ts index fcdb6fe5..5b336bda 100644 --- a/packages/app/src/components/workbench/source.ts +++ b/packages/app/src/components/workbench/source.ts @@ -110,13 +110,11 @@ export class DevtoolsSource extends Element { ) } - /** File to show: an explicit selection/call-site, else the first available. */ + /** File to show: an explicit selection/call-site wins even when it wasn't + * captured as a source (the toolbar then shows a not-captured state instead + * of silently falling back to a different file), else the first available. */ get #effectiveFile(): string | undefined { - const files = this.#fileList - if (this.activeFile && files.includes(this.activeFile)) { - return this.activeFile - } - return files[0] + return this.activeFile ?? this.#fileList[0] } #contentFor(file: string): string | undefined { From dd4bc5bbcbf2bc71fe8d71fc6d5e559616b919ce Mon Sep 17 00:00:00 2001 From: Vishnu Vardhan Date: Wed, 8 Jul 2026 10:57:11 +0530 Subject: [PATCH 15/68] fix(service,nightwatch): distinct rerun-stable uids for Cucumber steps and outline rows --- .../src/helpers/cucumberScenarioBuilder.ts | 12 +++- packages/service/src/reporter.ts | 45 ++++++++++-- packages/service/tests/reporter.test.ts | 72 +++++++++++++++++++ 3 files changed, 120 insertions(+), 9 deletions(-) diff --git a/packages/nightwatch-devtools/src/helpers/cucumberScenarioBuilder.ts b/packages/nightwatch-devtools/src/helpers/cucumberScenarioBuilder.ts index 9ea426e9..54ab1da3 100644 --- a/packages/nightwatch-devtools/src/helpers/cucumberScenarioBuilder.ts +++ b/packages/nightwatch-devtools/src/helpers/cucumberScenarioBuilder.ts @@ -57,7 +57,9 @@ function buildScenarioStepTest( ? `${featureAbsPath}:${stepLines[i]}` : undefined return { - uid: deterministicUid(featureUri, `step:${scenarioName}:${step.text}`), + // Scope by the scenario uid (which carries the scenario line) so identical + // step text in sibling scenarios and outline example rows stays distinct. + uid: deterministicUid(featureUri, `step:${scenarioUid}:${step.text}`), cid: DEFAULTS.CID, title: stepLabel, fullTitle: `${scenarioName} ${stepLabel}`, @@ -87,8 +89,12 @@ export function buildCucumberScenarioSuite( parentFeatureSuiteUid } = input // deterministicUid (no counter) so the SAME scenario gets the SAME uid - // across retries — that's what makes retry-coalescing work upstream. - const scenarioUid = deterministicUid(featureUri, `scenario:${scenarioName}`) + // across retries — that's what makes retry-coalescing work upstream. The + // scenario line disambiguates outline example rows that share a name. + const scenarioUid = deterministicUid( + featureUri, + `scenario:${scenarioName}:${scenarioLine}` + ) const scenarioSuite: SuiteStats = { uid: scenarioUid, cid: DEFAULTS.CID, diff --git a/packages/service/src/reporter.ts b/packages/service/src/reporter.ts index ad792131..801a86ad 100644 --- a/packages/service/src/reporter.ts +++ b/packages/service/src/reporter.ts @@ -26,8 +26,13 @@ function isScenario(item: SuiteStats | TestStats): boolean { // Generate stable UID for a WDIO suite/test stats object. Handles WDIO's // Cucumber-specific shapes (scenarios with featureFile/featureLine, or with // numeric uid + example-row fallback), then delegates the Mocha/Jasmine path -// to core's generateStableUid. -function generateStableUid(item: SuiteStats | TestStats): string { +// to core's generateStableUid. `parentScope` (the owning scenario's stable +// uid) disambiguates Cucumber steps so identical step text in sibling +// scenarios yields distinct, rerun-stable uids. +function generateStableUid( + item: SuiteStats | TestStats, + parentScope?: string +): string { // For Cucumber scenarios, prefer the feature file URI:line as the stable // discriminator. The Cucumber pickle carries the actual line of the example // row, which is stable across reruns regardless of how many examples run. @@ -60,11 +65,25 @@ function generateStableUid(item: SuiteStats | TestStats): string { ) } + // Cucumber step: scope by the owning scenario's stable uid via + // deterministicUid (no run-order counter), so two scenarios sharing step + // text — and scenario-outline example rows — get distinct, rerun-stable uids. + const stepFile = 'file' in item ? (item.file ?? '') : '' + if (parentScope) { + return deterministicUid( + stepFile, + parentScope, + String(item.fullTitle || item.title) + ) + } + // For Mocha/Jasmine tests and suites, use only stable identifiers // that don't change between full and partial runs // DO NOT use cid or parent as they can vary based on run context - const file = 'file' in item ? (item.file ?? '') : '' - return generateStableUidByFileName(file, String(item.fullTitle || item.title)) + return generateStableUidByFileName( + stepFile, + String(item.fullTitle || item.title) + ) } /** @@ -154,6 +173,9 @@ export class TestReporter extends WebdriverIOReporter { #loadSource: (location: string) => void #currentSpecFile?: string #suitePath: string[] = [] + /** Stable uid of the Cucumber scenario currently open, used to scope its + * step uids. Undefined outside a scenario (Mocha/Jasmine). */ + #currentScenarioUid?: string constructor( options: Reporters.Options, @@ -205,6 +227,11 @@ export class TestReporter extends WebdriverIOReporter { // Generate stable UID for consistent identification across reruns suiteStats.uid = generateStableUid(suiteStats) + // Track the open Cucumber scenario so its steps scope their uids to it. + if (isScenario(suiteStats)) { + this.#currentScenarioUid = suiteStats.uid + } + this.#currentSpecFile = suiteStats.file setCurrentSpecFile(suiteStats.file) @@ -252,8 +279,10 @@ export class TestReporter extends WebdriverIOReporter { this.#loadSource(testStats.file) } - // Generate stable UID after enriching metadata for consistent test identification - testStats.uid = generateStableUid(testStats) + // Generate stable UID after enriching metadata for consistent test + // identification. Cucumber steps are scoped by their scenario's uid so + // identical step text across scenarios stays distinct. + testStats.uid = generateStableUid(testStats, this.#currentScenarioUid) this.#sendUpstream() } @@ -290,6 +319,10 @@ export class TestReporter extends WebdriverIOReporter { onSuiteEnd(suiteStats: SuiteStats): void { super.onSuiteEnd(suiteStats) + // Stop scoping steps once the owning scenario closes. + if (isScenario(suiteStats) && suiteStats.uid === this.#currentScenarioUid) { + this.#currentScenarioUid = undefined + } // Pop the suite we pushed on start if ( suiteStats.title && diff --git a/packages/service/tests/reporter.test.ts b/packages/service/tests/reporter.test.ts index 03461046..bdaccdca 100644 --- a/packages/service/tests/reporter.test.ts +++ b/packages/service/tests/reporter.test.ts @@ -324,4 +324,76 @@ describe('TestReporter - Rerun & Stable UID', () => { expect(() => reporter.report).not.toThrow() }) }) + + describe('Cucumber step uid scoping (no cross-scenario collision)', () => { + const FEATURE = '/proj/features/login.feature' + const STEP = + 'I should see a flash message saying You logged into a secure area!' + + const scenario = (title: string, line: number): SuiteStats => + ({ + uid: `raw-${title}`, + title, + fullTitle: `Login ${title}`, + file: FEATURE, + type: 'scenario', + argument: { uri: FEATURE, line } + }) as unknown as SuiteStats + + const step = (line: number): TestStats => + ({ + uid: 'raw-step', + title: STEP, + fullTitle: STEP, + file: FEATURE, + type: 'test', + argument: { uri: FEATURE, line } + }) as unknown as TestStats + + // Drive a scenario's step through the reporter and return the assigned uid. + const runStep = ( + r: TestReporter, + scen: SuiteStats, + stepStats: TestStats + ): string => { + r.onSuiteStart(scen) + r.onTestStart(stepStats) + r.onTestEnd(stepStats) + r.onSuiteEnd(scen) + return stepStats.uid + } + + it('gives identical step text in sibling scenarios distinct uids', () => { + const scenA = scenario('Scenario A', 5) + const scenB = scenario('Scenario B', 20) + const uidA = runStep(reporter, scenA, step(8)) + const uidB = runStep(reporter, scenB, step(23)) + expect(uidA).not.toBe(uidB) + }) + + it('keeps a step uid stable when its scenario is rerun alone', () => { + // Full run: scenario A then B. + const uidBFull = (() => { + runStep(reporter, scenario('Scenario A', 5), step(8)) + return runStep(reporter, scenario('Scenario B', 20), step(23)) + })() + + // Rerun scenario B on its own (fresh reporter resets the counter). A + // run-order-counter uid would shift to A's slot here; scoping by the + // scenario keeps it stable. + const reporter2 = new TestReporter( + { logFile: '/tmp/test.log' }, + sendUpstream as any + ) + const uidBAlone = runStep(reporter2, scenario('Scenario B', 20), step(23)) + + expect(uidBAlone).toBe(uidBFull) + }) + + it('distinguishes scenario-outline example rows (same title, different line)', () => { + const row1 = runStep(reporter, scenario('greet ', 10), step(11)) + const row2 = runStep(reporter, scenario('greet ', 14), step(15)) + expect(row1).not.toBe(row2) + }) + }) }) From 3da30fa76a22f963b2767d650957c118d936b6d8 Mon Sep 17 00:00:00 2001 From: Vishnu Vardhan Date: Wed, 8 Jul 2026 10:58:24 +0530 Subject: [PATCH 16/68] fix(core,service): collapse same-timestamp snapshots so blank frames don't clobber results --- packages/core/src/trace-snapshots.ts | 34 ++++- packages/service/src/snapshot-dedupe.ts | 20 +++ .../service/tests/dedupe-snapshots.test.ts | 124 +++++++++++++++++- 3 files changed, 174 insertions(+), 4 deletions(-) diff --git a/packages/core/src/trace-snapshots.ts b/packages/core/src/trace-snapshots.ts index 9e98704d..ab7ed3f5 100644 --- a/packages/core/src/trace-snapshots.ts +++ b/packages/core/src/trace-snapshots.ts @@ -16,10 +16,39 @@ export interface ScreencastFrameEvent { timestamp: number } +// Two snapshots at the same timestamp (a real post-action capture and a blank +// end-of-scenario one) map to the same `${pageId}-${ts}` resource name, so a +// last-wins write lets a blank frame clobber the real one — and the real +// screenshot and real elements can land on different captures. Collapse to one +// per timestamp: largest screenshot, richest metadata, merged across duplicates. +function collapseByTimestamp(snapshots: ActionSnapshot[]): ActionSnapshot[] { + const byTs = new Map() + const order: number[] = [] + for (const snap of snapshots) { + const existing = byTs.get(snap.timestamp) + if (!existing) { + byTs.set(snap.timestamp, { ...snap }) + order.push(snap.timestamp) + continue + } + if ((snap.screenshot?.length ?? 0) > (existing.screenshot?.length ?? 0)) { + existing.screenshot = snap.screenshot + } + if (!existing.elements?.length && snap.elements?.length) { + existing.elements = snap.elements + } + if (!existing.snapshotText && snap.snapshotText) { + existing.snapshotText = snap.snapshotText + } + } + return order.map((ts) => byTs.get(ts)!) +} + export function buildSnapshotResources( - snapshots: ActionSnapshot[], + rawSnapshots: ActionSnapshot[], pageId: string ): TraceZipResource[] { + const snapshots = collapseByTimestamp(rawSnapshots) const out: TraceZipResource[] = [] for (const snap of snapshots) { const base = `${pageId}-${snap.timestamp}` @@ -71,11 +100,12 @@ function frameForSnapshot( /** Filmstrip events; the first snapshot is re-anchored to t=0 for pre-interaction state. */ export function buildFilmstripEvents( - snapshots: ActionSnapshot[], + rawSnapshots: ActionSnapshot[], pageId: string, wallTime: number, viewport: { width: number; height: number } ): ScreencastFrameEvent[] { + const snapshots = collapseByTimestamp(rawSnapshots) const firstSnap = snapshots.find((s) => s.screenshot) const events: ScreencastFrameEvent[] = [] if (firstSnap) { diff --git a/packages/service/src/snapshot-dedupe.ts b/packages/service/src/snapshot-dedupe.ts index d7e14a00..5e51cccd 100644 --- a/packages/service/src/snapshot-dedupe.ts +++ b/packages/service/src/snapshot-dedupe.ts @@ -19,3 +19,23 @@ export function dedupeSnapshotsByTimestamp( } return [...best.values()].sort((a, b) => a.timestamp - b.timestamp) } + +/** Insert a snapshot, or — when one already shares its timestamp — keep only + * the richer screenshot, replacing in place to preserve any index ranges into + * the list. Applies the dedupe heuristic at capture time so a blank + * end-of-scenario frame can't clobber the last action's real result on export + * paths that don't run dedupeSnapshotsByTimestamp (e.g. per-spec traces). */ +export function upsertRichestSnapshot( + snapshots: ActionSnapshot[], + snap: ActionSnapshot +): void { + const idx = snapshots.findIndex((s) => s.timestamp === snap.timestamp) + if (idx === -1) { + snapshots.push(snap) + return + } + const existing = snapshots[idx]! + if ((snap.screenshot?.length ?? 0) > (existing.screenshot?.length ?? 0)) { + snapshots[idx] = snap + } +} diff --git a/packages/service/tests/dedupe-snapshots.test.ts b/packages/service/tests/dedupe-snapshots.test.ts index c33b9b38..d1ac50ee 100644 --- a/packages/service/tests/dedupe-snapshots.test.ts +++ b/packages/service/tests/dedupe-snapshots.test.ts @@ -1,6 +1,13 @@ +import fs from 'node:fs/promises' +import os from 'node:os' +import path from 'node:path' import { describe, it, expect } from 'vitest' -import type { ActionSnapshot } from '@wdio/devtools-shared' -import { dedupeSnapshotsByTimestamp } from '../src/snapshot-dedupe.js' +import { writeTraceZip, type TraceCapturer } from '@wdio/devtools-core' +import { TraceType, type ActionSnapshot } from '@wdio/devtools-shared' +import { + dedupeSnapshotsByTimestamp, + upsertRichestSnapshot +} from '../src/snapshot-dedupe.js' function snap(timestamp: number, screenshot: string): ActionSnapshot { return { timestamp, command: 'click', screenshot } @@ -34,3 +41,116 @@ describe('dedupeSnapshotsByTimestamp', () => { expect(dedupeSnapshotsByTimestamp([])).toEqual([]) }) }) + +describe('upsertRichestSnapshot', () => { + const blank = 'AA' + const content = 'A'.repeat(100) + + it("does not let a blank __final__ clobber the last action's real frame", () => { + // The last action's post-capture (real) and the end-of-scenario __final__ + // (blank) share the last action's timestamp; the real frame must survive. + const list: ActionSnapshot[] = [snap(50, content), snap(100, content)] + const final: ActionSnapshot = { + timestamp: 100, + command: '__final__', + screenshot: blank + } + upsertRichestSnapshot(list, final) + expect(list).toHaveLength(2) + expect(list[1].screenshot).toBe(content) + expect(list[1].command).toBe('click') + }) + + it('replaces in place when the final capture is richer', () => { + // Mirror the opposite case: the action was screenshotted mid-navigation + // (blank) and the settled __final__ is the real frame. + const list: ActionSnapshot[] = [snap(50, content), snap(100, blank)] + const final: ActionSnapshot = { + timestamp: 100, + command: '__final__', + screenshot: content + } + upsertRichestSnapshot(list, final) + expect(list).toHaveLength(2) + expect(list[1].screenshot).toBe(content) + expect(list[1].command).toBe('__final__') + }) + + it('preserves array length/indices on a timestamp collision', () => { + // Spec-range slicing indexes into this array, so a collision must never + // change its length — only replace in place or skip. + const list: ActionSnapshot[] = [snap(100, content)] + upsertRichestSnapshot(list, snap(100, blank)) + expect(list).toHaveLength(1) + }) + + it('appends when the timestamp is new', () => { + const list: ActionSnapshot[] = [snap(100, content)] + upsertRichestSnapshot(list, snap(200, blank)) + expect(list.map((s) => s.timestamp)).toEqual([100, 200]) + }) +}) + +describe('final-frame regression (capture → export)', () => { + it("exports the last action's real result, not the blank __final__ frame", async () => { + // Base64 payloads chosen so byte-length ranks blank < real < result and + // each round-trips cleanly through the resource writer's base64 decode. + const blank = 'AA' + const real = 'R'.repeat(200) + const result = 'B'.repeat(400) + + // The service builds pre/post captures per action; the last action's + // post-capture (result) collides on timestamp with the trailing __final__. + const snapshots: ActionSnapshot[] = [ + { timestamp: 1200, command: 'url', screenshot: real }, + { timestamp: 1200, command: 'click', screenshot: real }, + { timestamp: 1400, command: 'click', screenshot: result } + ] + upsertRichestSnapshot(snapshots, { + timestamp: 1400, + command: '__final__', + screenshot: blank + }) + const prepared = dedupeSnapshotsByTimestamp(snapshots) + + const capturer: TraceCapturer = { + mutations: [], + traceLogs: [], + consoleLogs: [], + networkRequests: [], + commandsLog: [ + { command: 'url', args: [], timestamp: 1200, startTime: 1150 }, + { command: 'click', args: [], timestamp: 1400, startTime: 1350 } + ], + sources: new Map(), + metadata: { + type: TraceType.Testrunner, + viewport: { + width: 800, + height: 600, + offsetLeft: 0, + offsetTop: 0, + scale: 1 + } + }, + startWallTime: 1000 + } + + const outputDir = await fs.mkdtemp(path.join(os.tmpdir(), 'final-frame-')) + const dir = await writeTraceZip(capturer, { + outputDir, + sessionId: 'sess1234', + format: 'ndjson-directory', + actionSnapshots: prepared + }) + + const frame = await fs.readFile( + path.join(dir, 'resources', 'page@sess1234-1400.jpeg') + ) + // The last action's frame is the real result, never the blank capture. + expect(frame.toString('base64')).toBe(result) + expect(frame.length).not.toBe(Buffer.from(blank, 'base64').length) + + await fs.rm(outputDir, { recursive: true, force: true }) + }) +}) From 2a26451a0bad0394ea66db1e9ccd1b12b5511748 Mon Sep 17 00:00:00 2001 From: Vishnu Vardhan Date: Wed, 8 Jul 2026 11:00:47 +0530 Subject: [PATCH 17/68] chore: Added more actions to the action mapping to appear in the timeline --- packages/app/tests/errors-collect.test.ts | 337 ++++++++++++++++++++++ packages/shared/src/trace-actions.ts | 37 ++- 2 files changed, 373 insertions(+), 1 deletion(-) create mode 100644 packages/app/tests/errors-collect.test.ts diff --git a/packages/app/tests/errors-collect.test.ts b/packages/app/tests/errors-collect.test.ts new file mode 100644 index 00000000..c92e3680 --- /dev/null +++ b/packages/app/tests/errors-collect.test.ts @@ -0,0 +1,337 @@ +import { describe, it, expect } from 'vitest' +import type { CommandLog } from '@wdio/devtools-shared' + +import { collectErrors } from '../src/components/workbench/errors/collect.js' +import type { + SuiteStatsFragment, + TestStatsFragment +} from '../src/controller/types.js' + +function command(overrides: Partial): CommandLog { + return { + command: 'click', + args: [], + timestamp: 0, + ...overrides + } +} + +function suiteMap( + ...suites: SuiteStatsFragment[] +): Record[] { + return [Object.fromEntries(suites.map((s) => [s.uid, s]))] +} + +function failedTest(overrides: Partial): TestStatsFragment { + return { + uid: 't1', + state: 'failed', + ...overrides + } +} + +describe('collectErrors', () => { + it('returns an empty list when nothing failed', () => { + expect(collectErrors([], [])).toEqual([]) + expect(collectErrors(undefined, undefined)).toEqual([]) + expect( + collectErrors( + [command({ command: 'click' })], + suiteMap({ + uid: 's1', + state: 'passed', + tests: [failedTest({ state: 'passed' })] + }) + ) + ).toEqual([]) + }) + + it('collects failed commands with title, message, stack and source', () => { + const errors = collectErrors( + [ + command({ + command: 'expect', + title: 'expect(el).toHaveText', + callSource: 'file:///spec.ts:12:3', + error: { + name: 'Error', + message: 'expected foo, got bar', + stack: 'at spec.ts:12' + }, + timestamp: 100 + }) + ], + [] + ) + expect(errors).toHaveLength(1) + expect(errors[0]).toMatchObject({ + title: 'expect(el).toHaveText', + message: 'expected foo, got bar', + stack: 'at spec.ts:12', + callSource: 'file:///spec.ts:12:3', + timestamp: 100 + }) + expect(errors[0].command?.command).toBe('expect') + }) + + it('falls back to the command name when there is no title', () => { + const [error] = collectErrors( + [ + command({ + command: 'navigateTo', + error: { name: 'Error', message: 'boom' } + }) + ], + [] + ) + expect(error.title).toBe('navigateTo') + }) + + it('orders command errors by timestamp', () => { + const errors = collectErrors( + [ + command({ + command: 'second', + error: { name: 'Error', message: 'b' }, + timestamp: 200 + }), + command({ + command: 'first', + error: { name: 'Error', message: 'a' }, + timestamp: 100 + }) + ], + [] + ) + expect(errors.map((e) => e.message)).toEqual(['a', 'b']) + }) + + it('collects failed tests from nested suites', () => { + const errors = collectErrors( + [], + suiteMap({ + uid: 'feature', + state: 'failed', + suites: [ + { + uid: 'scenario', + state: 'failed', + tests: [ + failedTest({ + uid: 'step-1', + title: 'Then it should pass', + fullTitle: 'Scenario > Then it should pass', + callSource: 'steps.ts:8:1', + error: { name: 'AssertionError', message: 'nope' } + }) + ] + } + ] + }) + ) + expect(errors).toHaveLength(1) + expect(errors[0]).toMatchObject({ + title: 'Scenario > Then it should pass', + message: 'nope', + callSource: 'steps.ts:8:1' + }) + expect(errors[0].command).toBeUndefined() + }) + + it('reads the first entry of errors[] when error is absent', () => { + const [error] = collectErrors( + [], + suiteMap({ + uid: 's', + state: 'failed', + tests: [ + failedTest({ + uid: 'a', + errors: [{ name: 'Error', message: 'from errors array' } as Error] + }) + ] + }) + ) + expect(error.message).toBe('from errors array') + }) + + it('ignores failed tests that carry no error payload', () => { + const errors = collectErrors( + [], + suiteMap({ + uid: 's', + state: 'failed', + tests: [failedTest({ uid: 'a' })] + }) + ) + expect(errors).toEqual([]) + }) + + it('dedupes a test failure that only echoes a command failure', () => { + const shared = 'expect(locator).toHaveText failed' + const errors = collectErrors( + [ + command({ + command: 'expect', + error: { name: 'Error', message: shared }, + timestamp: 5 + }) + ], + suiteMap({ + uid: 's', + state: 'failed', + tests: [ + failedTest({ + uid: 'a', + title: 'the scenario', + error: { name: 'Error', message: shared } + }) + ] + }) + ) + expect(errors).toHaveLength(1) + expect(errors[0].command?.command).toBe('expect') + }) + + it('keeps a distinct test failure alongside command failures', () => { + const errors = collectErrors( + [ + command({ + command: 'click', + error: { name: 'Error', message: 'click failed' }, + timestamp: 1 + }) + ], + suiteMap({ + uid: 's', + state: 'failed', + tests: [ + failedTest({ + uid: 'a', + title: 'setup', + error: { name: 'Error', message: 'hook failed' } + }) + ] + }) + ) + expect(errors.map((e) => e.message)).toEqual([ + 'click failed', + 'hook failed' + ]) + }) + + it('dedupes failed tests by uid across repeated suite maps', () => { + const test = failedTest({ + uid: 'dup', + title: 'flaky', + error: { name: 'Error', message: 'x' } + }) + const errors = collectErrors( + [], + [ + { s: { uid: 's', state: 'failed', tests: [test] } }, + { s: { uid: 's', state: 'failed', tests: [test] } } + ] + ) + expect(errors).toHaveLength(1) + }) + + it('strips ANSI, splits the stack, and pulls Expected/Received from a cucumber message', () => { + const raw = + 'Expect $(`#flash`) to have text\n\n' + + 'Expected: StringContaining "secure area"\n' + + 'Received: "invalid"\n' + + ' at World. (/specs/steps.ts:31:20)\n' + + ' at process.processTicksAndRejections (node:internal:104:5)' + const [error] = collectErrors( + [ + command({ + command: 'expect.assertion', + error: { name: 'Error', message: raw } + }) + ], + [] + ) + expect(error.message).toBe('Expect $(`#flash`) to have text') + expect(error.expected).toBe('StringContaining "secure area"') + expect(error.actual).toBe('"invalid"') + expect(error.stack).toContain('at World.') + expect(error.message).not.toContain('') + expect(error.message).not.toContain('at World') + }) + + it('extracts indented Expected/Received and dedents the value', () => { + const raw = + 'Expect $(`#flash`) to have text\n\n' + + ' Expected: StringContaining "secure area"\n' + + ' Received: "invalid!\n×"\n' + + ' at World. (/specs/steps.ts:31:20)' + const [error] = collectErrors( + [command({ command: 'getText', error: { name: 'Error', message: raw } })], + [] + ) + expect(error.message).toBe('Expect $(`#flash`) to have text') + expect(error.expected).toBe('StringContaining "secure area"') + expect(error.actual).toBe('"invalid!\n×"') + expect(error.stack).toContain('at World.') + }) + + it('extracts expected/received from an assertion command', () => { + const [error] = collectErrors( + [ + command({ + command: 'expect.toHaveText', + args: ['Your username is invalid!', 'You logged into a secure area!'], + error: { name: 'Error', message: 'text mismatch' } + }) + ], + [] + ) + expect(error.actual).toBe('Your username is invalid!') + expect(error.expected).toBe('You logged into a secure area!') + }) + + it('extracts expected/received from a failed-test matcher error', () => { + const [error] = collectErrors( + [], + suiteMap({ + uid: 's', + state: 'failed', + tests: [ + failedTest({ + uid: 'a', + title: 'the scenario', + error: { + name: 'Error', + message: 'mismatch', + expected: 42, + actual: 7 + } as unknown as Error + }) + ] + }) + ) + expect(error.expected).toBe('42') + expect(error.actual).toBe('7') + }) + + it('places command errors before test errors', () => { + const errors = collectErrors( + [ + command({ + command: 'click', + error: { name: 'Error', message: 'cmd' }, + timestamp: 999 + }) + ], + suiteMap({ + uid: 's', + state: 'failed', + tests: [ + failedTest({ uid: 'a', error: { name: 'Error', message: 'test' } }) + ] + }) + ) + expect(errors.map((e) => e.message)).toEqual(['cmd', 'test']) + }) +}) diff --git a/packages/shared/src/trace-actions.ts b/packages/shared/src/trace-actions.ts index bae73200..c3bd7462 100644 --- a/packages/shared/src/trace-actions.ts +++ b/packages/shared/src/trace-actions.ts @@ -33,6 +33,8 @@ export const TRACKED_ASSERT_METHODS = [ // assert. (node:assert), verify. (nightwatch soft variants), and // expect. (synthesized failing-matcher entries) all render as Assert. +// Only FAILING expect-webdriverio matchers reach the command log today (via the +// reporter); recording passing matchers needs a per-adapter capture change. const ASSERT_COMMAND_RE = /^(?:assert|verify|expect)\.(\w+)$/ export function mapAssertCommand(command: string): TraceAction | null { @@ -68,5 +70,38 @@ export const ACTION_MAP: Record = { execute: { class: 'Page', method: 'evaluate' }, executeAsync: { class: 'Page', method: 'evaluate' }, switchToFrame: { class: 'Frame', method: 'goto' }, - touchAction: { class: 'Element', method: 'tap' } + touchAction: { class: 'Element', method: 'tap' }, + // WDIO element reads — surfaced so query steps appear in the timeline the way + // locator queries do in standard trace viewers. Adapters already capture + // these; only the export allow-list kept them out. + getText: { class: 'Element', method: 'getText' }, + getValue: { class: 'Element', method: 'getValue' }, + getAttribute: { class: 'Element', method: 'getAttribute' }, + getProperty: { class: 'Element', method: 'getProperty' }, + getCSSProperty: { class: 'Element', method: 'getCSSProperty' }, + getTagName: { class: 'Element', method: 'getTagName' }, + getLocation: { class: 'Element', method: 'getLocation' }, + getSize: { class: 'Element', method: 'getSize' }, + isDisplayed: { class: 'Element', method: 'isDisplayed' }, + isExisting: { class: 'Element', method: 'isExisting' }, + isEnabled: { class: 'Element', method: 'isEnabled' }, + isSelected: { class: 'Element', method: 'isSelected' }, + isClickable: { class: 'Element', method: 'isClickable' }, + isFocused: { class: 'Element', method: 'isFocused' }, + // Explicit user-facing waits (not the internal polling loops behind them). + waitForDisplayed: { class: 'Element', method: 'waitForDisplayed' }, + waitForExist: { class: 'Element', method: 'waitForExist' }, + waitForEnabled: { class: 'Element', method: 'waitForEnabled' }, + waitForClickable: { class: 'Element', method: 'waitForClickable' }, + waitUntil: { class: 'Browser', method: 'waitForFunction' }, + // WDIO page/browser reads + getTitle: { class: 'Page', method: 'getTitle' }, + getUrl: { class: 'Page', method: 'getUrl' }, + getPageSource: { class: 'Page', method: 'getPageSource' }, + // Selenium read aliases — normalized onto the WDIO names above so both runners + // read identically. getText/getAttribute/getTagName/isDisplayed/isEnabled/ + // isSelected share the command name across runners and need no alias. + getCssValue: { class: 'Element', method: 'getCSSProperty' }, + getRect: { class: 'Element', method: 'getRect' }, + getCurrentUrl: { class: 'Page', method: 'getUrl' } } From 9fe36d03581a193db8fe5125fee346cad254c85d Mon Sep 17 00:00:00 2001 From: Vishnu Vardhan Date: Wed, 8 Jul 2026 11:56:54 +0530 Subject: [PATCH 18/68] chore(examples): split wdio example into cucumber/ and mocha/ with shared page objects --- .../wdio/cucumber/features/login-fail.feature | 9 +++ .../{ => cucumber}/features/login.feature | 2 +- .../features/step-definitions/steps.ts | 4 +- examples/wdio/{ => cucumber}/wdio.conf.ts | 0 .../wdio/{ => cucumber}/wdio.mobile.conf.ts | 0 examples/wdio/cucumber/wdio.retention.conf.ts | 68 +++++++++++++++++++ .../wdio/{ => cucumber}/wdio.trace.conf.ts | 0 examples/wdio/mocha/specs/login-fail.e2e.ts | 17 +++++ examples/wdio/mocha/specs/login.e2e.ts | 26 +++++++ examples/wdio/mocha/wdio.conf.ts | 53 +++++++++++++++ examples/wdio/package.json | 9 ++- .../{features => }/pageobjects/login.page.ts | 0 .../wdio/{features => }/pageobjects/page.ts | 0 .../{features => }/pageobjects/secure.page.ts | 0 examples/wdio/tsconfig.json | 8 ++- 15 files changed, 187 insertions(+), 9 deletions(-) create mode 100644 examples/wdio/cucumber/features/login-fail.feature rename examples/wdio/{ => cucumber}/features/login.feature (84%) rename examples/wdio/{ => cucumber}/features/step-definitions/steps.ts (89%) rename examples/wdio/{ => cucumber}/wdio.conf.ts (100%) rename examples/wdio/{ => cucumber}/wdio.mobile.conf.ts (100%) create mode 100644 examples/wdio/cucumber/wdio.retention.conf.ts rename examples/wdio/{ => cucumber}/wdio.trace.conf.ts (100%) create mode 100644 examples/wdio/mocha/specs/login-fail.e2e.ts create mode 100644 examples/wdio/mocha/specs/login.e2e.ts create mode 100644 examples/wdio/mocha/wdio.conf.ts rename examples/wdio/{features => }/pageobjects/login.page.ts (100%) rename examples/wdio/{features => }/pageobjects/page.ts (100%) rename examples/wdio/{features => }/pageobjects/secure.page.ts (100%) diff --git a/examples/wdio/cucumber/features/login-fail.feature b/examples/wdio/cucumber/features/login-fail.feature new file mode 100644 index 00000000..c8d325b8 --- /dev/null +++ b/examples/wdio/cucumber/features/login-fail.feature @@ -0,0 +1,9 @@ +Feature: Retention check — deliberately failing login + + # Logs in with bad credentials but asserts the SUCCESS message, so the Then + # step fails. Used to verify retain-on-failure keeps this spec's trace while + # dropping the passing login.feature. Reuses the existing step definitions. + Scenario: Failing assertion exercises retain-on-failure + Given I am on the login page + When I login with foobar and barfoo + Then I should see a flash message saying You logged into a secure area! diff --git a/examples/wdio/features/login.feature b/examples/wdio/cucumber/features/login.feature similarity index 84% rename from examples/wdio/features/login.feature rename to examples/wdio/cucumber/features/login.feature index e6dd0f87..8b9bcf9a 100644 --- a/examples/wdio/features/login.feature +++ b/examples/wdio/cucumber/features/login.feature @@ -8,5 +8,5 @@ Feature: The Internet Guinea Pig Website Examples: | username | password | message | - | tomsmith | SuperSecretPassword! | You logged into a secure area! | + | tomsmith | SuperSecretPassword1! | You logged into a secure area! | | foobar | barfoo | Your username is invalid! | diff --git a/examples/wdio/features/step-definitions/steps.ts b/examples/wdio/cucumber/features/step-definitions/steps.ts similarity index 89% rename from examples/wdio/features/step-definitions/steps.ts rename to examples/wdio/cucumber/features/step-definitions/steps.ts index 32522982..3367b631 100644 --- a/examples/wdio/features/step-definitions/steps.ts +++ b/examples/wdio/cucumber/features/step-definitions/steps.ts @@ -1,8 +1,8 @@ import { Given, When, Then, After } from '@wdio/cucumber-framework' import { browser, expect } from '@wdio/globals' -import LoginPage from '../pageobjects/login.page.js' -import SecurePage from '../pageobjects/secure.page.js' +import LoginPage from '../../../pageobjects/login.page.js' +import SecurePage from '../../../pageobjects/secure.page.js' const pages = { login: LoginPage diff --git a/examples/wdio/wdio.conf.ts b/examples/wdio/cucumber/wdio.conf.ts similarity index 100% rename from examples/wdio/wdio.conf.ts rename to examples/wdio/cucumber/wdio.conf.ts diff --git a/examples/wdio/wdio.mobile.conf.ts b/examples/wdio/cucumber/wdio.mobile.conf.ts similarity index 100% rename from examples/wdio/wdio.mobile.conf.ts rename to examples/wdio/cucumber/wdio.mobile.conf.ts diff --git a/examples/wdio/cucumber/wdio.retention.conf.ts b/examples/wdio/cucumber/wdio.retention.conf.ts new file mode 100644 index 00000000..a71b97bb --- /dev/null +++ b/examples/wdio/cucumber/wdio.retention.conf.ts @@ -0,0 +1,68 @@ +import path from 'node:path' + +// Disposable harness for verifying tracePolicy end-to-end. Runs one passing +// spec (login.feature) and one failing spec (login-fail.feature) at spec +// granularity so retain-on-failure can be seen dropping the passing spec's +// trace while keeping the failing one. Change `tracePolicy` below to try each. + +const __dirname = path.resolve(path.dirname(new URL(import.meta.url).pathname)) + +export const config: WebdriverIO.Config = { + runner: 'local', + tsConfigPath: './tsconfig.json', + + specs: ['./features/login.feature', './features/login-fail.feature'], + + maxInstances: 1, + + capabilities: [ + { + browserName: 'chrome', + browserVersion: '149.0.7827.201', // specify chromium browser version for testing + 'goog:chromeOptions': { + args: [ + '--headless', + '--disable-gpu', + '--remote-allow-origins=*', + '--window-size=1600,1200' + ] + } + } + ], + + logLevel: 'warn', + baseUrl: 'http://localhost', + waitforTimeout: 10000, + connectionRetryTimeout: 120000, + connectionRetryCount: 3, + + services: [ + [ + 'devtools', + { + mode: 'trace' as const, + traceGranularity: 'spec' as const + // tracePolicy: 'retain-on-failure' as const + } + ] + ], + + framework: 'cucumber', + reporters: ['spec'], + + cucumberOpts: { + require: [ + path.resolve(__dirname, 'features', 'step-definitions', 'steps.ts') + ], + backtrace: false, + requireModule: [], + dryRun: false, + failFast: false, + snippets: true, + source: true, + strict: false, + tagExpression: '', + timeout: 60000, + ignoreUndefinedDefinitions: false + } +} diff --git a/examples/wdio/wdio.trace.conf.ts b/examples/wdio/cucumber/wdio.trace.conf.ts similarity index 100% rename from examples/wdio/wdio.trace.conf.ts rename to examples/wdio/cucumber/wdio.trace.conf.ts diff --git a/examples/wdio/mocha/specs/login-fail.e2e.ts b/examples/wdio/mocha/specs/login-fail.e2e.ts new file mode 100644 index 00000000..b939a28b --- /dev/null +++ b/examples/wdio/mocha/specs/login-fail.e2e.ts @@ -0,0 +1,17 @@ +import { expect } from '@wdio/globals' + +import LoginPage from '../../pageobjects/login.page.js' +import SecurePage from '../../pageobjects/secure.page.js' + +// Deliberately failing — mirrors login-fail.feature, so the Errors tab and +// tracePolicy: 'retain-on-failure' have something to exercise. +describe('Login (failing)', () => { + it('asserts the wrong flash message so the run fails', async () => { + console.log('[TEST] submitting invalid credentials') + await LoginPage.open() + await LoginPage.login('foobar', 'barfoo') + await expect(SecurePage.flashAlert).toHaveText( + expect.stringContaining('You logged into a secure area!') + ) + }) +}) diff --git a/examples/wdio/mocha/specs/login.e2e.ts b/examples/wdio/mocha/specs/login.e2e.ts new file mode 100644 index 00000000..0282a744 --- /dev/null +++ b/examples/wdio/mocha/specs/login.e2e.ts @@ -0,0 +1,26 @@ +import { expect } from '@wdio/globals' + +import LoginPage from '../../pageobjects/login.page.js' +import SecurePage from '../../pageobjects/secure.page.js' + +describe('Login', () => { + it('logs into the secure area with valid credentials', async () => { + console.log('[TEST] logging in with valid credentials') + await LoginPage.open() + await LoginPage.login('tomsmith', 'SuperSecretPassword!') + await expect(SecurePage.flashAlert).toBeExisting() + await expect(SecurePage.flashAlert).toHaveText( + expect.stringContaining('You logged into a secure area!') + ) + console.log('[TEST] secure area reached') + }) + + it('shows an error message for an invalid username', async () => { + console.log('[TEST] logging in with an invalid username') + await LoginPage.open() + await LoginPage.login('foobar', 'barfoo') + await expect(SecurePage.flashAlert).toHaveText( + expect.stringContaining('Your username is invalid!') + ) + }) +}) diff --git a/examples/wdio/mocha/wdio.conf.ts b/examples/wdio/mocha/wdio.conf.ts new file mode 100644 index 00000000..474602e3 --- /dev/null +++ b/examples/wdio/mocha/wdio.conf.ts @@ -0,0 +1,53 @@ +import type { Options } from '@wdio/types' + +// Mocha counterpart to wdio.conf.ts (which runs the Cucumber example). Same +// capabilities and devtools service; only the framework + spec layout differ. +export const config: Options.Testrunner = { + runner: 'local', + autoCompileOpts: { + autoCompile: true, + tsNodeOpts: { + project: './tsconfig.json', + transpileOnly: true + } + }, + specs: ['./specs/**/*.e2e.ts'], + exclude: [], + maxInstances: 10, + capabilities: [ + { + browserName: 'chrome', + 'goog:chromeOptions': { + args: [ + '--headless', + '--disable-gpu', + '--remote-allow-origins=*', + '--window-size=1600,900' + ] + } + } + ], + logLevel: 'debug', + bail: 0, + baseUrl: 'http://localhost', + waitforTimeout: 10000, + connectionRetryTimeout: 120000, + connectionRetryCount: 3, + services: [ + [ + 'devtools', + { + mode: 'trace' as const, + traceGranularity: 'spec' as const + // tracePolicy: 'retain-on-failure' as const + // screencast: { enabled: true, pollIntervalMs: 200 } + } + ] + ], + framework: 'mocha', + reporters: ['spec'], + mochaOpts: { + ui: 'bdd', + timeout: 60000 + } +} diff --git a/examples/wdio/package.json b/examples/wdio/package.json index c783a577..4bbb0957 100644 --- a/examples/wdio/package.json +++ b/examples/wdio/package.json @@ -6,6 +6,7 @@ "devDependencies": { "@wdio/cli": "9.28.0", "@wdio/cucumber-framework": "9.28.0", + "@wdio/mocha-framework": "9.28.0", "@wdio/devtools-service": "workspace:*", "@wdio/globals": "9.28.0", "@wdio/local-runner": "9.28.0", @@ -18,8 +19,10 @@ "typescript": "^6.0.3" }, "scripts": { - "wdio": "wdio run ./wdio.conf.ts", - "mobile": "wdio run ./wdio.mobile.conf.ts", - "trace": "wdio run ./wdio.trace.conf.ts" + "cucumber": "wdio run ./cucumber/wdio.conf.ts", + "mocha": "wdio run ./mocha/wdio.conf.ts", + "mobile": "wdio run ./cucumber/wdio.mobile.conf.ts", + "trace": "wdio run ./cucumber/wdio.trace.conf.ts", + "retention": "wdio run ./cucumber/wdio.retention.conf.ts" } } diff --git a/examples/wdio/features/pageobjects/login.page.ts b/examples/wdio/pageobjects/login.page.ts similarity index 100% rename from examples/wdio/features/pageobjects/login.page.ts rename to examples/wdio/pageobjects/login.page.ts diff --git a/examples/wdio/features/pageobjects/page.ts b/examples/wdio/pageobjects/page.ts similarity index 100% rename from examples/wdio/features/pageobjects/page.ts rename to examples/wdio/pageobjects/page.ts diff --git a/examples/wdio/features/pageobjects/secure.page.ts b/examples/wdio/pageobjects/secure.page.ts similarity index 100% rename from examples/wdio/features/pageobjects/secure.page.ts rename to examples/wdio/pageobjects/secure.page.ts diff --git a/examples/wdio/tsconfig.json b/examples/wdio/tsconfig.json index 8acd81f7..8b8a6786 100644 --- a/examples/wdio/tsconfig.json +++ b/examples/wdio/tsconfig.json @@ -11,7 +11,8 @@ "node", "@wdio/globals/types", "expect-webdriverio", - "@wdio/cucumber-framework" + "@wdio/cucumber-framework", + "@wdio/mocha-framework" ], "skipLibCheck": true, "noEmit": true, @@ -24,7 +25,8 @@ "noFallthroughCasesInSwitch": true }, "include": [ - "test", - "wdio.conf.ts" + "cucumber", + "mocha", + "pageobjects" ] } From 62d3a32a3948aa3709d2eb74e1334605cdcc02ee Mon Sep 17 00:00:00 2001 From: Vishnu Vardhan Date: Wed, 8 Jul 2026 12:09:20 +0530 Subject: [PATCH 19/68] chore: Update path in readme and update pnpm-lock.yaml --- ARCHITECTURE.md | 2 +- README.md | 2 +- package.json | 3 +- pnpm-lock.yaml | 183 +++++++++++++++--------------------------------- 4 files changed, 60 insertions(+), 130 deletions(-) diff --git a/ARCHITECTURE.md b/ARCHITECTURE.md index 6d4fecc0..1324f454 100644 --- a/ARCHITECTURE.md +++ b/ARCHITECTURE.md @@ -144,7 +144,7 @@ The execution environment is the browser, not Node, so this package cannot impor Per-framework demo projects used for manual verification. -- `examples/wdio/` — WebdriverIO with Mocha (default). Run via `pnpm demo:wdio`. +- `examples/wdio/` — WebdriverIO, split into `cucumber/` and `mocha/` (shared page objects in `pageobjects/`). Run via `pnpm demo:wdio` (Cucumber) or `pnpm demo:wdio:mocha`. - `examples/nightwatch/` — Nightwatch (both vanilla and Cucumber). Run via `pnpm demo:nightwatch`. - `examples/selenium/` — Selenium with subdirs for `mocha-test/`, `jest-test/`, `cucumber-test/`, `jasmine-test/`, `vitest-test/`. `pnpm demo:selenium` runs mocha; `pnpm --filter @wdio/selenium-devtools example:` runs the others. diff --git a/README.md b/README.md index 96e7adf8..fa87cc71 100644 --- a/README.md +++ b/README.md @@ -129,7 +129,7 @@ services: [[DevToolsHookService, { Adapters detect mobile sessions via `platformName: 'android' | 'ios'` (case-insensitive) and adjust the per-action snapshot to extract elements from the mobile XML tree instead of the DOM. The trace's `context-options` records `title: 'android' — ` / `'ios' — ` so the viewer labels frames correctly. -A reference WDIO config is at [examples/wdio/wdio.mobile.conf.ts](examples/wdio/wdio.mobile.conf.ts). Prereqs to run it end-to-end with a local emulator: +A reference WDIO config is at [examples/wdio/cucumber/wdio.mobile.conf.ts](examples/wdio/cucumber/wdio.mobile.conf.ts). Prereqs to run it end-to-end with a local emulator: 1. **Java JDK** — `brew install --cask temurin` 2. **Android SDK** — `brew install --cask android-commandlinetools` then `yes | sdkmanager --licenses && sdkmanager "platform-tools" "emulator" "system-images;android-34;google_apis_playstore;arm64-v8a"`. The brew cask installs sdkmanager under `/opt/homebrew/share/android-commandlinetools/`, and sdkmanager downloads other SDK pieces alongside it — set `ANDROID_HOME` to that path (not `~/Library/Android/sdk/`). diff --git a/package.json b/package.json index e4037c25..65bf3171 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,8 @@ "type": "module", "scripts": { "build": "pnpm -r build", - "demo:wdio": "wdio run ./examples/wdio/wdio.conf.ts", + "demo:wdio": "wdio run ./examples/wdio/cucumber/wdio.conf.ts", + "demo:wdio:mocha": "wdio run ./examples/wdio/mocha/wdio.conf.ts", "demo:nightwatch": "pnpm --filter @wdio/nightwatch-devtools example", "demo:selenium": "pnpm --filter @wdio/selenium-devtools example", "dev": "pnpm --parallel dev", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index b338e0b8..a2354ac3 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -141,6 +141,9 @@ importers: '@wdio/local-runner': specifier: 9.28.0 version: 9.28.0(@wdio/globals@9.28.0)(webdriverio@9.28.0(puppeteer-core@21.11.0)) + '@wdio/mocha-framework': + specifier: 9.28.0 + version: 9.28.0 '@wdio/spec-reporter': specifier: 9.28.0 version: 9.28.0 @@ -365,7 +368,7 @@ importers: version: 6.0.3 vitest: specifier: ^4.0.16 - version: 4.1.9(@types/node@25.9.3)(@vitest/coverage-v8@4.1.9)(happy-dom@20.10.4)(jsdom@24.1.3)(vite@8.0.16(@types/node@25.9.3)(esbuild@0.27.7)(jiti@2.7.0)(tsx@4.22.4)(yaml@2.9.0)) + version: 4.1.9(@types/node@25.9.3)(@vitest/coverage-v8@4.1.9)(happy-dom@20.10.4)(jsdom@24.1.3)(vite@8.0.16(@types/node@25.9.3)(esbuild@0.28.0)(jiti@2.7.0)(tsx@4.22.4)(yaml@2.9.0)) packages/nightwatch-devtools: dependencies: @@ -1859,49 +1862,42 @@ packages: engines: {node: '>= 10'} cpu: [arm64] os: [linux] - libc: [glibc] '@napi-rs/nice-linux-arm64-musl@1.1.1': resolution: {integrity: sha512-+2Rzdb3nTIYZ0YJF43qf2twhqOCkiSrHx2Pg6DJaCPYhhaxbLcdlV8hCRMHghQ+EtZQWGNcS2xF4KxBhSGeutg==} engines: {node: '>= 10'} cpu: [arm64] os: [linux] - libc: [musl] '@napi-rs/nice-linux-ppc64-gnu@1.1.1': resolution: {integrity: sha512-4FS8oc0GeHpwvv4tKciKkw3Y4jKsL7FRhaOeiPei0X9T4Jd619wHNe4xCLmN2EMgZoeGg+Q7GY7BsvwKpL22Tg==} engines: {node: '>= 10'} cpu: [ppc64] os: [linux] - libc: [glibc] '@napi-rs/nice-linux-riscv64-gnu@1.1.1': resolution: {integrity: sha512-HU0nw9uD4FO/oGCCk409tCi5IzIZpH2agE6nN4fqpwVlCn5BOq0MS1dXGjXaG17JaAvrlpV5ZeyZwSon10XOXw==} engines: {node: '>= 10'} cpu: [riscv64] os: [linux] - libc: [glibc] '@napi-rs/nice-linux-s390x-gnu@1.1.1': resolution: {integrity: sha512-2YqKJWWl24EwrX0DzCQgPLKQBxYDdBxOHot1KWEq7aY2uYeX+Uvtv4I8xFVVygJDgf6/92h9N3Y43WPx8+PAgQ==} engines: {node: '>= 10'} cpu: [s390x] os: [linux] - libc: [glibc] '@napi-rs/nice-linux-x64-gnu@1.1.1': resolution: {integrity: sha512-/gaNz3R92t+dcrfCw/96pDopcmec7oCcAQ3l/M+Zxr82KT4DljD37CpgrnXV+pJC263JkW572pdbP3hP+KjcIg==} engines: {node: '>= 10'} cpu: [x64] os: [linux] - libc: [glibc] '@napi-rs/nice-linux-x64-musl@1.1.1': resolution: {integrity: sha512-xScCGnyj/oppsNPMnevsBe3pvNaoK7FGvMjT35riz9YdhB2WtTG47ZlbxtOLpjeO9SqqQ2J2igCmz6IJOD5JYw==} engines: {node: '>= 10'} cpu: [x64] os: [linux] - libc: [musl] '@napi-rs/nice-openharmony-arm64@1.1.1': resolution: {integrity: sha512-6uJPRVwVCLDeoOaNyeiW0gp2kFIM4r7PL2MczdZQHkFi9gVlgm+Vn+V6nTWRcu856mJ2WjYJiumEajfSm7arPQ==} @@ -2027,42 +2023,36 @@ packages: engines: {node: ^20.19.0 || >=22.12.0} cpu: [arm64] os: [linux] - libc: [glibc] '@rolldown/binding-linux-arm64-musl@1.0.3': resolution: {integrity: sha512-VWkUHwWriDciit80wleYwKILoR/KMvxh/IdwS/paX+ZgpuRpCrKLUdadJbc0NpBEiyhpYawsJ73j9aCvOH+f7Q==} engines: {node: ^20.19.0 || >=22.12.0} cpu: [arm64] os: [linux] - libc: [musl] '@rolldown/binding-linux-ppc64-gnu@1.0.3': resolution: {integrity: sha512-5f1laC0SlIR0yDbFCd8acUhvJIag6N3zC5P7oUPN6wX0aOma+uKJ0wBDH5aq7I1PVI2ttTlhJwzwRIBnLiSGEg==} engines: {node: ^20.19.0 || >=22.12.0} cpu: [ppc64] os: [linux] - libc: [glibc] '@rolldown/binding-linux-s390x-gnu@1.0.3': resolution: {integrity: sha512-Iq4ko0r4XsgbrF/LunNgHtAGLRRVE2kXonAXQ/MV0mC6jQpMOhW1SvtZja2EhC/kd05++bP78dsqBeIQyYJ6Yg==} engines: {node: ^20.19.0 || >=22.12.0} cpu: [s390x] os: [linux] - libc: [glibc] '@rolldown/binding-linux-x64-gnu@1.0.3': resolution: {integrity: sha512-B8m6tD5+/N5FeNQFbKlLA/2yVq9ycQP1SeedyEYYKWBNR3ZQbkvIUcNnDNM03lO1l5F2roiiFJGgvoLLyZXtSg==} engines: {node: ^20.19.0 || >=22.12.0} cpu: [x64] os: [linux] - libc: [glibc] '@rolldown/binding-linux-x64-musl@1.0.3': resolution: {integrity: sha512-pSdpdUJHkuCxun9LE7jvgUB9qsRgaiyNNCX7m/AvHTcq67AiT/Yhoxvw5zPfhrM8k/BfP8ce/hMOpthKDpEUow==} engines: {node: ^20.19.0 || >=22.12.0} cpu: [x64] os: [linux] - libc: [musl] '@rolldown/binding-openharmony-arm64@1.0.3': resolution: {integrity: sha512-OXXS3RKJgX2uLwM+gYyuH5omcH8fL1LJs96pZGgtetVCahON57+d4SJHzTgZiOjxgGkSnpXpOsWuPDGAKAigEg==} @@ -2133,79 +2123,66 @@ packages: resolution: {integrity: sha512-T1dMEQhXA/jkJ/jyMIw9IovK8bSUq7A8kLIlvZTb/6YIVsp2zLavr4F3oyllHWo7eIVJRyE5n3tUjQJEbE1IuQ==} cpu: [arm] os: [linux] - libc: [glibc] '@rollup/rollup-linux-arm-musleabihf@4.62.0': resolution: {integrity: sha512-2as0LgT7qQpyceQq6VUJYnumUMUrgGQCWIiDIN9DE0/tglsk6o66uCB4f3djRawAltvfCNLyZZrsqbPA6inCsA==} cpu: [arm] os: [linux] - libc: [musl] '@rollup/rollup-linux-arm64-gnu@4.62.0': resolution: {integrity: sha512-bVURMg+6eNN9C/yc0aVjooZcwTTtYF4YW3xta5pP0//r3o1V8gXEHXWCndj47w/HhwsFroZrFhR+6uQP5T0n0g==} cpu: [arm64] os: [linux] - libc: [glibc] '@rollup/rollup-linux-arm64-musl@4.62.0': resolution: {integrity: sha512-Ful8pM/2yYI83PViWdFdpZhdI8HJ5qsXANe5atypbHDf+KIBBDsZsbyy8hbXnULVvW9NsTh5DHwbcBftyLTfiw==} cpu: [arm64] os: [linux] - libc: [musl] '@rollup/rollup-linux-loong64-gnu@4.62.0': resolution: {integrity: sha512-9Gp/DgrkzfUBmNPVTyPTvay+4xEP7M/clXpj3efXBcm6uTIVIgDg4rqUpqKXvLEuFRVuEpSAOkhgNeecvaZ4Cg==} cpu: [loong64] os: [linux] - libc: [glibc] '@rollup/rollup-linux-loong64-musl@4.62.0': resolution: {integrity: sha512-m9tsJz54LUXkSYM8+8PG81B9IKK5r+2T0clMq4QrS16xFosufU7firBDAZEsDheDs7wTlP7h3++S7lMsU955HA==} cpu: [loong64] os: [linux] - libc: [musl] '@rollup/rollup-linux-ppc64-gnu@4.62.0': resolution: {integrity: sha512-3UvJ5PNVU16aJf6M3tFI24pWzAl2/ynfbyRN3ICyQajK1lSkrnVYNnLz3v04J32qKa0FczJc22zeToc0lr2A3w==} cpu: [ppc64] os: [linux] - libc: [glibc] '@rollup/rollup-linux-ppc64-musl@4.62.0': resolution: {integrity: sha512-vRWUAbYLGHBZS6Q8Msb2sfnf1fvJf+47t8l/TwOerM2qArzy+IeNMTHrYLHXh95h8MoatPHI5hhSZNs+mGXKPg==} cpu: [ppc64] os: [linux] - libc: [musl] '@rollup/rollup-linux-riscv64-gnu@4.62.0': resolution: {integrity: sha512-c00T5SYENHAt86cfW47URaP3Us5vLC/4QO7GYud1G5VNRffCwwCuBspwqYrriuJB+5m0WFzClCn9wed0FBjKvg==} cpu: [riscv64] os: [linux] - libc: [glibc] '@rollup/rollup-linux-riscv64-musl@4.62.0': resolution: {integrity: sha512-krrCDilhXOwFkSkO3Wm9I/f9H0L92XHHwy2fwxjukxIbh0dem8gZqOW5Y8BsHrpJv5qwlRBV+Wl4ZFyRWhUpwg==} cpu: [riscv64] os: [linux] - libc: [musl] '@rollup/rollup-linux-s390x-gnu@4.62.0': resolution: {integrity: sha512-7pfYFSTc4/rUC/FtAI0Qp6QthDBCIi6/AuP1xYqFk5vanI6KnL5dWKP60OM/05LOsbwTmIcvr6eXC4CJuJ75IA==} cpu: [s390x] os: [linux] - libc: [glibc] '@rollup/rollup-linux-x64-gnu@4.62.0': resolution: {integrity: sha512-7SDIalKeIpG0Ifogbbdn58HmSotYMlf23K3dCJEmiVd9Fg36Vmni82iPQec27N3wY4Bvbxftkxz6vSx9OcouTg==} cpu: [x64] os: [linux] - libc: [glibc] '@rollup/rollup-linux-x64-musl@4.62.0': resolution: {integrity: sha512-eRZevouTH2i1HeAVLqJuLnt256krQkGY0TN6WsTmsIhuzbh457HuWDMakKwmi0Cjadux983CoSr8Lim2QhUIFw==} cpu: [x64] os: [linux] - libc: [musl] '@rollup/rollup-openbsd-x64@4.62.0': resolution: {integrity: sha512-3oVS7FLGa4U1qcvao9ylGxrjXZyUQqR8UwxEcnUEyPX53O/C/mKDZegNXTdHCP+h3e6ta/f1EN38Yif1mmZHYg==} @@ -2327,28 +2304,24 @@ packages: engines: {node: '>= 20'} cpu: [arm64] os: [linux] - libc: [glibc] '@tailwindcss/oxide-linux-arm64-musl@4.3.1': resolution: {integrity: sha512-Bwv9KwOvE0VKa86xPFif9b9c3Y1NxOV1P0gLti/IYaWEsQYZXDlxfGEtA8mdDZ7SG3wyNXAWYT5SIn3giL57oA==} engines: {node: '>= 20'} cpu: [arm64] os: [linux] - libc: [musl] '@tailwindcss/oxide-linux-x64-gnu@4.3.1': resolution: {integrity: sha512-Ymi8O8T15HYQdOUWUtTI6ldN0neHP85FC+Qz32xTcZ7iJXtem/x8ITev0o1e9e5rkqj4lONZfTRLvkmin1+tKg==} engines: {node: '>= 20'} cpu: [x64] os: [linux] - libc: [glibc] '@tailwindcss/oxide-linux-x64-musl@4.3.1': resolution: {integrity: sha512-M+P/91qJ6uILLw4k2G93GMDRAXj61SMvFQYt39AqvUqYgExXpLL5aepfns7sj4HiAQeolirQF9E0lzRvdf4zPQ==} engines: {node: '>= 20'} cpu: [x64] os: [linux] - libc: [musl] '@tailwindcss/oxide-wasm32-wasi@4.3.1': resolution: {integrity: sha512-zsM8uOeqvVGHsAXsJxsT28ttosFahLJKCLOTUBqRAtKnVgGSRitds9T432QiT8b77Yga7JIBkulIRRlJPtYhRA==} @@ -2454,6 +2427,9 @@ packages: '@types/json5@0.0.29': resolution: {integrity: sha512-dRLjCWHYg4oaA77cxO64oO+7JwCwnIzkZPdrrC71jQmQtlhM556pwKo5bUzqvZndkVbeFLIIi+9TC40JNF5hNQ==} + '@types/mocha@10.0.10': + resolution: {integrity: sha512-xPyYSz1cMPnJQhl0CLMH68j3gprKZaTjG3s5Vi+fDgx+uhG9NOXwbVt52eFS8ECyXhyKcjDLCBEqBExKuiZb7Q==} + '@types/node@25.9.3': resolution: {integrity: sha512-603BddQMv3pUcr4U2dhujk83N2tTDVr/34wII2B6bJy6g+8WD6yUb11jszNs0gdi4PesVWl7ABt8nYMVpnLUcg==} @@ -2606,61 +2582,51 @@ packages: resolution: {integrity: sha512-zJc0H99FEPoFfSrNpa91HYfxzfAJCr502oxNK1cfdC9hlaFI43RT+JFCann9JUgZmLzzntChHyn13Sgn9ljHNg==} cpu: [arm64] os: [linux] - libc: [glibc] '@unrs/resolver-binding-linux-arm64-musl@1.12.2': resolution: {integrity: sha512-KQ3Lki6l+Pz1k/eBipN41ES+YUK30beLGb9YqcB1O542cyLCNE6GaxrfcY3T6EezmGGk84wb5XyO9loTM9tkcA==} cpu: [arm64] os: [linux] - libc: [musl] '@unrs/resolver-binding-linux-loong64-gnu@1.12.2': resolution: {integrity: sha512-3SJGEh1DborhG6pyxvhPzCT4bbSIVihsvgJc13P1bHG7KLdNDaF9T3gsTwFc7Jw/5Y5/iWOjkEx7Zy0NvCGX3Q==} cpu: [loong64] os: [linux] - libc: [glibc] '@unrs/resolver-binding-linux-loong64-musl@1.12.2': resolution: {integrity: sha512-jiuG/Obbel7uw1PwHNFfrkiKhLAF6mnyZ6aWlOAVN9WqKm8v0OFGnciJIHu8+CMvXLQ8AD51LPzAoUfT21D5Ew==} cpu: [loong64] os: [linux] - libc: [musl] '@unrs/resolver-binding-linux-ppc64-gnu@1.12.2': resolution: {integrity: sha512-q7xRvVpmcfeL+LlZg8Pbbo6QaTZwDU5BaGZbwfhkEsXJn3Was8xYfE0RBH266xZt0rM6B7i8xAYIvjthuUIWHg==} cpu: [ppc64] os: [linux] - libc: [glibc] '@unrs/resolver-binding-linux-riscv64-gnu@1.12.2': resolution: {integrity: sha512-0CVdx6lcnT3Q9inOH8tsMIOJ6ImndllMjqJHg8RLVdB7Vq4SfkEXl9mCSsVNuNA4MCYycRicCUxPCabVHJRr6A==} cpu: [riscv64] os: [linux] - libc: [glibc] '@unrs/resolver-binding-linux-riscv64-musl@1.12.2': resolution: {integrity: sha512-iOwlRo9vnp6R6ohHQS11n0NnfdXx/omhkocmIfaPRpQhKZ+3BDMkkdRVh53qjkFkpPddf+FETA28NwGN7l5l+w==} cpu: [riscv64] os: [linux] - libc: [musl] '@unrs/resolver-binding-linux-s390x-gnu@1.12.2': resolution: {integrity: sha512-HYJtLfXq94q8iZNFT1lknx258wlkkWhZeUXJRqzKBBUJ00CvZ+N33zgbCqimLjsyw5Va6uUxhVa12mI+kaveEw==} cpu: [s390x] os: [linux] - libc: [glibc] '@unrs/resolver-binding-linux-x64-gnu@1.12.2': resolution: {integrity: sha512-mPsUhunKKDih5O96Y6enDQyHc1SqBPlY1E/SfMWDM3EdJ95Z9CArPeCVwCCqbP45ljvivdEk8Fxn+SIb1rDAJQ==} cpu: [x64] os: [linux] - libc: [glibc] '@unrs/resolver-binding-linux-x64-musl@1.12.2': resolution: {integrity: sha512-azrt6+5ydLd8Vt210AAFis/lZevSfPw93EJRIJG+xPu4WCJ8K0kppCTpMyLPcKT7H15M4Jnt2tMp5bOvCkRC6A==} cpu: [x64] os: [linux] - libc: [musl] '@unrs/resolver-binding-openharmony-arm64@1.12.2': resolution: {integrity: sha512-YZ9hP4O0X9PQb8eO980qmLNGH4zT3I9+SZTdt0Pr0YyuGQhYKoOZkV02VzrzyOZJ5xIJ3UFIenKkUkGg8GjgWQ==} @@ -2785,6 +2751,10 @@ packages: resolution: {integrity: sha512-HdzDrRs+ywAqbXGKqe1i/bLtCv47plz4TvsHFH3j729OooT5VH38ctFn5aLXgECmiAKDkmH/A6kOq2Zh5DIxww==} engines: {node: '>=18.20.0'} + '@wdio/mocha-framework@9.28.0': + resolution: {integrity: sha512-UdkgigdyxJu0Rpx2ZRSNuB8u8B5nmLBeRjn5JHYTXBp8Ubi+wgFR1+EdLg8k2z+CtuYe3bNHBw7vPpBYuQMLZw==} + engines: {node: '>=18.20.0'} + '@wdio/protocols@8.40.3': resolution: {integrity: sha512-wK7+eyrB3TAei8RwbdkcyoNk2dPu+mduMBOdPJjp8jf/mavd15nIUXLID1zA+w5m1Qt1DsT1NbvaeO9+aJQ33A==} @@ -5262,28 +5232,24 @@ packages: engines: {node: '>= 12.0.0'} cpu: [arm64] os: [linux] - libc: [glibc] lightningcss-linux-arm64-musl@1.32.0: resolution: {integrity: sha512-UpQkoenr4UJEzgVIYpI80lDFvRmPVg6oqboNHfoH4CQIfNA+HOrZ7Mo7KZP02dC6LjghPQJeBsvXhJod/wnIBg==} engines: {node: '>= 12.0.0'} cpu: [arm64] os: [linux] - libc: [musl] lightningcss-linux-x64-gnu@1.32.0: resolution: {integrity: sha512-V7Qr52IhZmdKPVr+Vtw8o+WLsQJYCTd8loIfpDaMRWGUZfBOYEJeyJIkqGIDMZPwPx24pUMfwSxxI8phr/MbOA==} engines: {node: '>= 12.0.0'} cpu: [x64] os: [linux] - libc: [glibc] lightningcss-linux-x64-musl@1.32.0: resolution: {integrity: sha512-bYcLp+Vb0awsiXg/80uCRezCYHNg1/l3mt0gzHnWV9XP1W5sKa5/TCdGWaR/zBM2PeF/HbsQv/j2URNOiVuxWg==} engines: {node: '>= 12.0.0'} cpu: [x64] os: [linux] - libc: [musl] lightningcss-win32-arm64-msvc@1.32.0: resolution: {integrity: sha512-8SbC8BR40pS6baCM8sbtYDSwEVQd4JlFTOlaD3gWGHfThTcABnNDBda6eTZeqbofalIJhFx0qKzgHJmcPTnGdw==} @@ -7644,7 +7610,7 @@ snapshots: '@babel/types': 7.29.7 '@jridgewell/remapping': 2.3.5 convert-source-map: 2.0.0 - debug: 4.4.3(supports-color@10.2.2) + debug: 4.4.3(supports-color@5.5.0) gensync: 1.0.0-beta.2 json5: 2.2.3 semver: 6.3.1 @@ -7803,7 +7769,7 @@ snapshots: '@babel/parser': 7.29.7 '@babel/template': 7.29.7 '@babel/types': 7.29.7 - debug: 4.4.3(supports-color@10.2.2) + debug: 4.4.3(supports-color@5.5.0) transitivePeerDependencies: - supports-color @@ -8484,7 +8450,7 @@ snapshots: '@eslint/config-array@0.23.5': dependencies: '@eslint/object-schema': 3.0.5 - debug: 4.4.3(supports-color@10.2.2) + debug: 4.4.3(supports-color@5.5.0) minimatch: 10.2.5 transitivePeerDependencies: - supports-color @@ -9168,7 +9134,7 @@ snapshots: '@puppeteer/browsers@2.13.2': dependencies: - debug: 4.4.3(supports-color@10.2.2) + debug: 4.4.3(supports-color@5.5.0) extract-zip: 2.0.1 progress: 2.0.3 proxy-agent: 6.5.0 @@ -9514,6 +9480,8 @@ snapshots: '@types/json5@0.0.29': {} + '@types/mocha@10.0.10': {} + '@types/node@25.9.3': dependencies: undici-types: 7.24.6 @@ -9584,7 +9552,7 @@ snapshots: '@typescript-eslint/types': 8.61.1 '@typescript-eslint/typescript-estree': 8.61.1(typescript@6.0.3) '@typescript-eslint/visitor-keys': 8.61.1 - debug: 4.4.3(supports-color@10.2.2) + debug: 4.4.3(supports-color@5.5.0) eslint: 10.5.0(jiti@2.7.0) typescript: 6.0.3 transitivePeerDependencies: @@ -9594,7 +9562,7 @@ snapshots: dependencies: '@typescript-eslint/tsconfig-utils': 8.61.1(typescript@6.0.3) '@typescript-eslint/types': 8.61.1 - debug: 4.4.3(supports-color@10.2.2) + debug: 4.4.3(supports-color@5.5.0) typescript: 6.0.3 transitivePeerDependencies: - supports-color @@ -9613,7 +9581,7 @@ snapshots: '@typescript-eslint/types': 8.61.1 '@typescript-eslint/typescript-estree': 8.61.1(typescript@6.0.3) '@typescript-eslint/utils': 8.61.1(eslint@10.5.0(jiti@2.7.0))(typescript@6.0.3) - debug: 4.4.3(supports-color@10.2.2) + debug: 4.4.3(supports-color@5.5.0) eslint: 10.5.0(jiti@2.7.0) ts-api-utils: 2.5.0(typescript@6.0.3) typescript: 6.0.3 @@ -9628,7 +9596,7 @@ snapshots: '@typescript-eslint/tsconfig-utils': 8.61.1(typescript@6.0.3) '@typescript-eslint/types': 8.61.1 '@typescript-eslint/visitor-keys': 8.61.1 - debug: 4.4.3(supports-color@10.2.2) + debug: 4.4.3(supports-color@5.5.0) minimatch: 10.2.5 semver: 7.8.4 tinyglobby: 0.2.17 @@ -9767,14 +9735,6 @@ snapshots: chai: 6.2.2 tinyrainbow: 3.1.0 - '@vitest/mocker@4.1.9(vite@8.0.16(@types/node@25.9.3)(esbuild@0.27.7)(jiti@2.7.0)(tsx@4.22.4)(yaml@2.9.0))': - dependencies: - '@vitest/spy': 4.1.9 - estree-walker: 3.0.3 - magic-string: 0.30.21 - optionalDependencies: - vite: 8.0.16(@types/node@25.9.3)(esbuild@0.27.7)(jiti@2.7.0)(tsx@4.22.4)(yaml@2.9.0) - '@vitest/mocker@4.1.9(vite@8.0.16(@types/node@25.9.3)(esbuild@0.28.0)(jiti@2.7.0)(tsx@4.22.4)(yaml@2.9.0))': dependencies: '@vitest/spy': 4.1.9 @@ -9958,6 +9918,20 @@ snapshots: safe-regex2: 5.1.1 strip-ansi: 7.2.0 + '@wdio/mocha-framework@9.28.0': + dependencies: + '@types/mocha': 10.0.10 + '@types/node': 25.9.3 + '@wdio/logger': 9.18.0 + '@wdio/types': 9.28.0 + '@wdio/utils': 9.28.0 + mocha: 10.8.2 + transitivePeerDependencies: + - bare-abort-controller + - bare-buffer + - react-native-b4a + - supports-color + '@wdio/protocols@8.40.3': {} '@wdio/protocols@9.28.0': {} @@ -10084,7 +10058,7 @@ snapshots: agent-base@6.0.2: dependencies: - debug: 4.4.3(supports-color@10.2.2) + debug: 4.4.3(supports-color@5.5.0) transitivePeerDependencies: - supports-color @@ -11515,7 +11489,7 @@ snapshots: '@types/estree': 1.0.9 ajv: 6.15.0 cross-spawn: 7.0.6 - debug: 4.4.3(supports-color@10.2.2) + debug: 4.4.3(supports-color@5.5.0) escape-string-regexp: 4.0.0 eslint-scope: 9.1.2 eslint-visitor-keys: 5.0.1 @@ -11632,7 +11606,7 @@ snapshots: extract-zip@2.0.1: dependencies: - debug: 4.4.3(supports-color@10.2.2) + debug: 4.4.3(supports-color@5.5.0) get-stream: 5.2.0 yauzl: 2.10.0 optionalDependencies: @@ -11963,7 +11937,7 @@ snapshots: dependencies: basic-ftp: 5.3.1 data-uri-to-buffer: 6.0.2 - debug: 4.4.3(supports-color@10.2.2) + debug: 4.4.3(supports-color@5.5.0) transitivePeerDependencies: - supports-color @@ -11971,7 +11945,7 @@ snapshots: dependencies: basic-ftp: 5.3.1 data-uri-to-buffer: 8.0.0 - debug: 4.4.3(supports-color@10.2.2) + debug: 4.4.3(supports-color@5.5.0) transitivePeerDependencies: - supports-color @@ -12167,35 +12141,35 @@ snapshots: http-proxy-agent@7.0.2: dependencies: agent-base: 7.1.4 - debug: 4.4.3(supports-color@10.2.2) + debug: 4.4.3(supports-color@5.5.0) transitivePeerDependencies: - supports-color http-proxy-agent@9.0.0: dependencies: agent-base: 9.0.0 - debug: 4.4.3(supports-color@10.2.2) + debug: 4.4.3(supports-color@5.5.0) transitivePeerDependencies: - supports-color https-proxy-agent@5.0.1: dependencies: agent-base: 6.0.2 - debug: 4.4.3(supports-color@10.2.2) + debug: 4.4.3(supports-color@5.5.0) transitivePeerDependencies: - supports-color https-proxy-agent@7.0.6: dependencies: agent-base: 7.1.4 - debug: 4.4.3(supports-color@10.2.2) + debug: 4.4.3(supports-color@5.5.0) transitivePeerDependencies: - supports-color https-proxy-agent@9.0.0: dependencies: agent-base: 9.0.0 - debug: 4.4.3(supports-color@10.2.2) + debug: 4.4.3(supports-color@5.5.0) transitivePeerDependencies: - supports-color @@ -12492,7 +12466,7 @@ snapshots: istanbul-lib-source-maps@5.0.6: dependencies: '@jridgewell/trace-mapping': 0.3.31 - debug: 4.4.3(supports-color@10.2.2) + debug: 4.4.3(supports-color@5.5.0) istanbul-lib-coverage: 3.2.2 transitivePeerDependencies: - supports-color @@ -13578,7 +13552,7 @@ snapshots: dependencies: '@tootallnate/quickjs-emscripten': 0.23.0 agent-base: 7.1.4 - debug: 4.4.3(supports-color@10.2.2) + debug: 4.4.3(supports-color@5.5.0) get-uri: 6.0.5 http-proxy-agent: 7.0.2 https-proxy-agent: 7.0.6 @@ -13590,7 +13564,7 @@ snapshots: pac-proxy-agent@9.0.1: dependencies: agent-base: 9.0.0 - debug: 4.4.3(supports-color@10.2.2) + debug: 4.4.3(supports-color@5.5.0) get-uri: 8.0.0 http-proxy-agent: 9.0.0 https-proxy-agent: 9.0.0 @@ -13880,7 +13854,7 @@ snapshots: proxy-agent@6.5.0: dependencies: agent-base: 7.1.4 - debug: 4.4.3(supports-color@10.2.2) + debug: 4.4.3(supports-color@5.5.0) http-proxy-agent: 7.0.2 https-proxy-agent: 7.0.6 lru-cache: 7.18.3 @@ -13893,7 +13867,7 @@ snapshots: proxy-agent@8.0.1: dependencies: agent-base: 9.0.0 - debug: 4.4.3(supports-color@10.2.2) + debug: 4.4.3(supports-color@5.5.0) http-proxy-agent: 9.0.0 https-proxy-agent: 9.0.0 lru-cache: 7.18.3 @@ -14413,7 +14387,7 @@ snapshots: socks-proxy-agent@10.0.0: dependencies: agent-base: 9.0.0 - debug: 4.4.3(supports-color@10.2.2) + debug: 4.4.3(supports-color@5.5.0) socks: 2.8.9 transitivePeerDependencies: - supports-color @@ -14421,7 +14395,7 @@ snapshots: socks-proxy-agent@8.0.5: dependencies: agent-base: 7.1.4 - debug: 4.4.3(supports-color@10.2.2) + debug: 4.4.3(supports-color@5.5.0) socks: 2.8.9 transitivePeerDependencies: - supports-color @@ -14630,7 +14604,7 @@ snapshots: cosmiconfig: 9.0.2(typescript@6.0.3) css-functions-list: 3.3.3 css-tree: 3.2.1 - debug: 4.4.3(supports-color@10.2.2) + debug: 4.4.3(supports-color@5.5.0) fast-glob: 3.3.3 fastest-levenshtein: 1.0.16 file-entry-cache: 11.1.3 @@ -14893,7 +14867,7 @@ snapshots: cac: 6.7.14 chokidar: 4.0.3 consola: 3.4.2 - debug: 4.4.3(supports-color@10.2.2) + debug: 4.4.3(supports-color@5.5.0) esbuild: 0.27.7 fix-dts-default-cjs-exports: 1.0.1 joycon: 3.1.1 @@ -15026,7 +15000,7 @@ snapshots: '@rollup/pluginutils': 5.4.0(rollup@4.62.0) '@volar/typescript': 2.4.28 compare-versions: 6.1.1 - debug: 4.4.3(supports-color@10.2.2) + debug: 4.4.3(supports-color@5.5.0) kolorist: 1.8.0 local-pkg: 1.2.1 magic-string: 0.30.21 @@ -15169,21 +15143,6 @@ snapshots: optionalDependencies: rollup: 4.62.0 - vite@8.0.16(@types/node@25.9.3)(esbuild@0.27.7)(jiti@2.7.0)(tsx@4.22.4)(yaml@2.9.0): - dependencies: - lightningcss: 1.32.0 - picomatch: 4.0.4 - postcss: 8.5.15 - rolldown: 1.0.3 - tinyglobby: 0.2.17 - optionalDependencies: - '@types/node': 25.9.3 - esbuild: 0.27.7 - fsevents: 2.3.3 - jiti: 2.7.0 - tsx: 4.22.4 - yaml: 2.9.0 - vite@8.0.16(@types/node@25.9.3)(esbuild@0.28.0)(jiti@2.7.0)(tsx@4.22.4)(yaml@2.9.0): dependencies: lightningcss: 1.32.0 @@ -15199,36 +15158,6 @@ snapshots: tsx: 4.22.4 yaml: 2.9.0 - vitest@4.1.9(@types/node@25.9.3)(@vitest/coverage-v8@4.1.9)(happy-dom@20.10.4)(jsdom@24.1.3)(vite@8.0.16(@types/node@25.9.3)(esbuild@0.27.7)(jiti@2.7.0)(tsx@4.22.4)(yaml@2.9.0)): - dependencies: - '@vitest/expect': 4.1.9 - '@vitest/mocker': 4.1.9(vite@8.0.16(@types/node@25.9.3)(esbuild@0.27.7)(jiti@2.7.0)(tsx@4.22.4)(yaml@2.9.0)) - '@vitest/pretty-format': 4.1.9 - '@vitest/runner': 4.1.9 - '@vitest/snapshot': 4.1.9 - '@vitest/spy': 4.1.9 - '@vitest/utils': 4.1.9 - es-module-lexer: 2.1.0 - expect-type: 1.3.0 - magic-string: 0.30.21 - obug: 2.1.3 - pathe: 2.0.3 - picomatch: 4.0.4 - std-env: 4.1.0 - tinybench: 2.9.0 - tinyexec: 1.2.4 - tinyglobby: 0.2.17 - tinyrainbow: 3.1.0 - vite: 8.0.16(@types/node@25.9.3)(esbuild@0.27.7)(jiti@2.7.0)(tsx@4.22.4)(yaml@2.9.0) - why-is-node-running: 2.3.0 - optionalDependencies: - '@types/node': 25.9.3 - '@vitest/coverage-v8': 4.1.9(@vitest/browser@4.1.9)(vitest@4.1.9) - happy-dom: 20.10.4 - jsdom: 24.1.3 - transitivePeerDependencies: - - msw - vitest@4.1.9(@types/node@25.9.3)(@vitest/coverage-v8@4.1.9)(happy-dom@20.10.4)(jsdom@24.1.3)(vite@8.0.16(@types/node@25.9.3)(esbuild@0.28.0)(jiti@2.7.0)(tsx@4.22.4)(yaml@2.9.0)): dependencies: '@vitest/expect': 4.1.9 @@ -15271,7 +15200,7 @@ snapshots: dependencies: chalk: 4.1.2 commander: 9.5.0 - debug: 4.4.3(supports-color@10.2.2) + debug: 4.4.3(supports-color@5.5.0) transitivePeerDependencies: - supports-color From fabd78265636b6b2c30161a889052d62fca900f0 Mon Sep 17 00:00:00 2001 From: Vishnu Vardhan Date: Wed, 8 Jul 2026 12:09:49 +0530 Subject: [PATCH 20/68] fix(core): drop dependency-internal assertions from the trace --- packages/core/src/assert-patcher.ts | 5 ++ packages/core/tests/assert-patcher.test.ts | 62 +++++++++++++++++++++- 2 files changed, 66 insertions(+), 1 deletion(-) diff --git a/packages/core/src/assert-patcher.ts b/packages/core/src/assert-patcher.ts index ab30c1f7..8917a1ef 100644 --- a/packages/core/src/assert-patcher.ts +++ b/packages/core/src/assert-patcher.ts @@ -57,6 +57,11 @@ function makeAssertEmitters( onCommand: (cmd: CapturedAssert) => void ): { passed: () => void; failed: (err: unknown) => void } { const callInfo = getCallSourceFromStack() + // No user-code frame means the assert came from a dependency or framework + // internal, not the user's test — drop it so it never reaches the trace. + if (callInfo.filePath === undefined) { + return { passed: () => {}, failed: () => {} } + } const startedAt = Date.now() const sanitizedArgs = args.map(safeSerializeAssertArg) const emit = (result: 'passed' | undefined, error: Error | undefined) => diff --git a/packages/core/tests/assert-patcher.test.ts b/packages/core/tests/assert-patcher.test.ts index 641c2e59..86774967 100644 --- a/packages/core/tests/assert-patcher.test.ts +++ b/packages/core/tests/assert-patcher.test.ts @@ -1,4 +1,4 @@ -import { describe, it, expect, beforeEach } from 'vitest' +import { describe, it, expect, beforeEach, vi } from 'vitest' import assert from 'node:assert' import { ASSERT_PATCHED_SYMBOL, @@ -8,6 +8,19 @@ import { safeSerializeAssertArg, type CapturedAssert } from '../src/assert-patcher.js' +import { getCallSourceFromStack } from '../src/stack.js' + +// Stub the call-source resolver so tests choose whether an assert looks like +// it originated in user code (a frame) or a dependency/internal (no frame). +vi.mock('../src/stack.js', () => ({ + getCallSourceFromStack: vi.fn() +})) + +const USER_FRAME = { + filePath: '/specs/login.ts', + callSource: '/specs/login.ts:12:3' +} +const INTERNAL_FRAME = { filePath: undefined, callSource: 'unknown:0' } // Snapshot real methods once so each test starts from a fresh assert. const ASSERT_MUT = assert as unknown as Record @@ -21,6 +34,8 @@ beforeEach(() => { for (const m of TRACKED_ASSERT_METHODS) { ASSERT_MUT[m] = originals[m] } + // Default: every assert looks like it came from user code so captures fire. + vi.mocked(getCallSourceFromStack).mockReturnValue(USER_FRAME) }) describe('safeSerializeAssertArg', () => { @@ -68,6 +83,51 @@ describe('patchNodeAssert', () => { expect(results).toEqual(['passed', undefined]) // first resolved, second rejected }) + // Only assertions that originate in user test code should reach the trace. + // Dependency/framework-internal asserts have no user-code frame on the + // stack (getCallSourceFromStack yields 'unknown:0') and must be dropped. + describe('user-origin filtering', () => { + it('drops a passing assert that has no user-code frame', () => { + vi.mocked(getCallSourceFromStack).mockReturnValue(INTERNAL_FRAME) + const captured: CapturedAssert[] = [] + patchNodeAssert((c) => captured.push(c)) + assert.equal(1, 1) + expect(captured).toHaveLength(0) + }) + + it('drops a failing internal assert but still re-throws', () => { + vi.mocked(getCallSourceFromStack).mockReturnValue(INTERNAL_FRAME) + const captured: CapturedAssert[] = [] + patchNodeAssert((c) => captured.push(c)) + expect(() => assert.equal(1, 2)).toThrow() + expect(captured).toHaveLength(0) + }) + + it('drops an internal async assert (rejects/doesNotReject)', async () => { + vi.mocked(getCallSourceFromStack).mockReturnValue(INTERNAL_FRAME) + const captured: CapturedAssert[] = [] + patchNodeAssert((c) => captured.push(c)) + await assert.doesNotReject(async () => 1) + await expect(assert.rejects(async () => 1)).rejects.toThrow() + expect(captured).toHaveLength(0) + }) + + it('keeps a user-origin assert and carries its callSource through', () => { + vi.mocked(getCallSourceFromStack).mockReturnValue(USER_FRAME) + const captured: CapturedAssert[] = [] + patchNodeAssert((c) => captured.push(c)) + assert.equal(1, 1) + expect(() => assert.equal(1, 2)).toThrow() + expect(captured).toHaveLength(2) + expect(captured[0]).toMatchObject({ + result: 'passed', + callSource: USER_FRAME.callSource + }) + expect(captured[1].result).toBeUndefined() + expect(captured[1].error).toBeInstanceOf(Error) + }) + }) + it('is idempotent — second patch is a no-op and sets the guard symbol', () => { const a: CapturedAssert[] = [] patchNodeAssert((c) => a.push(c)) From c7f3ca4560d45fc5adcc686e06b8f2fb39d00d20 Mon Sep 17 00:00:00 2001 From: Vishnu Vardhan Date: Wed, 8 Jul 2026 12:40:08 +0530 Subject: [PATCH 21/68] fix(service): give Cucumber scenario-outline rows distinct trace groups --- packages/service/src/action-snapshot.ts | 28 +++- packages/service/src/index.ts | 126 +++++++----------- packages/service/src/test-metadata.ts | 13 ++ packages/service/tests/trace-metadata.test.ts | 21 ++- 4 files changed, 108 insertions(+), 80 deletions(-) diff --git a/packages/service/src/action-snapshot.ts b/packages/service/src/action-snapshot.ts index 96cf60b4..12710114 100644 --- a/packages/service/src/action-snapshot.ts +++ b/packages/service/src/action-snapshot.ts @@ -8,9 +8,13 @@ // library. No external input reaches new Function() — the lint flag here is // a false positive given the closed input set. -import { captureActionSnapshot as coreCapture } from '@wdio/devtools-core' +import { + captureActionSnapshot as coreCapture, + mapCommandToAction +} from '@wdio/devtools-core' import type { ActionSnapshot } from '@wdio/devtools-shared' import { isNativeMobile, mobilePlatform } from './mobile.js' +import { INTERNAL_COMMANDS } from './constants.js' function reviveScript(src: string): () => unknown { // `src` from core/element-scripts is already a self-invoking IIFE @@ -55,6 +59,28 @@ export async function waitForActionResult( await browser.pause(250).catch(() => undefined) } +/** Post-action capture: settle the resulting page, screenshot it, and push the + * snapshot stamped at the latest logged action. No-op for internal/non-mapped + * commands. Skipped by the caller outside trace mode. */ +export async function captureActionResult( + browser: WebdriverIO.Browser, + command: string, + actionSnapshots: ActionSnapshot[], + stampTimestamp: () => number +): Promise { + if (!mapCommandToAction(command) || INTERNAL_COMMANDS.includes(command)) { + return + } + if (!isNativeMobile(browser)) { + await waitForActionResult(browser) + } + const snap = await captureActionSnapshot(browser, command) + if (snap) { + snap.timestamp = stampTimestamp() + actionSnapshots.push(snap) + } +} + export function captureActionSnapshot( browser: WebdriverIO.Browser, command: string diff --git a/packages/service/src/index.ts b/packages/service/src/index.ts index 44be2498..cf984f20 100644 --- a/packages/service/src/index.ts +++ b/packages/service/src/index.ts @@ -1,7 +1,6 @@ /// import logger from '@wdio/logger' import { - deterministicUid, errorMessage, finalizeScreencast, finalizeTraceExport, @@ -14,11 +13,15 @@ import { type TraceExportContext } from '@wdio/devtools-core' import { captureExpectFailure, wireAssertCapture } from './assert-capture.js' -import { stampTestState, testMetadataUid } from './test-metadata.js' +import { + cucumberScenarioUid, + stampTestState, + testMetadataUid +} from './test-metadata.js' import { resolveCallSourceFromFrame } from './call-source.js' import { - captureActionSnapshot, - waitForActionResult + captureActionResult, + captureActionSnapshot } from './action-snapshot.js' import { dedupeSnapshotsByTimestamp, @@ -128,6 +131,21 @@ export default class DevToolsHookService implements Services.ServiceInstance { ) } + /** Record a spec boundary (spec granularity) and flush the previous spec. */ + #recordBoundary(specFile: string | undefined): void { + if (!specFile) { + return + } + const prevRange = recordSpecBoundary( + this.#boundaryContext, + specFile, + this.#options.traceGranularity + ) + if (prevRange) { + this.#fireAndForgetFlush(prevRange) + } + } + /** Assemble the framework-agnostic trace-export context from this service's * state. Output dir ignores the spec range — WDIO writes next to config. */ #traceContext(browser: WebdriverIO.Browser): TraceExportContext { @@ -269,33 +287,24 @@ export default class DevToolsHookService implements Services.ServiceInstance { } } - /** - * Hook for Cucumber framework. - * Detects feature-file boundaries for per-spec tracing and tags commands - * with a stable testUid so tracingGroup spans render in the trace output. - * WDIO passes the Cucumber World as the first argument. - */ - beforeScenario(world?: { pickle?: { uri?: string; name?: string } }) { + /** Cucumber hook: records feature-file boundaries and tags commands with a stable testUid. */ + beforeScenario(world?: { + pickle?: { uri?: string; name?: string; astNodeIds?: readonly string[] } + }) { this.resetStack() const featureFile = world?.pickle?.uri const scenarioName = world?.pickle?.name - // ── Per-spec boundary detection (Cucumber) ── - if (featureFile) { - const prevRange = recordSpecBoundary( - this.#boundaryContext, - featureFile, - this.#options.traceGranularity - ) - if (prevRange) { - this.#fireAndForgetFlush(prevRange) - } - } + this.#recordBoundary(featureFile) // ── Test identity for command tagging ── if (featureFile && scenarioName) { - const uid = deterministicUid(featureFile, scenarioName) + const uid = cucumberScenarioUid( + featureFile, + scenarioName, + world?.pickle?.astNodeIds + ) this.#currentTestUid = uid this.#testMetadata.set(uid, { title: scenarioName, @@ -304,28 +313,11 @@ export default class DevToolsHookService implements Services.ServiceInstance { } } - /** - * Hook for Mocha/Jasmine frameworks. - * It does the exact same thing as beforeScenario. - */ + /** Mocha/Jasmine hook: the beforeScenario equivalent for file-based specs. */ beforeTest(test?: { file?: string; title?: string; fullTitle?: string }) { this.resetStack() - const newSpec = test?.file - - // ── Per-spec boundary detection ── - // Only tracked when traceGranularity is 'spec'. Records array index - // ranges so #flushSpecTrace can slice the accumulated data per spec. - if (newSpec) { - const prevRange = recordSpecBoundary( - this.#boundaryContext, - newSpec, - this.#options.traceGranularity - ) - if (prevRange) { - this.#fireAndForgetFlush(prevRange) - } - } + this.#recordBoundary(test?.file) // Track test identity for command tagging. Generate a stable UID // from file + title so commands can be partitioned across reruns. @@ -364,12 +356,15 @@ export default class DevToolsHookService implements Services.ServiceInstance { } async afterScenario( - world?: { pickle?: { uri?: string; name?: string } }, + world?: { + pickle?: { uri?: string; name?: string; astNodeIds?: readonly string[] } + }, result?: { error?: unknown; passed?: boolean; skipped?: boolean } ) { - const { uri, name } = world?.pickle ?? {} + const { uri, name, astNodeIds } = world?.pickle ?? {} if (uri && name) { - stampTestState(this.#testMetadata, deterministicUid(uri, name), result) + const uid = cucumberScenarioUid(uri, name, astNodeIds) + stampTestState(this.#testMetadata, uid, result) } await this.#finalizePerScenario() } @@ -417,38 +412,6 @@ export default class DevToolsHookService implements Services.ServiceInstance { return Date.now() } - // Post-action capture: the state RESULTING from the action just completed. - // The pre-action capture in beforeCommand only records the state before the - // NEXT mapped action — when intervening commands (assertions, reloadSession) - // change the page first, an action's result (e.g. the page a click navigated - // to) is never captured. - // - // readyState alone is unreliable: right after a click the OLD document still - // reports 'complete', so a naive wait snapshots a blank mid-navigation frame. - // Instead, beforeCommand tags the document; if the tag is gone the action - // navigated, so we wait for the NEW document to finish loading AND render - // content before screenshotting its destination. Stamped at this command's - // own end (the latest logged action). - async #captureActionResult(command: string): Promise { - if ( - this.#options.mode !== 'trace' || - !this.#browser || - !mapCommandToAction(command) || - INTERNAL_COMMANDS.includes(command) - ) { - return - } - const browser = this.#browser - if (!isNativeMobile(browser)) { - await waitForActionResult(browser) - } - const snap = await captureActionSnapshot(browser, command) - if (snap) { - snap.timestamp = this.#lastActionTimestamp() - this.#actionSnapshots.push(snap) - } - } - private resetStack() { this.#commandStack = [] this.#sessionCapturer.resetLastSelector() @@ -557,7 +520,14 @@ export default class DevToolsHookService implements Services.ServiceInstance { frame.startTimestamp, this.#currentTestUid ) - await this.#captureActionResult(command) + if (this.#options.mode === 'trace') { + await captureActionResult( + this.#browser, + command, + this.#actionSnapshots, + () => this.#lastActionTimestamp() + ) + } return captured } } diff --git a/packages/service/src/test-metadata.ts b/packages/service/src/test-metadata.ts index bf802762..339e1dc1 100644 --- a/packages/service/src/test-metadata.ts +++ b/packages/service/src/test-metadata.ts @@ -13,6 +13,19 @@ export function testMetadataUid( return file ? deterministicUid(file, title) : title } +/** Scenario key that separates scenario-outline example rows: they share a + * name, so the pickle's astNodeIds (distinct per row, stable across reruns) + * are folded in. beforeScenario and afterScenario derive it identically. */ +export function cucumberScenarioUid( + uri: string, + name: string, + astNodeIds?: readonly string[] +): string { + return astNodeIds?.length + ? deterministicUid(uri, name, astNodeIds.join(':')) + : deterministicUid(uri, name) +} + /** Canonical test state from a WDIO afterTest/afterScenario result. */ export function resultToState(result: { passed?: boolean diff --git a/packages/service/tests/trace-metadata.test.ts b/packages/service/tests/trace-metadata.test.ts index 044067c2..fe88dc1a 100644 --- a/packages/service/tests/trace-metadata.test.ts +++ b/packages/service/tests/trace-metadata.test.ts @@ -1,7 +1,11 @@ import { describe, it, expect, vi, beforeEach } from 'vitest' import type * as DevtoolsCore from '@wdio/devtools-core' import { deterministicUid } from '@wdio/devtools-core' -import { resultToState, testMetadataUid } from '../src/test-metadata.js' +import { + cucumberScenarioUid, + resultToState, + testMetadataUid +} from '../src/test-metadata.js' // Captures the ctx handed to finalizeTraceExport so the test can inspect the // state stamped onto testMetadata and the policy that flowed in. @@ -36,6 +40,7 @@ vi.mock('../src/session.js', () => ({ // Keep the after* hooks from touching a real browser/CDP. vi.mock('../src/action-snapshot.js', () => ({ captureActionSnapshot: vi.fn().mockResolvedValue(null), + captureActionResult: vi.fn().mockResolvedValue(undefined), waitForActionResult: vi.fn().mockResolvedValue(undefined) })) @@ -66,6 +71,20 @@ describe('test-metadata helpers', () => { ) expect(testMetadataUid(undefined, 'renders')).toBe('renders') }) + + it('cucumberScenarioUid separates outline rows sharing a name by astNodeIds', () => { + const uri = '/proj/features/login.feature' + const row1 = cucumberScenarioUid(uri, 'log in', ['node-1']) + const row2 = cucumberScenarioUid(uri, 'log in', ['node-2']) + // Distinct example rows → distinct uids (so they render as separate groups). + expect(row1).not.toBe(row2) + // A rerun of the same row → same uid (retry-coalescing stays intact). + expect(cucumberScenarioUid(uri, 'log in', ['node-1'])).toBe(row1) + // No astNodeIds → plain name-based uid. + expect(cucumberScenarioUid(uri, 'log in')).toBe( + deterministicUid(uri, 'log in') + ) + }) }) describe('DevtoolsService - afterTest state stamping', () => { From 8d1f98a22ee7c6c7977728c889d774635f8db688 Mon Sep 17 00:00:00 2001 From: Vishnu Vardhan Date: Wed, 8 Jul 2026 14:04:09 +0530 Subject: [PATCH 22/68] fix(service): show waits as actions and suppress their internal polling --- examples/wdio/cucumber/wdio.conf.ts | 6 +++-- examples/wdio/mocha/wdio.conf.ts | 6 +++-- packages/service/src/constants.ts | 2 -- packages/service/tests/index.test.ts | 33 ++++++++++++++++++---------- 4 files changed, 29 insertions(+), 18 deletions(-) diff --git a/examples/wdio/cucumber/wdio.conf.ts b/examples/wdio/cucumber/wdio.conf.ts index b6305819..68f7ee8b 100644 --- a/examples/wdio/cucumber/wdio.conf.ts +++ b/examples/wdio/cucumber/wdio.conf.ts @@ -54,7 +54,9 @@ export const config: Options.Testrunner = { // and 30 processes will get spawned. The property handles how many capabilities // from the same test should run tests. // - maxInstances: 10, + // Live mode drives a single-session dashboard; >1 worker streams two feature + // files into it at once and neither renders cleanly. One instance = readable demo. + maxInstances: 1, // // If you have trouble getting all important capabilities together, check out the // Sauce Labs platform configurator - a great tool to configure your capabilities: @@ -131,7 +133,7 @@ export const config: Options.Testrunner = { [ 'devtools', { - mode: 'trace' as const, + mode: 'live' as const, screencast: { enabled: true, pollIntervalMs: 200 } } ] diff --git a/examples/wdio/mocha/wdio.conf.ts b/examples/wdio/mocha/wdio.conf.ts index 474602e3..7ccee43f 100644 --- a/examples/wdio/mocha/wdio.conf.ts +++ b/examples/wdio/mocha/wdio.conf.ts @@ -13,7 +13,9 @@ export const config: Options.Testrunner = { }, specs: ['./specs/**/*.e2e.ts'], exclude: [], - maxInstances: 10, + // Live mode drives a single-session dashboard; >1 worker streams two sessions + // into it at once and neither renders cleanly. One instance = readable demo. + maxInstances: 1, capabilities: [ { browserName: 'chrome', @@ -37,7 +39,7 @@ export const config: Options.Testrunner = { [ 'devtools', { - mode: 'trace' as const, + mode: 'live' as const, traceGranularity: 'spec' as const // tracePolicy: 'retain-on-failure' as const // screencast: { enabled: true, pollIntervalMs: 200 } diff --git a/packages/service/src/constants.ts b/packages/service/src/constants.ts index 46e38878..a30f6e3a 100644 --- a/packages/service/src/constants.ts +++ b/packages/service/src/constants.ts @@ -43,7 +43,6 @@ export const INTERNAL_COMMANDS = [ 'emit', 'browsingContextLocateNodes', 'browsingContextNavigate', - 'waitUntil', 'getTitle', 'getUrl', 'getWindowSize', @@ -51,7 +50,6 @@ export const INTERNAL_COMMANDS = [ 'deleteSession', 'findElementFromShadowRoot', 'findElementsFromShadowRoot', - 'waitForExist', 'browsingContextGetTree', 'scriptCallFunction', 'getElement', diff --git a/packages/service/tests/index.test.ts b/packages/service/tests/index.test.ts index 6d2a56ff..4fab53d5 100644 --- a/packages/service/tests/index.test.ts +++ b/packages/service/tests/index.test.ts @@ -105,13 +105,7 @@ describe('DevtoolsService - Internal Command Filtering', () => { describe('beforeCommand', () => { it('should not add internal commands to command stack', () => { - const internalCommands = [ - 'getTitle', - 'waitUntil', - 'getUrl', - 'execute', - 'findElement' - ] + const internalCommands = ['getTitle', 'getUrl', 'execute', 'findElement'] internalCommands.forEach((cmd) => service.beforeCommand(cmd as any, [])) expect(true).toBe(true) }) @@ -137,19 +131,34 @@ describe('DevtoolsService - Internal Command Filtering', () => { executeCommand('url', ['https://example.com']) executeCommand('getTitle', [], 'Page Title') // internal executeCommand('click', ['.button']) - executeCommand('waitUntil', [expect.any(Function)], true) // internal + executeCommand('waitUntil', [expect.any(Function)], true) // a user wait executeCommand('getText', ['.result'], 'Success') - // Only user commands (url, click, getText) should be captured - expect(mockSessionCapturerInstance.afterCommand).toHaveBeenCalledTimes(3) + // getTitle is internal; the rest — including the wait — are user actions. + expect(mockSessionCapturerInstance.afterCommand).toHaveBeenCalledTimes(4) const capturedCommands = mockSessionCapturerInstance.afterCommand.mock.calls.map( (call) => call[1] ) - expect(capturedCommands).toEqual(['url', 'click', 'getText']) + expect(capturedCommands).toEqual(['url', 'click', 'waitUntil', 'getText']) expect(capturedCommands).not.toContain('getTitle') - expect(capturedCommands).not.toContain('waitUntil') + }) + + it('captures a wait once and suppresses the commands it polls', () => { + // waitUntil opens; its predicate polls isDisplayed while the wait is + // still on the stack, so those polls are top-level-suppressed. + service.beforeCommand('waitUntil' as any, [expect.any(Function)]) + service.beforeCommand('isDisplayed' as any, []) + service.afterCommand('isDisplayed' as any, [], false) + service.beforeCommand('isDisplayed' as any, []) + service.afterCommand('isDisplayed' as any, [], true) + service.afterCommand('waitUntil' as any, [expect.any(Function)], true) + + const captured = mockSessionCapturerInstance.afterCommand.mock.calls.map( + (call) => call[1] + ) + expect(captured).toEqual(['waitUntil']) }) // Service-fired commands (preload injection, Puppeteer handle for CDP, From 40d920838c456524582266c1b80ec68e793e5d23 Mon Sep 17 00:00:00 2001 From: Vishnu Vardhan Date: Thu, 9 Jul 2026 13:47:46 +0530 Subject: [PATCH 23/68] feat(core,adapters): capture per-test retry attempts for retry-aware policies --- packages/core/src/attempt-tracker.ts | 39 +++++ packages/core/src/index.ts | 1 + packages/core/src/trace-finalizer.ts | 11 +- packages/core/tests/attempt-tracker.test.ts | 46 ++++++ .../src/cucumber-lifecycle.ts | 26 ++-- .../src/helpers/cucumberScenarioBuilder.ts | 13 +- packages/nightwatch-devtools/src/index.ts | 53 ++++--- .../src/plugin-internals.ts | 6 + packages/nightwatch-devtools/src/reporter.ts | 22 ++- .../nightwatch-devtools/src/session-init.ts | 14 +- .../nightwatch-devtools/src/test-lifecycle.ts | 3 + .../nightwatch-devtools/src/trace-context.ts | 60 ++++++++ .../tests/attemptTracking.test.ts | 134 ++++++++++++++++++ .../src/helpers/testManager.ts | 21 ++- packages/selenium-devtools/src/index.ts | 54 +++---- .../src/runnerHooks/mocha.ts | 3 +- .../src/session-lifecycle.ts | 5 +- .../selenium-devtools/src/test-management.ts | 59 ++++++-- packages/selenium-devtools/src/types.ts | 51 ++----- packages/service/src/index.ts | 53 +++++-- packages/service/src/reporter.ts | 4 + packages/service/src/test-metadata.ts | 32 ++++- packages/service/tests/trace-metadata.test.ts | 51 +++++++ 23 files changed, 606 insertions(+), 155 deletions(-) create mode 100644 packages/core/src/attempt-tracker.ts create mode 100644 packages/core/tests/attempt-tracker.test.ts create mode 100644 packages/nightwatch-devtools/src/trace-context.ts create mode 100644 packages/nightwatch-devtools/tests/attemptTracking.test.ts diff --git a/packages/core/src/attempt-tracker.ts b/packages/core/src/attempt-tracker.ts new file mode 100644 index 00000000..e4915584 --- /dev/null +++ b/packages/core/src/attempt-tracker.ts @@ -0,0 +1,39 @@ +/** + * Framework-agnostic per-test attempt counting. Every supported runner + * re-enters its per-test start hook when a test is retried, so recording the + * same uid again yields an incremented attempt number (0-based: the first run + * is attempt 0, the first retry is attempt 1). This is the primary, + * runner-independent retry signal feeding `TestOutcome.attempt` for the + * retry-aware trace policies (see trace-retention.ts). + */ +export class TestAttemptTracker { + #attempts = new Map() + #sawRetry = false + + /** Record a starting test; returns its attempt number (0 first, +1 per rerun). */ + recordStart(uid: string): number { + const prior = this.#attempts.get(uid) + const attempt = prior === undefined ? 0 : prior + 1 + this.#attempts.set(uid, attempt) + if (attempt > 0) { + this.#sawRetry = true + } + return attempt + } + + /** Latest attempt recorded for `uid`, or undefined if it never started. */ + attemptFor(uid: string): number | undefined { + return this.#attempts.get(uid) + } + + /** True once any test has started more than once (a retry occurred). */ + get sawRetry(): boolean { + return this.#sawRetry + } + + /** Clear all state — used at session boundaries and reuse-mode reconnects. */ + reset(): void { + this.#attempts.clear() + this.#sawRetry = false + } +} diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index db5b315d..3c3325e2 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -3,6 +3,7 @@ export * from './action-mapping.js' export * from './action-snapshot.js' +export * from './attempt-tracker.js' export * from './with-timeout.js' export * from './assert-patcher.js' export * from './element-snapshot.js' diff --git a/packages/core/src/trace-finalizer.ts b/packages/core/src/trace-finalizer.ts index f0de3023..1750d3a4 100644 --- a/packages/core/src/trace-finalizer.ts +++ b/packages/core/src/trace-finalizer.ts @@ -40,6 +40,9 @@ export interface TraceExportContext { mode?: DevToolsMode /** undefined → treated as `on` (always retain) by the retention evaluator. */ policy?: TraceRetentionPolicy + /** True when the adapter fed real per-test attempt numbers (B4); retry-aware + * policies degrade to retain-on-failure when this is false. */ + attemptInfoAvailable?: boolean granularity?: TraceGranularity format?: TraceFormat capturer: TraceCapturer @@ -75,9 +78,9 @@ function toOutcomes(metadata: TestMetadataMap): TestOutcome[] { } /** - * Evaluate the retention policy for one trace slice. B4 supplies real attempt - * info; today `attemptInfoAvailable` is false and the default `on` policy - * always retains — but the decision is wired now so B3 only flips the option. + * Evaluate the retention policy for one trace slice. Adapters that feed real + * per-test attempt numbers also set `attemptInfoAvailable`; when they don't, + * retry-aware policies degrade to retain-on-failure (see trace-retention.ts). */ function shouldRetain( ctx: TraceExportContext, @@ -85,7 +88,7 @@ function shouldRetain( ): boolean { return shouldRetainTrace(ctx.policy, { outcomes: toOutcomes(metadata), - attemptInfoAvailable: false + attemptInfoAvailable: ctx.attemptInfoAvailable ?? false }).retain } diff --git a/packages/core/tests/attempt-tracker.test.ts b/packages/core/tests/attempt-tracker.test.ts new file mode 100644 index 00000000..5d9a44b4 --- /dev/null +++ b/packages/core/tests/attempt-tracker.test.ts @@ -0,0 +1,46 @@ +import { describe, it, expect } from 'vitest' +import { TestAttemptTracker } from '../src/attempt-tracker.js' + +describe('TestAttemptTracker', () => { + it('numbers the first start 0 and each rerun +1', () => { + const t = new TestAttemptTracker() + expect(t.recordStart('a')).toBe(0) + expect(t.recordStart('a')).toBe(1) + expect(t.recordStart('a')).toBe(2) + }) + + it('tracks attempts per uid independently', () => { + const t = new TestAttemptTracker() + expect(t.recordStart('a')).toBe(0) + expect(t.recordStart('b')).toBe(0) + expect(t.recordStart('a')).toBe(1) + expect(t.attemptFor('a')).toBe(1) + expect(t.attemptFor('b')).toBe(0) + }) + + it('attemptFor is undefined for an unseen uid', () => { + const t = new TestAttemptTracker() + expect(t.attemptFor('missing')).toBeUndefined() + }) + + it('sawRetry flips only after a second start of some test', () => { + const t = new TestAttemptTracker() + expect(t.sawRetry).toBe(false) + t.recordStart('a') + t.recordStart('b') + expect(t.sawRetry).toBe(false) + t.recordStart('a') + expect(t.sawRetry).toBe(true) + }) + + it('reset clears attempts and the retry flag', () => { + const t = new TestAttemptTracker() + t.recordStart('a') + t.recordStart('a') + expect(t.sawRetry).toBe(true) + t.reset() + expect(t.sawRetry).toBe(false) + expect(t.attemptFor('a')).toBeUndefined() + expect(t.recordStart('a')).toBe(0) + }) +}) diff --git a/packages/nightwatch-devtools/src/cucumber-lifecycle.ts b/packages/nightwatch-devtools/src/cucumber-lifecycle.ts index afb8a832..201e2ca0 100644 --- a/packages/nightwatch-devtools/src/cucumber-lifecycle.ts +++ b/packages/nightwatch-devtools/src/cucumber-lifecycle.ts @@ -13,7 +13,11 @@ import logger from '@wdio/logger' import { errorMessage } from '@wdio/devtools-core' -import { WS_SCOPE } from '@wdio/devtools-shared' +import { + WS_SCOPE, + type CucumberPickle, + type CucumberPickleStep +} from '@wdio/devtools-shared' import type { SessionCapturer } from './session.js' import type { TestReporter } from './reporter.js' @@ -32,20 +36,8 @@ import { parseCucumberScenario } from './helpers/utils.js' const log = logger('@wdio/nightwatch-devtools:cucumber') -/** Minimal shapes for the Cucumber objects we touch. Cucumber's own types - * vary across major versions; we pin only fields we read. */ -export interface CucumberPickleStep { - text?: string - astNodeIds?: string[] - location?: { line?: number } -} -export interface CucumberPickle { - uri?: string - name?: string - location?: { line?: number } - astNodeIds?: string[] - steps?: CucumberPickleStep[] -} +/** Cucumber's result shape varies across major versions; we pin only the + * status field we read. Pickle shapes come from shared. */ export interface CucumberResult { status?: string } @@ -66,6 +58,7 @@ export interface CucumberLifecycleCtx { setCurrentStep(s: unknown): void getCurrentStep(): unknown setCurrentTest(t: unknown): void + recordAttempt(uid: string): number } type MutStep = { @@ -178,7 +171,8 @@ export async function initCucumberScenario( stepLines, stepKeywords, scenarioLine, - parentFeatureSuiteUid: featureSuite.uid + parentFeatureSuiteUid: featureSuite.uid, + recordAttempt: (uid) => ctx.recordAttempt(uid) }) attachScenarioToFeature(ctx, featureSuite, scenarioSuite) ctx.setCurrentScenarioSuite(scenarioSuite) diff --git a/packages/nightwatch-devtools/src/helpers/cucumberScenarioBuilder.ts b/packages/nightwatch-devtools/src/helpers/cucumberScenarioBuilder.ts index 54ab1da3..b60d5ad4 100644 --- a/packages/nightwatch-devtools/src/helpers/cucumberScenarioBuilder.ts +++ b/packages/nightwatch-devtools/src/helpers/cucumberScenarioBuilder.ts @@ -21,6 +21,9 @@ export interface CucumberScenarioBuildInput { scenarioLine: number /** Parent feature-suite uid — scenarios nest under this. */ parentFeatureSuiteUid: string + /** Records the scenario start under its (retry-stable) uid and returns the + * 0-based attempt number stamped on every step. Omitted → attempt 0. */ + recordAttempt?: (scenarioUid: string) => number } /** @@ -36,6 +39,7 @@ export interface CucumberScenarioBuildInput { function buildScenarioStepTest( input: CucumberScenarioBuildInput, scenarioUid: string, + attempt: number, i: number ): SuiteStats['tests'][number] { const { @@ -69,7 +73,9 @@ function buildScenarioStepTest( end: null, type: 'test' as const, file: featureUri, - retries: 0, + // Scenario-level attempt from the tracker (0 first run, +1 per retry); + // flows to TestOutcome.attempt via collectSuiteTestMetadata. + retries: attempt, _duration: 0, hooks: [], callSource @@ -95,6 +101,7 @@ export function buildCucumberScenarioSuite( featureUri, `scenario:${scenarioName}:${scenarioLine}` ) + const attempt = input.recordAttempt?.(scenarioUid) ?? 0 const scenarioSuite: SuiteStats = { uid: scenarioUid, cid: DEFAULTS.CID, @@ -116,7 +123,9 @@ export function buildCucumberScenarioSuite( : undefined } for (let i = 0; i < steps.length; i++) { - scenarioSuite.tests.push(buildScenarioStepTest(input, scenarioUid, i)) + scenarioSuite.tests.push( + buildScenarioStepTest(input, scenarioUid, attempt, i) + ) } return scenarioSuite } diff --git a/packages/nightwatch-devtools/src/index.ts b/packages/nightwatch-devtools/src/index.ts index 41a6c404..e94877bc 100644 --- a/packages/nightwatch-devtools/src/index.ts +++ b/packages/nightwatch-devtools/src/index.ts @@ -7,20 +7,25 @@ import { fileURLToPath } from 'node:url' import { - collectSuiteTestMetadata, errorMessage, finalizeTraceExport, flushRangeTrace, recordSpecBoundary, - resolveAdapterOutputDir, + TestAttemptTracker, tracePolicyModeWarning, type SpecRange, type TraceArtifact, type TraceExportContext } from '@wdio/devtools-core' +import { buildTraceContext } from './trace-context.js' import { wireAssertCapture } from './helpers/assertCapture.js' import { stop as stopBackend } from '@wdio/devtools-backend' -import { REUSE_ENV, SCREENCAST_DEFAULTS } from '@wdio/devtools-shared' +import { + REUSE_ENV, + SCREENCAST_DEFAULTS, + type CucumberPickle, + type CucumberPickleStep +} from '@wdio/devtools-shared' import logger from '@wdio/logger' import { handleReuseMode, @@ -53,8 +58,6 @@ import { cucumberAfter as cucumberLifecycleAfter, cucumberBeforeStep as cucumberLifecycleBeforeStep, cucumberAfterStep as cucumberLifecycleAfterStep, - type CucumberPickle, - type CucumberPickleStep, type CucumberResult } from './cucumber-lifecycle.js' import { @@ -116,6 +119,10 @@ class NightwatchDevToolsPlugin { #bidiEnabled = false #bidiAttachAttempted = false + // Nightwatch `--retries` and cross-worker reruns may reset this in-process + // tracker; only retries that re-enter this process's start hook are counted. + #attemptTracker = new TestAttemptTracker() + constructor(options: DevToolsOptions = {}) { const mode = options.mode ?? 'live' const ignore = mode === 'trace' && options.screencast?.enabled === true @@ -286,7 +293,10 @@ class NightwatchDevToolsPlugin { clearExecutionData: () => { self.testReporter.clearExecutionData() self.suiteManager.clearExecutionData() + self.#attemptTracker.reset() }, + recordAttempt: (uid) => self.#attemptTracker.recordStart(uid), + attemptFor: (uid) => self.#attemptTracker.attemptFor(uid), buildMetadataOptions: () => self.#buildMetadataOptions(), ensureSessionInitialized: (b) => self.#ensureSessionInitialized(b), wrapBrowserOnce: (b) => self.#wrapBrowserOnce(b), @@ -544,24 +554,21 @@ class NightwatchDevToolsPlugin { /** Assemble the framework-agnostic trace-export context from plugin state. * Output dir ignores the spec range — nightwatch writes next to config. */ #traceContext(sessionId: string): TraceExportContext { - return { - mode: this.options.mode, - policy: this.options.tracePolicy, - granularity: this.options.traceGranularity, - format: this.options.traceFormat, - capturer: this.sessionCapturer, - actionSnapshots: this.sessionCapturer.actionSnapshots, - sessionId, - testMetadata: collectSuiteTestMetadata( - this.suiteManager.getAllSuites().values() - ), - ranges: this.#specRanges, - flushed: this.#flushedSpecs, - resolveOutputDir: () => - resolveAdapterOutputDir({ configPath: this.#configPath }), - awaitPending: this.sessionCapturer.snapshotCaptures, - log: (level, msg) => log[level](msg) - } + return buildTraceContext( + { + mode: this.options.mode, + policy: this.options.tracePolicy, + granularity: this.options.traceGranularity, + format: this.options.traceFormat, + capturer: this.sessionCapturer, + suites: this.suiteManager.getAllSuites().values(), + ranges: this.#specRanges, + flushed: this.#flushedSpecs, + configPath: this.#configPath, + log: (level, msg) => log[level](msg) + }, + sessionId + ) } async #writeTraceIfNeeded(): Promise { diff --git a/packages/nightwatch-devtools/src/plugin-internals.ts b/packages/nightwatch-devtools/src/plugin-internals.ts index afce58b4..1d4d45a2 100644 --- a/packages/nightwatch-devtools/src/plugin-internals.ts +++ b/packages/nightwatch-devtools/src/plugin-internals.ts @@ -73,4 +73,10 @@ export interface PluginInternals { testIcon(state: TestStats['state']): string setCucumberRunner(v: boolean): void getRerunLabel(): string | undefined + + /** Records a test/scenario start under its retry-stable uid; returns the + * 0-based attempt number (0 first run, +1 per rerun). */ + recordAttempt(uid: string): number + /** Latest attempt recorded for `uid`, or undefined if it never started. */ + attemptFor(uid: string): number | undefined } diff --git a/packages/nightwatch-devtools/src/reporter.ts b/packages/nightwatch-devtools/src/reporter.ts index a6e8282e..63626d27 100644 --- a/packages/nightwatch-devtools/src/reporter.ts +++ b/packages/nightwatch-devtools/src/reporter.ts @@ -1,5 +1,5 @@ import logger from '@wdio/logger' -import { TestReporterBase } from '@wdio/devtools-core' +import { TestReporterBase, type ReporterUpstream } from '@wdio/devtools-core' import { REUSE_ENV } from '@wdio/devtools-shared' import { DEFAULTS } from './constants.js' import { extractTestMetadata, generateStableUid } from './helpers/utils.js' @@ -11,6 +11,15 @@ export class TestReporter extends TestReporterBase { #currentSpecFile?: string #testNamesCache = new Map() #currentSuite?: SuiteStats + #attemptFor: (uid: string) => number | undefined + + constructor( + report: ReporterUpstream, + attemptFor: (uid: string) => number | undefined + ) { + super(report) + this.#attemptFor = attemptFor + } onSuiteStart(suiteStats: SuiteStats) { this.#currentSpecFile = suiteStats.file @@ -92,11 +101,12 @@ export class TestReporter extends TestReporterBase { cachedNames.forEach((testName) => { if (!processedTestNames.has(testName)) { + const uid = generateStableUid({ + file: suiteStats.file, + fullTitle: `${suiteStats.title} ${testName}` + } as TestStats) const skippedTest: TestStats = { - uid: generateStableUid({ - file: suiteStats.file, - fullTitle: `${suiteStats.title} ${testName}` - } as TestStats), + uid, cid: DEFAULTS.CID, title: testName, fullTitle: `${suiteStats.title} ${testName}`, @@ -106,7 +116,7 @@ export class TestReporter extends TestReporterBase { end: new Date(), type: 'test', file: suiteStats.file, - retries: DEFAULTS.RETRIES, + retries: this.#attemptFor(uid) ?? DEFAULTS.RETRIES, _duration: DEFAULTS.DURATION, hooks: [] } diff --git a/packages/nightwatch-devtools/src/session-init.ts b/packages/nightwatch-devtools/src/session-init.ts index 71a1c21b..2bccfc82 100644 --- a/packages/nightwatch-devtools/src/session-init.ts +++ b/packages/nightwatch-devtools/src/session-init.ts @@ -57,6 +57,7 @@ export interface SessionInitCtx { getCurrentTest(): unknown getCurrentScenarioSuite(): SuiteStats | null buildMetadataOptions(): unknown + attemptFor(uid: string): number | undefined } async function handleSessionChange( @@ -84,11 +85,14 @@ function initReporterChain(ctx: SessionInitCtx): void { // These must NOT be recreated on session change — doing so generates a // new feature suite with a fresh start timestamp, which DataManager sees // as a new run and wipes all accumulated commands. - ctx.testReporter = new TestReporter((suitesData) => { - if (ctx.sessionCapturer) { - ctx.sessionCapturer.sendUpstream('suites', suitesData) - } - }) + ctx.testReporter = new TestReporter( + (suitesData) => { + if (ctx.sessionCapturer) { + ctx.sessionCapturer.sendUpstream('suites', suitesData) + } + }, + (uid) => ctx.attemptFor(uid) + ) ctx.testManager = new TestManager(ctx.testReporter) ctx.suiteManager = new SuiteManager(ctx.testReporter) ctx.browserProxy = new BrowserProxy( diff --git a/packages/nightwatch-devtools/src/test-lifecycle.ts b/packages/nightwatch-devtools/src/test-lifecycle.ts index 4bbd52e6..5caf0537 100644 --- a/packages/nightwatch-devtools/src/test-lifecycle.ts +++ b/packages/nightwatch-devtools/src/test-lifecycle.ts @@ -41,6 +41,7 @@ export interface TestLifecycleCtx { incrementCount(state: TestStats['state']): void testIcon(state: TestStats['state']): string setCurrentTest(t: unknown): void + recordAttempt(uid: string): number } interface SuiteMetadata { @@ -124,6 +125,8 @@ export async function startNextTest( } const test = ctx.testManager.findTestInSuite(currentSuite, currentTestName) if (test) { + // Nightwatch has no per-test retry index; the tracker is the retry signal. + test.retries = ctx.recordAttempt(test.uid) test.state = TEST_STATE.RUNNING as TestStats['state'] test.start = new Date() test.end = null diff --git a/packages/nightwatch-devtools/src/trace-context.ts b/packages/nightwatch-devtools/src/trace-context.ts new file mode 100644 index 00000000..1e451ac7 --- /dev/null +++ b/packages/nightwatch-devtools/src/trace-context.ts @@ -0,0 +1,60 @@ +/** + * Assembles the framework-agnostic trace-export context from plugin state. + * + * Extracted from `NightwatchDevToolsPlugin.#traceContext` so the assembly is + * unit-testable and to keep `index.ts` under the file-size cap. Both the + * per-spec boundary flush and the final trace write share this one builder. + */ + +import { + collectSuiteTestMetadata, + resolveAdapterOutputDir, + type SpecRange, + type TraceExportContext +} from '@wdio/devtools-core' +import type { SessionCapturer } from './session.js' +import type { + DevToolsMode, + SuiteStats, + TraceFormat, + TraceGranularity, + TraceRetentionPolicy +} from './types.js' + +export interface TraceContextInput { + mode: DevToolsMode + policy: TraceRetentionPolicy + granularity: TraceGranularity + format: TraceFormat + capturer: SessionCapturer + suites: Iterable + ranges: SpecRange[] + flushed: Set + configPath: string | undefined + log: (level: 'info' | 'warn', msg: string) => void +} + +export function buildTraceContext( + input: TraceContextInput, + sessionId: string +): TraceExportContext { + return { + mode: input.mode, + policy: input.policy, + granularity: input.granularity, + format: input.format, + capturer: input.capturer, + actionSnapshots: input.capturer.actionSnapshots, + sessionId, + testMetadata: collectSuiteTestMetadata(input.suites), + ranges: input.ranges, + flushed: input.flushed, + resolveOutputDir: () => + resolveAdapterOutputDir({ configPath: input.configPath }), + awaitPending: input.capturer.snapshotCaptures, + // Nightwatch feeds real per-test attempt numbers via TestAttemptTracker + // (B4), so retry-aware policies use per-test attempts, not the fallback. + attemptInfoAvailable: true, + log: input.log + } +} diff --git a/packages/nightwatch-devtools/tests/attemptTracking.test.ts b/packages/nightwatch-devtools/tests/attemptTracking.test.ts new file mode 100644 index 00000000..c08313c4 --- /dev/null +++ b/packages/nightwatch-devtools/tests/attemptTracking.test.ts @@ -0,0 +1,134 @@ +import { describe, it, expect, vi } from 'vitest' +import { + TestAttemptTracker, + collectSuiteTestMetadata +} from '@wdio/devtools-core' +import { buildCucumberScenarioSuite } from '../src/helpers/cucumberScenarioBuilder.js' +import { buildTraceContext } from '../src/trace-context.js' +import { startNextTest, type TestLifecycleCtx } from '../src/test-lifecycle.js' +import { TEST_STATE } from '../src/constants.js' +import type { SessionCapturer } from '../src/session.js' +import type { SuiteStats, TestStats } from '../src/types.js' + +function scenarioInput(recordAttempt: (uid: string) => number) { + return { + featureUri: 'login.feature', + scenarioName: 'Valid login', + featureName: 'Login', + stepDefFiles: [], + steps: [{ text: 'I open the page' }, { text: 'I log in' }], + stepLines: [0, 0], + stepKeywords: ['Given', 'When'], + scenarioLine: 3, + parentFeatureSuiteUid: 'feature-uid', + recordAttempt + } +} + +function makeTest(uid: string, title: string): TestStats { + return { + uid, + cid: '0-0', + title, + fullTitle: title, + parent: 'suite-uid', + state: TEST_STATE.PENDING, + start: new Date(), + end: null, + type: 'test', + file: '/spec.ts', + retries: 0, + _duration: 0, + hooks: [] + } +} + +describe('cucumber scenario retry attempt', () => { + it('stamps the tracker attempt on every step and flows it to metadata.attempt', () => { + const tracker = new TestAttemptTracker() + const record = (uid: string) => tracker.recordStart(uid) + + const first = buildCucumberScenarioSuite(scenarioInput(record)) + expect((first.tests as TestStats[]).map((t) => t.retries)).toEqual([0, 0]) + + // Same scenario name + line → same (retry-stable) uid → attempt increments. + const retried = buildCucumberScenarioSuite(scenarioInput(record)) + expect((retried.tests as TestStats[]).map((t) => t.retries)).toEqual([1, 1]) + expect(tracker.sawRetry).toBe(true) + + const metadata = collectSuiteTestMetadata([retried]) + for (const step of retried.tests as TestStats[]) { + expect(metadata.get(step.uid)?.attempt).toBe(1) + } + }) + + it('defaults step attempt to 0 when no tracker is wired', () => { + const suite = buildCucumberScenarioSuite({ + ...scenarioInput(() => 0), + recordAttempt: undefined + }) + expect((suite.tests as TestStats[]).every((t) => t.retries === 0)).toBe( + true + ) + }) +}) + +describe('regular test retry attempt via startNextTest', () => { + it('records the attempt under the test uid and increments on rerun', async () => { + const tracker = new TestAttemptTracker() + const test = makeTest('test-uid', 'my test') + const suite = { + uid: 'suite-uid', + tests: [test] + } as unknown as SuiteStats + const ctx = { + suiteManager: { markSuiteAsRunning: vi.fn() }, + testManager: { findTestInSuite: () => test }, + testReporter: { onTestStart: vi.fn() }, + setCurrentTest: vi.fn(), + recordAttempt: (uid: string) => tracker.recordStart(uid) + } as unknown as TestLifecycleCtx + + await startNextTest(ctx, suite, 'my test', new Set()) + expect(test.retries).toBe(0) + + await startNextTest(ctx, suite, 'my test', new Set(['my test'])) + expect(test.retries).toBe(1) + }) +}) + +describe('buildTraceContext', () => { + it('sets attemptInfoAvailable and carries retries through to attempt', () => { + const capturer = { + actionSnapshots: [], + snapshotCaptures: [] + } as unknown as SessionCapturer + const test = makeTest('t1', 'retried test') + test.retries = 2 + const suite = { + uid: 'suite-uid', + tests: [test], + suites: [] + } as unknown as SuiteStats + + const ctx = buildTraceContext( + { + mode: 'trace', + policy: 'on', + granularity: 'session', + format: 'zip', + capturer, + suites: [suite], + ranges: [], + flushed: new Set(), + configPath: undefined, + log: () => {} + }, + 'session-1' + ) + + expect(ctx.attemptInfoAvailable).toBe(true) + expect(ctx.sessionId).toBe('session-1') + expect(ctx.testMetadata.get('t1')?.attempt).toBe(2) + }) +}) diff --git a/packages/selenium-devtools/src/helpers/testManager.ts b/packages/selenium-devtools/src/helpers/testManager.ts index fc3bfebf..70fcb14b 100644 --- a/packages/selenium-devtools/src/helpers/testManager.ts +++ b/packages/selenium-devtools/src/helpers/testManager.ts @@ -1,5 +1,9 @@ import logger from '@wdio/logger' -import { createTestStats, stampTestEnd } from '@wdio/devtools-core' +import { + createTestStats, + stampTestEnd, + TestAttemptTracker +} from '@wdio/devtools-core' import { DEFAULTS, TEST_STATE } from '../constants.js' import type { SuiteStats, TestStats } from '../types.js' import type { TestReporter } from '../reporter.js' @@ -19,6 +23,9 @@ export class TestManager { #currentTest: TestStats | null = null #lastMarkedTest: TestStats | null = null #mode: 'session' | 'marked' = 'session' + // Per-test attempt counter. Persists for the whole in-process session so + // same-process retries (Mocha/Jest/etc re-entering the start hook) accumulate. + #attemptTracker = new TestAttemptTracker() /** Set true the first time the user calls startMarkedTest. Once true we * never auto-create the synthetic session test — orphan commands attach * to the most-recently-marked test instead. */ @@ -90,7 +97,7 @@ export class TestManager { */ startMarkedTest( name: string, - opts: { file?: string; callSource?: string } = {} + opts: { file?: string; callSource?: string; attempt?: number } = {} ): TestStats { if (!this.#userTookOver) { this.#userTookOver = true @@ -115,14 +122,22 @@ export class TestManager { const file = opts.file || this.suite.file // Scope by parent so two suites with the same test/step name don't // collide on signatureCounter disambiguation across rerun processes. + const signature = `${this.suite.uid}::${name}` const test = createTestStats({ - uid: generateStableUid(file, `${this.suite.uid}::${name}`), + uid: generateStableUid(file, signature), cid: DEFAULTS.CID, title: name, file, parent: this.suite.uid, callSource: opts.callSource }) + // deterministicUid is retry-stable (no counter), so re-entering with the + // same logical test increments the heuristic. Mocha supplies an + // authoritative per-test retry index via opts.attempt; prefer it. + const heuristicAttempt = this.#attemptTracker.recordStart( + deterministicUid(file, signature) + ) + test.retries = opts.attempt ?? heuristicAttempt log.info( `Started marked test "${name}" (callSource: ${opts.callSource || 'n/a'})` ) diff --git a/packages/selenium-devtools/src/index.ts b/packages/selenium-devtools/src/index.ts index 030e935f..2fcdb885 100644 --- a/packages/selenium-devtools/src/index.ts +++ b/packages/selenium-devtools/src/index.ts @@ -30,8 +30,10 @@ import { startScenario as tmStartScenario, endScenario as tmEndScenario, flushPendingTestActions as tmFlushPendingTestActions, + buildStartTestMeta, type StartTestMeta, - type StartScenarioMeta + type StartScenarioMeta, + type PendingTestAction } from './test-management.js' import type { PluginInternals } from './plugin-internals.js' import { @@ -104,16 +106,7 @@ class SeleniumDevToolsPlugin { #uiReadyPromise?: Promise // First it() body fires before onDriverCreated's async setup completes — // buffer startTest/endTest until testManager exists. - #pendingTestActions: Array< - | { - kind: 'start' - name: string - meta: { file?: string; callSource?: string } - suiteName?: string - suiteCallSource?: string - } - | { kind: 'end'; state: TestStats['state'] } - > = [] + #pendingTestActions: PendingTestAction[] = [] // Cucumber Before fires before the driver-build Before — stash and replay. #pendingScenario: { name: string @@ -596,27 +589,24 @@ patchNodeAssert((cmd) => { // Runner globals are published after `--require`, so retry briefly. function registerHooks() { return tryRegisterRunnerHooks({ - onTestStart: (name, file, callSource, suiteName, suiteCallSource) => { - const meta: { - file?: string - callSource?: string - suiteName?: string - suiteCallSource?: string - } = {} - if (file) { - meta.file = file - } - if (callSource) { - meta.callSource = callSource - } - if (suiteName) { - meta.suiteName = suiteName - } - if (suiteCallSource) { - meta.suiteCallSource = suiteCallSource - } - plugin.startTest(name, meta) - }, + onTestStart: ( + name, + file, + callSource, + suiteName, + suiteCallSource, + attempt + ) => + plugin.startTest( + name, + buildStartTestMeta( + file, + callSource, + suiteName, + suiteCallSource, + attempt + ) + ), onTestEnd: (state) => { plugin.endTest(state === 'pending' ? 'skipped' : state) }, diff --git a/packages/selenium-devtools/src/runnerHooks/mocha.ts b/packages/selenium-devtools/src/runnerHooks/mocha.ts index 5f2ad0f5..e7b8a616 100644 --- a/packages/selenium-devtools/src/runnerHooks/mocha.ts +++ b/packages/selenium-devtools/src/runnerHooks/mocha.ts @@ -87,7 +87,8 @@ function makeMochaBeforeEach( parentTitle, parentTitle ? resolveCallSource(test.file, parentTitle, 'suite') - : undefined + : undefined, + typeof test._currentRetry === 'number' ? test._currentRetry : undefined ) } } diff --git a/packages/selenium-devtools/src/session-lifecycle.ts b/packages/selenium-devtools/src/session-lifecycle.ts index 294d0921..b12699dd 100644 --- a/packages/selenium-devtools/src/session-lifecycle.ts +++ b/packages/selenium-devtools/src/session-lifecycle.ts @@ -290,7 +290,7 @@ export async function onSessionEnd(ctx: SessionLifecycleCtx): Promise { * file and the session trace next to the run's first test file. Test metadata * is recomputed from the suite tree so a boundary flush sees the current tree. */ -function buildTraceExportContext( +export function buildTraceExportContext( ctx: SessionLifecycleCtx, capturer: SessionCapturer, sessionId: string, @@ -306,6 +306,9 @@ function buildTraceExportContext( actionSnapshots: ctx.actionSnapshots, sessionId, testMetadata: collectSuiteTestMetadata(root ? [root] : []), + // TestStats.retries carries the per-test attempt (Mocha authoritative, + // other runners heuristic), so retry-aware policies can trust it. + attemptInfoAvailable: true, ranges: ctx.specRanges, flushed: ctx.flushedSpecs, resolveOutputDir: (range) => diff --git a/packages/selenium-devtools/src/test-management.ts b/packages/selenium-devtools/src/test-management.ts index 842c0654..e62d17de 100644 --- a/packages/selenium-devtools/src/test-management.ts +++ b/packages/selenium-devtools/src/test-management.ts @@ -24,7 +24,7 @@ export type PendingTestAction = | { kind: 'start' name: string - meta: { file?: string; callSource?: string } + meta: { file?: string; callSource?: string; attempt?: number } suiteName?: string suiteCallSource?: string } @@ -54,6 +54,8 @@ export interface StartTestMeta { callSource?: string suiteName?: string suiteCallSource?: string + /** Authoritative per-test retry index when the runner exposes one (Mocha). */ + attempt?: number } export interface StartScenarioMeta { @@ -63,6 +65,53 @@ export interface StartScenarioMeta { featureCallSource?: string } +/** Assemble a StartTestMeta from a runner hook's positional callback args. */ +export function buildStartTestMeta( + file?: string, + callSource?: string, + suiteName?: string, + suiteCallSource?: string, + attempt?: number +): StartTestMeta { + const meta: StartTestMeta = {} + if (file) { + meta.file = file + } + if (callSource) { + meta.callSource = callSource + } + if (suiteName) { + meta.suiteName = suiteName + } + if (suiteCallSource) { + meta.suiteCallSource = suiteCallSource + } + if (typeof attempt === 'number') { + meta.attempt = attempt + } + return meta +} + +type ResolvedTestMeta = { file?: string; callSource?: string; attempt?: number } + +function resolveStartMeta( + meta: StartTestMeta, + file: string | undefined, + callSource: string | undefined +): ResolvedTestMeta { + const resolved: ResolvedTestMeta = {} + if (file) { + resolved.file = file + } + if (callSource && callSource !== 'unknown:0') { + resolved.callSource = callSource + } + if (typeof meta.attempt === 'number') { + resolved.attempt = meta.attempt + } + return resolved +} + export function startTest( ctx: TestManagementCtx, name: string, @@ -74,13 +123,7 @@ export function startTest( const stackInfo = getCallSourceFromStack() const file = meta.file || stackInfo.filePath const callSource = meta.callSource || stackInfo.callSource - const resolvedMeta: { file?: string; callSource?: string } = {} - if (file) { - resolvedMeta.file = file - } - if (callSource && callSource !== 'unknown:0') { - resolvedMeta.callSource = callSource - } + const resolvedMeta = resolveStartMeta(meta, file, callSource) if (!ctx.suiteManager || !ctx.testReporter) { ctx.pendingTestActions.push({ kind: 'start', diff --git a/packages/selenium-devtools/src/types.ts b/packages/selenium-devtools/src/types.ts index e801799d..af764d69 100644 --- a/packages/selenium-devtools/src/types.ts +++ b/packages/selenium-devtools/src/types.ts @@ -20,31 +20,13 @@ export { type TraceRetentionPolicy } from '@wdio/devtools-shared' -export interface DevToolsOptions { - port?: number - hostname?: string +export interface DevToolsOptions extends BaseDevToolsOptions { /** Open a Chrome window pointing at the UI. Default true. */ openUi?: boolean - /** `live` (default) launches the DevTools UI; `trace` skips it. Overrides `openUi`. */ - mode?: DevToolsMode - /** Trace output layout — `zip` (default) writes a single archive, - * `ndjson-directory` unpacks into `trace-/`. Only applies in trace mode. */ - traceFormat?: TraceFormat - /** Trace output granularity — `session` (default) writes one trace per - * worker session; `spec` writes one trace per spec file. Only applies in - * trace mode. */ - traceGranularity?: TraceGranularity - /** Trace retention policy — gates which traces are kept (e.g. - * `retain-on-failure`). Default `on` (keep all). Only applies in trace mode. */ - tracePolicy?: TraceRetentionPolicy /** Capture screenshots after each command. Default true. */ captureScreenshots?: boolean - /** Capture node:assert assertions as first-class commands. Default true. */ - captureAssertions?: boolean /** Command template for per-test rerun. {{testName}} is substituted. */ rerunCommand?: string - /** Per-session screencast recording. Disabled by default. */ - screencast?: ScreencastOptions /** * Force the *test* browser into headless mode by injecting --headless=new * into Chrome capabilities. The dashboard window (auto-opened by openUi) @@ -55,13 +37,7 @@ export interface DevToolsOptions { // ScreencastFrame, ScreencastOptions hoisted to @wdio/devtools-shared; re-exported // here for backwards compatibility with existing selenium-internal imports. -import type { - DevToolsMode, - ScreencastOptions, - TraceFormat, - TraceGranularity, - TraceRetentionPolicy -} from '@wdio/devtools-shared' +import type { BaseDevToolsOptions, TestStatus } from '@wdio/devtools-shared' export type { ScreencastFrame, ScreencastOptions } from '@wdio/devtools-shared' /** @@ -133,24 +109,16 @@ export interface ElementOriginals { getTagName?: (element: unknown) => Promise } -// ─── bidi ─────────────────────────────────────────────────────────────────── - -import type { ConsoleLog, NetworkRequest } from '@wdio/devtools-shared' - -export interface BidiHandlerSinks { - pushConsoleLog: (entry: ConsoleLog) => void - pushNetworkRequest: (entry: NetworkRequest) => void - replaceNetworkRequest: (id: string, entry: NetworkRequest) => void -} - // ─── runnerHooks ──────────────────────────────────────────────────────────── export interface MochaTestCtx { title?: string file?: string - state?: 'passed' | 'failed' | 'pending' | 'running' | 'skipped' + state?: TestStatus duration?: number parent?: { title?: string } + /** Mocha's authoritative retry index — 0 on the first run, +1 per retry. */ + _currentRetry?: number } export interface RunnerHookCallbacks { @@ -159,9 +127,12 @@ export interface RunnerHookCallbacks { file?: string, callSource?: string, suiteName?: string, - suiteCallSource?: string + suiteCallSource?: string, + // Authoritative per-test retry index when the runner exposes one (Mocha's + // `_currentRetry`); undefined leaves the heuristic tracker to decide. + attempt?: number ) => void - onTestEnd: (state: 'passed' | 'failed' | 'skipped' | 'pending') => void + onTestEnd: (state: Exclude) => void // Cucumber-only: scenario boundary creates a sub-suite under the feature // rootSuite; subsequent onTestStart/onTestEnd attach as Gherkin steps inside. onScenarioStart?: ( @@ -171,7 +142,7 @@ export interface RunnerHookCallbacks { featureName?: string, featureCallSource?: string ) => void - onScenarioEnd?: (state: 'passed' | 'failed' | 'skipped' | 'pending') => void + onScenarioEnd?: (state: Exclude) => void // Fires from the runner's after-all hook so the dashboard suite header // updates without waiting for process exit. onTestRunComplete?: (summary: { diff --git a/packages/service/src/index.ts b/packages/service/src/index.ts index cf984f20..ca73e0a2 100644 --- a/packages/service/src/index.ts +++ b/packages/service/src/index.ts @@ -8,15 +8,23 @@ import { mapCommandToAction, recordSpecBoundary, resolveAdapterOutputDir, + TestAttemptTracker, tracePolicyModeWarning, type SpecRange, type TraceExportContext } from '@wdio/devtools-core' -import { captureExpectFailure, wireAssertCapture } from './assert-capture.js' +import { + captureExpectFailure, + expectAssertionToCommandLog, + wireAssertCapture, + type ExpectAssertion +} from './assert-capture.js' import { cucumberScenarioUid, + resolveTestAttempt, stampTestState, - testMetadataUid + testMetadataUid, + type TestOutcomeResult } from './test-metadata.js' import { resolveCallSourceFromFrame } from './call-source.js' import { @@ -100,6 +108,11 @@ export default class DevToolsHookService implements Services.ServiceInstance { /** Map of testUid → metadata for trace group events and per-spec partitioning. */ #testMetadata: TestMetadataMap = new Map() + /** Per-test attempt counter. specFileRetries spawns a fresh worker (hence a + * fresh instance) per retry, so this only reflects same-process retries + * (Mocha this.retries(n)); cross-worker attempts rely on the WDIO result. */ + #attemptTracker = new TestAttemptTracker() + /** Index ranges into the session capturer's flat arrays, one per spec file. */ #specRanges: SpecRange[] = [] @@ -159,6 +172,7 @@ export default class DevToolsHookService implements Services.ServiceInstance { sessionId: browser.sessionId, capabilities: browser.capabilities, testMetadata: this.#testMetadata, + attemptInfoAvailable: true, ranges: this.#specRanges, flushed: this.#flushedSpecs, resolveOutputDir: () => this.#outputDir, @@ -306,6 +320,7 @@ export default class DevToolsHookService implements Services.ServiceInstance { world?.pickle?.astNodeIds ) this.#currentTestUid = uid + this.#attemptTracker.recordStart(uid) this.#testMetadata.set(uid, { title: scenarioName, specFile: featureFile @@ -327,6 +342,7 @@ export default class DevToolsHookService implements Services.ServiceInstance { if (testTitle) { const uid = testMetadataUid(test?.file, testTitle) this.#currentTestUid = uid + this.#attemptTracker.recordStart(uid) this.#testMetadata.set(uid, { title: testTitle, specFile: test?.file ?? '' @@ -355,16 +371,23 @@ export default class DevToolsHookService implements Services.ServiceInstance { ) } + /** Stamp final state + the resolved 0-based attempt onto the test's metadata + * entry, taking the max of the tracker count and WDIO's retry field. */ + #stampOutcome(uid: string, result?: TestOutcomeResult): void { + const fallback = this.#attemptTracker.attemptFor(uid) ?? 0 + const attempt = resolveTestAttempt(result, fallback) + stampTestState(this.#testMetadata, uid, result, attempt) + } + async afterScenario( world?: { pickle?: { uri?: string; name?: string; astNodeIds?: readonly string[] } }, - result?: { error?: unknown; passed?: boolean; skipped?: boolean } + result?: TestOutcomeResult ) { const { uri, name, astNodeIds } = world?.pickle ?? {} if (uri && name) { - const uid = cucumberScenarioUid(uri, name, astNodeIds) - stampTestState(this.#testMetadata, uid, result) + this.#stampOutcome(cucumberScenarioUid(uri, name, astNodeIds), result) } await this.#finalizePerScenario() } @@ -372,20 +395,28 @@ export default class DevToolsHookService implements Services.ServiceInstance { async afterTest( test?: { file?: string; title?: string; fullTitle?: string }, _context?: unknown, - result?: { error?: unknown; passed?: boolean; skipped?: boolean } + result?: TestOutcomeResult ) { this.#captureExpectFailure(result?.error) const testTitle = test?.fullTitle || test?.title if (testTitle) { - stampTestState( - this.#testMetadata, - testMetadataUid(test?.file, testTitle), - result - ) + this.#stampOutcome(testMetadataUid(test?.file, testTitle), result) } await this.#finalizePerScenario() } + /** expect-webdriverio fires this per matcher (pass or fail) via WDIO's + * assertion hook — capture it as an `expect.` action so passing + * assertions show in the trace, not just failures. */ + afterAssertion(params: ExpectAssertion): void { + if (this.#options.captureAssertions === false) { + return + } + this.#sessionCapturer.captureAssertCommand( + expectAssertionToCommandLog(params, this.#currentTestUid) + ) + } + async #finalizePerScenario() { if (this.#options.mode !== 'trace' || !this.#browser) { return diff --git a/packages/service/src/reporter.ts b/packages/service/src/reporter.ts index 801a86ad..ee22f23b 100644 --- a/packages/service/src/reporter.ts +++ b/packages/service/src/reporter.ts @@ -314,6 +314,10 @@ export class TestReporter extends WebdriverIOReporter { matcherResult: rawErr.matcherResult } as Error } + // WDIO stamps the 0-based attempt on each test:start, so the final attempt's + // stat already carries the retry count; the field is optional upstream, so + // pin it to a number for the shared TestStats.retries contract. + testStats.retries = testStats.retries ?? 0 this.#sendUpstream() } diff --git a/packages/service/src/test-metadata.ts b/packages/service/src/test-metadata.ts index 339e1dc1..dc2babad 100644 --- a/packages/service/src/test-metadata.ts +++ b/packages/service/src/test-metadata.ts @@ -4,6 +4,15 @@ import { deterministicUid } from '@wdio/devtools-core' import type { TestMetadataMap, TestStatus } from '@wdio/devtools-shared' +/** The subset of a WDIO afterTest/afterScenario hook result this adapter reads: + * pass/skip state, the error, and WDIO's authoritative 0-based retry count. */ +export interface TestOutcomeResult { + error?: unknown + passed?: boolean + skipped?: boolean + retries?: { attempts?: number } +} + /** Stable per-test key. beforeTest and afterTest derive it identically so keys * match. File-less runners (some non-WDIO frameworks) key on the title alone. */ export function testMetadataUid( @@ -37,15 +46,32 @@ export function resultToState(result: { return result.passed ? 'passed' : 'failed' } -/** Stamp the final state onto the metadata entry beforeTest/beforeScenario - * created, so retention can gate its trace. No-op when there's no entry. */ +/** Stamp the final state (and, when known, the 0-based attempt) onto the + * metadata entry beforeTest/beforeScenario created, so retention can gate its + * trace per attempt. No-op when there's no entry. */ export function stampTestState( metadata: TestMetadataMap, uid: string, - result?: { passed?: boolean; skipped?: boolean } + result?: Pick, + attempt?: number ): void { const entry = result && metadata.get(uid) if (entry) { entry.state = resultToState(result) + if (attempt !== undefined) { + entry.attempt = attempt + } } } + +/** Resolve the 0-based attempt for a test. WDIO's mocha framework reports + * `retries.attempts` as 0 even on a retry, so it can't override the in-process + * tracker — take the max so a present-but-zero runner field never clobbers a + * real retry count, while a genuine runner value still wins when it's higher. */ +export function resolveTestAttempt( + result: Pick | undefined, + fallback: number +): number { + const attempts = result?.retries?.attempts + return Math.max(typeof attempts === 'number' ? attempts : 0, fallback) +} diff --git a/packages/service/tests/trace-metadata.test.ts b/packages/service/tests/trace-metadata.test.ts index fe88dc1a..9b2ee653 100644 --- a/packages/service/tests/trace-metadata.test.ts +++ b/packages/service/tests/trace-metadata.test.ts @@ -161,6 +161,57 @@ describe('DevtoolsService - afterTest state stamping', () => { ).toBe('failed') }) + it('flags attemptInfoAvailable so retry-aware policies use per-test attempt', async () => { + const ctx = (await runTest('trace opts', { passed: true })) as { + attemptInfoAvailable?: boolean + } + expect(ctx.attemptInfoAvailable).toBe(true) + }) + + it('uses the tracker attempt even when WDIO reports retries.attempts:0', async () => { + const service = new DevToolsHookService({ + mode: 'trace', + tracePolicy: 'retain-on-failure-and-retries' + }) + await service.before({} as never, [], mockBrowser) + const title = 'flaky login' + // Two starts before a single end simulate a same-process (Mocha) retry. + // WDIO's mocha framework reports retries.attempts:0 even on the retry, so + // the runner field must NOT clobber the tracker's real count of 1. + service.beforeTest({ file, fullTitle: title }) + service.beforeTest({ file, fullTitle: title }) + await service.afterTest( + { file, fullTitle: title }, + {}, + { passed: true, retries: { attempts: 0 } } + ) + await service.after() + const ctx = finalizeTraceExport.mock.calls.at(-1)?.[0] as { + testMetadata: Map + } + expect(ctx.testMetadata.get(deterministicUid(file, title))?.attempt).toBe(1) + }) + + it('uses the runner attempts count when it exceeds the tracker', async () => { + const service = new DevToolsHookService({ + mode: 'trace', + tracePolicy: 'retain-on-failure-and-retries' + }) + await service.before({} as never, [], mockBrowser) + const title = 'checkout retries' + service.beforeTest({ file, fullTitle: title }) + await service.afterTest( + { file, fullTitle: title }, + {}, + { passed: true, retries: { attempts: 3 } } + ) + await service.after() + const ctx = finalizeTraceExport.mock.calls.at(-1)?.[0] as { + testMetadata: Map + } + expect(ctx.testMetadata.get(deterministicUid(file, title))?.attempt).toBe(3) + }) + it('stamps a Cucumber scenario state in afterScenario', async () => { const service = new DevToolsHookService({ mode: 'trace', From ce5c673a4e173be941584614bc26ceb50dac619f Mon Sep 17 00:00:00 2001 From: Vishnu Vardhan Date: Thu, 9 Jul 2026 13:48:48 +0530 Subject: [PATCH 24/68] feat(service,core): render expect-webdriverio matchers as trace actions --- packages/core/src/assert-patcher.ts | 40 ++++++++ packages/core/tests/assert-patcher.test.ts | 40 ++++++++ .../tests/attempt-capture.test.ts | 95 +++++++++++++++++++ packages/service/src/assert-capture.ts | 54 ++++++++++- 4 files changed, 224 insertions(+), 5 deletions(-) create mode 100644 packages/selenium-devtools/tests/attempt-capture.test.ts diff --git a/packages/core/src/assert-patcher.ts b/packages/core/src/assert-patcher.ts index 8917a1ef..71ebd9fe 100644 --- a/packages/core/src/assert-patcher.ts +++ b/packages/core/src/assert-patcher.ts @@ -2,6 +2,7 @@ import { createRequire } from 'node:module' import { TRACKED_ASSERT_METHODS, type CommandLog } from '@wdio/devtools-shared' import { getCallSourceFromStack } from './stack.js' import { toError } from './error.js' +import { stripAnsi } from './console.js' export { TRACKED_ASSERT_METHODS } @@ -152,6 +153,45 @@ export function capturedAssertToCommandLog( return entry } +/** + * Params any adapter's matcher tap produces. `prefix` selects the command + * namespace (`expect` / `assert` / `verify`), all of which map to an `Assert` + * action via the shared action map. Adapters do the thin framework-specific + * extraction (matcher name, args, pass flag, message); this conversion is the + * single shared path — the generic counterpart to `capturedAssertToCommandLog` + * for libraries that aren't node:assert (expect-webdriverio, chai, Nightwatch). + */ +export interface MatcherAssertion { + prefix?: string + method: string + args?: unknown[] + passed: boolean + message?: string | (() => string) + callSource?: string +} + +export function matcherAssertionToCommandLog( + input: MatcherAssertion, + testUid?: string +): CommandLog { + const command = `${input.prefix ?? 'expect'}.${input.method}` + const message = + typeof input.message === 'function' ? input.message() : input.message + return capturedAssertToCommandLog( + { + command, + args: (input.args ?? []).map(safeSerializeAssertArg), + result: input.passed ? 'passed' : undefined, + error: input.passed + ? undefined + : new Error(stripAnsi(message ?? `${command} failed`)), + callSource: input.callSource, + timestamp: Date.now() + }, + testUid + ) +} + /** * Patch `node:assert` so each tracked method emits a `CapturedAssert` to the * supplied hook. Idempotent across calls (guarded by `ASSERT_PATCHED_SYMBOL`). diff --git a/packages/core/tests/assert-patcher.test.ts b/packages/core/tests/assert-patcher.test.ts index 86774967..38bea721 100644 --- a/packages/core/tests/assert-patcher.test.ts +++ b/packages/core/tests/assert-patcher.test.ts @@ -4,6 +4,7 @@ import { ASSERT_PATCHED_SYMBOL, TRACKED_ASSERT_METHODS, capturedAssertToCommandLog, + matcherAssertionToCommandLog, patchNodeAssert, safeSerializeAssertArg, type CapturedAssert @@ -245,3 +246,42 @@ describe('capturedAssertToCommandLog', () => { expect(entry.testUid).toBeUndefined() }) }) + +describe('matcherAssertionToCommandLog', () => { + it('builds a passing expect. command (default prefix)', () => { + const entry = matcherAssertionToCommandLog( + { method: 'toHaveTitle', args: ['The Internet'], passed: true }, + 'uid-1' + ) + expect(entry).toMatchObject({ + command: 'expect.toHaveTitle', + args: ['The Internet'], + result: 'passed', + testUid: 'uid-1' + }) + expect(entry.error).toBeUndefined() + }) + + it('carries the ANSI-stripped message as the error when failed', () => { + const entry = matcherAssertionToCommandLog({ + method: 'toHaveText', + args: ['foo'], + passed: false, + message: () => 'expected foo' + }) + expect(entry.result).toBeUndefined() + expect(entry.error).toMatchObject({ message: 'expected foo' }) + }) + + it('honors a non-default prefix and sanitizes args', () => { + const entry = matcherAssertionToCommandLog({ + prefix: 'verify', + method: 'equal', + args: [/re/, () => 1], + passed: true + }) + expect(entry.command).toBe('verify.equal') + // RegExp → string, function → '[Function]' (via safeSerializeAssertArg). + expect(entry.args).toEqual(['/re/', '[Function]']) + }) +}) diff --git a/packages/selenium-devtools/tests/attempt-capture.test.ts b/packages/selenium-devtools/tests/attempt-capture.test.ts new file mode 100644 index 00000000..255280f6 --- /dev/null +++ b/packages/selenium-devtools/tests/attempt-capture.test.ts @@ -0,0 +1,95 @@ +import { describe, it, expect, vi } from 'vitest' +import { collectSuiteTestMetadata } from '@wdio/devtools-core' + +import { resetSignatureCounters } from '../src/helpers/utils.js' +import { TestManager } from '../src/helpers/testManager.js' +import { SuiteManager } from '../src/helpers/suiteManager.js' +import { TestReporter } from '../src/reporter.js' +import { SessionCapturer } from '../src/session.js' +import { + buildTraceExportContext, + type SessionLifecycleCtx +} from '../src/session-lifecycle.js' + +function makeManager() { + resetSignatureCounters() + const reporter = new TestReporter(vi.fn()) + const suiteManager = new SuiteManager(reporter) + const rootSuite = suiteManager.getOrCreateRootSuite('/spec.ts', 'Suite') + const mgr = new TestManager(rootSuite, reporter, suiteManager) + return { mgr, suiteManager, rootSuite } +} + +describe('retry/attempt capture', () => { + it('re-entering the start hook for the same test increments retries (heuristic)', () => { + const { mgr } = makeManager() + + const first = mgr.startMarkedTest('flaky') + expect(first.retries).toBe(0) + + mgr.endCurrent('failed') + const retry = mgr.startMarkedTest('flaky') + expect(retry.retries).toBe(1) + + mgr.endCurrent('failed') + const retry2 = mgr.startMarkedTest('flaky') + expect(retry2.retries).toBe(2) + }) + + it('prefers the authoritative Mocha attempt over the heuristic', () => { + const { mgr } = makeManager() + + // First start: heuristic would be 0, authoritative says 2 → 2 wins. + const t = mgr.startMarkedTest('login', { attempt: 2 }) + expect(t.retries).toBe(2) + + mgr.endCurrent('failed') + // Re-entry: heuristic would be 1, authoritative says 5 → 5 wins. + const retry = mgr.startMarkedTest('login', { attempt: 5 }) + expect(retry.retries).toBe(5) + }) + + it('flows retries into metadata attempt and the trace ctx flags attempt info', () => { + const { mgr, suiteManager, rootSuite } = makeManager() + + mgr.startMarkedTest('flaky') + mgr.endCurrent('failed') + const retry = mgr.startMarkedTest('flaky') + mgr.endCurrent('passed') + expect(retry.retries).toBe(1) + + const metadata = collectSuiteTestMetadata([rootSuite]) + expect(metadata.get(retry.uid)?.attempt).toBe(1) + + const capturer = new SessionCapturer() + try { + // Minimal structural ctx: buildTraceExportContext only reads options, + // suiteManager and the trace accumulators, so we cast a partial to the + // full lifecycle interface rather than standing up the whole plugin. + const ctx = { + options: { + mode: 'trace', + tracePolicy: 'retain-on-failure-and-retries', + traceGranularity: 'session', + traceFormat: 'zip' + }, + suiteManager, + actionSnapshots: [], + specRanges: [], + flushedSpecs: new Set(), + snapshotCaptures: [] + } as unknown as SessionLifecycleCtx + + const traceCtx = buildTraceExportContext( + ctx, + capturer, + 'sess-1', + '/spec.ts' + ) + expect(traceCtx.attemptInfoAvailable).toBe(true) + expect(traceCtx.testMetadata.get(retry.uid)?.attempt).toBe(1) + } finally { + capturer.cleanup() + } + }) +}) diff --git a/packages/service/src/assert-capture.ts b/packages/service/src/assert-capture.ts index 43c75131..2c277cf8 100644 --- a/packages/service/src/assert-capture.ts +++ b/packages/service/src/assert-capture.ts @@ -4,10 +4,11 @@ import logger from '@wdio/logger' import { capturedAssertToCommandLog, + matcherAssertionToCommandLog, patchNodeAssert, stripAnsi } from '@wdio/devtools-core' -import type { SerializedError } from '@wdio/devtools-shared' +import type { CommandLog, SerializedError } from '@wdio/devtools-shared' import type { SessionCapturer } from './session.js' const log = logger('@wdio/devtools-service:assert-capture') @@ -34,8 +35,10 @@ export function wireAssertCapture( * Normalize a framework failure into a SerializedError. Cucumber hands a plain * message string (@wdio/cucumber-framework getResultObject → world.result.message); * Mocha/Jasmine hand an Error object. Returns undefined when there's nothing to - * show, or for a node:assert AssertionError (the patcher already made that its - * own command). ANSI is stripped so every consumer gets a clean message. + * show, or when the failure was already captured as its own command: a + * node:assert AssertionError (via the patcher) or an expect-webdriverio matcher + * error (via afterAssertion — it carries `matcherResult`). Skipping those keeps + * `failLastAction` from double-marking a passing command. ANSI is stripped. */ export function toCommandError(error: unknown): SerializedError | undefined { if (!error) { @@ -48,8 +51,13 @@ export function toCommandError(error: unknown): SerializedError | undefined { if (typeof error !== 'object') { return undefined } - const err = error as { name?: string; message?: string; stack?: string } - if (err.name === 'AssertionError') { + const err = error as { + name?: string + message?: string + stack?: string + matcherResult?: unknown + } + if (err.name === 'AssertionError' || err.matcherResult !== undefined) { return undefined } return { @@ -79,3 +87,39 @@ export function captureExpectFailure( capturer.failLastAction(testUid, commandError) } } + +/** The subset of expect-webdriverio's afterAssertion hook params we read to + * turn a matcher call into a trace command. The matcher passes `{ pass }` at + * runtime, but @wdio/types declares `{ result }`, so we accept and read both. */ +export interface ExpectAssertion { + matcherName: string + expectedValue?: unknown + result: { pass?: boolean; result?: boolean; message?: () => string } +} + +/** + * Adapt expect-webdriverio's afterAssertion params to the shared matcher + * converter. Framework-specific extraction only (matcher name, expectedValue → + * args, the runtime `pass` vs typed `result` flag); the actual CommandLog + * shaping lives once in core's `matcherAssertionToCommandLog`. + */ +export function expectAssertionToCommandLog( + params: ExpectAssertion, + testUid: string | undefined +): CommandLog { + const { matcherName, expectedValue, result } = params + return matcherAssertionToCommandLog( + { + method: matcherName, + args: + expectedValue === undefined + ? [] + : Array.isArray(expectedValue) + ? expectedValue + : [expectedValue], + passed: result.pass ?? result.result ?? false, + message: result.message + }, + testUid + ) +} From 7c870336e7d77a8b4f8e93d4a6a9dfef8b2de898 Mon Sep 17 00:00:00 2001 From: Vishnu Vardhan Date: Thu, 9 Jul 2026 13:49:14 +0530 Subject: [PATCH 25/68] test(backend): remove skip-forever foreign-zip tests --- .../backend/tests/trace-reader-bodies.test.ts | 27 +----------- packages/backend/tests/trace-reader.test.ts | 43 +------------------ 2 files changed, 2 insertions(+), 68 deletions(-) diff --git a/packages/backend/tests/trace-reader-bodies.test.ts b/packages/backend/tests/trace-reader-bodies.test.ts index 947daf38..62f2cec3 100644 --- a/packages/backend/tests/trace-reader-bodies.test.ts +++ b/packages/backend/tests/trace-reader-bodies.test.ts @@ -1,8 +1,7 @@ import { createHash } from 'node:crypto' -import { existsSync } from 'node:fs' import { describe, it, expect } from 'vitest' import { zipSync, strToU8 } from 'fflate' -import { parseTraceZip, readTraceZip } from '../src/trace-reader.js' +import { parseTraceZip } from '../src/trace-reader.js' const sha1 = (data: string): string => createHash('sha1').update(data).digest('hex') @@ -142,27 +141,3 @@ describe('trace-reader response bodies', () => { expect(bodyByUrl('https://example.com/missing')).toBeUndefined() }) }) - -// Machine-local foreign zip; exercises the extension-suffixed _sha1 convention -// against a real archive when present. -const REAL_FOREIGN_ZIP = - '/Users/vishnu.p@browserstack.com/Documents/Test projects/Test-playwright/test-results/trace-features-trace-feature-showcase-chromium-retry2/trace.zip' - -describe('foreign zip response bodies', () => { - it.skipIf(!existsSync(REAL_FOREIGN_ZIP))( - 'restores bodies referenced via extension-suffixed _sha1 entries', - async () => { - const data = await readTraceZip(REAL_FOREIGN_ZIP) - const requests = data.trace.networkRequests - const apiRequest = requests.find((r) => - r.url.startsWith('https://api.github.com/') - ) - expect(apiRequest?.responseBody).toContain('"full_name"') - const css = requests.find((r) => r.url.endsWith('base.css')) - expect(css?.responseBody?.length).toBeGreaterThan(0) - // Entries without a _sha1 ref stay body-less. - const script = requests.find((r) => r.url.endsWith('.js')) - expect(script?.responseBody).toBeUndefined() - } - ) -}) diff --git a/packages/backend/tests/trace-reader.test.ts b/packages/backend/tests/trace-reader.test.ts index be736c90..25f9a2f9 100644 --- a/packages/backend/tests/trace-reader.test.ts +++ b/packages/backend/tests/trace-reader.test.ts @@ -1,12 +1,11 @@ import { createHash } from 'node:crypto' -import { existsSync } from 'node:fs' import { describe, it, expect } from 'vitest' import { zipSync, strToU8 } from 'fflate' import type { TraceActionChild, TraceActionGroupNode } from '@wdio/devtools-shared' -import { parseTraceZip, readTraceZip } from '../src/trace-reader.js' +import { parseTraceZip } from '../src/trace-reader.js' import { buildSources, stackToCallSource } from '../src/trace-reader-utils.js' import type { BeforeEvent } from '../src/trace-reader-types.js' @@ -598,43 +597,3 @@ describe('glued callSource recovery from older zips', () => { expect(sources).toEqual({ [clean]: 'glued source' }) }) }) - -// Manual verification against a real foreign zip; skipped where absent. -const FOREIGN_ZIP_PATH = - process.env.FOREIGN_TRACE_ZIP ?? - '/Users/vishnu.p@browserstack.com/Documents/Test projects/Test-playwright/test-results/trace-features-trace-feature-showcase-chromium/trace.zip' - -describe('readTraceZip against a real foreign zip', () => { - it.skipIf(!existsSync(FOREIGN_ZIP_PATH))( - 'renders actions, filmstrip, network and a sane duration', - async () => { - const { trace, frames, startTime, duration, groups } = - await readTraceZip(FOREIGN_ZIP_PATH) - expect(trace.commands.length).toBeGreaterThan(10) - expect(frames.length).toBeGreaterThan(10) - expect(frames.every((f) => f.screenshot.length > 0)).toBe(true) - expect(startTime).toBeGreaterThan(1_700_000_000_000) - expect(duration).toBeGreaterThan(5_000) - expect(duration).toBeLessThan(60_000) - for (const frame of frames) { - expect(frame.timestamp).toBeGreaterThanOrEqual(startTime) - expect(frame.timestamp).toBeLessThanOrEqual(startTime + duration) - } - expect(trace.networkRequests.length).toBeGreaterThan(0) - expect(trace.consoleLogs.length).toBeGreaterThanOrEqual(3) - expect(Object.keys(trace.sources).length).toBeGreaterThan(0) - expect(trace.commands.some((c) => c.error)).toBe(true) - // The action tree covers every command exactly once and flags failures. - const tree = allGroups(groups ?? []) - expect(tree.length).toBeGreaterThan(5) - expect([...commandIndices(groups ?? [])].sort((a, b) => a - b)).toEqual( - trace.commands.map((_, index) => index) - ) - const failed = tree.filter((group) => group.failed) - expect(failed.length).toBeGreaterThan(0) - expect(failed.some((g) => g.title.startsWith('Soft assertion'))).toBe( - true - ) - } - ) -}) From d6bcd38dd74f76b02b6087fbb299500b8cad7c65 Mon Sep 17 00:00:00 2001 From: Vishnu Vardhan Date: Thu, 9 Jul 2026 13:50:27 +0530 Subject: [PATCH 26/68] chore(examples): add WDIO retry harness for retry-aware trace policies --- examples/wdio/mocha/retry/flaky.e2e.ts | 19 +++++++++ examples/wdio/mocha/wdio.retry.conf.ts | 58 ++++++++++++++++++++++++++ package.json | 1 + 3 files changed, 78 insertions(+) create mode 100644 examples/wdio/mocha/retry/flaky.e2e.ts create mode 100644 examples/wdio/mocha/wdio.retry.conf.ts diff --git a/examples/wdio/mocha/retry/flaky.e2e.ts b/examples/wdio/mocha/retry/flaky.e2e.ts new file mode 100644 index 00000000..0b87bc83 --- /dev/null +++ b/examples/wdio/mocha/retry/flaky.e2e.ts @@ -0,0 +1,19 @@ +import { browser, expect } from '@wdio/globals' + +// Deterministically flaky: throws on the first attempt and passes on the retry. +// The module-level counter survives mocha's in-process retry, so attempt 0 fails +// and attempt 1 succeeds. The test ends PASSED — so retain-on-failure would drop +// its trace, but on-first-retry keeps it, which is exactly what B4 verifies: +// the retry attempt is captured and is distinct from a failure. +let attempts = 0 + +describe('Flaky (passes on retry)', () => { + it('fails the first attempt, then passes', async () => { + await browser.url('https://the-internet.herokuapp.com/login') + attempts += 1 + if (attempts === 1) { + throw new Error('intentional first-attempt failure — should retry') + } + await expect(browser).toHaveTitle('The Internet') + }) +}) diff --git a/examples/wdio/mocha/wdio.retry.conf.ts b/examples/wdio/mocha/wdio.retry.conf.ts new file mode 100644 index 00000000..90b87858 --- /dev/null +++ b/examples/wdio/mocha/wdio.retry.conf.ts @@ -0,0 +1,58 @@ +import type { Options } from '@wdio/types' + +// Disposable harness for verifying B4 (retry-aware trace policies). Runs the +// deterministically-flaky spec (fails once, passes on retry) alongside a clean +// passing spec at spec granularity. With tracePolicy 'on-first-retry' only the +// flaky spec's trace is retained — proving the retry attempt is captured and is +// distinct from failure (the flaky test ends PASSED, so retain-on-failure would +// drop it). Flip tracePolicy below to exercise the other retry-aware policies. +export const config: Options.Testrunner = { + runner: 'local', + autoCompileOpts: { + autoCompile: true, + tsNodeOpts: { + project: './tsconfig.json', + transpileOnly: true + } + }, + specs: ['./retry/flaky.e2e.ts', './specs/login.e2e.ts'], + exclude: [], + maxInstances: 1, + capabilities: [ + { + browserName: 'chrome', + 'goog:chromeOptions': { + args: [ + '--headless', + '--disable-gpu', + '--remote-allow-origins=*', + '--window-size=1600,900' + ] + } + } + ], + logLevel: 'warn', + bail: 0, + baseUrl: 'http://localhost', + waitforTimeout: 10000, + connectionRetryTimeout: 120000, + connectionRetryCount: 3, + services: [ + [ + 'devtools', + { + mode: 'trace' as const, + traceGranularity: 'spec' as const, + tracePolicy: 'on-first-retry' as const + } + ] + ], + framework: 'mocha', + reporters: ['spec'], + mochaOpts: { + ui: 'bdd', + timeout: 60000, + // 1 retry = 2 total attempts; the flaky spec fails attempt 0, passes attempt 1. + retries: 1 + } +} diff --git a/package.json b/package.json index 65bf3171..45fba2f4 100644 --- a/package.json +++ b/package.json @@ -5,6 +5,7 @@ "build": "pnpm -r build", "demo:wdio": "wdio run ./examples/wdio/cucumber/wdio.conf.ts", "demo:wdio:mocha": "wdio run ./examples/wdio/mocha/wdio.conf.ts", + "demo:wdio:retry": "wdio run ./examples/wdio/mocha/wdio.retry.conf.ts", "demo:nightwatch": "pnpm --filter @wdio/nightwatch-devtools example", "demo:selenium": "pnpm --filter @wdio/selenium-devtools example", "dev": "pnpm --parallel dev", From 015fb75d81faf474fa7825908731e7813985ffcc Mon Sep 17 00:00:00 2001 From: Vishnu Vardhan Date: Thu, 9 Jul 2026 13:51:49 +0530 Subject: [PATCH 27/68] docs: correct node:assert note; record retain-on-first-failure limitation --- .gitignore | 2 ++ CLAUDE.md | 3 ++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index e95e0003..47c58732 100644 --- a/.gitignore +++ b/.gitignore @@ -44,3 +44,5 @@ coverage/ # pnpm state, cache, logs, and debug files /packages/**/*.mjs + +.claude diff --git a/CLAUDE.md b/CLAUDE.md index 0416c6ae..f06ea24d 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -233,8 +233,9 @@ Documented divergences from the conventions above. They exist today as debt to b ### Architecture - `replaceCommand` has two semantics — Selenium mutates in place (preserves `_id`/`id` for chained calls); Nightwatch splices and reissues. Both call the same `core/suite-helpers` factories; the storage strategy stays adapter-specific because runner integrations differ. Could be unified by parameterizing the policy if the divergence ever causes a real problem. -- `patchNodeAssert` is wired only in `selenium-devtools`. The shared helper lives in `core/assert-patcher`; Service and Nightwatch can opt in via a one-line call when ready. Not auto-enabled — both communities lean on chai/expect. +- `patchNodeAssert` (via `core/assert-patcher`) is now wired in all three adapters, default-on behind each adapter's `captureAssertions` option (opt out with `captureAssertions: false`). Framework matcher libraries differ: Service additionally taps expect-webdriverio's `afterAssertion` hook so passing+failing matchers render as `expect.*` actions; Selenium (chai/jest) and Nightwatch (`browser.assert`/`verify`) don't yet surface passing framework-matcher assertions as trace actions. - BiDi is auto-attached in Service and Selenium; Nightwatch is opt-in via `bidi: true` and requires `webSocketUrl: true` in capabilities. +- Retry-aware trace policies aren't uniform across adapters. Selenium emits one metadata entry per attempt (counter-based `generateStableUid` uid + retry-stable tracker key), so all six policies — including `retain-on-first-failure` (needs the first attempt's outcome) — evaluate correctly. Service and Nightwatch key metadata on the retry-stable uid and overwrite/mutate per attempt, collapsing to the *final* attempt's outcome; the retry-count policies (`retain-on-failure`, `on-first-retry`, `on-all-retries`, `retain-on-failure-and-retries`) are still correct, but `retain-on-first-failure` can't see a failed-then-passed first attempt and under-retains. Proper fix rides with B5 (per-test granularity preserves per-attempt slices); until then all three set `attemptInfoAvailable: true`. ### File-size (raw line counts; soft cap is 500 logic lines) From 56e92ff2df330199631a4ed153861dc1cff11af2 Mon Sep 17 00:00:00 2001 From: Vishnu Vardhan Date: Thu, 9 Jul 2026 13:59:41 +0530 Subject: [PATCH 28/68] refactor(shared,adapters): consolidate adapter types into shared --- packages/nightwatch-devtools/src/types.ts | 41 ++-------------- .../src/runnerHooks/cucumber.ts | 18 +++---- packages/service/src/types.ts | 41 ++-------------- packages/shared/src/types.ts | 47 +++++++++++++++++++ 4 files changed, 59 insertions(+), 88 deletions(-) diff --git a/packages/nightwatch-devtools/src/types.ts b/packages/nightwatch-devtools/src/types.ts index e8ef90f6..ab776db5 100644 --- a/packages/nightwatch-devtools/src/types.ts +++ b/packages/nightwatch-devtools/src/types.ts @@ -23,13 +23,7 @@ export { type TraceLog } from '@wdio/devtools-shared' -import type { - DevToolsMode, - ScreencastOptions, - TraceFormat, - TraceGranularity, - TraceRetentionPolicy -} from '@wdio/devtools-shared' +import type { BaseDevToolsOptions } from '@wdio/devtools-shared' export interface CommandStackFrame { command: string @@ -87,16 +81,7 @@ export interface StepLocation { line: number } -export interface DevToolsOptions { - port?: number - hostname?: string - /** - * Screencast recording options. When enabled, a continuous video of the - * browser session is recorded and saved as a .webm file at the end of the - * test run. Polling mode only on Nightwatch (no CDP push); works on every - * browser Nightwatch supports. - */ - screencast?: ScreencastOptions +export interface DevToolsOptions extends BaseDevToolsOptions { /** * Enable WebDriver BiDi capture (browser console + JS exceptions + network * via `selenium-webdriver/bidi`). Requires `webSocketUrl: true` in your @@ -105,21 +90,6 @@ export interface DevToolsOptions { * entries. Defaults to `false` — opt-in. */ bidi?: boolean - /** Capture node:assert assertions as first-class commands (Nightwatch's - * built-in asserts already flow as commands). Default true. */ - captureAssertions?: boolean - /** `live` (default) launches the DevTools UI; `trace` skips it. */ - mode?: DevToolsMode - /** Trace output layout — `zip` (default) writes a single archive, - * `ndjson-directory` unpacks into `trace-/`. Only applies in trace mode. */ - traceFormat?: TraceFormat - /** Trace output granularity — `session` (default) writes one trace per - * worker session; `spec` writes one trace per spec file. Only applies in - * trace mode. */ - traceGranularity?: TraceGranularity - /** Trace retention policy — gates which traces are kept (e.g. - * `retain-on-failure`). Default `on` (keep all). Only applies in trace mode. */ - tracePolicy?: TraceRetentionPolicy } export interface NightwatchBrowser { @@ -144,12 +114,7 @@ export interface NightwatchBrowser { webdriver?: { host?: string } [key: string]: unknown } - currentTest?: { - name?: string - module?: string - group?: string - [key: string]: unknown - } + currentTest?: NightwatchCurrentTest results?: unknown queue?: unknown } diff --git a/packages/selenium-devtools/src/runnerHooks/cucumber.ts b/packages/selenium-devtools/src/runnerHooks/cucumber.ts index cffc6c76..86f8d3c2 100644 --- a/packages/selenium-devtools/src/runnerHooks/cucumber.ts +++ b/packages/selenium-devtools/src/runnerHooks/cucumber.ts @@ -1,6 +1,11 @@ import { createRequire } from 'node:module' import logger from '@wdio/logger' import { errorMessage } from '@wdio/devtools-core' +import type { + CucumberPickle, + CucumberPickleStep, + TestStatus +} from '@wdio/devtools-shared' import type { RunnerHookCallbacks } from '../types.js' const log = logger('@wdio/selenium-devtools:runnerHooks:cucumber') @@ -178,17 +183,6 @@ interface GherkinFeature { location?: { line?: number } children?: GherkinFeatureChild[] } -interface CucumberPickleStep { - text?: string - astNodeIds?: string[] - location?: { line?: number } -} -interface CucumberPickle { - name?: string - uri?: string - location?: { line?: number } - astNodeIds?: string[] -} interface CucumberTestCase { gherkinDocument?: { feature?: GherkinFeature } pickle?: CucumberPickle @@ -227,7 +221,7 @@ function populateGherkinIndex( } } -type ScenarioState = 'passed' | 'failed' | 'pending' +type ScenarioState = Extract function mapCucumberStatus(status: string): ScenarioState | 'skipped' { const s = status.toUpperCase() diff --git a/packages/service/src/types.ts b/packages/service/src/types.ts index e6322867..ce10a42e 100644 --- a/packages/service/src/types.ts +++ b/packages/service/src/types.ts @@ -21,15 +21,10 @@ export { type Viewport } from '@wdio/devtools-shared' +import type { BaseDevToolsOptions } from '@wdio/devtools-shared' + // ScreencastFrame, ScreencastOptions hoisted to @wdio/devtools-shared; re-exported // here for backwards compatibility with existing service-internal imports. -import type { - DevToolsMode, - ScreencastOptions, - TraceFormat, - TraceGranularity, - TraceRetentionPolicy -} from '@wdio/devtools-shared' export type { DevToolsMode, ScreencastFrame, @@ -43,16 +38,7 @@ export interface ExtendedCapabilities extends WebdriverIO.Capabilities { 'wdio:devtoolsOptions'?: ServiceOptions } -export interface ServiceOptions { - /** - * port to launch the application on (default: random) - */ - port?: number - /** - * hostname to launch the application on - * @default localhost - */ - hostname?: string +export interface ServiceOptions extends BaseDevToolsOptions { /** * capabilities used to launch the devtools application * @default @@ -65,27 +51,6 @@ export interface ServiceOptions { * } */ devtoolsCapabilities?: WebdriverIO.Capabilities - /** - * Screencast recording options. When enabled, a continuous video of the - * browser session is recorded and saved as a .webm file. Chrome/Chromium - * uses CDP push mode; all other browsers fall back to screenshot polling. - */ - screencast?: ScreencastOptions - /** Capture node:assert assertions (and failing `expect` matchers) as - * first-class commands. Default true. */ - captureAssertions?: boolean - /** `live` (default) launches the DevTools UI; `trace` skips it. */ - mode?: DevToolsMode - /** Trace output layout — `zip` (default) writes a single archive, - * `ndjson-directory` unpacks into `trace-/`. Only applies in trace mode. */ - traceFormat?: TraceFormat - /** Trace output granularity — `session` (default) writes one trace per - * worker session; `spec` writes one trace per spec file. Only applies in - * trace mode. */ - traceGranularity?: TraceGranularity - /** Trace retention policy — gates which traces are kept (e.g. - * `retain-on-failure`). Default `on` (keep all). Only applies in trace mode. */ - tracePolicy?: TraceRetentionPolicy } declare namespace WebdriverIO { diff --git a/packages/shared/src/types.ts b/packages/shared/src/types.ts index c2902e19..f90db146 100644 --- a/packages/shared/src/types.ts +++ b/packages/shared/src/types.ts @@ -283,6 +283,53 @@ export const SCREENCAST_DEFAULTS: Required = { pollIntervalMs: 200 } +/** + * Options every framework adapter accepts. Each adapter's own options interface + * extends this and adds only its framework-specific fields (e.g. WDIO's + * devtoolsCapabilities, Selenium's openUi, Nightwatch's bidi). + */ +export interface BaseDevToolsOptions { + /** Port to launch the application on (default: random). */ + port?: number + /** Hostname to launch the application on. @default localhost */ + hostname?: string + /** Screencast recording options. When enabled, a continuous video of the + * browser session is recorded and saved as a .webm file. */ + screencast?: ScreencastOptions + /** Capture node:assert assertions (and framework `expect` matchers where + * supported) as first-class commands. Default true. */ + captureAssertions?: boolean + /** `live` (default) launches the DevTools UI; `trace` skips it. */ + mode?: DevToolsMode + /** Trace output layout — `zip` (default) writes a single archive, + * `ndjson-directory` unpacks into `trace-/`. Only applies in trace mode. */ + traceFormat?: TraceFormat + /** Trace output granularity — `session` (default) writes one trace per + * worker session; `spec` writes one per spec file. Only applies in trace mode. */ + traceGranularity?: TraceGranularity + /** Trace retention policy — gates which traces are kept (e.g. + * `retain-on-failure`). Default `on` (keep all). Only applies in trace mode. */ + tracePolicy?: TraceRetentionPolicy +} + +/** Minimal Cucumber pickle-step shape — only the fields the adapters read. + * Cucumber's own types vary across versions, so we pin just these. */ +export interface CucumberPickleStep { + text?: string + astNodeIds?: string[] + location?: { line?: number } +} + +/** Minimal Cucumber pickle shape — only the fields the adapters read. `steps` + * is present only where the adapter walks step boundaries (Nightwatch). */ +export interface CucumberPickle { + name?: string + uri?: string + location?: { line?: number } + astNodeIds?: string[] + steps?: CucumberPickleStep[] +} + export interface Metadata { type: TraceType url?: string From 3e5203b4119092f0ea977fb2a7b529b48826c25a Mon Sep 17 00:00:00 2001 From: Vishnu Vardhan Date: Thu, 9 Jul 2026 14:00:03 +0530 Subject: [PATCH 29/68] test(service): cover expect-webdriverio matcher capture --- packages/service/tests/assert-capture.test.ts | 88 +++++++++++++++++-- 1 file changed, 82 insertions(+), 6 deletions(-) diff --git a/packages/service/tests/assert-capture.test.ts b/packages/service/tests/assert-capture.test.ts index 55ed1ee8..d1486257 100644 --- a/packages/service/tests/assert-capture.test.ts +++ b/packages/service/tests/assert-capture.test.ts @@ -6,19 +6,20 @@ import { } from '@wdio/devtools-core' import { captureExpectFailure, + expectAssertionToCommandLog, toCommandError, wireAssertCapture } from '../src/assert-capture.js' import type { SessionCapturer } from '../src/session.js' describe('toCommandError', () => { - it('normalizes a matcher Error object (ANSI stripped)', () => { - const error = Object.assign(new Error('expected 1 to be 2'), { - matcherResult: { matcherName: 'toBe', actual: 1, expected: 2 } - }) + it('normalizes a plain Error object (ANSI stripped)', () => { + // Matcher errors are skipped now (afterAssertion owns them); a plain + // thrown Error still routes through failLastAction, so test that path. + const error = new Error('something broke') expect(toCommandError(error)).toMatchObject({ name: 'Error', - message: 'expected 1 to be 2' + message: 'something broke' }) }) @@ -30,11 +31,17 @@ describe('toCommandError', () => { }) }) - it('returns undefined for node:assert AssertionError, empties and non-errors', () => { + it('returns undefined for self-captured errors (AssertionError / matcher), empties and non-errors', () => { const assertionError = Object.assign(new Error('a !== b'), { name: 'AssertionError' }) expect(toCommandError(assertionError)).toBeUndefined() + // expect-webdriverio matcher errors carry matcherResult and are already + // captured by afterAssertion — must not double-mark via failLastAction. + const matcherError = Object.assign(new Error('expected a to be b'), { + matcherResult: { pass: false } + }) + expect(toCommandError(matcherError)).toBeUndefined() expect(toCommandError(' ')).toBeUndefined() expect(toCommandError(undefined)).toBeUndefined() expect(toCommandError(42)).toBeUndefined() @@ -65,6 +72,17 @@ describe('captureExpectFailure', () => { captureExpectFailure(capturer, 'test-1', undefined, true) expect(capturer.failLastAction).not.toHaveBeenCalled() }) + + it('does not mark an action for a self-captured matcher error', () => { + const capturer = fakeCapturer() + // A failing expect-webdriverio matcher (carries matcherResult) is already + // its own command via afterAssertion — failLastAction must stay off it. + const matcherError = Object.assign(new Error('expected'), { + matcherResult: { pass: false } + }) + captureExpectFailure(capturer, 'test-1', matcherError, true) + expect(capturer.failLastAction).not.toHaveBeenCalled() + }) }) describe('wireAssertCapture', () => { @@ -121,3 +139,61 @@ describe('wireAssertCapture', () => { expect(spy).not.toHaveBeenCalled() }) }) + +describe('expectAssertionToCommandLog', () => { + it('captures a passing matcher as an expect. command', () => { + const entry = expectAssertionToCommandLog( + { + matcherName: 'toHaveTitle', + expectedValue: 'The Internet', + result: { pass: true, message: () => 'ok' } + }, + 'uid-1' + ) + expect(entry).toMatchObject({ + command: 'expect.toHaveTitle', + args: ['The Internet'], + result: 'passed', + testUid: 'uid-1' + }) + expect(entry.error).toBeUndefined() + }) + + it('captures a failing matcher with its ANSI-stripped message as the error', () => { + const entry = expectAssertionToCommandLog( + { + matcherName: 'toHaveText', + expectedValue: 'foo', + result: { pass: false, message: () => 'expected foo' } + }, + undefined + ) + expect(entry.result).toBeUndefined() + expect(entry.error).toMatchObject({ message: 'expected foo' }) + }) + + it('spreads an array expectedValue and reads the typed `result` flag', () => { + // @wdio/types declares the pass flag on `result`, not `pass` — read both. + const entry = expectAssertionToCommandLog( + { + matcherName: 'toHaveAttribute', + expectedValue: ['href', '/x'], + result: { result: true } + }, + undefined + ) + expect(entry).toMatchObject({ + command: 'expect.toHaveAttribute', + args: ['href', '/x'], + result: 'passed' + }) + }) + + it('treats a matcher with no expectedValue as a no-arg assertion', () => { + const entry = expectAssertionToCommandLog( + { matcherName: 'toBeClickable', result: { pass: true } }, + undefined + ) + expect(entry).toMatchObject({ command: 'expect.toBeClickable', args: [] }) + }) +}) From 4d1c5699e63c7f83b0a7de183d60e3f73d8dd19c Mon Sep 17 00:00:00 2001 From: Vishnu Vardhan Date: Thu, 9 Jul 2026 16:32:07 +0530 Subject: [PATCH 30/68] feat(core): per-test trace granularity foundation --- packages/core/src/spec-trace-helpers.ts | 188 ++++++++++++++---- packages/core/src/trace-finalizer.ts | 62 ++++-- .../core/tests/spec-trace-helpers.test.ts | 129 ++++++++++++ packages/core/tests/trace-finalizer.test.ts | 104 ++++++++++ 4 files changed, 424 insertions(+), 59 deletions(-) diff --git a/packages/core/src/spec-trace-helpers.ts b/packages/core/src/spec-trace-helpers.ts index e49f2bd6..dd091efb 100644 --- a/packages/core/src/spec-trace-helpers.ts +++ b/packages/core/src/spec-trace-helpers.ts @@ -18,9 +18,15 @@ import { deterministicUid } from './uid.js' // ─── SpecRange ──────────────────────────────────────────────────────────────── -/** Index ranges into a SessionCapturer's flat arrays for a single spec file. */ +/** Index ranges into a SessionCapturer's flat arrays for one trace slice + * (a spec file, or a single test under `test` granularity). */ export interface SpecRange { specFile: string + /** Dedupe/identity key: spec path for spec slices; testUid for test slices, + * or `${testUid}-retry${n}` so each retried attempt is its own slice. */ + key: string + /** Present only for test-granularity slices; the base (non-retry) testUid. */ + testUid?: string commandStartIdx: number consoleStartIdx: number networkStartIdx: number @@ -63,6 +69,25 @@ export function buildSpecSessionId( return `${base}-${hash}-${sessionId.slice(0, 8)}` } +// ─── Test slice session ID ────────────────────────────────────────────────── + +/** + * Build a collision-safe test-level session ID from the slice's spec file, its + * identity `key` (testUid, or `${testUid}-retry${n}` for retries), and the + * parent session ID. Hashing the key keeps retries and sibling tests in the + * same spec from colliding on filename, while the spec basename keeps the name + * human-readable. + */ +export function buildTestSliceSessionId( + specFile: string, + key: string, + sessionId: string +): string { + const base = sanitizeSpecName(specFile) + const hash = deterministicUid(key).split('-').pop()!.slice(0, 8) + return `${base}-${hash}-${sessionId.slice(0, 8)}` +} + // ─── TraceCapturer slice ───────────────────────────────────────────────────── /** @@ -127,11 +152,29 @@ export function filterTestMetadataBySpec( return filtered } +/** + * Filter a full `testUid → metadata` map down to a single test's entry. The + * per-test analog of {@link filterTestMetadataBySpec}: a test slice's metadata + * is just that one test's entry, attached as its tracingGroup name. + */ +export function filterTestMetadataByUid( + allMetadata: TestMetadataMap, + testUid: string +): TestMetadataMap { + const filtered: TestMetadataMap = new Map() + const entry = allMetadata.get(testUid) + if (entry) { + filtered.set(testUid, entry) + } + return filtered +} + // ─── Spec boundary recording ────────────────────────────────────────────────── /** - * Minimal context needed by `recordSpecBoundary` to detect spec-file - * transitions and capture array index ranges. + * Minimal context needed by `recordSliceBoundary` to detect spec-file / test + * transitions and capture array index ranges. `flushedSpecs` holds already- + * flushed slice keys (spec paths or test keys), shared with the finalizer. */ export interface SpecBoundaryContext { specRanges: SpecRange[] @@ -146,32 +189,28 @@ export interface SpecBoundaryContext { actionSnapshots: ArrayLike } -/** - * Record a spec-file boundary. When `traceGranularity` is `'spec'` and the - * spec file has changed, this pushes a new `SpecRange` and returns the - * previous range so the caller can flush its trace artifact. - * - * Returns `null` when no flush is needed (same spec, or granularity isn't - * `'spec'`, or no capturer). - */ -export function recordSpecBoundary( +/** Push a new slice range and return the previous (unflushed) range to flush. + * `suppressSameKey` skips recording when the incoming key matches the last + * range's — used for spec granularity so consecutive tests in one file share + * a slice; test granularity records every attempt (retries included). */ +function pushSliceRange( ctx: SpecBoundaryContext, specFile: string, - traceGranularity: TraceGranularity | undefined + key: string, + testUid: string | undefined, + suppressSameKey: boolean ): SpecRange | null { - if (traceGranularity !== 'spec') { - return null - } const lastRange = ctx.specRanges[ctx.specRanges.length - 1] - if (lastRange && lastRange.specFile === specFile) { + if (suppressSameKey && lastRange && lastRange.key === key) { return null } - const prevRange = - lastRange && !ctx.flushedSpecs.has(lastRange.specFile) ? lastRange : null + lastRange && !ctx.flushedSpecs.has(lastRange.key) ? lastRange : null ctx.specRanges.push({ specFile, + key, + testUid, commandStartIdx: ctx.capturer.commandsLog.length, consoleStartIdx: ctx.capturer.consoleLogs.length, networkStartIdx: ctx.capturer.networkRequests.length, @@ -183,6 +222,48 @@ export function recordSpecBoundary( return prevRange } +/** + * Record a trace-slice boundary. For `spec` granularity, a new slice starts + * when the spec file changes (existing behavior). For `test` granularity, a + * new slice starts on every recorded test — including retries: a repeated + * `testUid` is keyed `${testUid}-retry${n}` so each attempt is its own slice. + * Returns the previous, not-yet-flushed range so the caller can flush it, or + * `null` when nothing needs flushing (same spec, missing testUid, or a + * non-sliced granularity). + */ +export function recordSliceBoundary( + ctx: SpecBoundaryContext, + granularity: TraceGranularity | undefined, + specFile: string, + testUid?: string +): SpecRange | null { + if (granularity === 'spec') { + return pushSliceRange(ctx, specFile, specFile, undefined, true) + } + if (granularity === 'test' && testUid !== undefined) { + const priorAttempts = ctx.specRanges.filter( + (r) => r.testUid === testUid + ).length + const key = + priorAttempts === 0 ? testUid : `${testUid}-retry${priorAttempts}` + return pushSliceRange(ctx, specFile, key, testUid, false) + } + return null +} + +/** + * Record a spec-file boundary. Thin back-compat wrapper over + * {@link recordSliceBoundary}; behavior is unchanged for `spec` granularity + * and returns `null` for every other granularity. + */ +export function recordSpecBoundary( + ctx: SpecBoundaryContext, + specFile: string, + traceGranularity: TraceGranularity | undefined +): SpecRange | null { + return recordSliceBoundary(ctx, traceGranularity, specFile) +} + // ─── Spec trace I/O ──────────────────────────────────────────────────────────── /** @@ -205,41 +286,66 @@ export interface WriteSpecTraceInput { capabilities?: unknown } -/** - * Write a standalone trace artifact (zip or ndjson-directory) for a single - * spec file. This is the shared I/O path — all three adapters delegate to it - * from their own `flushSpecTrace` wrappers. - */ -export async function writeSpecTrace( - input: WriteSpecTraceInput +/** Slice the parent capturer/snapshots for one range and write the artifact + * under `sliceSessionId` with the pre-filtered `testMetadata`. Shared by the + * spec and test write paths so both slice identically. */ +async function writeSliceTrace( + input: WriteSpecTraceInput, + sliceSessionId: string, + testMetadata: TestMetadataMap ): Promise { - const specCapturer = buildSpecCapturer( + const sliceCapturer = buildSpecCapturer( input.capturer, input.range, input.nextRange ) - const specSnapshots = input.actionSnapshots.slice( + const sliceSnapshots = input.actionSnapshots.slice( input.range.snapshotCount, input.nextRange?.snapshotCount ?? input.actionSnapshots.length ) - const specSessionId = buildSpecSessionId( - input.range.specFile, - input.sessionId - ) - - const testMetadata = filterTestMetadataBySpec( - input.testMetadata, - input.range.specFile - ) - - return writeTraceZip(specCapturer, { + return writeTraceZip(sliceCapturer, { outputDir: input.outputDir, - sessionId: specSessionId, + sessionId: sliceSessionId, capabilities: input.capabilities, - actionSnapshots: specSnapshots.length > 0 ? specSnapshots : undefined, + actionSnapshots: sliceSnapshots.length > 0 ? sliceSnapshots : undefined, format: input.format, testMetadata }) } + +/** + * Write a standalone trace artifact (zip or ndjson-directory) for a single + * spec file. This is the shared I/O path — all three adapters delegate to it + * from their own `flushSpecTrace` wrappers. + */ +export async function writeSpecTrace( + input: WriteSpecTraceInput +): Promise { + return writeSliceTrace( + input, + buildSpecSessionId(input.range.specFile, input.sessionId), + filterTestMetadataBySpec(input.testMetadata, input.range.specFile) + ) +} + +/** + * Write a standalone trace artifact for a single test slice. Reuses + * {@link WriteSpecTraceInput}; the slice identity comes from `range.key` + * (retry-aware) and its metadata from the base `range.testUid`. + */ +export async function writeTestSliceTrace( + input: WriteSpecTraceInput +): Promise { + const testUid = input.range.testUid ?? input.range.key + return writeSliceTrace( + input, + buildTestSliceSessionId( + input.range.specFile, + input.range.key, + input.sessionId + ), + filterTestMetadataByUid(input.testMetadata, testUid) + ) +} diff --git a/packages/core/src/trace-finalizer.ts b/packages/core/src/trace-finalizer.ts index 1750d3a4..840d9a51 100644 --- a/packages/core/src/trace-finalizer.ts +++ b/packages/core/src/trace-finalizer.ts @@ -17,7 +17,9 @@ import type { import { errorMessage } from './error.js' import { filterTestMetadataBySpec, + filterTestMetadataByUid, writeSpecTrace, + writeTestSliceTrace, type SpecRange } from './spec-trace-helpers.js' import { shouldRetainTrace, type TestOutcome } from './trace-retention.js' @@ -69,6 +71,22 @@ const SPEC_WITHOUT_BOUNDARIES_WARNING = '(the runner may not expose per-test hooks). Falling back to ' + 'session-level trace.' +const TEST_WITHOUT_BOUNDARIES_WARNING = + 'traceGranularity is "test" but no test boundaries were detected ' + + '(the runner may not expose per-test hooks). Falling back to ' + + 'session-level trace.' + +/** Above this many slices, warn to pair granularity with a retention policy. */ +const SLICE_COUNT_WARN_THRESHOLD = 200 + +function sliceCountWarning(count: number): string { + return ( + `traceGranularity produced ${count} trace slices. Consider pairing it ` + + 'with a retention policy (e.g. tracePolicy: "retain-on-failure") to ' + + 'avoid writing hundreds of trace archives.' + ) +} + /** Project a metadata slice onto the retention evaluator's outcome shape. */ function toOutcomes(metadata: TestMetadataMap): TestOutcome[] { return Array.from(metadata.values(), (m) => ({ @@ -93,30 +111,31 @@ function shouldRetain( } /** - * Policy-aware single-range flush: dedupes via `ctx.flushed`, applies the - * retention decision, and delegates the byte-level slicing/naming to - * `writeSpecTrace`. Returns the artifact, or undefined when the range was - * already flushed. + * Policy-aware single-range flush: dedupes via `ctx.flushed` on the slice + * `key`, applies the retention decision, and delegates the byte-level + * slicing/naming to `writeSpecTrace` (spec slices) or `writeTestSliceTrace` + * (test slices, distinguished by `range.testUid`). Returns the artifact, or + * undefined when the range was already flushed. */ export async function flushRangeTrace( ctx: TraceExportContext, range: SpecRange, nextRange?: SpecRange ): Promise { - if (ctx.flushed.has(range.specFile)) { + if (ctx.flushed.has(range.key)) { return undefined } - ctx.flushed.add(range.specFile) + ctx.flushed.add(range.key) - const sliceMetadata = filterTestMetadataBySpec( - ctx.testMetadata, - range.specFile - ) + const isTestSlice = range.testUid !== undefined + const sliceMetadata = isTestSlice + ? filterTestMetadataByUid(ctx.testMetadata, range.testUid!) + : filterTestMetadataBySpec(ctx.testMetadata, range.specFile) const artifact: TraceArtifact = { kind: 'trace', path: '', - scope: 'spec', - key: range.specFile, + scope: isTestSlice ? 'test' : 'spec', + key: range.key, testUids: Array.from(sliceMetadata.keys()), retained: shouldRetain(ctx, sliceMetadata) } @@ -125,7 +144,8 @@ export async function flushRangeTrace( return artifact } - artifact.path = await writeSpecTrace({ + const writeSlice = isTestSlice ? writeTestSliceTrace : writeSpecTrace + artifact.path = await writeSlice({ range, nextRange, capturer: ctx.capturer, @@ -138,7 +158,7 @@ export async function flushRangeTrace( }) ctx.log?.( 'info', - `Trace for spec "${range.specFile}" saved to ${artifact.path}` + `Trace for ${isTestSlice ? 'test' : 'spec'} "${range.key}" saved to ${artifact.path}` ) ctx.onArtifact?.(artifact) return artifact @@ -202,9 +222,9 @@ async function flushAllRanges( /** * Entry point for the after/end-of-run hook. No-op outside trace mode. Awaits - * any pending snapshot captures, then fans out to per-spec or session writes. - * `spec` granularity with no recorded boundaries warns and falls back to a - * single session-level trace. + * any pending snapshot captures, then fans out to per-spec, per-test, or + * session writes. `spec`/`test` granularity with no recorded boundaries warns + * and falls back to a single session-level trace. */ export async function finalizeTraceExport( ctx: TraceExportContext @@ -215,11 +235,17 @@ export async function finalizeTraceExport( if (ctx.awaitPending?.length) { await Promise.allSettled(ctx.awaitPending) } - if (ctx.granularity === 'spec' && ctx.ranges.length > 0) { + const sliced = ctx.granularity === 'spec' || ctx.granularity === 'test' + if (sliced && ctx.ranges.length > 0) { + if (ctx.ranges.length > SLICE_COUNT_WARN_THRESHOLD) { + ctx.log?.('warn', sliceCountWarning(ctx.ranges.length)) + } return flushAllRanges(ctx) } if (ctx.granularity === 'spec') { ctx.log?.('warn', SPEC_WITHOUT_BOUNDARIES_WARNING) + } else if (ctx.granularity === 'test') { + ctx.log?.('warn', TEST_WITHOUT_BOUNDARIES_WARNING) } const artifact = await safely(ctx, () => writeSessionTrace(ctx)) return artifact ? [artifact] : [] diff --git a/packages/core/tests/spec-trace-helpers.test.ts b/packages/core/tests/spec-trace-helpers.test.ts index f41efc5c..4be8ad6e 100644 --- a/packages/core/tests/spec-trace-helpers.test.ts +++ b/packages/core/tests/spec-trace-helpers.test.ts @@ -2,8 +2,13 @@ import { describe, it, expect } from 'vitest' import { buildSpecCapturer, buildSpecSessionId, + buildTestSliceSessionId, filterTestMetadataBySpec, + filterTestMetadataByUid, + recordSliceBoundary, + recordSpecBoundary, sanitizeSpecName, + type SpecBoundaryContext, type SpecRange, type TraceCapturer } from '@wdio/devtools-core' @@ -30,6 +35,7 @@ function capturer(): TraceCapturer { const range = (over: Partial = {}): SpecRange => ({ specFile: '/a.js', + key: over.key ?? over.specFile ?? '/a.js', commandStartIdx: 0, consoleStartIdx: 0, networkStartIdx: 0, @@ -39,6 +45,24 @@ const range = (over: Partial = {}): SpecRange => ({ ...over }) +function boundaryCtx( + over: Partial = {} +): SpecBoundaryContext { + return { + specRanges: [], + flushedSpecs: new Set(), + capturer: { + commandsLog: [], + consoleLogs: [], + networkRequests: [], + mutations: [], + traceLogs: [] + }, + actionSnapshots: [], + ...over + } +} + describe('buildSpecCapturer', () => { it('slices from the range start to the end when no nextRange is given', () => { const sliced = buildSpecCapturer(capturer(), range({ commandStartIdx: 2 })) @@ -91,3 +115,108 @@ describe('spec name / session id', () => { expect(a.startsWith('login-')).toBe(true) }) }) + +describe('filterTestMetadataByUid', () => { + it('keeps only the entry for the given uid', () => { + const all: TestMetadataMap = new Map([ + ['u1', { title: 'A', specFile: '/a.js' }], + ['u2', { title: 'B', specFile: '/b.js' }] + ]) + expect([...filterTestMetadataByUid(all, 'u1').keys()]).toEqual(['u1']) + expect(filterTestMetadataByUid(all, 'missing').size).toBe(0) + }) +}) + +describe('buildTestSliceSessionId', () => { + it('derives distinct, stable ids per key and stays readable', () => { + const a = buildTestSliceSessionId('/dir/login.js', 'u1', 'session-xyz') + const b = buildTestSliceSessionId('/dir/login.js', 'u2', 'session-xyz') + const retry = buildTestSliceSessionId( + '/dir/login.js', + 'u1-retry1', + 'session-xyz' + ) + expect(a).not.toBe(b) + expect(a).not.toBe(retry) + expect(a).toBe( + buildTestSliceSessionId('/dir/login.js', 'u1', 'session-xyz') + ) + expect(a.startsWith('login-')).toBe(true) + }) +}) + +describe('recordSliceBoundary (test granularity)', () => { + it('opens a new slice per test uid and returns the previous range', () => { + const ctx = boundaryCtx({ + capturer: { + commandsLog: [1, 2], + consoleLogs: [1], + networkRequests: [], + mutations: [1, 2, 3], + traceLogs: [] + }, + actionSnapshots: [1, 1] + }) + expect(recordSliceBoundary(ctx, 'test', '/a.js', 'u1')).toBeNull() + expect(ctx.specRanges).toHaveLength(1) + expect(ctx.specRanges[0]!.key).toBe('u1') + expect(ctx.specRanges[0]!.testUid).toBe('u1') + expect(ctx.specRanges[0]!.commandStartIdx).toBe(2) + expect(ctx.specRanges[0]!.snapshotCount).toBe(2) + + const prev = recordSliceBoundary(ctx, 'test', '/a.js', 'u2') + expect(prev?.key).toBe('u1') + expect(ctx.specRanges).toHaveLength(2) + expect(ctx.specRanges[1]!.key).toBe('u2') + }) + + it('keys each retry of the same uid as its own slice', () => { + const ctx = boundaryCtx() + recordSliceBoundary(ctx, 'test', '/a.js', 'u1') + recordSliceBoundary(ctx, 'test', '/a.js', 'u1') + recordSliceBoundary(ctx, 'test', '/a.js', 'u1') + expect(ctx.specRanges.map((r) => r.key)).toEqual([ + 'u1', + 'u1-retry1', + 'u1-retry2' + ]) + expect(ctx.specRanges.every((r) => r.testUid === 'u1')).toBe(true) + }) + + it('returns null when no testUid is provided', () => { + const ctx = boundaryCtx() + expect(recordSliceBoundary(ctx, 'test', '/a.js')).toBeNull() + expect(ctx.specRanges).toHaveLength(0) + }) + + it('does not return a previous range already in the flushed set', () => { + const ctx = boundaryCtx() + recordSliceBoundary(ctx, 'test', '/a.js', 'u1') + ctx.flushedSpecs.add('u1') + expect(recordSliceBoundary(ctx, 'test', '/a.js', 'u2')).toBeNull() + }) +}) + +describe('recordSliceBoundary / recordSpecBoundary (spec granularity)', () => { + it('keeps the spec-file behavior: one slice per spec, key equals specFile', () => { + const ctx = boundaryCtx() + expect(recordSliceBoundary(ctx, 'spec', '/a.js')).toBeNull() + expect(recordSliceBoundary(ctx, 'spec', '/a.js')).toBeNull() + expect(ctx.specRanges).toHaveLength(1) + expect(ctx.specRanges[0]!.key).toBe('/a.js') + expect(ctx.specRanges[0]!.testUid).toBeUndefined() + + const prev = recordSliceBoundary(ctx, 'spec', '/b.js') + expect(prev?.specFile).toBe('/a.js') + expect(ctx.specRanges).toHaveLength(2) + }) + + it('recordSpecBoundary returns null for session and test granularities', () => { + expect(recordSpecBoundary(boundaryCtx(), '/a.js', 'session')).toBeNull() + expect(recordSpecBoundary(boundaryCtx(), '/a.js', 'test')).toBeNull() + const ctx = boundaryCtx() + recordSpecBoundary(ctx, '/a.js', 'spec') + expect(ctx.specRanges).toHaveLength(1) + expect(ctx.specRanges[0]!.key).toBe('/a.js') + }) +}) diff --git a/packages/core/tests/trace-finalizer.test.ts b/packages/core/tests/trace-finalizer.test.ts index 73a9ae36..04362b01 100644 --- a/packages/core/tests/trace-finalizer.test.ts +++ b/packages/core/tests/trace-finalizer.test.ts @@ -4,6 +4,7 @@ import path from 'node:path' import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' import { buildSpecSessionId, + buildTestSliceSessionId, finalizeTraceExport, flushRangeTrace, type SpecRange, @@ -51,6 +52,7 @@ function meta(entries: Array<[string, TestMetadataEntry]>): TestMetadataMap { function range(specFile: string, startIdx: number): SpecRange { return { specFile, + key: specFile, commandStartIdx: startIdx, consoleStartIdx: 0, networkStartIdx: 0, @@ -60,6 +62,15 @@ function range(specFile: string, startIdx: number): SpecRange { } } +function testRange( + specFile: string, + testUid: string, + startIdx: number, + key = testUid +): SpecRange { + return { ...range(specFile, startIdx), key, testUid } +} + describe('finalizeTraceExport', () => { let outputDir: string let artifacts: TraceArtifact[] @@ -158,6 +169,99 @@ describe('finalizeTraceExport', () => { ).toBe(true) }) + it('fans out one trace per recorded test range', async () => { + const result = await finalizeTraceExport( + baseCtx({ + granularity: 'test', + ranges: [testRange('/a.js', 'a1', 0), testRange('/a.js', 'a2', 2)], + testMetadata: meta([ + ['a1', { title: 'A1', specFile: '/a.js' }], + ['a2', { title: 'A2', specFile: '/a.js' }] + ]) + }) + ) + expect(result).toHaveLength(2) + expect(result.map((a) => a.scope)).toEqual(['test', 'test']) + expect(result.map((a) => a.key)).toEqual(['a1', 'a2']) + // Each test slice carries only its own test's metadata. + expect(result[0]!.testUids).toEqual(['a1']) + expect(result[1]!.testUids).toEqual(['a2']) + const nameA1 = buildTestSliceSessionId('/a.js', 'a1', 'abcd1234') + const nameA2 = buildTestSliceSessionId('/a.js', 'a2', 'abcd1234') + expect(nameA1).not.toBe(nameA2) + expect(await exists(path.join(outputDir, `trace-${nameA1}.zip`))).toBe(true) + expect(await exists(path.join(outputDir, `trace-${nameA2}.zip`))).toBe(true) + }) + + it('treats a retry-keyed range as its own test slice', async () => { + const result = await finalizeTraceExport( + baseCtx({ + granularity: 'test', + ranges: [ + testRange('/a.js', 'a1', 0), + testRange('/a.js', 'a1', 2, 'a1-retry1') + ], + testMetadata: meta([['a1', { title: 'A1', specFile: '/a.js' }]]) + }) + ) + expect(result).toHaveLength(2) + expect(result.map((a) => a.key)).toEqual(['a1', 'a1-retry1']) + const first = buildTestSliceSessionId('/a.js', 'a1', 'abcd1234') + const retry = buildTestSliceSessionId('/a.js', 'a1-retry1', 'abcd1234') + expect(first).not.toBe(retry) + expect(await exists(path.join(outputDir, `trace-${first}.zip`))).toBe(true) + expect(await exists(path.join(outputDir, `trace-${retry}.zip`))).toBe(true) + }) + + it('warns and falls back to a session trace when test has no boundaries', async () => { + const result = await finalizeTraceExport( + baseCtx({ granularity: 'test', ranges: [] }) + ) + expect(result).toHaveLength(1) + expect(result[0]!.scope).toBe('session') + expect(await exists(path.join(outputDir, 'trace-abcd1234.zip'))).toBe(true) + expect( + logs.some( + ([level, msg]) => + level === 'warn' && msg.includes('no test boundaries were detected') + ) + ).toBe(true) + }) + + it('warns to pair with a retention policy above the slice-count threshold', async () => { + const ranges = Array.from({ length: 201 }, (_, i) => + testRange('/a.js', `u${i}`, i) + ) + const result = await finalizeTraceExport( + baseCtx({ + granularity: 'test', + // retain-on-failure + all-passing declines every write, so the guard + // is exercised without emitting 201 archives. + policy: 'retain-on-failure', + ranges, + testMetadata: meta( + ranges.map((r) => [ + r.testUid!, + { + title: r.testUid!, + specFile: '/a.js', + state: 'passed', + attempt: 0 + } + ]) + ) + }) + ) + expect(result).toHaveLength(201) + expect(result.every((a) => !a.retained)).toBe(true) + expect(await fs.readdir(outputDir)).toEqual([]) + expect( + logs.some( + ([level, msg]) => level === 'warn' && msg.includes('retention policy') + ) + ).toBe(true) + }) + it('does not rewrite a range already in the flushed set', async () => { const flushed = new Set(['/a.js']) const result = await finalizeTraceExport( From 2626631cfd810a4ee98dfc4ae3462c9ab4e0d9e1 Mon Sep 17 00:00:00 2001 From: Vishnu Vardhan Date: Fri, 10 Jul 2026 23:23:01 +0530 Subject: [PATCH 31/68] feat(trace): per-test granularity, output folders, and test-results/ parent --- .gitignore | 3 + packages/core/src/finalize-screencast.ts | 3 + packages/core/src/output-dir.ts | 13 +- packages/core/src/spec-trace-helpers.ts | 70 ++++- packages/core/src/trace-exporter.ts | 8 +- packages/core/src/trace-finalizer.ts | 41 ++- packages/core/tests/output-dir.test.ts | 25 +- .../core/tests/spec-trace-helpers.test.ts | 119 ++++++++- packages/core/tests/trace-finalizer.test.ts | 24 +- .../src/cucumber-lifecycle.ts | 27 +- .../src/plugin-internals.ts | 13 +- .../nightwatch-devtools/src/test-lifecycle.ts | 11 +- .../nightwatch-devtools/src/trace-slices.ts | 102 ++++++++ .../tests/traceSlices.test.ts | 155 +++++++++++ packages/selenium-devtools/src/index.ts | 23 +- .../selenium-devtools/src/plugin-internals.ts | 2 + .../src/session-lifecycle.ts | 116 +++++++-- .../tests/attempt-capture.test.ts | 1 + .../tests/trace-granularity.test.ts | 191 ++++++++++++++ packages/service/src/trace-slices.ts | 49 ++++ .../service/tests/trace-granularity.test.ts | 240 ++++++++++++++++++ 21 files changed, 1167 insertions(+), 69 deletions(-) create mode 100644 packages/nightwatch-devtools/src/trace-slices.ts create mode 100644 packages/nightwatch-devtools/tests/traceSlices.test.ts create mode 100644 packages/selenium-devtools/tests/trace-granularity.test.ts create mode 100644 packages/service/src/trace-slices.ts create mode 100644 packages/service/tests/trace-granularity.test.ts diff --git a/.gitignore b/.gitignore index 47c58732..e8a7a934 100644 --- a/.gitignore +++ b/.gitignore @@ -39,6 +39,9 @@ packages/nightwatch-devtools/nightwatch-video-*.webm trace-*.zip examples/**/trace-*/ +# test results +examples/**/test-results/ + # vitest --coverage output coverage/ diff --git a/packages/core/src/finalize-screencast.ts b/packages/core/src/finalize-screencast.ts index e8c6b00c..4e1b0b9c 100644 --- a/packages/core/src/finalize-screencast.ts +++ b/packages/core/src/finalize-screencast.ts @@ -62,6 +62,9 @@ export async function finalizeScreencast({ const candidate = outputDir || process.cwd() let videoPath = path.join(candidate, fileName) try { + // Create the (test-results) dir if absent, then confirm it's writable; + // fall back to tmpdir on any failure so a bad path never aborts the run. + fs.mkdirSync(candidate, { recursive: true }) fs.accessSync(candidate, fs.constants.W_OK) } catch { videoPath = path.join(os.tmpdir(), fileName) diff --git a/packages/core/src/output-dir.ts b/packages/core/src/output-dir.ts index fa659ace..463c5960 100644 --- a/packages/core/src/output-dir.ts +++ b/packages/core/src/output-dir.ts @@ -25,6 +25,9 @@ export interface ResolveAdapterOutputDirInput { const NODE_MODULES_SEGMENT = `${path.sep}node_modules${path.sep}` +/** All run output is grouped under this subfolder. */ +const OUTPUT_SUBDIR = 'test-results' + function isWritable(dir: string): boolean { try { fs.accessSync(dir, fs.constants.W_OK) @@ -36,9 +39,11 @@ function isWritable(dir: string): boolean { /** * Resolve the directory where an adapter should write output files - * (screencast .webm, trace JSON, etc.). + * (screencast .webm, trace JSON, etc.). Every artifact is grouped under a + * single `test-results/` subfolder so a run's output is + * self-contained regardless of where the base directory resolves to. * - * Priority: + * The base directory is resolved by priority: * 1. `userConfiguredDir` — explicit opt-in, honored as-is. * 2. `dirname(testFilePath)` — same folder as the spec that just ran. * 3. `dirname(configPath)` — fallback to the framework config dir. @@ -56,6 +61,10 @@ function isWritable(dir: string): boolean { export function resolveAdapterOutputDir( input: ResolveAdapterOutputDirInput = {} ): string { + return path.join(resolveBaseDir(input), OUTPUT_SUBDIR) +} + +function resolveBaseDir(input: ResolveAdapterOutputDirInput): string { const fallback = input.fallbackDir ?? process.cwd() // userConfiguredDir bypasses the node_modules and writability filters // because the user opted into it explicitly — surprising overrides are diff --git a/packages/core/src/spec-trace-helpers.ts b/packages/core/src/spec-trace-helpers.ts index dd091efb..2eb69298 100644 --- a/packages/core/src/spec-trace-helpers.ts +++ b/packages/core/src/spec-trace-helpers.ts @@ -6,6 +6,7 @@ * is the single source of truth — adapters import from here. */ +import path from 'node:path' import type { ActionSnapshot, TestMetadataMap, @@ -88,6 +89,44 @@ export function buildTestSliceSessionId( return `${base}-${hash}-${sessionId.slice(0, 8)}` } +// ─── Test slice output folder ──────────────────────────────────────────────── + +/** Max slug length so a title doesn't blow past filesystem path limits. */ +const MAX_SLUG_LENGTH = 60 + +/** Lowercase, collapse runs of non-alphanumerics to `-`, trim edge dashes, + * and cap length (trimming a dash the cut may leave behind). */ +function slugify(text: string): string { + return text + .toLowerCase() + .replace(/[^a-z0-9]+/g, '-') + .replace(/^-+|-+$/g, '') + .slice(0, MAX_SLUG_LENGTH) + .replace(/-+$/g, '') +} + +/** + * Build the per-test output folder name: `--<browser>[-retryN]`. + * The spec basename is sanitized via {@link sanitizeSpecName}; title and browser + * are slugified. An empty title falls back to a short hash of `key`, and a + * `${uid}-retry${n}` key appends a `-retry<N>` suffix so retries don't collide. + */ +export function buildTestSliceFolder( + specFile: string, + testTitle: string | undefined, + browser: string | undefined, + key: string +): string { + const specBase = sanitizeSpecName(path.basename(specFile)) + const titleSlug = + slugify(testTitle ?? '') || + deterministicUid(key).split('-').pop()!.slice(0, 8) + const browserSlug = slugify(browser ?? '') || 'browser' + const retryMatch = key.match(/-retry(\d+)$/) + const retrySuffix = retryMatch ? `-retry${retryMatch[1]}` : '' + return `${specBase}-${titleSlug}-${browserSlug}${retrySuffix}` +} + // ─── TraceCapturer slice ───────────────────────────────────────────────────── /** @@ -288,11 +327,13 @@ export interface WriteSpecTraceInput { /** Slice the parent capturer/snapshots for one range and write the artifact * under `sliceSessionId` with the pre-filtered `testMetadata`. Shared by the - * spec and test write paths so both slice identically. */ + * spec and test write paths so both slice identically. `overrides` lets the + * test path redirect into a named folder with a fixed `trace` file stem. */ async function writeSliceTrace( input: WriteSpecTraceInput, sliceSessionId: string, - testMetadata: TestMetadataMap + testMetadata: TestMetadataMap, + overrides: { outputDir?: string; fileStem?: string } = {} ): Promise<string> { const sliceCapturer = buildSpecCapturer( input.capturer, @@ -306,8 +347,9 @@ async function writeSliceTrace( ) return writeTraceZip(sliceCapturer, { - outputDir: input.outputDir, + outputDir: overrides.outputDir ?? input.outputDir, sessionId: sliceSessionId, + fileStem: overrides.fileStem, capabilities: input.capabilities, actionSnapshots: sliceSnapshots.length > 0 ? sliceSnapshots : undefined, format: input.format, @@ -331,14 +373,27 @@ export async function writeSpecTrace( } /** - * Write a standalone trace artifact for a single test slice. Reuses - * {@link WriteSpecTraceInput}; the slice identity comes from `range.key` - * (retry-aware) and its metadata from the base `range.testUid`. + * Write a standalone trace artifact for a single test slice into its own + * folder: `<outputDir>/<specBasename>-<titleSlug>-<browserSlug>[-retryN]/trace.zip`. + * Reuses {@link WriteSpecTraceInput}; the folder is the slice's external + * identity (title/browser/retry), while {@link buildTestSliceSessionId} names + * the sessionId embedded inside the archive. */ export async function writeTestSliceTrace( input: WriteSpecTraceInput ): Promise<string> { const testUid = input.range.testUid ?? input.range.key + const title = input.testMetadata.get(testUid)?.title + // capabilities is framework-typed unknown; read only browserName here. + const browserName = ( + input.capabilities as { browserName?: string } | undefined + )?.browserName + const folder = buildTestSliceFolder( + input.range.specFile, + title, + browserName, + input.range.key + ) return writeSliceTrace( input, buildTestSliceSessionId( @@ -346,6 +401,7 @@ export async function writeTestSliceTrace( input.range.key, input.sessionId ), - filterTestMetadataByUid(input.testMetadata, testUid) + filterTestMetadataByUid(input.testMetadata, testUid), + { outputDir: path.join(input.outputDir, folder), fileStem: 'trace' } ) } diff --git a/packages/core/src/trace-exporter.ts b/packages/core/src/trace-exporter.ts index bc223e14..40079b9a 100644 --- a/packages/core/src/trace-exporter.ts +++ b/packages/core/src/trace-exporter.ts @@ -466,6 +466,9 @@ export interface WriteTraceZipOptions { format?: TraceFormat /** Test metadata keyed by testUid for Tracing.tracingGroup events. */ testMetadata?: TestMetadataMap + /** Base name for the artifact (zip file stem / directory name). Defaults to + * `trace-<sessionId>`; per-test slices pass `'trace'` inside a named folder. */ + fileStem?: string } /** @@ -501,14 +504,15 @@ export async function writeTraceZip( wallTimeOverride: capturer.startWallTime, testMetadata: opts.testMetadata } + const stem = opts.fileStem ?? `trace-${opts.sessionId}` if (opts.format === 'ndjson-directory') { - const dir = path.join(opts.outputDir, `trace-${opts.sessionId}`) + const dir = path.join(opts.outputDir, stem) await fs.mkdir(dir, { recursive: true }) await exportTraceDirectory(traceLog, dir, exportOpts) return dir } const zip = await exportTraceZip(traceLog, exportOpts) - const zipPath = path.join(opts.outputDir, `trace-${opts.sessionId}.zip`) + const zipPath = path.join(opts.outputDir, `${stem}.zip`) await fs.writeFile(zipPath, zip) return zipPath } diff --git a/packages/core/src/trace-finalizer.ts b/packages/core/src/trace-finalizer.ts index 840d9a51..415b6289 100644 --- a/packages/core/src/trace-finalizer.ts +++ b/packages/core/src/trace-finalizer.ts @@ -164,6 +164,36 @@ export async function flushRangeTrace( return artifact } +/** + * Flush one slice via {@link flushRangeTrace}, logging the shared spec/test + * error string on failure so a failed boundary flush can't abort the next test. + * All three adapters wrapped this identically; the label + identity are derived + * from `range.testUid` (test slice → `test "<key>"`, else `spec "<specFile>"`), + * so each call site keeps its exact message. Errors are logged and swallowed + * (resolves `undefined`), so callers `await` it when the write must land before + * a retry overwrites metadata, or fire-and-forget (`void`, or tracked in an + * in-flight list) otherwise. Callers keep their own find-current-range strategy + * and pass the resolved range in. + */ +export async function flushRangeLogged( + ctx: TraceExportContext, + range: SpecRange +): Promise<TraceArtifact | undefined> { + try { + return await flushRangeTrace(ctx, range) + } catch (err) { + const label = + range.testUid !== undefined + ? `test "${range.key}"` + : `spec "${range.specFile}"` + ctx.log?.( + 'warn', + `Failed to flush trace for ${label}: ${errorMessage(err)}` + ) + return undefined + } +} + async function writeSessionTrace( ctx: TraceExportContext ): Promise<TraceArtifact | undefined> { @@ -211,8 +241,15 @@ async function flushAllRanges( ctx: TraceExportContext ): Promise<TraceArtifact[]> { const artifacts: TraceArtifact[] = [] - for (const range of ctx.ranges) { - const artifact = await safely(ctx, () => flushRangeTrace(ctx, range)) + // Bound each slice by the next range's start indices; the final range (no + // nextRange) runs to the end of the arrays. Without this, every slice would + // run to the end and each test slice would swallow all later tests. + for (let i = 0; i < ctx.ranges.length; i++) { + const range = ctx.ranges[i]! + const nextRange = ctx.ranges[i + 1] + const artifact = await safely(ctx, () => + flushRangeTrace(ctx, range, nextRange) + ) if (artifact) { artifacts.push(artifact) } diff --git a/packages/core/tests/output-dir.test.ts b/packages/core/tests/output-dir.test.ts index 12d80315..f44ba3ef 100644 --- a/packages/core/tests/output-dir.test.ts +++ b/packages/core/tests/output-dir.test.ts @@ -4,6 +4,9 @@ import path from 'node:path' import { afterEach, beforeEach, describe, expect, it } from 'vitest' import { resolveAdapterOutputDir } from '../src/output-dir.js' +/** Every resolved dir is grouped under this subfolder (Playwright-style). */ +const grouped = (base: string) => path.join(base, 'test-results') + describe('resolveAdapterOutputDir', () => { let tmpDir: string @@ -15,10 +18,16 @@ describe('resolveAdapterOutputDir', () => { fs.rmSync(tmpDir, { recursive: true, force: true }) }) + it('groups the resolved dir under a test-results/ subfolder', () => { + expect(resolveAdapterOutputDir({ fallbackDir: tmpDir })).toBe( + path.join(tmpDir, 'test-results') + ) + }) + it('returns userConfiguredDir verbatim when set, even if non-existent', () => { expect( resolveAdapterOutputDir({ userConfiguredDir: '/whatever/path' }) - ).toBe('/whatever/path') + ).toBe(grouped('/whatever/path')) }) it('prefers testFilePath dir over configPath dir over fallback', () => { @@ -35,14 +44,14 @@ describe('resolveAdapterOutputDir', () => { configPath, fallbackDir: tmpDir }) - ).toBe(path.dirname(testFile)) + ).toBe(grouped(path.dirname(testFile))) }) it('falls back to configPath dir when testFilePath is missing', () => { const configPath = path.join(tmpDir, 'wdio.conf.ts') fs.writeFileSync(configPath, '') expect(resolveAdapterOutputDir({ configPath, fallbackDir: tmpDir })).toBe( - tmpDir + grouped(tmpDir) ) }) @@ -59,11 +68,11 @@ describe('resolveAdapterOutputDir', () => { testFilePath: testFile, configPath }) - ).toBe(tmpDir) + ).toBe(grouped(tmpDir)) }) it('falls back to process.cwd() when no inputs are given', () => { - expect(resolveAdapterOutputDir()).toBe(process.cwd()) + expect(resolveAdapterOutputDir()).toBe(grouped(process.cwd())) }) it('falls back to fallbackDir when given and none of the candidates are writable', () => { @@ -72,12 +81,12 @@ describe('resolveAdapterOutputDir', () => { testFilePath: '/definitely/missing/a.test.ts', fallbackDir: tmpDir }) - ).toBe(tmpDir) + ).toBe(grouped(tmpDir)) }) it('userConfiguredDir bypasses node_modules skip (explicit opt-in)', () => { const nm = '/some/node_modules/pkg/dir' - expect(resolveAdapterOutputDir({ userConfiguredDir: nm })).toBe(nm) + expect(resolveAdapterOutputDir({ userConfiguredDir: nm })).toBe(grouped(nm)) }) it('returns fallback (cwd) when all candidate dirs are missing', () => { @@ -86,6 +95,6 @@ describe('resolveAdapterOutputDir', () => { testFilePath: '/missing/x.test.ts', configPath: '/missing/wdio.conf.ts' }) - ).toBe(process.cwd()) + ).toBe(grouped(process.cwd())) }) }) diff --git a/packages/core/tests/spec-trace-helpers.test.ts b/packages/core/tests/spec-trace-helpers.test.ts index 4be8ad6e..95257afe 100644 --- a/packages/core/tests/spec-trace-helpers.test.ts +++ b/packages/core/tests/spec-trace-helpers.test.ts @@ -1,16 +1,23 @@ -import { describe, it, expect } from 'vitest' +import fs from 'node:fs/promises' +import os from 'node:os' +import path from 'node:path' +import { afterEach, beforeEach, describe, it, expect } from 'vitest' import { buildSpecCapturer, buildSpecSessionId, + buildTestSliceFolder, buildTestSliceSessionId, filterTestMetadataBySpec, filterTestMetadataByUid, recordSliceBoundary, recordSpecBoundary, sanitizeSpecName, + writeSpecTrace, + writeTestSliceTrace, type SpecBoundaryContext, type SpecRange, - type TraceCapturer + type TraceCapturer, + type WriteSpecTraceInput } from '@wdio/devtools-core' import { TraceType, type TestMetadataMap } from '@wdio/devtools-shared' @@ -197,6 +204,114 @@ describe('recordSliceBoundary (test granularity)', () => { }) }) +describe('buildTestSliceFolder', () => { + it('combines sanitized spec, title slug, and browser slug', () => { + expect( + buildTestSliceFolder( + '/tests/login.e2e.js', + 'shows an error message for an invalid username', + 'chrome', + 'u1' + ) + ).toBe('login_e2e-shows-an-error-message-for-an-invalid-username-chrome') + }) + + it('appends a -retry<N> suffix when the key is a retry key', () => { + expect( + buildTestSliceFolder('/a.js', 'My Test', 'chrome', 'u1-retry2') + ).toBe('a-my-test-chrome-retry2') + }) + + it('defaults the browser slug to "browser" when the browser is absent', () => { + expect(buildTestSliceFolder('/a.js', 'My Test', undefined, 'u1')).toBe( + 'a-my-test-browser' + ) + }) + + it('falls back to a stable short hash of the key when the title is empty', () => { + const folder = buildTestSliceFolder('/a.js', '', 'chrome', 'u1') + expect(folder).toMatch(/^a-[a-z0-9]+-chrome$/) + expect(buildTestSliceFolder('/a.js', undefined, 'chrome', 'u1')).toBe( + folder + ) + }) + + it('lowercases, collapses non-alphanumerics, and caps the slug length', () => { + const folder = buildTestSliceFolder( + '/a.js', + `${'A'.repeat(80)} !!! End`, + 'Chrome', + 'u1' + ) + const titleSlug = folder.slice('a-'.length, folder.lastIndexOf('-chrome')) + expect(titleSlug.length).toBeLessThanOrEqual(60) + expect(titleSlug).toMatch(/^[a-z0-9-]+$/) + expect(titleSlug.endsWith('-')).toBe(false) + }) +}) + +describe('writeTestSliceTrace / writeSpecTrace output layout', () => { + let outputDir: string + beforeEach(async () => { + outputDir = await fs.mkdtemp(path.join(os.tmpdir(), 'slice-layout-')) + }) + afterEach(async () => { + await fs.rm(outputDir, { recursive: true, force: true }) + }) + + const writableCapturer = (): TraceCapturer => ({ + mutations: [], + traceLogs: [], + consoleLogs: [], + networkRequests: [], + commandsLog: [ + { command: 'url', args: ['https://example.test'], timestamp: 1000 } + ], + sources: new Map(), + metadata: { type: TraceType.Standalone }, + startWallTime: 1000 + }) + + const input = ( + over: Partial<WriteSpecTraceInput> = {} + ): WriteSpecTraceInput => ({ + range: range({ specFile: '/tests/login.js', key: 'u1', testUid: 'u1' }), + capturer: writableCapturer(), + actionSnapshots: [], + sessionId: 'sess1234', + outputDir, + testMetadata: new Map([ + ['u1', { title: 'My Test', specFile: '/tests/login.js' }] + ]), + capabilities: { browserName: 'firefox' }, + ...over + }) + + it('writes a test slice into <folder>/trace.zip', async () => { + const written = await writeTestSliceTrace(input()) + const folder = buildTestSliceFolder( + '/tests/login.js', + 'My Test', + 'firefox', + 'u1' + ) + expect(folder).toBe('login-my-test-firefox') + expect(written).toBe(path.join(outputDir, folder, 'trace.zip')) + await expect(fs.access(written)).resolves.toBeUndefined() + }) + + it('keeps the spec write flat as trace-<id>.zip (unchanged layout)', async () => { + const written = await writeSpecTrace( + input({ + range: range({ specFile: '/tests/login.js', key: '/tests/login.js' }) + }) + ) + const name = buildSpecSessionId('/tests/login.js', 'sess1234') + expect(written).toBe(path.join(outputDir, `trace-${name}.zip`)) + await expect(fs.access(written)).resolves.toBeUndefined() + }) +}) + describe('recordSliceBoundary / recordSpecBoundary (spec granularity)', () => { it('keeps the spec-file behavior: one slice per spec, key equals specFile', () => { const ctx = boundaryCtx() diff --git a/packages/core/tests/trace-finalizer.test.ts b/packages/core/tests/trace-finalizer.test.ts index 04362b01..6d2b5013 100644 --- a/packages/core/tests/trace-finalizer.test.ts +++ b/packages/core/tests/trace-finalizer.test.ts @@ -4,7 +4,7 @@ import path from 'node:path' import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' import { buildSpecSessionId, - buildTestSliceSessionId, + buildTestSliceFolder, finalizeTraceExport, flushRangeTrace, type SpecRange, @@ -186,11 +186,13 @@ describe('finalizeTraceExport', () => { // Each test slice carries only its own test's metadata. expect(result[0]!.testUids).toEqual(['a1']) expect(result[1]!.testUids).toEqual(['a2']) - const nameA1 = buildTestSliceSessionId('/a.js', 'a1', 'abcd1234') - const nameA2 = buildTestSliceSessionId('/a.js', 'a2', 'abcd1234') - expect(nameA1).not.toBe(nameA2) - expect(await exists(path.join(outputDir, `trace-${nameA1}.zip`))).toBe(true) - expect(await exists(path.join(outputDir, `trace-${nameA2}.zip`))).toBe(true) + // Each slice lands in its own <spec>--<title>-<browser>/trace.zip folder. + const folderA1 = buildTestSliceFolder('/a.js', 'A1', undefined, 'a1') + const folderA2 = buildTestSliceFolder('/a.js', 'A2', undefined, 'a2') + expect(folderA1).not.toBe(folderA2) + expect(result[0]!.path).toBe(path.join(outputDir, folderA1, 'trace.zip')) + expect(await exists(path.join(outputDir, folderA1, 'trace.zip'))).toBe(true) + expect(await exists(path.join(outputDir, folderA2, 'trace.zip'))).toBe(true) }) it('treats a retry-keyed range as its own test slice', async () => { @@ -206,11 +208,13 @@ describe('finalizeTraceExport', () => { ) expect(result).toHaveLength(2) expect(result.map((a) => a.key)).toEqual(['a1', 'a1-retry1']) - const first = buildTestSliceSessionId('/a.js', 'a1', 'abcd1234') - const retry = buildTestSliceSessionId('/a.js', 'a1-retry1', 'abcd1234') + // The retry attempt gets a distinct folder via the -retry<N> suffix. + const first = buildTestSliceFolder('/a.js', 'A1', undefined, 'a1') + const retry = buildTestSliceFolder('/a.js', 'A1', undefined, 'a1-retry1') expect(first).not.toBe(retry) - expect(await exists(path.join(outputDir, `trace-${first}.zip`))).toBe(true) - expect(await exists(path.join(outputDir, `trace-${retry}.zip`))).toBe(true) + expect(retry.endsWith('-retry1')).toBe(true) + expect(await exists(path.join(outputDir, first, 'trace.zip'))).toBe(true) + expect(await exists(path.join(outputDir, retry, 'trace.zip'))).toBe(true) }) it('warns and falls back to a session trace when test has no boundaries', async () => { diff --git a/packages/nightwatch-devtools/src/cucumber-lifecycle.ts b/packages/nightwatch-devtools/src/cucumber-lifecycle.ts index 201e2ca0..44197c0e 100644 --- a/packages/nightwatch-devtools/src/cucumber-lifecycle.ts +++ b/packages/nightwatch-devtools/src/cucumber-lifecycle.ts @@ -19,7 +19,6 @@ import { type CucumberPickleStep } from '@wdio/devtools-shared' -import type { SessionCapturer } from './session.js' import type { TestReporter } from './reporter.js' import type { TestManager } from './helpers/testManager.js' import type { SuiteManager } from './helpers/suiteManager.js' @@ -33,6 +32,11 @@ import { import { buildCucumberScenarioSuite } from './helpers/cucumberScenarioBuilder.js' import { scanFeatureFile } from './helpers/featureFileScan.js' import { parseCucumberScenario } from './helpers/utils.js' +import { + recordTestSliceBoundary, + flushTestSlice, + type TestSliceCtx +} from './trace-slices.js' const log = logger('@wdio/nightwatch-devtools:cucumber') @@ -42,8 +46,7 @@ export interface CucumberResult { status?: string } -export interface CucumberLifecycleCtx { - readonly sessionCapturer: SessionCapturer +export interface CucumberLifecycleCtx extends TestSliceCtx { readonly testReporter: TestReporter readonly testManager: TestManager readonly suiteManager: SuiteManager @@ -132,6 +135,15 @@ function normalizeSteps( return (pickleSteps ?? []).map((s) => ({ text: s.text ?? '' })) } +function captureFeatureSources( + ctx: CucumberLifecycleCtx, + paths: string[] +): void { + for (const p of paths) { + ctx.sessionCapturer.captureSource(p).catch(() => {}) + } +} + export async function initCucumberScenario( ctx: CucumberLifecycleCtx, browser: NightwatchBrowser, @@ -148,9 +160,7 @@ export async function initCucumberScenario( stepDefFiles, capturedPaths } = scanFeatureFile(featureUri) - for (const p of capturedPaths) { - ctx.sessionCapturer.captureSource(p).catch(() => {}) - } + captureFeatureSources(ctx, capturedPaths) const { featureSuite, scenarioLine, stepLines, stepKeywords } = createFeatureSuite( ctx, @@ -175,6 +185,8 @@ export async function initCucumberScenario( recordAttempt: (uid) => ctx.recordAttempt(uid) }) attachScenarioToFeature(ctx, featureSuite, scenarioSuite) + // The scenario is the `test` unit; its steps are the leaf metadata entries. + recordTestSliceBoundary(ctx, featureUri, scenarioSuite.uid) ctx.setCurrentScenarioSuite(scenarioSuite) ctx.setCurrentStep(null) ctx.setCurrentTest(null) @@ -222,6 +234,9 @@ export async function finalizeCucumberScenario( ctx.setCurrentTest(null) } await ctx.sessionCapturer.captureTrace(browser) + // Flush before the next attempt's attachScenarioToFeature overwrites this + // scenario's suite (and thus its outcome) in the tree. + flushTestSlice(ctx) } catch (err) { log.error(`Failed to finalize Cucumber scenario: ${errorMessage(err)}`) } diff --git a/packages/nightwatch-devtools/src/plugin-internals.ts b/packages/nightwatch-devtools/src/plugin-internals.ts index 1d4d45a2..1753d09c 100644 --- a/packages/nightwatch-devtools/src/plugin-internals.ts +++ b/packages/nightwatch-devtools/src/plugin-internals.ts @@ -7,6 +7,7 @@ * four) while still letting each lifecycle module narrow its dependencies. */ +import type { SpecRange, TraceArtifact } from '@wdio/devtools-core' import type { SessionCapturer } from './session.js' import type { TestReporter } from './reporter.js' import type { ScreencastRecorder } from './screencast.js' @@ -18,7 +19,8 @@ import type { NightwatchBrowser, ScreencastOptions, SuiteStats, - TestStats + TestStats, + TraceGranularity } from './types.js' export interface PluginInternals { @@ -29,6 +31,7 @@ export interface PluginInternals { readonly mode: DevToolsMode readonly screencastOptions: ScreencastOptions readonly bidiEnabled: boolean + readonly captureAssertions: boolean // Runtime instances (mutable — bringup/session-change replaces them) sessionCapturer: SessionCapturer @@ -79,4 +82,12 @@ export interface PluginInternals { recordAttempt(uid: string): number /** Latest attempt recorded for `uid`, or undefined if it never started. */ attemptFor(uid: string): number | undefined + + // Per-test trace slicing (`test` granularity). Boundary state is shared with + // the finalizer; flushTraceRange writes one slice via the plugin's context. + readonly traceMode: boolean + readonly traceGranularity: TraceGranularity + readonly specRanges: SpecRange[] + readonly flushedSpecs: Set<string> + flushTraceRange(range: SpecRange): Promise<TraceArtifact | undefined> } diff --git a/packages/nightwatch-devtools/src/test-lifecycle.ts b/packages/nightwatch-devtools/src/test-lifecycle.ts index 5caf0537..7192c12a 100644 --- a/packages/nightwatch-devtools/src/test-lifecycle.ts +++ b/packages/nightwatch-devtools/src/test-lifecycle.ts @@ -9,7 +9,6 @@ */ import logger from '@wdio/logger' -import type { SessionCapturer } from './session.js' import type { TestReporter } from './reporter.js' import type { TestManager } from './helpers/testManager.js' import type { SuiteManager } from './helpers/suiteManager.js' @@ -26,11 +25,11 @@ import { DEFAULTS, TIMING, TEST_STATE } from './constants.js' import { resolveSpecFilePath } from './helpers/specFileResolver.js' import { closePreviousTest } from './helpers/closePreviousTest.js' import { extractTestMetadata, determineTestState } from './helpers/utils.js' +import { recordTestSliceBoundary, type TestSliceCtx } from './trace-slices.js' const log = logger('@wdio/nightwatch-devtools:test-lifecycle') -export interface TestLifecycleCtx { - readonly sessionCapturer: SessionCapturer +export interface TestLifecycleCtx extends TestSliceCtx { readonly testReporter: TestReporter readonly testManager: TestManager readonly suiteManager: SuiteManager @@ -118,7 +117,8 @@ export async function startNextTest( ctx: TestLifecycleCtx, currentSuite: SuiteStats, currentTestName: string, - processedTests: Set<string> + processedTests: Set<string>, + specFile: string | null ): Promise<void> { if (processedTests.size === 0) { ctx.suiteManager.markSuiteAsRunning(currentSuite) @@ -127,6 +127,9 @@ export async function startNextTest( if (test) { // Nightwatch has no per-test retry index; the tracker is the retry signal. test.retries = ctx.recordAttempt(test.uid) + if (specFile) { + recordTestSliceBoundary(ctx, specFile, test.uid) + } test.state = TEST_STATE.RUNNING as TestStats['state'] test.start = new Date() test.end = null diff --git a/packages/nightwatch-devtools/src/trace-slices.ts b/packages/nightwatch-devtools/src/trace-slices.ts new file mode 100644 index 00000000..bdd0cf0d --- /dev/null +++ b/packages/nightwatch-devtools/src/trace-slices.ts @@ -0,0 +1,102 @@ +/** + * Trace-slice boundary recording for `spec` and `test` granularity. + * + * `spec` slices are recorded/flushed at each spec-file transition (unchanged + * from the original inline `beforeEach` logic). For `test`, Nightwatch builds + * test outcomes into the suite tree in place — a retry overwrites the previous + * attempt's state (regular tests reuse the same test object; cucumber replaces + * the scenario suite). So each attempt's slice is flushed at its own + * test/scenario END, before the next attempt can overwrite the outcome the + * flush reads. Regular tests drive this from test-lifecycle, cucumber scenarios + * from cucumber-lifecycle; both share this module. + */ + +import { + recordSliceBoundary, + recordSpecBoundary, + type SpecBoundaryContext, + type SpecRange, + type TraceArtifact +} from '@wdio/devtools-core' +import type { SessionCapturer } from './session.js' +import type { TraceGranularity } from './types.js' + +export interface TestSliceCtx { + readonly sessionCapturer: SessionCapturer + readonly traceMode: boolean + readonly traceGranularity: TraceGranularity + readonly specRanges: SpecRange[] + readonly flushedSpecs: Set<string> + flushTraceRange(range: SpecRange): Promise<TraceArtifact | undefined> +} + +function sliceActive(ctx: TestSliceCtx): boolean { + return ctx.traceMode && ctx.traceGranularity === 'test' +} + +function boundaryContext(ctx: TestSliceCtx): SpecBoundaryContext { + return { + specRanges: ctx.specRanges, + flushedSpecs: ctx.flushedSpecs, + capturer: ctx.sessionCapturer, + actionSnapshots: ctx.sessionCapturer.actionSnapshots + } +} + +function flushPrevious(ctx: TestSliceCtx, prevRange: SpecRange | null): void { + if (!prevRange) { + return + } + // flushTraceRange (→ core flushRangeLogged) logs+swallows on failure. + void ctx.flushTraceRange(prevRange) +} + +/** + * Record a spec-file boundary at test/scenario start and eagerly flush the + * previous spec's slice. No-op for `session`/`test` granularity (core returns + * null). Preserves the original `beforeEach` behavior verbatim. + */ +export function recordSpecSliceBoundary( + ctx: TestSliceCtx, + specFile: string +): void { + const prevRange = recordSpecBoundary( + boundaryContext(ctx), + specFile, + ctx.traceGranularity + ) + flushPrevious(ctx, prevRange) +} + +/** + * Record a per-test slice boundary at test/scenario start. No-op outside trace + * mode + `test` granularity. Retries push a distinct range (core keys a repeat + * `${testUid}-retry${n}`), so each attempt becomes its own artifact. + */ +export function recordTestSliceBoundary( + ctx: TestSliceCtx, + specFile: string, + testUid: string +): void { + if (!sliceActive(ctx)) { + return + } + recordSliceBoundary(boundaryContext(ctx), 'test', specFile, testUid) +} + +/** + * Flush the current test's slice at test/scenario end — before a retry can + * overwrite its outcome in the suite tree. Fire-and-forget; the finalize pass + * is the safety net for any range this misses. No-op outside trace + `test`. + */ +export function flushTestSlice(ctx: TestSliceCtx): void { + if (!sliceActive(ctx)) { + return + } + const range = ctx.specRanges[ctx.specRanges.length - 1] + if (!range) { + return + } + // flushTraceRange (→ core flushRangeLogged) logs+swallows on failure. + void ctx.flushTraceRange(range) +} diff --git a/packages/nightwatch-devtools/tests/traceSlices.test.ts b/packages/nightwatch-devtools/tests/traceSlices.test.ts new file mode 100644 index 00000000..b42e92d5 --- /dev/null +++ b/packages/nightwatch-devtools/tests/traceSlices.test.ts @@ -0,0 +1,155 @@ +import { describe, it, expect, vi } from 'vitest' +import type { SpecRange, TraceArtifact } from '@wdio/devtools-core' +import { + recordTestSliceBoundary, + recordSpecSliceBoundary, + flushTestSlice, + type TestSliceCtx +} from '../src/trace-slices.js' +import type { SessionCapturer } from '../src/session.js' +import type { TraceGranularity } from '../src/types.js' + +function makeCtx(traceMode: boolean, granularity: TraceGranularity) { + const capturer = { + commandsLog: [], + consoleLogs: [], + networkRequests: [], + mutations: [], + traceLogs: [], + actionSnapshots: [] + } as unknown as SessionCapturer + const artifacts: TraceArtifact[] = [] + const flushTraceRange = vi.fn( + async (range: SpecRange): Promise<TraceArtifact> => { + const artifact: TraceArtifact = { + kind: 'trace', + path: `/out/${range.key}.zip`, + scope: range.testUid ? 'test' : 'spec', + key: range.key, + testUids: range.testUid ? [range.testUid] : [], + retained: true + } + artifacts.push(artifact) + return artifact + } + ) + const ctx: TestSliceCtx = { + sessionCapturer: capturer, + traceMode, + traceGranularity: granularity, + specRanges: [], + flushedSpecs: new Set<string>(), + flushTraceRange + } + return { ctx, artifacts, flushTraceRange } +} + +describe('test granularity — recordTestSliceBoundary', () => { + it('records a per-test slice keyed on the test uid', () => { + const { ctx } = makeCtx(true, 'test') + recordTestSliceBoundary(ctx, '/login.spec.js', 'uid-1') + expect(ctx.specRanges).toHaveLength(1) + expect(ctx.specRanges[0]).toMatchObject({ + key: 'uid-1', + testUid: 'uid-1', + specFile: '/login.spec.js' + }) + }) + + it('keys a retry of the same test as a distinct slice', () => { + const { ctx } = makeCtx(true, 'test') + recordTestSliceBoundary(ctx, '/a.spec.js', 'uid-1') + recordTestSliceBoundary(ctx, '/a.spec.js', 'uid-1') // retry + recordTestSliceBoundary(ctx, '/a.spec.js', 'uid-2') // sibling test + expect(ctx.specRanges.map((r) => r.key)).toEqual([ + 'uid-1', + 'uid-1-retry1', + 'uid-2' + ]) + // Every slice stays a test slice (base uid preserved for metadata filter). + expect(ctx.specRanges.map((r) => r.testUid)).toEqual([ + 'uid-1', + 'uid-1', + 'uid-2' + ]) + }) +}) + +describe('test granularity — flushTestSlice', () => { + it('emits one artifact per test slice, flushing the current test at its end', () => { + const { ctx, artifacts, flushTraceRange } = makeCtx(true, 'test') + recordTestSliceBoundary(ctx, '/a.spec.js', 'uid-1') + flushTestSlice(ctx) + recordTestSliceBoundary(ctx, '/a.spec.js', 'uid-2') + flushTestSlice(ctx) + + expect(flushTraceRange).toHaveBeenCalledTimes(2) + expect(artifacts.map((a) => a.key)).toEqual(['uid-1', 'uid-2']) + expect(artifacts.every((a) => a.scope === 'test')).toBe(true) + }) + + it('flushes each attempt separately so retries become distinct artifacts', () => { + const { ctx, artifacts } = makeCtx(true, 'test') + recordTestSliceBoundary(ctx, '/a.spec.js', 'uid-1') + flushTestSlice(ctx) // attempt 0 flushed before the retry can overwrite it + recordTestSliceBoundary(ctx, '/a.spec.js', 'uid-1') + flushTestSlice(ctx) // attempt 1 + + expect(artifacts.map((a) => a.key)).toEqual(['uid-1', 'uid-1-retry1']) + }) + + it('does nothing when there is no recorded slice', () => { + const { ctx, flushTraceRange } = makeCtx(true, 'test') + flushTestSlice(ctx) + expect(flushTraceRange).not.toHaveBeenCalled() + }) +}) + +describe('test-slice helpers are inert outside trace + test granularity', () => { + it('no-ops for spec and session granularity', () => { + for (const granularity of ['spec', 'session'] as TraceGranularity[]) { + const { ctx, flushTraceRange } = makeCtx(true, granularity) + recordTestSliceBoundary(ctx, '/a.spec.js', 'uid-1') + flushTestSlice(ctx) + expect(ctx.specRanges).toHaveLength(0) + expect(flushTraceRange).not.toHaveBeenCalled() + } + }) + + it('no-ops in live mode even at test granularity', () => { + const { ctx, flushTraceRange } = makeCtx(false, 'test') + recordTestSliceBoundary(ctx, '/a.spec.js', 'uid-1') + flushTestSlice(ctx) + expect(ctx.specRanges).toHaveLength(0) + expect(flushTraceRange).not.toHaveBeenCalled() + }) +}) + +describe('spec granularity is unchanged by the test-slice work', () => { + it('records one slice per spec file and flushes the previous on transition', () => { + const { ctx, artifacts, flushTraceRange } = makeCtx(true, 'spec') + recordSpecSliceBoundary(ctx, '/a.spec.js') + expect(ctx.specRanges.map((r) => r.key)).toEqual(['/a.spec.js']) + expect(flushTraceRange).not.toHaveBeenCalled() // no previous spec yet + + recordSpecSliceBoundary(ctx, '/a.spec.js') // same spec → shared slice + expect(ctx.specRanges).toHaveLength(1) + + recordSpecSliceBoundary(ctx, '/b.spec.js') // new spec → flush previous + expect(ctx.specRanges.map((r) => r.key)).toEqual([ + '/a.spec.js', + '/b.spec.js' + ]) + expect(artifacts.map((a) => a.key)).toEqual(['/a.spec.js']) + expect(artifacts[0].scope).toBe('spec') + }) + + it('records nothing for session or test granularity', () => { + for (const granularity of ['session', 'test'] as TraceGranularity[]) { + const { ctx, flushTraceRange } = makeCtx(true, granularity) + recordSpecSliceBoundary(ctx, '/a.spec.js') + expect(ctx.specRanges).toHaveLength(0) + expect(flushTraceRange).not.toHaveBeenCalled() + } + }) +}) diff --git a/packages/selenium-devtools/src/index.ts b/packages/selenium-devtools/src/index.ts index 2fcdb885..7289cd81 100644 --- a/packages/selenium-devtools/src/index.ts +++ b/packages/selenium-devtools/src/index.ts @@ -21,7 +21,8 @@ import { onDriverEnd as sessionOnDriverEnd, onSessionEnd as sessionOnSessionEnd, setPluginRef, - recordSpecBoundary + recordTraceBoundary, + flushCurrentTestTrace } from './session-lifecycle.js' import type { SpecRange } from '@wdio/devtools-core' import { @@ -122,6 +123,9 @@ class SeleniumDevToolsPlugin { /** Set of spec files already flushed to disk. */ #flushedSpecs = new Set<string>() + /** In-flight per-test eager flushes (test granularity), awaited at finalize. */ + #traceFlushes: Promise<unknown>[] = [] + constructor(options: DevToolsOptions = {}) { this.#options = { port: options.port ?? 3000, @@ -427,6 +431,9 @@ class SeleniumDevToolsPlugin { get flushedSpecs() { return self.#flushedSpecs }, + get traceFlushes() { + return self.#traceFlushes + }, setFinalized: (v) => { self.#finalized = v }, @@ -451,20 +458,17 @@ class SeleniumDevToolsPlugin { /** Public API: start a marked test. */ startTest(name: string, meta: StartTestMeta = {}) { tmStartTest(this.#getInternals(), name, meta) - if (meta.file) { - recordSpecBoundary(this.#getInternals(), meta.file) - } + recordTraceBoundary(this.#getInternals(), meta.file) } endTest(state: TestStats['state'] = 'passed') { tmEndTest(this.#getInternals(), state) + flushCurrentTestTrace(this.#getInternals()) } startScenario(name: string, meta: StartScenarioMeta = {}) { tmStartScenario(this.#getInternals(), name, meta) - if (meta.file) { - recordSpecBoundary(this.#getInternals(), meta.file) - } + recordTraceBoundary(this.#getInternals(), meta.file) } endScenario(state: TestStats['state'] = 'passed') { @@ -473,6 +477,11 @@ class SeleniumDevToolsPlugin { #flushPendingTestActions() { tmFlushPendingTestActions(this.#getInternals()) + // The first test's startTest fires before the driver/capturer exists, so + // its per-test boundary is recorded here once capture is live. + if (this.#options.traceGranularity === 'test') { + recordTraceBoundary(this.#getInternals(), this.#testFilePath) + } } async onDriverCreated(driver: SeleniumDriverLike) { diff --git a/packages/selenium-devtools/src/plugin-internals.ts b/packages/selenium-devtools/src/plugin-internals.ts index c769c4ad..a9672e50 100644 --- a/packages/selenium-devtools/src/plugin-internals.ts +++ b/packages/selenium-devtools/src/plugin-internals.ts @@ -67,6 +67,8 @@ export interface PluginInternals { // Per-spec trace tracking (populated at spec file boundaries). readonly specRanges: SpecRange[] readonly flushedSpecs: Set<string> + // In-flight per-test eager flushes (test granularity), awaited at finalize. + readonly traceFlushes: Promise<unknown>[] // Plugin-side delegates setFinalized(v: boolean): void diff --git a/packages/selenium-devtools/src/session-lifecycle.ts b/packages/selenium-devtools/src/session-lifecycle.ts index b12699dd..856d05ee 100644 --- a/packages/selenium-devtools/src/session-lifecycle.ts +++ b/packages/selenium-devtools/src/session-lifecycle.ts @@ -14,9 +14,11 @@ import { errorMessage, finalizeScreencast, finalizeTraceExport, - flushRangeTrace, + flushRangeLogged, + recordSliceBoundary as coreRecordSliceBoundary, recordSpecBoundary as coreRecordSpecBoundary, resolveAdapterOutputDir, + type SpecBoundaryContext, type SpecRange, type TraceExportContext } from '@wdio/devtools-core' @@ -79,6 +81,9 @@ export interface SessionLifecycleCtx { // Per-spec trace tracking (populated at spec file boundaries). readonly specRanges: SpecRange[] readonly flushedSpecs: Set<string> + // In-flight per-test eager flushes (test granularity); awaited at finalize + // so the last test's write completes before the process exits. + readonly traceFlushes: Promise<unknown>[] setFinalized(v: boolean): void ensureBackendStarted(): Promise<void> @@ -315,30 +320,55 @@ export function buildTraceExportContext( resolveAdapterOutputDir({ testFilePath: range ? range.specFile : testFilePath }), - awaitPending: ctx.snapshotCaptures, + awaitPending: [...ctx.snapshotCaptures, ...ctx.traceFlushes], log: (level, msg) => log[level](msg) } } +/** Narrow view of the lifecycle ctx that the core boundary recorder needs. */ +function boundaryContext( + ctx: SessionLifecycleCtx, + capturer: SessionCapturer +): SpecBoundaryContext { + return { + specRanges: ctx.specRanges, + flushedSpecs: ctx.flushedSpecs, + capturer, + actionSnapshots: ctx.actionSnapshots + } +} + /** - * Record a spec-file boundary in the session lifecycle context. Called from - * the plugin's startTest / startScenario when `traceGranularity` is `'spec'`. - * Flushes the previous spec's trace if it hasn't been written yet. + * Record a trace-slice boundary for the active granularity, called from the + * plugin's startTest / startScenario. `spec` keys by spec file (flushing the + * previous spec lazily at the next boundary); `test` keys by the just-started + * marked test's uid so each test — and each retry, which selenium gives a + * distinct uid — becomes its own slice. No-op for `session`. */ -export function recordSpecBoundary( +export function recordTraceBoundary( ctx: SessionLifecycleCtx, - specFile: string + specFile: string | undefined ): void { + if (ctx.options.traceGranularity === 'test') { + recordTestBoundary(ctx, specFile) + return + } + if (specFile) { + recordSpecBoundary(ctx, specFile) + } +} + +/** + * Record a spec-file boundary and lazily flush the previous spec's trace if it + * hasn't been written yet. `coreRecordSpecBoundary` no-ops for non-spec + * granularities, so this only records under `spec`. + */ +function recordSpecBoundary(ctx: SessionLifecycleCtx, specFile: string): void { if (!ctx.sessionCapturer) { return } const prevRange = coreRecordSpecBoundary( - { - specRanges: ctx.specRanges, - flushedSpecs: ctx.flushedSpecs, - capturer: ctx.sessionCapturer, - actionSnapshots: ctx.actionSnapshots - }, + boundaryContext(ctx, ctx.sessionCapturer), specFile, ctx.options.traceGranularity ) @@ -346,7 +376,7 @@ export function recordSpecBoundary( if (!prevRange || !sessionId) { return } - void flushRangeTrace( + void flushRangeLogged( buildTraceExportContext( ctx, ctx.sessionCapturer, @@ -354,13 +384,63 @@ export function recordSpecBoundary( ctx.testFilePath ), prevRange - ).catch((err) => - log.warn( - `Failed to flush trace for spec "${prevRange.specFile}": ${errorMessage(err)}` - ) ) } +/** + * Record a per-test boundary keyed by the currently-active marked test's uid. + * The first test's startTest fires before the driver exists (no capturer yet), + * so flushPendingTestActions re-invokes this once capture is live; the + * same-uid guard keeps that replay from minting a spurious retry slice. + */ +function recordTestBoundary( + ctx: SessionLifecycleCtx, + specFile: string | undefined +): void { + const testUid = ctx.testManager?.getCurrentTest()?.uid + const file = specFile ?? ctx.testFilePath + if (!ctx.sessionCapturer || !testUid || !file) { + return + } + const lastRange = ctx.specRanges[ctx.specRanges.length - 1] + if (lastRange?.testUid === testUid) { + return + } + coreRecordSliceBoundary( + boundaryContext(ctx, ctx.sessionCapturer), + 'test', + file, + testUid + ) +} + +/** + * Eager-flush the just-ended test's trace slice (test granularity), after + * endTest has finalized its state so collectSuiteTestMetadata sees the final + * outcome. flushRangeTrace dedupes by key, so finalizeTraceExport won't + * re-write it; the promise is tracked so finalize awaits the last write. + */ +export function flushCurrentTestTrace(ctx: SessionLifecycleCtx): void { + if (ctx.options.traceGranularity !== 'test' || !ctx.sessionCapturer) { + return + } + const sessionId = ctx.sessionCapturer.metadata?.sessionId + const currentRange = ctx.specRanges[ctx.specRanges.length - 1] + if (!sessionId || currentRange?.testUid === undefined) { + return + } + const flush = flushRangeLogged( + buildTraceExportContext( + ctx, + ctx.sessionCapturer, + sessionId, + ctx.testFilePath + ), + currentRange + ) + ctx.traceFlushes.push(flush) +} + async function writeTraceIfNeeded( ctx: SessionLifecycleCtx, capturer: SessionCapturer | undefined, diff --git a/packages/selenium-devtools/tests/attempt-capture.test.ts b/packages/selenium-devtools/tests/attempt-capture.test.ts index 255280f6..2f2840ff 100644 --- a/packages/selenium-devtools/tests/attempt-capture.test.ts +++ b/packages/selenium-devtools/tests/attempt-capture.test.ts @@ -77,6 +77,7 @@ describe('retry/attempt capture', () => { actionSnapshots: [], specRanges: [], flushedSpecs: new Set<string>(), + traceFlushes: [], snapshotCaptures: [] } as unknown as SessionLifecycleCtx diff --git a/packages/selenium-devtools/tests/trace-granularity.test.ts b/packages/selenium-devtools/tests/trace-granularity.test.ts new file mode 100644 index 00000000..8f133a24 --- /dev/null +++ b/packages/selenium-devtools/tests/trace-granularity.test.ts @@ -0,0 +1,191 @@ +import fs from 'node:fs' +import os from 'node:os' +import path from 'node:path' +import { afterEach, describe, expect, it, vi } from 'vitest' + +import { resetSignatureCounters } from '../src/helpers/utils.js' +import { TestManager } from '../src/helpers/testManager.js' +import { SuiteManager } from '../src/helpers/suiteManager.js' +import { TestReporter } from '../src/reporter.js' +import { SessionCapturer } from '../src/session.js' +import { + flushCurrentTestTrace, + recordTraceBoundary, + type SessionLifecycleCtx +} from '../src/session-lifecycle.js' +import type { TraceGranularity } from '../src/types.js' + +const SESSION_ID = 'sess-abcd1234ef' + +const capturers: SessionCapturer[] = [] +const tmpDirs: string[] = [] + +afterEach(() => { + while (capturers.length) { + capturers.pop()!.cleanup() + } + while (tmpDirs.length) { + fs.rmSync(tmpDirs.pop()!, { recursive: true, force: true }) + } +}) + +function makeTmpSpec(): string { + const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'sel-trace-gran-')) + tmpDirs.push(dir) + return path.join(dir, 'login.spec.ts') +} + +function makeCtx( + granularity: TraceGranularity, + specFile: string, + withSessionId = false +) { + resetSignatureCounters() + const reporter = new TestReporter(vi.fn()) + const suiteManager = new SuiteManager(reporter) + const rootSuite = suiteManager.getOrCreateRootSuite(specFile, 'Suite') + const testManager = new TestManager(rootSuite, reporter, suiteManager) + const capturer = new SessionCapturer() + capturers.push(capturer) + if (withSessionId) { + // Only sessionId matters to the flush path; the rest of Metadata is + // filled with writeTraceZip's defaults, so a partial cast is safe here. + capturer.metadata = { sessionId: SESSION_ID } as SessionCapturer['metadata'] + } + + // Minimal structural ctx: the boundary/flush helpers read only the trace + // accumulators, options, capturer, test/suite managers and testFilePath, so + // we cast a partial to the full lifecycle interface. + const ctx = { + options: { + mode: 'trace', + tracePolicy: 'on', + traceGranularity: granularity, + traceFormat: 'zip' + }, + sessionCapturer: capturer, + testManager, + suiteManager, + testFilePath: specFile, + actionSnapshots: [], + snapshotCaptures: [], + specRanges: [], + flushedSpecs: new Set<string>(), + traceFlushes: [] + } as unknown as SessionLifecycleCtx + + return { ctx, capturer, testManager } +} + +describe('trace granularity: test', () => { + it('records one per-test slice keyed by the started test uid', () => { + const spec = makeTmpSpec() + const { ctx, testManager } = makeCtx('test', spec) + + const test = testManager.startMarkedTest('logs in') + recordTraceBoundary(ctx, spec) + + expect(ctx.specRanges).toHaveLength(1) + expect(ctx.specRanges[0].testUid).toBe(test.uid) + expect(ctx.specRanges[0].key).toBe(test.uid) + expect(ctx.specRanges[0].specFile).toBe(spec) + }) + + it('re-recording the same active test is idempotent (no spurious slice)', () => { + const spec = makeTmpSpec() + const { ctx, testManager } = makeCtx('test', spec) + + testManager.startMarkedTest('logs in') + recordTraceBoundary(ctx, spec) + // Mirrors the buffered-first-test replay: startTest recorded nothing (no + // capturer yet), flushPendingTestActions re-invokes for the same test. + recordTraceBoundary(ctx, spec) + + expect(ctx.specRanges).toHaveLength(1) + }) + + it('a retry gets its own distinct slice (selenium gives each attempt a uid)', () => { + const spec = makeTmpSpec() + const { ctx, testManager } = makeCtx('test', spec) + + const first = testManager.startMarkedTest('flaky') + recordTraceBoundary(ctx, spec) + testManager.endCurrent('failed') + + const retry = testManager.startMarkedTest('flaky') + recordTraceBoundary(ctx, spec) + testManager.endCurrent('passed') + + expect(first.uid).not.toBe(retry.uid) + expect(ctx.specRanges).toHaveLength(2) + expect(ctx.specRanges.map((r) => r.key)).toEqual([first.uid, retry.uid]) + }) + + it('eager-flushes each test to its own artifact at test end', async () => { + const spec = makeTmpSpec() + const outDir = path.dirname(spec) + const { ctx, testManager } = makeCtx('test', spec, true) + + testManager.startMarkedTest('logs in') + recordTraceBoundary(ctx, spec) + testManager.endCurrent('passed') + flushCurrentTestTrace(ctx) + + testManager.startMarkedTest('logs out') + recordTraceBoundary(ctx, spec) + testManager.endCurrent('passed') + flushCurrentTestTrace(ctx) + + expect(ctx.traceFlushes).toHaveLength(2) + // Both slice keys are recorded as flushed synchronously, so end-of-run + // finalizeTraceExport dedupes and won't re-write them. + expect(ctx.flushedSpecs.size).toBe(2) + await Promise.all(ctx.traceFlushes) + + // Per-test slices land under test-results/<spec--title-browser>/trace.zip, + // so recurse to find them rather than scanning the flat dir. + const zips = fs + .readdirSync(outDir, { recursive: true }) + .map(String) + .filter((f) => f.endsWith('.zip')) + expect(zips).toHaveLength(2) + expect(zips.every((f) => path.basename(f) === 'trace.zip')).toBe(true) + expect(zips.every((f) => f.includes('test-results'))).toBe(true) + }) + + it('flushCurrentTestTrace is a no-op with no recorded range', () => { + const spec = makeTmpSpec() + const { ctx } = makeCtx('test', spec, true) + flushCurrentTestTrace(ctx) + expect(ctx.traceFlushes).toHaveLength(0) + expect(ctx.flushedSpecs.size).toBe(0) + }) +}) + +describe('trace granularity: spec/session unchanged', () => { + it('spec granularity still records one slice per spec file, keyed by file', () => { + const specA = makeTmpSpec() + const specB = makeTmpSpec() + const { ctx } = makeCtx('spec', specA) + + recordTraceBoundary(ctx, specA) + recordTraceBoundary(ctx, specA) // same file → shares the slice + recordTraceBoundary(ctx, specB) + + expect(ctx.specRanges.map((r) => r.key)).toEqual([specA, specB]) + expect(ctx.specRanges.every((r) => r.testUid === undefined)).toBe(true) + }) + + it('session granularity records no slices and never eager-flushes', () => { + const spec = makeTmpSpec() + const { ctx, testManager } = makeCtx('session', spec, true) + + testManager.startMarkedTest('logs in') + recordTraceBoundary(ctx, spec) + testManager.endCurrent('passed') + flushCurrentTestTrace(ctx) + + expect(ctx.specRanges).toHaveLength(0) + expect(ctx.traceFlushes).toHaveLength(0) + }) +}) diff --git a/packages/service/src/trace-slices.ts b/packages/service/src/trace-slices.ts new file mode 100644 index 00000000..fcc36962 --- /dev/null +++ b/packages/service/src/trace-slices.ts @@ -0,0 +1,49 @@ +// Trace-slice flushing for the WDIO adapter: previous-slice flush at boundary +// changes plus the eager per-test flush. Kept out of index.ts so the slice +// selection and flush I/O are unit-testable and the god-file stays lean. + +import { + flushRangeLogged, + type SpecRange, + type TraceExportContext +} from '@wdio/devtools-core' + +/** The range for the test that just ended is the most recent slice recorded + * under this base testUid — retries push a new range under the same testUid, + * so reverse-scanning finds the attempt whose afterTest is now firing. */ +export function findCurrentTestRange( + ranges: readonly SpecRange[], + testUid: string +): SpecRange | undefined { + for (let i = ranges.length - 1; i >= 0; i--) { + if (ranges[i]!.testUid === testUid) { + return ranges[i] + } + } + return undefined +} + +/** Fire-and-forget flush of the previous unflushed slice at a boundary change + * (spec granularity, or a test slice whose eager flush was missed). Errors are + * logged, never thrown, so a failed flush can't abort the next test. */ +export function flushPrevSlice( + ctx: TraceExportContext, + range: SpecRange +): void { + void flushRangeLogged(ctx, range) +} + +/** Awaited flush of the just-ended test's slice (test granularity), so this + * attempt's just-stamped metadata is written before a retry's beforeTest + * overwrites the entry. No-op when the test recorded no range. */ +export async function flushTestSlice( + ctx: TraceExportContext, + ranges: readonly SpecRange[], + testUid: string +): Promise<void> { + const range = findCurrentTestRange(ranges, testUid) + if (!range) { + return + } + await flushRangeLogged(ctx, range) +} diff --git a/packages/service/tests/trace-granularity.test.ts b/packages/service/tests/trace-granularity.test.ts new file mode 100644 index 00000000..c43b6b66 --- /dev/null +++ b/packages/service/tests/trace-granularity.test.ts @@ -0,0 +1,240 @@ +import { describe, it, expect, vi, beforeEach } from 'vitest' +import type * as DevtoolsCore from '@wdio/devtools-core' +import { deterministicUid, type SpecRange } from '@wdio/devtools-core' +import { findCurrentTestRange } from '../src/trace-slices.js' + +// Records the key/state observed at each per-slice flush and replays the real +// dedupe (flushed.add) so recordSliceBoundary's prev-slice logic behaves as in +// production — otherwise a boundary change would re-flush an already-flushed +// slice. Capturing state at call time is what proves the eager flush sees this +// attempt's outcome before a retry's beforeTest overwrites the entry. +const flushedSlices: Array<{ key: string; testUid?: string; state?: string }> = + [] + +type FlushCtx = { + flushed: Set<string> + testMetadata: Map<string, { state?: string }> +} + +const flushRangeTrace = vi.fn( + (ctx: FlushCtx, range: { key: string; testUid?: string }) => { + ctx.flushed.add(range.key) + flushedSlices.push({ + key: range.key, + testUid: range.testUid, + state: range.testUid + ? ctx.testMetadata.get(range.testUid)?.state + : undefined + }) + return Promise.resolve(undefined) + } +) + +const finalizeTraceExport = vi.fn().mockResolvedValue([]) + +vi.mock('stack-trace', () => ({ parse: () => [] })) + +const mockSessionCapturerInstance = { + afterCommand: vi.fn(), + sendUpstream: vi.fn(), + injectScript: vi.fn().mockResolvedValue(undefined), + captureAssertCommand: vi.fn(), + failLastAction: vi.fn(), + resetLastSelector: vi.fn(), + resetRetryTracker: vi.fn(), + cleanup: vi.fn(), + commandsLog: [] as unknown[], + sources: new Map(), + mutations: [], + traceLogs: [], + consoleLogs: [], + networkRequests: [], + metadata: { url: 'http://test.com', viewport: {} } +} + +vi.mock('../src/session.js', () => ({ + SessionCapturer: vi.fn(function (this: unknown) { + return mockSessionCapturerInstance + }) +})) + +vi.mock('../src/action-snapshot.js', () => ({ + captureActionSnapshot: vi.fn().mockResolvedValue(null), + captureActionResult: vi.fn().mockResolvedValue(undefined), + waitForActionResult: vi.fn().mockResolvedValue(undefined) +})) + +vi.mock('@wdio/devtools-core', async (importOriginal) => { + const actual = await importOriginal<typeof DevtoolsCore>() + return { + ...actual, + finalizeTraceExport: (ctx: unknown) => finalizeTraceExport(ctx), + // The adapter now flushes via core's flushRangeLogged wrapper; route it to + // the same spy so call-count/argument assertions still observe each flush. + flushRangeLogged: (ctx: unknown, range: unknown) => + flushRangeTrace( + ctx as FlushCtx, + range as { key: string; testUid?: string } + ), + flushRangeTrace: (ctx: unknown, range: unknown) => + flushRangeTrace( + ctx as FlushCtx, + range as { key: string; testUid?: string } + ) + } +}) + +// Imported after the mocks are declared so the mocked core module is used. +const { default: DevToolsHookService } = await import('../src/index.js') + +describe('findCurrentTestRange', () => { + const mk = (key: string, testUid?: string): SpecRange => ({ + specFile: 'f', + key, + testUid, + commandStartIdx: 0, + consoleStartIdx: 0, + networkStartIdx: 0, + mutationStartIdx: 0, + traceLogStartIdx: 0, + snapshotCount: 0 + }) + + it('returns the most recent range recorded under the base testUid', () => { + const ranges = [mk('a', 'a'), mk('b', 'b'), mk('b-retry1', 'b')] + // A retry pushes a new range under the same testUid; the latest one wins. + expect(findCurrentTestRange(ranges, 'b')?.key).toBe('b-retry1') + expect(findCurrentTestRange(ranges, 'a')?.key).toBe('a') + }) + + it('returns undefined when no range matches (spec/session slices)', () => { + expect( + findCurrentTestRange([mk('spec.ts', undefined)], 'x') + ).toBeUndefined() + expect(findCurrentTestRange([], 'x')).toBeUndefined() + }) +}) + +describe('DevtoolsService - trace granularity slicing', () => { + const file = '/proj/specs/login.spec.ts' + const mockBrowser = { + isBidi: true, + sessionId: 'sess-1', + scriptAddPreloadScript: vi.fn().mockResolvedValue(undefined), + takeScreenshot: vi.fn().mockResolvedValue('shot'), + execute: vi + .fn() + .mockResolvedValue({ width: 1, height: 1, offsetLeft: 0, offsetTop: 0 }), + on: vi.fn(), + emit: vi.fn(), + options: { rootDir: '/proj' }, + capabilities: { browserName: 'chrome' } + } as never + + beforeEach(() => { + vi.clearAllMocks() + finalizeTraceExport.mockResolvedValue([]) + flushedSlices.length = 0 + }) + + async function newService(granularity: 'session' | 'spec' | 'test') { + const service = new DevToolsHookService({ + mode: 'trace', + tracePolicy: 'retain-on-failure', + traceGranularity: granularity + }) + await service.before({} as never, [], mockBrowser) + return service + } + + it('test granularity: records a per-test slice at beforeTest and eager-flushes it at afterTest', async () => { + const service = await newService('test') + const title = 'login works' + const uid = deterministicUid(file, title) + + service.beforeTest({ file, fullTitle: title }) + expect(flushRangeTrace).not.toHaveBeenCalled() + + await service.afterTest({ file, fullTitle: title }, {}, { passed: true }) + + expect(flushRangeTrace).toHaveBeenCalledTimes(1) + // The flushed range is the one recorded at beforeTest (found via testUid), + // proving both the start-boundary and the eager end-flush. + expect(flushRangeTrace.mock.calls[0]![1]).toMatchObject({ + key: uid, + testUid: uid + }) + expect(flushedSlices).toEqual([{ key: uid, testUid: uid, state: 'passed' }]) + }) + + it('test granularity: a retried test produces a distinct retry slice, each with its own attempt outcome', async () => { + const service = await newService('test') + const title = 'flaky login' + const uid = deterministicUid(file, title) + + // Attempt 1 fails, then a same-process retry (Mocha) passes. + service.beforeTest({ file, fullTitle: title }) + await service.afterTest( + { file, fullTitle: title }, + {}, + { passed: false, error: new Error('boom') } + ) + service.beforeTest({ file, fullTitle: title }) + await service.afterTest({ file, fullTitle: title }, {}, { passed: true }) + + expect(flushRangeTrace).toHaveBeenCalledTimes(2) + // Each attempt is its own slice, and each slice was written with that + // attempt's just-stamped state — the failed first attempt survives the + // retry's beforeTest overwrite (the retain-on-first-failure fix). + expect(flushedSlices).toEqual([ + { key: uid, testUid: uid, state: 'failed' }, + { key: `${uid}-retry1`, testUid: uid, state: 'passed' } + ]) + }) + + it('test granularity: Cucumber scenario eager-flushes its slice at afterScenario', async () => { + const service = await newService('test') + const uri = '/proj/features/login.feature' + const name = 'log in' + const uid = deterministicUid(uri, name) + + service.beforeScenario({ pickle: { uri, name } }) + await service.afterScenario({ pickle: { uri, name } }, { passed: true }) + + expect(flushRangeTrace).toHaveBeenCalledTimes(1) + expect(flushRangeTrace.mock.calls[0]![1]).toMatchObject({ + key: uid, + testUid: uid + }) + }) + + it('spec granularity: no eager flush at afterTest; flushes only when the spec file changes', async () => { + const service = await newService('spec') + const other = '/proj/specs/cart.spec.ts' + + service.beforeTest({ file, fullTitle: 't1' }) + await service.afterTest({ file, fullTitle: 't1' }, {}, { passed: true }) + // Two tests in the same spec: neither the end-flush nor a boundary fires. + service.beforeTest({ file, fullTitle: 't2' }) + await service.afterTest({ file, fullTitle: 't2' }, {}, { passed: true }) + expect(flushRangeTrace).not.toHaveBeenCalled() + + // A new spec file flushes the previous spec's slice (fire-and-forget). + service.beforeTest({ file: other, fullTitle: 't3' }) + expect(flushRangeTrace).toHaveBeenCalledTimes(1) + const range = flushRangeTrace.mock.calls[0]![1] as SpecRange + expect(range.key).toBe(file) + expect(range.testUid).toBeUndefined() + }) + + it('session granularity: records no slices and never flushes per test', async () => { + const service = await newService('session') + + service.beforeTest({ file, fullTitle: 't1' }) + await service.afterTest({ file, fullTitle: 't1' }, {}, { passed: true }) + service.beforeTest({ file, fullTitle: 't2' }) + await service.afterTest({ file, fullTitle: 't2' }, {}, { passed: false }) + + expect(flushRangeTrace).not.toHaveBeenCalled() + }) +}) From 41db0aa0f1a6e1598b1aab165b6bad953b757ad0 Mon Sep 17 00:00:00 2001 From: Vishnu Vardhan <vishnu.p@browserstack.com> Date: Fri, 10 Jul 2026 23:23:26 +0530 Subject: [PATCH 32/68] feat(service): capture expect-webdriverio matchers as assertion actions --- packages/core/src/assert-patcher.ts | 10 +- packages/service/src/action-snapshot.ts | 18 ++ packages/service/src/assert-capture.ts | 45 +++- packages/service/src/call-source.ts | 12 +- packages/service/src/index.ts | 218 ++++++++++++++---- .../service/tests/action-snapshot.test.ts | 29 +++ packages/service/tests/assert-capture.test.ts | 56 +++++ packages/service/tests/assertion-rows.test.ts | 157 +++++++++++++ packages/service/tests/index.test.ts | 64 +++++ 9 files changed, 557 insertions(+), 52 deletions(-) create mode 100644 packages/service/tests/action-snapshot.test.ts create mode 100644 packages/service/tests/assertion-rows.test.ts diff --git a/packages/core/src/assert-patcher.ts b/packages/core/src/assert-patcher.ts index 71ebd9fe..c6b91bf2 100644 --- a/packages/core/src/assert-patcher.ts +++ b/packages/core/src/assert-patcher.ts @@ -168,6 +168,10 @@ export interface MatcherAssertion { passed: boolean message?: string | (() => string) callSource?: string + /** Explicit display label for the action row. Falls back to `command` when + * absent — set it when the framework carries a richer human message than + * `prefix.method` (e.g. Nightwatch's "Testing if the page title contains …"). */ + title?: string } export function matcherAssertionToCommandLog( @@ -177,7 +181,7 @@ export function matcherAssertionToCommandLog( const command = `${input.prefix ?? 'expect'}.${input.method}` const message = typeof input.message === 'function' ? input.message() : input.message - return capturedAssertToCommandLog( + const entry = capturedAssertToCommandLog( { command, args: (input.args ?? []).map(safeSerializeAssertArg), @@ -190,6 +194,10 @@ export function matcherAssertionToCommandLog( }, testUid ) + if (input.title) { + entry.title = input.title + } + return entry } /** diff --git a/packages/service/src/action-snapshot.ts b/packages/service/src/action-snapshot.ts index 12710114..f8dc7fe9 100644 --- a/packages/service/src/action-snapshot.ts +++ b/packages/service/src/action-snapshot.ts @@ -81,6 +81,24 @@ export async function captureActionResult( } } +/** Capture a DOM snapshot for a synthesized action row (e.g. an `expect.*` + * assertion) and push it stamped at the row's OWN timestamp — the trace + * player's Snapshot tab claims it by timestamp the same way it claims a + * regular command's post-action snapshot (see FrameSnapshotIndex.claimAfter). + * Mirrors the tail of `captureActionResult` for a command with no page-settle. */ +export async function pushActionSnapshotAt( + browser: WebdriverIO.Browser, + command: string, + timestamp: number, + actionSnapshots: ActionSnapshot[] +): Promise<void> { + const snap = await captureActionSnapshot(browser, command) + if (snap) { + snap.timestamp = timestamp + actionSnapshots.push(snap) + } +} + export function captureActionSnapshot( browser: WebdriverIO.Browser, command: string diff --git a/packages/service/src/assert-capture.ts b/packages/service/src/assert-capture.ts index 2c277cf8..49725ec8 100644 --- a/packages/service/src/assert-capture.ts +++ b/packages/service/src/assert-capture.ts @@ -9,10 +9,44 @@ import { stripAnsi } from '@wdio/devtools-core' import type { CommandLog, SerializedError } from '@wdio/devtools-shared' +import { parse } from 'stack-trace' +import { + resolveCallSourceFromFrame, + resolveFilePathFromFrame +} from './call-source.js' +import { isUserSpecFile } from './utils.js' import type { SessionCapturer } from './session.js' const log = logger('@wdio/devtools-service:assert-capture') +/** + * Capture the user's `expect()` call site from the SYNCHRONOUS stack at matcher + * entry (call from `beforeAssertion`). The matcher then runs async, so this is + * the only point a user frame is still on the stack — reading it in + * `afterAssertion` resolves to the service bundle instead. Mirrors + * `beforeCommand`'s resolver (`parse(new Error()).reverse()` → first user-spec + * frame → `resolveCallSourceFromFrame`) so assertion rows share regular + * commands' Source-tab behaviour. Also loads that file's source via + * `captureSource` so the tab renders. Returns `undefined` when no user frame is + * present (row falls back to no callSource, exactly as before this fix). + */ +export function resolveAssertionCallSource( + captureSource: (filePath: string) => void +): string | undefined { + Error.stackTraceLimit = 20 + const frame = parse(new Error('')) + .reverse() + .find((f) => isUserSpecFile(f.getFileName())) + if (!frame) { + return undefined + } + const filePath = resolveFilePathFromFrame(frame) + if (filePath) { + captureSource(filePath) + } + return resolveCallSourceFromFrame(frame) +} + /** * Patch node:assert so every tracked assertion lands in the session capturer * as a command. Getters are read at capture time — the capturer instance is @@ -101,11 +135,15 @@ export interface ExpectAssertion { * Adapt expect-webdriverio's afterAssertion params to the shared matcher * converter. Framework-specific extraction only (matcher name, expectedValue → * args, the runtime `pass` vs typed `result` flag); the actual CommandLog - * shaping lives once in core's `matcherAssertionToCommandLog`. + * shaping lives once in core's `matcherAssertionToCommandLog`. `callSource` is + * the user's `expect()` call site captured in `beforeAssertion` (the matcher + * runs async, so afterAssertion's own stack no longer holds a user frame) — it + * makes the row's Source tab point at the spec, not the service bundle. */ export function expectAssertionToCommandLog( params: ExpectAssertion, - testUid: string | undefined + testUid: string | undefined, + callSource?: string ): CommandLog { const { matcherName, expectedValue, result } = params return matcherAssertionToCommandLog( @@ -118,7 +156,8 @@ export function expectAssertionToCommandLog( ? expectedValue : [expectedValue], passed: result.pass ?? result.result ?? false, - message: result.message + message: result.message, + callSource }, testUid ) diff --git a/packages/service/src/call-source.ts b/packages/service/src/call-source.ts index 59325905..53a6fc80 100644 --- a/packages/service/src/call-source.ts +++ b/packages/service/src/call-source.ts @@ -2,8 +2,8 @@ import type { parse } from 'stack-trace' type StackFrame = ReturnType<typeof parse>[number] -/** `<file>:<line>:<column>` from a parsed stack frame; strips file:// and query. */ -export function resolveCallSourceFromFrame( +/** Absolute file path from a parsed stack frame; strips file:// and query. */ +export function resolveFilePathFromFrame( frame: StackFrame ): string | undefined { const rawFile = frame.getFileName() ?? undefined @@ -18,6 +18,14 @@ export function resolveCallSourceFromFrame( if (absPath?.includes('?')) { absPath = absPath.split('?')[0] } + return absPath +} + +/** `<file>:<line>:<column>` from a parsed stack frame; strips file:// and query. */ +export function resolveCallSourceFromFrame( + frame: StackFrame +): string | undefined { + const absPath = resolveFilePathFromFrame(frame) if (absPath === undefined) { return undefined } diff --git a/packages/service/src/index.ts b/packages/service/src/index.ts index ca73e0a2..411d75d7 100644 --- a/packages/service/src/index.ts +++ b/packages/service/src/index.ts @@ -4,9 +4,8 @@ import { errorMessage, finalizeScreencast, finalizeTraceExport, - flushRangeTrace, mapCommandToAction, - recordSpecBoundary, + recordSliceBoundary, resolveAdapterOutputDir, TestAttemptTracker, tracePolicyModeWarning, @@ -16,6 +15,7 @@ import { import { captureExpectFailure, expectAssertionToCommandLog, + resolveAssertionCallSource, wireAssertCapture, type ExpectAssertion } from './assert-capture.js' @@ -27,9 +27,11 @@ import { type TestOutcomeResult } from './test-metadata.js' import { resolveCallSourceFromFrame } from './call-source.js' +import { flushPrevSlice, flushTestSlice } from './trace-slices.js' import { captureActionResult, - captureActionSnapshot + captureActionSnapshot, + pushActionSnapshotAt } from './action-snapshot.js' import { dedupeSnapshotsByTimestamp, @@ -102,6 +104,24 @@ export default class DevToolsHookService implements Services.ServiceInstance { */ #commandStack: CommandFrame[] = [] + /** Depth of the current expect-webdriverio matcher evaluation. While > 0, the + * matcher's internal WebDriver commands (getText/isExisting polling) are + * suppressed so only the `expect.<matcher>` assertion row is captured — + * matching Playwright and the Nightwatch native-assert behaviour. */ + #assertionDepth = 0 + + /** Matcher start timestamps (stack, paired with #assertionDepth) so a captured + * assertion spans [matcher start → end] — its poll duration — instead of + * collapsing to a zero-width point at completion, which left the screencast + * tracking the preceding command during the poll. */ + #assertionStartTimes: number[] = [] + + /** User `expect()` call sites (stack, paired with #assertionStartTimes), + * captured in beforeAssertion while a user frame is still synchronous. The + * matcher runs async, so afterAssertion's own stack resolves to the service + * bundle — this parallel stack lets each row keep its spec-file callSource. */ + #assertionCallSources: (string | undefined)[] = [] + /** Current test UID, set in beforeTest(), used by afterCommand() to tag commands. */ #currentTestUid?: string @@ -119,7 +139,7 @@ export default class DevToolsHookService implements Services.ServiceInstance { /** Set of spec files already flushed to disk. */ #flushedSpecs = new Set<string>() - /** Build the boundary context for recordSpecBoundary — the same shape is + /** Build the boundary context for recordSliceBoundary — the same shape is * needed in both beforeTest and beforeScenario. */ get #boundaryContext() { return { @@ -130,33 +150,41 @@ export default class DevToolsHookService implements Services.ServiceInstance { } } - /** Fire-and-forget flush of a previous spec's trace. The error log is - * inline so the spec-file reference stays precise. */ - #fireAndForgetFlush(prevRange: SpecRange): void { - if (!this.#browser) { + /** Record a trace-slice boundary. `spec` slices per file; `test` per test + * (retries keyed per attempt by core); `session` records nothing. The + * previous-slice flush fires for `spec`; `test` slices eager-flush at their + * own test end (see #eagerFlushTestSlice) so this is only a missed-slice net. */ + #recordBoundary(specFile: string | undefined, testUid?: string): void { + if (!specFile) { return } - void flushRangeTrace(this.#traceContext(this.#browser), prevRange).catch( - (err) => - log.warn( - `Failed to flush trace for spec "${prevRange.specFile}": ${errorMessage(err)}` - ) + const prevRange = recordSliceBoundary( + this.#boundaryContext, + this.#options.traceGranularity, + specFile, + testUid ) + if (prevRange && this.#browser) { + flushPrevSlice(this.#traceContext(this.#browser), prevRange) + } } - /** Record a spec boundary (spec granularity) and flush the previous spec. */ - #recordBoundary(specFile: string | undefined): void { - if (!specFile) { + /** Eager per-test flush at test end (test granularity only), run after the + * outcome is stamped so this attempt's metadata is written before a retry + * overwrites it; the end-of-run finalizer then dedupes it via the key set. */ + async #eagerFlushTestSlice(testUid: string): Promise<void> { + if ( + this.#options.traceGranularity !== 'test' || + this.#options.mode !== 'trace' || + !this.#browser + ) { return } - const prevRange = recordSpecBoundary( - this.#boundaryContext, - specFile, - this.#options.traceGranularity + await flushTestSlice( + this.#traceContext(this.#browser), + this.#specRanges, + testUid ) - if (prevRange) { - this.#fireAndForgetFlush(prevRange) - } } /** Assemble the framework-agnostic trace-export context from this service's @@ -309,16 +337,21 @@ export default class DevToolsHookService implements Services.ServiceInstance { const featureFile = world?.pickle?.uri const scenarioName = world?.pickle?.name + // Derived before recording the boundary so `test` granularity keys the + // slice on the same uid the metadata map uses. + const uid = + featureFile && scenarioName + ? cucumberScenarioUid( + featureFile, + scenarioName, + world?.pickle?.astNodeIds + ) + : undefined - this.#recordBoundary(featureFile) + this.#recordBoundary(featureFile, uid) // ── Test identity for command tagging ── - if (featureFile && scenarioName) { - const uid = cucumberScenarioUid( - featureFile, - scenarioName, - world?.pickle?.astNodeIds - ) + if (uid && scenarioName && featureFile) { this.#currentTestUid = uid this.#attemptTracker.recordStart(uid) this.#testMetadata.set(uid, { @@ -332,15 +365,17 @@ export default class DevToolsHookService implements Services.ServiceInstance { beforeTest(test?: { file?: string; title?: string; fullTitle?: string }) { this.resetStack() - this.#recordBoundary(test?.file) - // Track test identity for command tagging. Generate a stable UID // from file + title so commands can be partitioned across reruns. // WDIO's Test type always provides `fullTitle`; `title` is a - // fallback for non-WDIO frameworks. + // fallback for non-WDIO frameworks. Derived before the boundary so + // `test` granularity keys the slice on the metadata-map uid. const testTitle = test?.fullTitle || test?.title - if (testTitle) { - const uid = testMetadataUid(test?.file, testTitle) + const uid = testTitle ? testMetadataUid(test?.file, testTitle) : undefined + + this.#recordBoundary(test?.file, uid) + + if (uid && testTitle) { this.#currentTestUid = uid this.#attemptTracker.recordStart(uid) this.#testMetadata.set(uid, { @@ -386,10 +421,16 @@ export default class DevToolsHookService implements Services.ServiceInstance { result?: TestOutcomeResult ) { const { uri, name, astNodeIds } = world?.pickle ?? {} - if (uri && name) { - this.#stampOutcome(cucumberScenarioUid(uri, name, astNodeIds), result) + const uid = + uri && name ? cucumberScenarioUid(uri, name, astNodeIds) : undefined + if (uid) { + this.#stampOutcome(uid, result) } await this.#finalizePerScenario() + // Flush now so this slice includes the final snapshot and stamped outcome. + if (uid) { + await this.#eagerFlushTestSlice(uid) + } } async afterTest( @@ -399,22 +440,81 @@ export default class DevToolsHookService implements Services.ServiceInstance { ) { this.#captureExpectFailure(result?.error) const testTitle = test?.fullTitle || test?.title - if (testTitle) { - this.#stampOutcome(testMetadataUid(test?.file, testTitle), result) + const uid = testTitle ? testMetadataUid(test?.file, testTitle) : undefined + if (uid) { + this.#stampOutcome(uid, result) } await this.#finalizePerScenario() + // Flush now so this slice includes the final snapshot and stamped outcome. + if (uid) { + await this.#eagerFlushTestSlice(uid) + } } - /** expect-webdriverio fires this per matcher (pass or fail) via WDIO's - * assertion hook — capture it as an `expect.<matcher>` action so passing - * assertions show in the trace, not just failures. */ - afterAssertion(params: ExpectAssertion): void { + /** expect-webdriverio fires this before each matcher evaluates. The matcher's + * internal polling commands run inside the before→after window and must not + * surface as their own rows — only the `expect.<matcher>` assertion should. */ + beforeAssertion(): void { + // Matchers don't nest, so any residual depth here is a prior matcher whose + // afterAssertion was skipped by a hard throw. Reset before pushing so the + // suppression window can't stay stuck open across assertions. + if (this.#assertionDepth > 0) { + this.#assertionDepth = 0 + this.#assertionStartTimes = [] + this.#assertionCallSources = [] + } + this.#assertionDepth++ + this.#assertionStartTimes.push(Date.now()) + // Capture the user's expect() call site now — see #assertionCallSources. + this.#assertionCallSources.push( + resolveAssertionCallSource( + (file) => void this.#sessionCapturer.captureSource(file) + ) + ) + } + + async afterAssertion(params: ExpectAssertion): Promise<void> { + // Decrement first (even when capture is off) so the window stays balanced. + const startTime = this.#assertionStartTimes.pop() + const callSource = this.#assertionCallSources.pop() + if (this.#assertionDepth > 0) { + this.#assertionDepth-- + } if (this.#options.captureAssertions === false) { return } - this.#sessionCapturer.captureAssertCommand( - expectAssertionToCommandLog(params, this.#currentTestUid) + const entry = expectAssertionToCommandLog( + params, + this.#currentTestUid, + callSource ) + // Span the matcher's poll window so the row's duration is real and the + // screencast tracks the assertion (not the preceding command) during it. + if (startTime !== undefined) { + entry.startTime = startTime + } + // The suppressed matcher-internal command carried the DOM screenshot; + // capture one here so the assertion row's Snapshot panel isn't blank. + if (this.#browser && !isNativeMobile(this.#browser)) { + try { + entry.screenshot = await this.#browser.takeScreenshot() + } catch (err) { + // best-effort: a missing screenshot must not fail the assertion hook + log.debug(`assertion screenshot skipped: ${errorMessage(err)}`) + } + } + // Trace mode: push a DOM action-snapshot stamped at this row's timestamp so + // the trace player's Snapshot tab renders it, exactly like a regular + // command's post-action snapshot (captureActionResult). + if (this.#options.mode === 'trace' && this.#browser) { + await pushActionSnapshotAt( + this.#browser, + entry.command, + entry.timestamp, + this.#actionSnapshots + ) + } + this.#sessionCapturer.captureAssertCommand(entry) } async #finalizePerScenario() { @@ -445,6 +545,9 @@ export default class DevToolsHookService implements Services.ServiceInstance { private resetStack() { this.#commandStack = [] + this.#assertionDepth = 0 + this.#assertionStartTimes = [] + this.#assertionCallSources = [] this.#sessionCapturer.resetLastSelector() this.#sessionCapturer.resetRetryTracker() } @@ -486,7 +589,30 @@ export default class DevToolsHookService implements Services.ServiceInstance { Error.stackTraceLimit = 20 const stack = parse(new Error('')).reverse() const source = stack.find((frame) => isUserSpecFile(frame.getFileName())) - if (source && this.#commandStack.length === 0) { + // #assertionDepth > 0 → we believe we're inside an expect matcher; its + // internal commands trace back to the user's expect() line but must not + // become their own rows (only the expect.<matcher> assertion should). + // expect-webdriverio doesn't wrap afterAssertion in try/finally, though, so + // a matcher whose internal command hard-throws skips it and leaves the depth + // stuck ≥1 — which would then suppress every later user command. When a + // top-level user command arrives with the depth stuck but no live + // expect-webdriverio frame on the stack, the matcher already ended: self-heal + // the leftover depth + parallel stacks so this command captures normally. + if (source && this.#commandStack.length === 0 && this.#assertionDepth > 0) { + const inMatcher = stack.some((frame) => + frame.getFileName()?.includes('expect-webdriverio') + ) + if (!inMatcher) { + this.#assertionDepth = 0 + this.#assertionStartTimes = [] + this.#assertionCallSources = [] + } + } + if ( + source && + this.#commandStack.length === 0 && + this.#assertionDepth === 0 + ) { this.#pushTopLevelCommandFrame( command, resolveCallSourceFromFrame(source) diff --git a/packages/service/tests/action-snapshot.test.ts b/packages/service/tests/action-snapshot.test.ts new file mode 100644 index 00000000..f81be33c --- /dev/null +++ b/packages/service/tests/action-snapshot.test.ts @@ -0,0 +1,29 @@ +import { describe, it, expect, vi } from 'vitest' +import type { ActionSnapshot } from '@wdio/devtools-shared' +import { pushActionSnapshotAt } from '../src/action-snapshot.js' + +const mockBrowser = () => + ({ + execute: vi.fn().mockResolvedValue([]), + takeScreenshot: vi.fn().mockResolvedValue('SHOT'), + getUrl: vi.fn().mockResolvedValue('http://example.com/'), + getTitle: vi.fn().mockResolvedValue('Example') + }) as unknown as WebdriverIO.Browser + +describe('pushActionSnapshotAt', () => { + it('captures a DOM snapshot and stamps it at the row timestamp', async () => { + const snapshots: ActionSnapshot[] = [] + await pushActionSnapshotAt( + mockBrowser(), + 'expect.toExist', + 12345, + snapshots + ) + expect(snapshots).toHaveLength(1) + // Stamped at the row's own timestamp — not the capture time — so the trace + // player's FrameSnapshotIndex.claimAfter(cmd.timestamp) matches it. + expect(snapshots[0]!.timestamp).toBe(12345) + expect(snapshots[0]!.command).toBe('expect.toExist') + expect(snapshots[0]!.screenshot).toBe('SHOT') + }) +}) diff --git a/packages/service/tests/assert-capture.test.ts b/packages/service/tests/assert-capture.test.ts index d1486257..534945e3 100644 --- a/packages/service/tests/assert-capture.test.ts +++ b/packages/service/tests/assert-capture.test.ts @@ -7,11 +7,30 @@ import { import { captureExpectFailure, expectAssertionToCommandLog, + resolveAssertionCallSource, toCommandError, wireAssertCapture } from '../src/assert-capture.js' import type { SessionCapturer } from '../src/session.js' +const stackFrames = vi.hoisted(() => ({ + value: [] as Array<{ + getFileName: () => string | null + getLineNumber: () => number | null + getColumnNumber: () => number | null + }> +})) + +// Only resolveAssertionCallSource reads 'stack-trace'; node:assert capture uses +// core's stacktrace-parser path, so mocking here doesn't affect those tests. +vi.mock('stack-trace', () => ({ parse: () => stackFrames.value })) + +const frame = (file: string | null, line = 1, column = 1) => ({ + getFileName: () => file, + getLineNumber: () => line, + getColumnNumber: () => column +}) + describe('toCommandError', () => { it('normalizes a plain Error object (ANSI stripped)', () => { // Matcher errors are skipped now (afterAssertion owns them); a plain @@ -196,4 +215,41 @@ describe('expectAssertionToCommandLog', () => { ) expect(entry).toMatchObject({ command: 'expect.toBeClickable', args: [] }) }) + + it('forwards the captured callSource onto the assertion row', () => { + const entry = expectAssertionToCommandLog( + { matcherName: 'toExist', result: { pass: true } }, + 'uid-1', + '/proj/specs/login.e2e.ts:30:7' + ) + expect(entry.callSource).toBe('/proj/specs/login.e2e.ts:30:7') + }) +}) + +describe('resolveAssertionCallSource', () => { + it('returns the outermost user-spec frame and loads its source', () => { + // innermost → outermost: service bundle, expect-webdriverio matcher, the + // user spec, then node internals. The user spec must win, not the bundle. + stackFrames.value = [ + frame('/repo/packages/service/dist/index.js', 4045, 12), + frame('/repo/node_modules/expect-webdriverio/lib/matchers.js', 9, 3), + frame('/proj/specs/login.e2e.ts', 30, 7), + frame('node:internal/process/task_queues', 95, 5) + ] + const captured: string[] = [] + const callSource = resolveAssertionCallSource((f) => captured.push(f)) + expect(callSource).toBe('/proj/specs/login.e2e.ts:30:7') + expect(callSource).not.toContain('/dist/') + expect(captured).toEqual(['/proj/specs/login.e2e.ts']) + }) + + it('returns undefined and loads nothing when only dependency frames exist', () => { + stackFrames.value = [ + frame('/repo/node_modules/expect-webdriverio/lib/matchers.js', 9, 3), + frame('node:internal/process/task_queues', 95, 5) + ] + const captured: string[] = [] + expect(resolveAssertionCallSource((f) => captured.push(f))).toBeUndefined() + expect(captured).toEqual([]) + }) }) diff --git a/packages/service/tests/assertion-rows.test.ts b/packages/service/tests/assertion-rows.test.ts new file mode 100644 index 00000000..5a5b09ac --- /dev/null +++ b/packages/service/tests/assertion-rows.test.ts @@ -0,0 +1,157 @@ +import { describe, it, expect, vi, beforeEach, afterAll } from 'vitest' +import assert from 'node:assert' +import { + ASSERT_PATCHED_SYMBOL, + TRACKED_ASSERT_METHODS +} from '@wdio/devtools-core' + +// Controlled synchronous stack for the beforeAssertion call-source walk. +const stackFrames = vi.hoisted(() => ({ + value: [] as Array<{ + getFileName: () => string | null + getLineNumber: () => number | null + getColumnNumber: () => number | null + }> +})) +vi.mock('stack-trace', () => ({ parse: () => stackFrames.value })) + +const capturer = vi.hoisted(() => ({ + captureAssertCommand: vi.fn(), + captureSource: vi.fn().mockResolvedValue(undefined), + injectScript: vi.fn().mockResolvedValue(undefined), + sendUpstream: vi.fn(), + cleanup: vi.fn(), + resetLastSelector: vi.fn(), + resetRetryTracker: vi.fn(), + commandsLog: [] as unknown[], + sources: new Map<string, string>() +})) +vi.mock('../src/session.js', () => ({ + SessionCapturer: vi.fn(function (this: unknown) { + return capturer + }) +})) + +const pushActionSnapshotAt = vi.hoisted(() => + vi.fn().mockResolvedValue(undefined) +) +vi.mock('../src/action-snapshot.js', () => ({ + pushActionSnapshotAt, + captureActionSnapshot: vi.fn().mockResolvedValue(null), + captureActionResult: vi.fn().mockResolvedValue(undefined) +})) + +import DevToolsHookService from '../src/index.js' +import type { ExpectAssertion } from '../src/assert-capture.js' + +const userFrame = { + getFileName: () => '/proj/specs/login.e2e.ts', + getLineNumber: () => 30, + getColumnNumber: () => 7 +} + +const mockBrowser = { + isBidi: true, + sessionId: 's1', + options: {}, + capabilities: {}, + takeScreenshot: vi.fn().mockResolvedValue('SHOT'), + execute: vi + .fn() + .mockResolvedValue({ width: 1, height: 1, offsetLeft: 0, offsetTop: 0 }), + on: vi.fn(), + emit: vi.fn() +} as unknown as WebdriverIO.Browser + +// before() wires node:assert capture; restore the real methods afterwards. +const ASSERT_MUT = assert as unknown as Record<string | symbol, unknown> +const originals: Record<string, unknown> = {} +for (const method of TRACKED_ASSERT_METHODS) { + originals[method] = ASSERT_MUT[method] +} +afterAll(() => { + delete ASSERT_MUT[ASSERT_PATCHED_SYMBOL] + for (const method of TRACKED_ASSERT_METHODS) { + ASSERT_MUT[method] = originals[method] + } +}) + +const capturedEntry = () => capturer.captureAssertCommand.mock.calls[0]![0] + +describe('DevtoolsService — expect.* assertion rows', () => { + beforeEach(() => { + vi.clearAllMocks() + stackFrames.value = [userFrame] + }) + + it('trace mode: user-spec callSource, spec source loaded, DOM snapshot pushed', async () => { + const service = new DevToolsHookService({ mode: 'trace' }) + await service.before({} as never, [], mockBrowser) + + service.beforeAssertion() + const params: ExpectAssertion = { + matcherName: 'toExist', + result: { pass: true, message: () => 'ok' } + } + await service.afterAssertion(params) + + const entry = capturedEntry() + expect(entry.command).toBe('expect.toExist') + // The regression: callSource must point at the user's spec, not the + // service bundle (…/service/dist/index.js). + expect(entry.callSource).toBe('/proj/specs/login.e2e.ts:30:7') + expect(entry.callSource).not.toContain('/dist/') + // Source of that file is loaded so the Source tab can render it. + expect(capturer.captureSource).toHaveBeenCalledWith( + '/proj/specs/login.e2e.ts' + ) + // Live-mode screenshot kept for the CommandLog. + expect(entry.screenshot).toBe('SHOT') + // Trace-player Snapshot tab: a DOM snapshot stamped at the row timestamp. + expect(pushActionSnapshotAt).toHaveBeenCalledWith( + mockBrowser, + 'expect.toExist', + entry.timestamp, + expect.any(Array) + ) + // WDIO reconciles rows by timestamp (like every regular WDIO command), + // so assertion rows carry no public id — parity with regular commands. + expect(entry.id).toBeUndefined() + }) + + it('live mode: keeps the screenshot but pushes no DOM snapshot', async () => { + const service = new DevToolsHookService({ mode: 'live' }) + await service.before({} as never, [], mockBrowser) + + service.beforeAssertion() + await service.afterAssertion({ + matcherName: 'toHaveText', + expectedValue: 'Hi', + result: { pass: false, message: () => 'nope' } + }) + + const entry = capturedEntry() + expect(entry.command).toBe('expect.toHaveText') + expect(entry.callSource).toBe('/proj/specs/login.e2e.ts:30:7') + expect(entry.error).toMatchObject({ message: 'nope' }) + expect(entry.screenshot).toBe('SHOT') + expect(pushActionSnapshotAt).not.toHaveBeenCalled() + }) + + it('captureAssertions: false suppresses the row but keeps the window balanced', async () => { + const service = new DevToolsHookService({ + mode: 'trace', + captureAssertions: false + }) + await service.before({} as never, [], mockBrowser) + + service.beforeAssertion() + await service.afterAssertion({ + matcherName: 'toExist', + result: { pass: true } + }) + + expect(capturer.captureAssertCommand).not.toHaveBeenCalled() + expect(pushActionSnapshotAt).not.toHaveBeenCalled() + }) +}) diff --git a/packages/service/tests/index.test.ts b/packages/service/tests/index.test.ts index 4fab53d5..d46921e0 100644 --- a/packages/service/tests/index.test.ts +++ b/packages/service/tests/index.test.ts @@ -15,6 +15,8 @@ const mockSessionCapturerInstance = { afterCommand: vi.fn(), sendUpstream: vi.fn(), injectScript: vi.fn().mockResolvedValue(undefined), + captureSource: vi.fn(), + captureAssertCommand: vi.fn(), cleanup: vi.fn(), commandsLog: [], sources: new Map(), @@ -278,3 +280,65 @@ describe('DevtoolsService - Screencast Integration', () => { expect(mockScreencastRecorder.start).toHaveBeenCalledWith(mockBrowser) }) }) + +describe('DevtoolsService - assertion suppression self-heal', () => { + let service: DevToolsHookService + const mockBrowser = { + isBidi: true, + sessionId: 'heal-session', + scriptAddPreloadScript: vi.fn().mockResolvedValue(undefined), + takeScreenshot: vi.fn().mockResolvedValue('screenshot'), + execute: vi.fn().mockResolvedValue({ + width: 1200, + height: 800, + offsetLeft: 0, + offsetTop: 0 + }), + on: vi.fn(), + emit: vi.fn() + } as any + + const capturedCommands = () => + mockSessionCapturerInstance.afterCommand.mock.calls.map((call) => call[1]) + + beforeEach(async () => { + vi.clearAllMocks() + service = new DevToolsHookService() + await service.before({} as any, [], mockBrowser) + vi.clearAllMocks() + }) + + // expect-webdriverio doesn't wrap afterAssertion in try/finally, so a matcher + // whose internal command hard-throws runs beforeAssertion but skips + // afterAssertion, leaving #assertionDepth stuck ≥ 1. Without the self-heal + // that stuck depth suppresses every later user command in the test. + it('captures a top-level command after a matcher throw left the assertion depth stuck', async () => { + service.beforeAssertion() + service.beforeAssertion() // depth now 2, no matching afterAssertion + + // The mocked stack is a user-spec frame with no expect-webdriverio frame, + // so the command is recognised as top-level and the leftover depth resets. + service.beforeCommand('click' as any, ['.button']) + await service.afterCommand('click' as any, ['.button'], undefined) + + expect(capturedCommands()).toEqual(['click']) + }) + + it('a fresh beforeAssertion resets a stuck depth so the window stays balanced', async () => { + service.beforeAssertion() + service.beforeAssertion() // leftover depth from a prior throw + + // A new matcher starts (resetting the stuck depth) and completes normally. + service.beforeAssertion() + await service.afterAssertion({ + matcherName: 'toBeDisplayed', + result: { pass: true } + } as any) + + // Depth is balanced again, so the next top-level command is captured. + service.beforeCommand('setValue' as any, ['.input', 'hi']) + await service.afterCommand('setValue' as any, ['.input', 'hi'], undefined) + + expect(capturedCommands()).toEqual(['setValue']) + }) +}) From 6b068e2d459c60451380c51006ce44829f93f447 Mon Sep 17 00:00:00 2001 From: Vishnu Vardhan <vishnu.p@browserstack.com> Date: Fri, 10 Jul 2026 23:23:51 +0530 Subject: [PATCH 33/68] feat(nightwatch): capture native browser.assert/verify as assertion actions --- .../src/helpers/browserProxy.ts | 99 ++++- .../src/helpers/nativeAssertions.ts | 299 +++++++++++++++ packages/nightwatch-devtools/src/index.ts | 80 +++-- .../nightwatch-devtools/src/session-init.ts | 4 +- packages/nightwatch-devtools/src/session.ts | 35 ++ packages/nightwatch-devtools/src/types.ts | 16 +- .../tests/attemptTracking.test.ts | 4 +- .../tests/browserProxy.test.ts | 168 +++++++++ .../tests/nativeAssertions.test.ts | 340 ++++++++++++++++++ 9 files changed, 1011 insertions(+), 34 deletions(-) create mode 100644 packages/nightwatch-devtools/src/helpers/nativeAssertions.ts create mode 100644 packages/nightwatch-devtools/tests/browserProxy.test.ts create mode 100644 packages/nightwatch-devtools/tests/nativeAssertions.test.ts diff --git a/packages/nightwatch-devtools/src/helpers/browserProxy.ts b/packages/nightwatch-devtools/src/helpers/browserProxy.ts index 019ebfb2..dff01a1f 100644 --- a/packages/nightwatch-devtools/src/helpers/browserProxy.ts +++ b/packages/nightwatch-devtools/src/helpers/browserProxy.ts @@ -10,11 +10,16 @@ import { } from '../constants.js' import { getCallSourceFromStack } from './utils.js' import { serializeCommandResult } from './serializeCommandResult.js' +import { + latestResolvedScreenshot, + pendingAssertionCommand +} from './nativeAssertions.js' import { RetryTracker, toError } from '@wdio/devtools-core' import type { SessionCapturer } from '../session.js' import type { TestManager } from './testManager.js' import type { CommandLog, + NativeAssertCall, NightwatchBrowser, NightwatchCurrentTest, CommandStackFrame @@ -42,10 +47,20 @@ export class BrowserProxy { */ private retryTracker = new RetryTracker() + /** + * Per-test buffer of explicit `browser.assert.*` / `browser.verify.*` calls, + * recorded synchronously at call time by {@link wrapAssertionNamespaces}. + * Nightwatch exposes no per-assertion hook and its test-end results carry no + * source location for passing assertions, so call-time capture is the only + * way to get real args + callSource. Drained (and cleared) each `afterEach`. + */ + private nativeAssertCalls: NativeAssertCall[] = [] + constructor( private sessionCapturer: SessionCapturer, private testManager: TestManager, - private getCurrentTest: () => { uid?: string } | null + private getCurrentTest: () => { uid?: string } | null, + private captureAssertions = true ) {} /** @@ -60,6 +75,78 @@ export class BrowserProxy { this.commandStack = [] this.lastCommandSig = null this.retryTracker.reset() + this.nativeAssertCalls = [] + } + + /** Hand off this test's recorded native-assertion calls and clear the + * buffer so the next test starts fresh. */ + drainNativeAssertCalls(): NativeAssertCall[] { + const calls = this.nativeAssertCalls + this.nativeAssertCalls = [] + return calls + } + + /** + * Replace `browser.assert` / `browser.verify` (both are Nightwatch Proxies + * whose `get` returns a fresh function per access) with a recording Proxy: + * on each method call it captures `{prefix, method, args, callSource}` from a + * user-code frame, streams a neutral pending row immediately (live), then + * delegates to the ORIGINAL namespace method so Nightwatch's queue, chaining, + * and abortOnFailure semantics (assert aborts, verify does not) are + * byte-for-byte unchanged. Called once per browser. + */ + private wrapAssertionNamespaces(browser: NightwatchBrowser): void { + // captureAssertions:false → leave assert/verify original so no neutral + // pending rows stream (the afterEach finalize path is gated to match). + if (!this.captureAssertions) { + return + } + // Cast once: the assert/verify namespaces aren't on the public type; each + // is a dynamic method bag reached by property name. + const b = browser as unknown as Record<string, unknown> + ;(['assert', 'verify'] as const).forEach((prefix) => { + const original = b[prefix] + if (!original || typeof original !== 'object') { + return + } + b[prefix] = new Proxy(original as object, { + get: (target, name, receiver) => { + const orig = Reflect.get(target, name, receiver) + // `not` (negation Proxy) and non-method props pass through untouched. + if (typeof orig !== 'function' || typeof name !== 'string') { + return orig + } + return (...args: unknown[]) => { + const callInfo = getCallSourceFromStack() + if (callInfo.filePath !== undefined) { + this.emitPendingAssertion({ + prefix, + method: name, + args, + callSource: callInfo.callSource, + timestamp: Date.now() + }) + } + return (orig as (...a: unknown[]) => unknown)(...args) + } + } + }) + }) + } + + /** Stream a neutral pending row for an explicit assert/verify call the moment + * it's invoked, and buffer the call (with its emitted row) for `afterEach` + * to finalize pass/fail in place. */ + private emitPendingAssertion(call: NativeAssertCall): void { + const testUid = this.getCurrentTest()?.uid + const entry = pendingAssertionCommand( + call, + testUid, + latestResolvedScreenshot(this.sessionCapturer) + ) + this.sessionCapturer.captureAssertCommand(entry) + call.entry = entry + this.nativeAssertCalls.push(call) } getCurrentTestFullPath(): string | null { @@ -189,6 +276,7 @@ export class BrowserProxy { wrappedMethods.push(methodName) }) + this.wrapAssertionNamespaces(browser) this.proxiedBrowsers.add(browser as object) log.info(`✓ Wrapped ${wrappedMethods.length} browser methods`) } @@ -307,6 +395,7 @@ export class BrowserProxy { logArgs: unknown[], cmdSig: string, callSource: string | undefined, + hasUserSource: boolean, commandTimestamp: number, testUid: string | undefined, userCallback: Function | null @@ -318,7 +407,12 @@ export class BrowserProxy { methodName ) const effectiveUid = this.getCurrentTest()?.uid ?? testUid - if (effectiveUid) { + // Only surface commands that originate from a user-code frame. Commands + // Nightwatch issues from inside its own queue (e.g. the getTitle a + // `browser.assert.titleContains` runs) execute in a detached tick with no + // user frame on the stack, so they'd otherwise leak as top-level actions + // with an "unknown" source. Mirrors the service's user-spec-source guard. + if (effectiveUid && hasUserSource) { if (this.retryTracker.isRetry(cmdSig)) { this.handleRetryReplacement( browser, @@ -402,6 +496,7 @@ export class BrowserProxy { logArgs, cmdSig, callSource, + callInfo.filePath !== undefined, commandTimestamp, this.getCurrentTest()?.uid, userCallback diff --git a/packages/nightwatch-devtools/src/helpers/nativeAssertions.ts b/packages/nightwatch-devtools/src/helpers/nativeAssertions.ts new file mode 100644 index 00000000..16dd3bd2 --- /dev/null +++ b/packages/nightwatch-devtools/src/helpers/nativeAssertions.ts @@ -0,0 +1,299 @@ +// Native-assertion capture: turns explicit `browser.assert.*` / +// `browser.verify.*` calls into concise trace/UI action rows with real args, +// a clickable source location, and pass/fail colour — streamed LIVE. +// +// Nightwatch exposes no per-assertion hook, and its test-end +// `currentTest.results` carries no source location for PASSING assertions +// (results.assertions[i].stackTrace is '' on pass) and only stringified args. +// So each explicit call is intercepted at CALL TIME +// (BrowserProxy.wrapAssertionNamespaces): a neutral "pending" row is emitted +// immediately (concise title, real args, callSource) so rows stream in one by +// one like normal commands. At TEST END the pass/fail truth + verbose failure +// message are read from results.assertions and the already-emitted row is +// UPDATED IN PLACE (same stable id) — never re-created, so no duplicates. +// Iterating the recorded calls (not results.assertions) also excludes +// Nightwatch's implicit command-generated assertions (e.g. +// waitForElementVisible's "element was visible" entry). + +import logger from '@wdio/logger' +import { + matcherAssertionToCommandLog, + safeSerializeAssertArg, + stripAnsi +} from '@wdio/devtools-core' +import type { SessionCapturer } from '../session.js' +import type { + CommandLog, + NativeAssertCall, + NightwatchBrowser, + NightwatchCurrentTest +} from '../types.js' + +const log = logger('@wdio/nightwatch-devtools:nativeAssertions') + +/** + * One entry Nightwatch pushes to `results.assertions` (from + * `NightwatchAssertion.getAssertResult`, lib/assertion/assertion.js). Carries + * only a human message — no method/args. `failure === false` is the reliable + * pass signal; any truthy `failure` (message string, or `true`) means failed. + */ +interface NwAssertionEntry { + message?: string + fullMsg?: string + failure?: string | boolean +} + +/** One entry Nightwatch pushes to `results.commands` (lib/reporter/index.js + * logCommandResult). For an assert/verify `name` is the namespaced method and + * `startTime`/`endTime` are the real queue-execution window in ms + * (treenode.js). Read only to position the finalized row on the timeline. */ +interface NwCommandEntry { + name?: string + startTime?: number + endTime?: number +} + +const ASSERT_CMD_RE = /^(assert|verify)\.\w+$/ + +/** Real per-assertion execution windows, in call order, aligned to `count` + * recorded calls. Nightwatch enqueues assertions synchronously (all at once) + * but runs them later one at a time, so the enqueue timestamp clusters the + * rows; this recovers each row's true execution time so the trace timeline + * spreads them out. Returns `null` per slot when the executed-command count + * doesn't line up (e.g. retries) — the enqueue timestamp is kept then. */ +function assertCommandTimings( + commands: NwCommandEntry[], + count: number +): Array<{ startTime: number; endTime: number } | null> { + const executed = commands.filter( + (c) => typeof c?.name === 'string' && ASSERT_CMD_RE.test(c.name) + ) + if (executed.length !== count) { + return new Array(count).fill(null) + } + return executed.map((c) => + typeof c.startTime === 'number' && typeof c.endTime === 'number' + ? { startTime: c.startTime, endTime: c.endTime } + : null + ) +} + +/** Nightwatch embeds the assertion arguments in the message text + * (AssertionInstance.initialize → Logger.formatMessage), so a passing entry + * for `titleContains('Example')` reads "Testing if the page title contains + * 'Example'". Match a call to its result entry when every string/number arg + * appears in the message. */ +function messageMatchesArgs(entry: NwAssertionEntry, args: unknown[]): boolean { + const text = String(entry.fullMsg ?? entry.message ?? '') + const literals = args.filter( + (a): a is string | number => typeof a === 'string' || typeof a === 'number' + ) + return literals.length > 0 && literals.every((a) => text.includes(String(a))) +} + +interface Outcome { + passed: boolean + message: string +} + +/** + * Pair each recorded call with its `results.assertions` entry for pass/fail + + * verbose message. Both lists are in call/execution order and each explicit + * call produces exactly one assertion entry, so: match by args-in-message + * first (most specific, skips interleaved implicit entries), then fall back to + * the next unconsumed entry positionally. A call with no matching entry left + * (never happens for a real assertion) is dropped (`null`). + */ +function correlate( + calls: NativeAssertCall[], + assertions: NwAssertionEntry[] +): Array<Outcome | null> { + const consumed = new Array(assertions.length).fill(false) + const toOutcome = (idx: number): Outcome => { + consumed[idx] = true + const entry = assertions[idx] + return { + passed: !entry.failure, + message: stripAnsi(String(entry.fullMsg ?? entry.message ?? '')).trim() + } + } + const matched = calls.map((call) => { + const idx = assertions.findIndex( + (entry, i) => !consumed[i] && messageMatchesArgs(entry, call.args) + ) + return idx === -1 ? null : toOutcome(idx) + }) + return matched.map((outcome) => { + if (outcome) { + return outcome + } + const idx = consumed.findIndex((used) => !used) + return idx === -1 ? null : toOutcome(idx) + }) +} + +/** Last already-resolved screenshot in the command log — the DOM the assertion + * evaluated against (title/most asserts don't mutate it). Synchronous, so it's + * usable from the call-time wrapper. */ +export function latestResolvedScreenshot( + capturer: SessionCapturer +): string | null { + for (let i = capturer.commandsLog.length - 1; i >= 0; i--) { + const shot = capturer.commandsLog[i]?.screenshot + if (shot) { + return shot + } + } + return null +} + +/** Reuse the nearest preceding command's screenshot; if the fire-and-forget + * capture hasn't resolved yet (race), fall back to a fresh end-of-test one. */ +async function resolveAssertionScreenshot( + capturer: SessionCapturer, + browser: NightwatchBrowser +): Promise<string | null> { + return ( + latestResolvedScreenshot(capturer) ?? + (await capturer.takeScreenshotViaHttp(browser)) + ) +} + +/** `assert.titleContains('SOFT_FAIL_ME')` — the concise row label. Strings are + * quoted, objects elided; never the verbose failure message. */ +function conciseTitle(call: NativeAssertCall): string { + const preview = call.args + .map((a) => + typeof a === 'string' + ? `'${a}'` + : a !== null && typeof a === 'object' + ? '…' + : String(a) + ) + .join(', ') + return `${call.prefix}.${call.method}(${preview})` +} + +/** + * Build the neutral "pending" row emitted the moment an assert/verify is + * called — everything known at call time (concise title, real args, callSource, + * screenshot) but NO result/error yet, so it renders neutral (not red/green) + * and streams in like a normal in-flight command. `startTime`/`timestamp` are + * the call time; the row is finalized in place later by + * {@link captureNativeAssertions}. + */ +export function pendingAssertionCommand( + call: NativeAssertCall, + testUid: string | undefined, + screenshot: string | null +): CommandLog { + const entry: CommandLog = { + command: `${call.prefix}.${call.method}`, + args: call.args.map(safeSerializeAssertArg), + title: conciseTitle(call), + timestamp: call.timestamp, + startTime: call.timestamp + } + if (call.callSource) { + entry.callSource = call.callSource + } + if (testUid) { + entry.testUid = testUid + } + if (screenshot) { + entry.screenshot = screenshot + } + return entry +} + +/** Update one streamed pending row in place: apply pass/fail + verbose error, + * a screenshot, and its real execution window, then re-broadcast by stable id. */ +function finalizeAssertionRow( + capturer: SessionCapturer, + call: NativeAssertCall, + outcome: Outcome, + timing: { startTime: number; endTime: number } | null, + screenshot: string | null, + testUid: string | undefined +): void { + const entry = call.entry! + const finalized = matcherAssertionToCommandLog( + { + prefix: call.prefix, + method: call.method, + args: call.args, + passed: outcome.passed, + message: outcome.message || `${call.prefix}.${call.method} failed`, + callSource: call.callSource, + title: entry.title + }, + testUid + ) + entry.result = finalized.result + entry.error = finalized.error + if (!entry.screenshot && screenshot) { + entry.screenshot = screenshot + } + // Reposition the row on its REAL execution window (Nightwatch enqueues all + // asserts at once, so the emit/enqueue timestamp clustered them). The row is + // matched for replacement by its stable id, so the old enqueue timestamp is + // what the UI still keys on until this swap lands. + const oldTimestamp = entry.timestamp + if (timing) { + entry.startTime = timing.startTime + entry.timestamp = + timing.endTime > timing.startTime ? timing.endTime : timing.startTime + } + capturer.sendReplaceCommand(oldTimestamp, entry) + log.info(`[assert] ${entry.title} → ${outcome.passed ? 'pass' : 'fail'}`) +} + +/** + * Finalize the streamed pending rows at test-end: correlate the recorded calls + * with `results.assertions`, then UPDATE each row's `entry` in place with + * pass/fail (`result`) + the verbose failure message (`error`, failures only) + * + a screenshot + its real execution window, and re-broadcast via + * `sendReplaceCommand` keyed on the row's stable id. No new rows are created + * (no duplicates); a call with no matching result is left pending (defensive). + */ +export async function captureNativeAssertions( + capturer: SessionCapturer, + browser: NightwatchBrowser, + currentTest: NightwatchCurrentTest | undefined, + testUid: string | undefined, + calls: NativeAssertCall[] +): Promise<void> { + if (calls.length === 0) { + return + } + // Boundary cast: `results` is Nightwatch's loosely-typed per-test bag; we read + // only the assertions + commands arrays whose shapes are documented above. + const results = currentTest?.results as + | { assertions?: NwAssertionEntry[]; commands?: NwCommandEntry[] } + | undefined + const assertions = Array.isArray(results?.assertions) + ? results.assertions + : [] + const outcomes = correlate(calls, assertions) + const timings = assertCommandTimings( + Array.isArray(results?.commands) ? results.commands : [], + calls.length + ) + // One shared screenshot for all rows — same DOM, ran consecutively. + const screenshot = await resolveAssertionScreenshot(capturer, browser) + + calls.forEach((call, index) => { + const outcome = outcomes[index] + // Leave an unmatched (or never-emitted) row in its last state. + if (call.entry && outcome) { + finalizeAssertionRow( + capturer, + call, + outcome, + timings[index], + screenshot, + testUid + ) + } + }) +} diff --git a/packages/nightwatch-devtools/src/index.ts b/packages/nightwatch-devtools/src/index.ts index e94877bc..db5cefc0 100644 --- a/packages/nightwatch-devtools/src/index.ts +++ b/packages/nightwatch-devtools/src/index.ts @@ -9,8 +9,7 @@ import { fileURLToPath } from 'node:url' import { errorMessage, finalizeTraceExport, - flushRangeTrace, - recordSpecBoundary, + flushRangeLogged, TestAttemptTracker, tracePolicyModeWarning, type SpecRange, @@ -72,6 +71,8 @@ import { ensureSessionInitialized, finalizeCurrentScreencast } from './session-init.js' +import { captureNativeAssertions } from './helpers/nativeAssertions.js' +import { flushTestSlice, recordSpecSliceBoundary } from './trace-slices.js' import { getTestIcon, incrementCounters, @@ -179,6 +180,9 @@ class NightwatchDevToolsPlugin { get bidiEnabled() { return self.#bidiEnabled }, + get captureAssertions() { + return self.options.captureAssertions + }, get sessionCapturer() { return self.sessionCapturer }, @@ -305,11 +309,29 @@ class NightwatchDevToolsPlugin { setCucumberRunner: (v) => { self.#isCucumberRunner = v }, - getRerunLabel: () => self.#getRerunLabel() + getRerunLabel: () => self.#getRerunLabel(), + get traceMode() { + return self.options.mode === 'trace' + }, + get traceGranularity() { + return self.options.traceGranularity + }, + get specRanges() { + return self.#specRanges + }, + get flushedSpecs() { + return self.#flushedSpecs + }, + flushTraceRange: (range) => self.#flushSpecTrace(range) } return this.#internals } + /** Boundary cast: currentTest is Nightwatch's loose bag; only uid is read. */ + #currentTestUid(): string | undefined { + return (this.#currentTest as { uid?: string } | null)?.uid + } + #handleReuseMode(): void { handleReuseMode(this.#getInternals()) } @@ -330,8 +352,7 @@ class NightwatchDevToolsPlugin { if (this.options.captureAssertions) { wireAssertCapture( () => this.sessionCapturer, - // Boundary cast: currentTest is Nightwatch's loose bag; only uid is read. - () => (this.#currentTest as { uid?: string } | null)?.uid + () => this.#currentTestUid() ) } } @@ -401,13 +422,15 @@ class NightwatchDevToolsPlugin { async #startNextTest( currentSuite: SuiteStats, currentTestName: string, - processedTests: Set<string> + processedTests: Set<string>, + specFile: string | null ): Promise<void> { await startNextTest( this.#getInternals(), currentSuite, currentTestName, - processedTests + processedTests, + specFile ) } @@ -453,25 +476,8 @@ class NightwatchDevToolsPlugin { this.sessionCapturer.captureSource(fullPath).catch(() => {}) } - // ── Per-spec boundary detection ── if (fullPath) { - const prevRange = recordSpecBoundary( - { - specRanges: this.#specRanges, - flushedSpecs: this.#flushedSpecs, - capturer: this.sessionCapturer, - actionSnapshots: this.sessionCapturer.actionSnapshots - }, - fullPath, - this.options.traceGranularity - ) - if (prevRange) { - void this.#flushSpecTrace(prevRange).catch((err) => - log.warn( - `Failed to flush trace for spec "${prevRange.specFile}": ${errorMessage(err)}` - ) - ) - } + recordSpecSliceBoundary(this.#getInternals(), fullPath) } await this.#closePreviousRunningTest(currentSuite, testFile, currentTest) @@ -483,7 +489,12 @@ class NightwatchDevToolsPlugin { processedTests ) if (currentTestName) { - await this.#startNextTest(currentSuite, currentTestName, processedTests) + await this.#startNextTest( + currentSuite, + currentTestName, + processedTests, + fullPath + ) } this.#wrapBrowserOnce(browser) } @@ -497,7 +508,18 @@ class NightwatchDevToolsPlugin { if (browser && this.sessionCapturer) { try { await this.#closeOutTestcases(browser) + if (this.options.captureAssertions) { + await captureNativeAssertions( + this.sessionCapturer, + browser, + browser.currentTest as NightwatchCurrentTest | undefined, + this.#currentTestUid(), + this.browserProxy.drainNativeAssertCalls() + ) + } await this.sessionCapturer.captureTrace(browser) + // Flush this test's slice before the next test overwrites its outcome. + flushTestSlice(this.#getInternals()) } catch (err) { log.error(`Failed to capture trace: ${errorMessage(err)}`) } @@ -542,13 +564,15 @@ class NightwatchDevToolsPlugin { logRunSummary(this.#getInternals()) } - /** Thin wrapper so boundary flushes and the final flush share one path. */ + /** Thin wrapper so boundary flushes and the final flush share one path. + * flushRangeLogged logs+swallows a failed flush (shared spec/test string) so + * the fire-and-forget boundary callers don't each re-implement the guard. */ #flushSpecTrace(range: SpecRange): Promise<TraceArtifact | undefined> { const sessionId = this.sessionCapturer.metadata?.sessionId if (!sessionId) { return Promise.resolve(undefined) } - return flushRangeTrace(this.#traceContext(sessionId), range) + return flushRangeLogged(this.#traceContext(sessionId), range) } /** Assemble the framework-agnostic trace-export context from plugin state. diff --git a/packages/nightwatch-devtools/src/session-init.ts b/packages/nightwatch-devtools/src/session-init.ts index 2bccfc82..e9d1004a 100644 --- a/packages/nightwatch-devtools/src/session-init.ts +++ b/packages/nightwatch-devtools/src/session-init.ts @@ -39,6 +39,7 @@ export interface SessionInitCtx { readonly screencastOptions: ScreencastOptions readonly bidiEnabled: boolean readonly mode: DevToolsMode + readonly captureAssertions: boolean sessionCapturer: SessionCapturer testReporter: TestReporter @@ -98,7 +99,8 @@ function initReporterChain(ctx: SessionInitCtx): void { ctx.browserProxy = new BrowserProxy( ctx.sessionCapturer, ctx.testManager, - () => ctx.getCurrentTest() ?? ctx.getCurrentScenarioSuite() + () => ctx.getCurrentTest() ?? ctx.getCurrentScenarioSuite(), + ctx.captureAssertions ) } diff --git a/packages/nightwatch-devtools/src/session.ts b/packages/nightwatch-devtools/src/session.ts index ae830af7..ed9c5457 100644 --- a/packages/nightwatch-devtools/src/session.ts +++ b/packages/nightwatch-devtools/src/session.ts @@ -213,6 +213,41 @@ export class SessionCapturer extends SessionCapturerBase { ) } + /** + * Ingest a pre-built assertion entry (native `browser.assert`/`verify` + * synthesized from Nightwatch's results, or a node:assert capture) into the + * same command stream driver commands use. Unlike {@link captureCommand} this + * preserves the entry's `title` (the human assertion message) and never + * dedups — each call is a distinct action row. Assigns a fresh `_id` and a + * matching stable public `id` (so a later `sendReplaceCommand` can update + * this exact row in place — native asserts stream a pending row at call time, + * then finalize their pass/fail), pushes, and broadcasts. + */ + captureAssertCommand(entry: CommandLog): void { + const withId = entry as CommandLog & { _id?: number } + withId._id = this.commandCounter++ + withId.id = withId._id + this.commandsLog.push(withId) + if ( + this.traceMode === 'trace' && + !entry.error && + this.#browser && + mapCommandToAction(entry.command) + ) { + const browser = this.#browser + this.snapshotCaptures.push( + captureActionSnapshot(browser, entry.command, () => + this.takeScreenshotViaHttp(browser) + ).then((snap) => { + if (snap) { + this.actionSnapshots.push(snap) + } + }) + ) + } + this.sendCommand(withId) + } + /** * Replace an already-captured command entry (used for retried commands so * only the final execution result is shown in the UI). diff --git a/packages/nightwatch-devtools/src/types.ts b/packages/nightwatch-devtools/src/types.ts index ab776db5..203dbbdb 100644 --- a/packages/nightwatch-devtools/src/types.ts +++ b/packages/nightwatch-devtools/src/types.ts @@ -23,7 +23,7 @@ export { type TraceLog } from '@wdio/devtools-shared' -import type { BaseDevToolsOptions } from '@wdio/devtools-shared' +import type { BaseDevToolsOptions, CommandLog } from '@wdio/devtools-shared' export interface CommandStackFrame { command: string @@ -31,6 +31,20 @@ export interface CommandStackFrame { signature: string } +/** One explicit `browser.assert.*` / `browser.verify.*` call, recorded at call + * time by BrowserProxy. A neutral "pending" row (`entry`) is streamed live at + * call time; `captureNativeAssertions` finalizes its pass/fail at test-end. */ +export interface NativeAssertCall { + prefix: 'assert' | 'verify' + method: string + args: unknown[] + callSource?: string + /** Wall-clock at call time; also the streamed row's timestamp/startTime. */ + timestamp: number + /** The pending row emitted at call time, updated in place when finalized. */ + entry?: CommandLog +} + export interface NightwatchTestCase { passed: number failed: number diff --git a/packages/nightwatch-devtools/tests/attemptTracking.test.ts b/packages/nightwatch-devtools/tests/attemptTracking.test.ts index c08313c4..30df38e2 100644 --- a/packages/nightwatch-devtools/tests/attemptTracking.test.ts +++ b/packages/nightwatch-devtools/tests/attemptTracking.test.ts @@ -89,10 +89,10 @@ describe('regular test retry attempt via startNextTest', () => { recordAttempt: (uid: string) => tracker.recordStart(uid) } as unknown as TestLifecycleCtx - await startNextTest(ctx, suite, 'my test', new Set()) + await startNextTest(ctx, suite, 'my test', new Set(), null) expect(test.retries).toBe(0) - await startNextTest(ctx, suite, 'my test', new Set(['my test'])) + await startNextTest(ctx, suite, 'my test', new Set(['my test']), null) expect(test.retries).toBe(1) }) }) diff --git a/packages/nightwatch-devtools/tests/browserProxy.test.ts b/packages/nightwatch-devtools/tests/browserProxy.test.ts new file mode 100644 index 00000000..d69ffcf3 --- /dev/null +++ b/packages/nightwatch-devtools/tests/browserProxy.test.ts @@ -0,0 +1,168 @@ +import { describe, it, expect, vi, beforeEach } from 'vitest' +import type { CommandLog, NightwatchBrowser } from '../src/types.js' + +// browserProxy resolves the caller's source via this helper; stub it so each +// test can decide whether the command looks user-issued or framework-internal. +// vi.hoisted so the stub exists when the hoisted vi.mock factory runs. +const { getCallSourceFromStack } = vi.hoisted(() => ({ + getCallSourceFromStack: vi.fn() +})) +vi.mock('../src/helpers/utils.js', () => ({ getCallSourceFromStack })) + +import { BrowserProxy } from '../src/helpers/browserProxy.js' +import type { SessionCapturer } from '../src/session.js' +import type { TestManager } from '../src/helpers/testManager.js' + +function makeCapturer() { + const commandsLog: (CommandLog & { _id?: number })[] = [] + let counter = 0 + const captureCommand = vi.fn( + async ( + command: string, + args: unknown[], + result: unknown, + error: Error | undefined, + testUid?: string, + callSource?: string, + timestamp?: number + ) => { + commandsLog.push({ + _id: counter++, + command, + args, + result, + error, + testUid, + callSource, + timestamp + }) + return true + } + ) + const capturer = { + commandsLog, + captureCommand, + replaceCommand: vi.fn(), + sendCommand: vi.fn(), + sendReplaceCommand: vi.fn(), + takeScreenshotViaHttp: vi.fn(async () => null), + captureTrace: vi.fn(async () => {}) + } as unknown as SessionCapturer + return { capturer, commandsLog, captureCommand } +} + +function makeTestManager() { + return { + detectTestBoundary: vi.fn(() => ''), + startTestIfPending: vi.fn() + } as unknown as TestManager +} + +/** A browser whose single command echoes its result into the capture callback + * Nightwatch appends as the last argument. */ +function makeBrowser() { + return { + titleContains: (_arg: unknown, cb: (result: unknown) => void) => { + cb('Example Domain') + return undefined + } + } as unknown as NightwatchBrowser +} + +describe('BrowserProxy internal-command suppression', () => { + beforeEach(() => { + getCallSourceFromStack.mockReset() + }) + + it('captures a command issued from a user-code frame', () => { + const { capturer, commandsLog, captureCommand } = makeCapturer() + getCallSourceFromStack.mockReturnValue({ + filePath: '/tests/spec.js', + callSource: '/tests/spec.js:5' + }) + const proxy = new BrowserProxy(capturer, makeTestManager(), () => ({ + uid: 'test-1' + })) + const browser = makeBrowser() + proxy.wrapBrowserCommands(browser) + ;( + browser as unknown as Record<string, (a: unknown) => unknown> + ).titleContains('Example') + + expect(captureCommand).toHaveBeenCalledTimes(1) + expect(commandsLog).toHaveLength(1) + expect(commandsLog[0].command).toBe('titleContains') + expect(commandsLog[0].callSource).toBe('/tests/spec.js:5') + }) + + it('suppresses a framework-internal command with no user-code frame', () => { + const { capturer, commandsLog, captureCommand } = makeCapturer() + // Mirrors the getTitle a `browser.assert.titleContains` issues from inside + // Nightwatch's queue: no user frame, so getCallSourceFromStack returns none. + getCallSourceFromStack.mockReturnValue({ + filePath: undefined, + callSource: 'unknown:0' + }) + const proxy = new BrowserProxy(capturer, makeTestManager(), () => ({ + uid: 'test-1' + })) + const browser = makeBrowser() + proxy.wrapBrowserCommands(browser) + ;( + browser as unknown as Record<string, (a: unknown) => unknown> + ).titleContains('Example') + + expect(captureCommand).not.toHaveBeenCalled() + expect(commandsLog).toHaveLength(0) + }) +}) + +describe('BrowserProxy captureAssertions gating', () => { + beforeEach(() => { + getCallSourceFromStack.mockReset() + }) + + /** A browser exposing `assert`/`verify` namespace objects (as Nightwatch + * does), so the test can check whether wrapAssertionNamespaces replaced them + * with a recording Proxy. */ + function makeAssertBrowser() { + return { + assert: { titleContains: vi.fn() }, + verify: { titleContains: vi.fn() } + } as unknown as NightwatchBrowser & { + assert: object + verify: object + } + } + + it('leaves assert/verify namespaces original when captureAssertions is false', () => { + const { capturer } = makeCapturer() + const browser = makeAssertBrowser() + const originalAssert = browser.assert + const originalVerify = browser.verify + const proxy = new BrowserProxy( + capturer, + makeTestManager(), + () => ({ uid: 't1' }), + false + ) + proxy.wrapBrowserCommands(browser) + // No wrapping → the namespaces are untouched, so no pending rows can stream. + expect(browser.assert).toBe(originalAssert) + expect(browser.verify).toBe(originalVerify) + }) + + it('wraps assert/verify namespaces by default (captureAssertions true)', () => { + const { capturer } = makeCapturer() + const browser = makeAssertBrowser() + const originalAssert = browser.assert + const originalVerify = browser.verify + const proxy = new BrowserProxy(capturer, makeTestManager(), () => ({ + uid: 't1' + })) + proxy.wrapBrowserCommands(browser) + // Replaced with a recording Proxy → no longer the original object. + expect(browser.assert).not.toBe(originalAssert) + expect(browser.verify).not.toBe(originalVerify) + }) +}) diff --git a/packages/nightwatch-devtools/tests/nativeAssertions.test.ts b/packages/nightwatch-devtools/tests/nativeAssertions.test.ts new file mode 100644 index 00000000..d904dbde --- /dev/null +++ b/packages/nightwatch-devtools/tests/nativeAssertions.test.ts @@ -0,0 +1,340 @@ +import { describe, it, expect, vi } from 'vitest' +import { + captureNativeAssertions, + latestResolvedScreenshot, + pendingAssertionCommand +} from '../src/helpers/nativeAssertions.js' +import type { SessionCapturer } from '../src/session.js' +import type { + CommandLog, + NativeAssertCall, + NightwatchBrowser, + NightwatchCurrentTest +} from '../src/types.js' + +/** Fake capturer mimicking the live stream: `captureAssertCommand` assigns a + * stable public `id` and appends to `commandsLog` (like the real one); + * `sendReplaceCommand` records in-place updates; `takeScreenshotViaHttp` is + * the fresh-fallback. */ +function makeFakeCapturer( + commandsLog: CommandLog[] = [], + freshScreenshot: string | null = 'FRESH_SHOT' +) { + const sent: CommandLog[] = [] + const replaced: Array<{ oldTimestamp: number; command: CommandLog }> = [] + let counter = 100 + const captureAssertCommand = vi.fn( + (entry: CommandLog & { _id?: number; id?: number }) => { + entry._id = counter++ + entry.id = entry._id + commandsLog.push(entry) + sent.push(entry) + } + ) + const sendReplaceCommand = vi.fn( + (oldTimestamp: number, command: CommandLog) => { + replaced.push({ oldTimestamp, command }) + } + ) + const takeScreenshotViaHttp = vi.fn(() => Promise.resolve(freshScreenshot)) + const capturer = { + commandsLog, + captureAssertCommand, + sendReplaceCommand, + takeScreenshotViaHttp + } as unknown as SessionCapturer + return { + capturer, + commandsLog, + sent, + replaced, + captureAssertCommand, + sendReplaceCommand, + takeScreenshotViaHttp + } +} + +const browser = {} as unknown as NightwatchBrowser + +/** Mimic BrowserProxy.emitPendingAssertion: stream a neutral pending row at + * call time and attach it to the call for later finalization. */ +function emitPending( + capturer: SessionCapturer, + calls: NativeAssertCall[], + testUid: string | undefined +) { + for (const call of calls) { + const entry = pendingAssertionCommand( + call, + testUid, + latestResolvedScreenshot(capturer) + ) + capturer.captureAssertCommand(entry) + call.entry = entry + } +} + +/** Nightwatch's `getAssertResult` entries: pass → `failure: false`; fail → + * `failure` is a message string. The message embeds the assertion args. */ +function passing(message: string) { + return { message, fullMsg: message, failure: false as const } +} +function failing(message: string) { + return { message, fullMsg: message, failure: `${message} — failed` } +} + +function currentTestWith( + assertions: unknown[], + commands: unknown[] = [] +): NightwatchCurrentTest { + return { + name: 'renders asserts', + module: 'assert-check', + results: { assertions, commands } + } as unknown as NightwatchCurrentTest +} + +let clock = 1000 +function call( + prefix: 'assert' | 'verify', + method: string, + args: unknown[], + callSource = 'spec.js:8' +): NativeAssertCall { + return { prefix, method, args, callSource, timestamp: clock++ } +} + +describe('captureNativeAssertions (live pending → finalize)', () => { + it('streams neutral pending rows at call time, then finalizes pass/fail in place with a stable id and no duplicates', async () => { + // Preceding real commands: the last with a resolved screenshot is the DOM + // the assertions evaluated against — reused for the pending rows. + const precedingCommands: CommandLog[] = [ + { command: 'url', args: [], timestamp: 1, screenshot: 'URL_SHOT' }, + { + command: 'waitForElementVisible', + args: ['body'], + timestamp: 2, + screenshot: 'BODY_SHOT' + } + ] + const { + capturer, + sent, + replaced, + commandsLog, + captureAssertCommand, + sendReplaceCommand, + takeScreenshotViaHttp + } = makeFakeCapturer(precedingCommands) + const calls = [ + call('verify', 'titleContains', ['Example'], 'spec.js:11'), + call('verify', 'titleContains', ['SOFT_FAIL_ME'], 'spec.js:12'), + call('assert', 'titleContains', ['Example'], 'spec.js:15'), + call('assert', 'titleContains', ['HARD_FAIL_ME'], 'spec.js:16') + ] + + // 1) CALL TIME: each call streams one neutral pending row. + emitPending(capturer, calls, 'test-uid') + expect(captureAssertCommand).toHaveBeenCalledTimes(4) + expect(sent).toHaveLength(4) + for (const row of sent) { + expect(row.result).toBeUndefined() // neutral — not green + expect(row.error).toBeUndefined() // neutral — not red + expect(row.testUid).toBe('test-uid') + expect(row.screenshot).toBe('BODY_SHOT') // reused preceding DOM + expect(typeof (row as { id?: number }).id).toBe('number') + } + expect(sent[0].title).toBe("verify.titleContains('Example')") + expect(sent[0].command).toBe('verify.titleContains') + expect(sent[0].args).toEqual(['Example']) + expect(sent[0].callSource).toBe('spec.js:11') + expect(sent[0].timestamp).toBe(sent[0].startTime) + const pendingIds = sent.map((r) => (r as { id?: number }).id) + + // 2) TEST END: results.assertions includes the implicit waitForElementVisible + // assertion (first entry) which must NOT create/finalize a row. + const assertions = [ + passing('Element <body> was visible after 16 milliseconds'), + passing("Testing if the page title contains 'Example'"), + failing("Testing if the page title contains 'SOFT_FAIL_ME'"), + passing("Testing if the page title contains 'Example'"), + failing("Testing if the page title contains 'HARD_FAIL_ME'") + ] + await captureNativeAssertions( + capturer, + browser, + currentTestWith(assertions), + 'test-uid', + calls + ) + + // Each row updated exactly once, in place — no new rows, no duplicates. + expect(captureAssertCommand).toHaveBeenCalledTimes(4) + expect(sendReplaceCommand).toHaveBeenCalledTimes(4) + expect(commandsLog).toHaveLength(2 + 4) + expect(takeScreenshotViaHttp).not.toHaveBeenCalled() + + // Same row objects, same stable ids (updated, not recreated). + const finalized = calls.map((c) => c.entry!) + expect(finalized.map((r) => (r as { id?: number }).id)).toEqual(pendingIds) + + // Pass/fail applied; failures carry the verbose message as error. + expect(finalized[0].result).toBe('passed') + expect(finalized[0].error).toBeUndefined() + expect(finalized[1].result).toBeUndefined() + expect((finalized[1].error as { message: string }).message).toContain( + 'SOFT_FAIL_ME' + ) + expect(finalized[2].result).toBe('passed') // duplicate 'Example' → 2nd entry + expect(finalized[3].error).toBeDefined() + + // Labels/args/callSource preserved from the pending row. + expect(finalized[3].title).toBe("assert.titleContains('HARD_FAIL_ME')") + expect(finalized[3].callSource).toBe('spec.js:16') + + // With no results.commands timing, rows keep their enqueue timestamp, so + // the replace is keyed on that same timestamp. + replaced.forEach(({ oldTimestamp, command }) => { + expect(oldTimestamp).toBe(command.timestamp) + }) + }) + + it('repositions each row on its real execution window from results.commands (not enqueue time)', async () => { + const { capturer, sent, replaced } = makeFakeCapturer() + const calls = [ + call('verify', 'titleContains', ['Example']), + call('assert', 'titleContains', ['SOFT_FAIL_ME']) + ] + emitPending(capturer, calls, 'uid') + // Both enqueued ~together (clock++), clustered. + const enqueue = sent.map((r) => r.timestamp) + + // Nightwatch ran them ~29ms apart; results.commands carries the real window. + const assertions = [ + passing("Testing if the page title contains 'Example'"), + failing("Testing if the page title contains 'SOFT_FAIL_ME'") + ] + const commands = [ + { name: 'url', startTime: 5000, endTime: 5100 }, + { name: 'verify.titleContains', startTime: 6000, endTime: 6029 }, + { name: 'assert.titleContains', startTime: 6100, endTime: 6132 } + ] + await captureNativeAssertions( + capturer, + browser, + currentTestWith(assertions, commands), + 'uid', + calls + ) + + const finalized = calls.map((c) => c.entry!) + // startTime/timestamp now reflect the real execution window, spread apart. + expect(finalized[0].startTime).toBe(6000) + expect(finalized[0].timestamp).toBe(6029) + expect(finalized[1].startTime).toBe(6100) + expect(finalized[1].timestamp).toBe(6132) + expect(finalized[1].timestamp - finalized[0].timestamp).toBeGreaterThan(50) + // The replace is keyed on the ORIGINAL enqueue timestamp (stable id also + // matches), while the row now carries its real execution timestamp. + expect(replaced[0].oldTimestamp).toBe(enqueue[0]) + expect(replaced[0].command.timestamp).toBe(6029) + }) + + it('finalizes with a fresh screenshot when no preceding one has resolved yet', async () => { + // Race: preceding capture is fire-and-forget and unresolved → pending rows + // stream without a screenshot; finalize takes a fresh end-of-test one. + const pending: CommandLog[] = [ + { command: 'waitForElementVisible', args: ['body'], timestamp: 2 } + ] + const { capturer, sent, takeScreenshotViaHttp } = makeFakeCapturer( + pending, + 'FRESH_SHOT' + ) + const calls = [call('verify', 'titleContains', ['Example'])] + emitPending(capturer, calls, 'uid') + expect(sent[0].screenshot).toBeUndefined() + + await captureNativeAssertions( + capturer, + browser, + currentTestWith([ + passing("Testing if the page title contains 'Example'") + ]), + 'uid', + calls + ) + expect(takeScreenshotViaHttp).toHaveBeenCalledTimes(1) + expect(calls[0].entry!.screenshot).toBe('FRESH_SHOT') + expect(calls[0].entry!.result).toBe('passed') + }) + + it('preserves real multi-arg assertions (no faked args)', async () => { + const { capturer, sent, replaced } = makeFakeCapturer() + const calls = [call('assert', 'containsText', ['#btn', 'Save'])] + emitPending(capturer, calls, 'uid') + expect(sent[0].args).toEqual(['#btn', 'Save']) + expect(sent[0].title).toBe("assert.containsText('#btn', 'Save')") + + await captureNativeAssertions( + capturer, + browser, + currentTestWith([ + failing("Testing if element <#btn> contains text 'Save'") + ]), + 'uid', + calls + ) + expect(replaced).toHaveLength(1) + expect(calls[0].entry!.error).toBeDefined() + }) + + it('falls back to positional correlation when args are not literals', async () => { + const { capturer, sent } = makeFakeCapturer() + // Element-object arg won't substring-match; positional order still pairs + // the single call with the single explicit assertion outcome. + const calls = [call('assert', 'elementPresent', [{ selector: 'body' }])] + emitPending(capturer, calls, 'uid') + expect(sent[0].title).toBe('assert.elementPresent(…)') + + await captureNativeAssertions( + capturer, + browser, + currentTestWith([failing('Testing if element <body> is present')]), + 'uid', + calls + ) + expect(calls[0].entry!.error).toBeDefined() + }) + + it('leaves an uncorrelated pending row in its neutral state (defensive)', async () => { + const { capturer, sent, sendReplaceCommand } = makeFakeCapturer() + const calls = [call('assert', 'ok', [true])] + emitPending(capturer, calls, 'uid') + // No matching results entry — row must not be dropped or mis-coloured. + await captureNativeAssertions( + capturer, + browser, + currentTestWith([]), + 'uid', + calls + ) + expect(sent).toHaveLength(1) + expect(sendReplaceCommand).not.toHaveBeenCalled() + expect(calls[0].entry!.result).toBeUndefined() + expect(calls[0].entry!.error).toBeUndefined() + }) + + it('is a no-op when there are no recorded calls (implicit assertions ignored)', async () => { + const { capturer, sendReplaceCommand, takeScreenshotViaHttp } = + makeFakeCapturer() + await captureNativeAssertions( + capturer, + browser, + currentTestWith([passing('Element <body> was visible')]), + 'uid', + [] + ) + expect(sendReplaceCommand).not.toHaveBeenCalled() + expect(takeScreenshotViaHttp).not.toHaveBeenCalled() + }) +}) From 084bdb97b3a4de66659da4ae130f1d852317e839 Mon Sep 17 00:00:00 2001 From: Vishnu Vardhan <vishnu.p@browserstack.com> Date: Fri, 10 Jul 2026 23:24:34 +0530 Subject: [PATCH 34/68] fix(core): rate-limit repeated terminal lines and fix failLastAction --- examples/wdio/cucumber/wdio.conf.ts | 2 +- examples/wdio/cucumber/wdio.mobile.conf.ts | 2 +- examples/wdio/mocha/wdio.conf.ts | 2 +- packages/core/src/index.ts | 1 + packages/core/src/session-capturer.ts | 26 +++++-- packages/core/src/terminal-throttle.ts | 70 ++++++++++++++++++ .../core/tests/session-capturer-base.test.ts | 46 ++++++++++++ packages/core/tests/terminal-throttle.test.ts | 74 +++++++++++++++++++ 8 files changed, 214 insertions(+), 9 deletions(-) create mode 100644 packages/core/src/terminal-throttle.ts create mode 100644 packages/core/tests/terminal-throttle.test.ts diff --git a/examples/wdio/cucumber/wdio.conf.ts b/examples/wdio/cucumber/wdio.conf.ts index 68f7ee8b..4c535acc 100644 --- a/examples/wdio/cucumber/wdio.conf.ts +++ b/examples/wdio/cucumber/wdio.conf.ts @@ -89,7 +89,7 @@ export const config: Options.Testrunner = { // Define all options that are relevant for the WebdriverIO instance here // // Level of logging verbosity: trace | debug | info | warn | error | silent - logLevel: 'debug', + logLevel: 'warn', // // Set specific log levels per logger // loggers: diff --git a/examples/wdio/cucumber/wdio.mobile.conf.ts b/examples/wdio/cucumber/wdio.mobile.conf.ts index 7f693ab4..13618ffe 100644 --- a/examples/wdio/cucumber/wdio.mobile.conf.ts +++ b/examples/wdio/cucumber/wdio.mobile.conf.ts @@ -44,7 +44,7 @@ export const config: Options.Testrunner = { // eslint-disable-next-line @typescript-eslint/no-explicit-any ] as any, - logLevel: 'info', + logLevel: 'warn', bail: 0, baseUrl: 'http://localhost', waitforTimeout: 15000, diff --git a/examples/wdio/mocha/wdio.conf.ts b/examples/wdio/mocha/wdio.conf.ts index 7ccee43f..966226f9 100644 --- a/examples/wdio/mocha/wdio.conf.ts +++ b/examples/wdio/mocha/wdio.conf.ts @@ -29,7 +29,7 @@ export const config: Options.Testrunner = { } } ], - logLevel: 'debug', + logLevel: 'warn', bail: 0, baseUrl: 'http://localhost', waitforTimeout: 10000, diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index 3c3325e2..89c6c301 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -25,6 +25,7 @@ export * from './console.js' export * from './uid.js' export * from './net.js' export * from './stack.js' +export * from './terminal-throttle.js' export * from './error.js' export * from './finalize-screencast.js' export * from './output-dir.js' diff --git a/packages/core/src/session-capturer.ts b/packages/core/src/session-capturer.ts index 82cfe0f6..f5b90f4e 100644 --- a/packages/core/src/session-capturer.ts +++ b/packages/core/src/session-capturer.ts @@ -21,6 +21,7 @@ import { isInternalStreamLine, stripAnsi } from './console.js' +import { TerminalLineThrottle } from './terminal-throttle.js' /** * Foundation class for adapter SessionCapturers. Owns the cross-framework @@ -62,6 +63,11 @@ export abstract class SessionCapturerBase { // stdout, OR when stream forwarding wants to log via console. #isCapturingConsole = false #isCapturingStream = false + // Collapses high-frequency identical terminal lines (e.g. WDIO's per-command + // COMMAND/RESULT logger frames reprinted every ~100ms during an expect poll) + // so they don't flood the console lane. Terminal-source only — user + // console.* is source='test' and never throttled. + #terminalThrottle = new TerminalLineThrottle() // Command bookkeeping — used by adapters that emit commands themselves // (nightwatch, selenium). The WDIO service adapter doesn't call sendCommand @@ -209,20 +215,27 @@ export abstract class SessionCapturerBase { } /** - * Mark the most recent user action of a test as failed. Assertion and step - * failures aren't their own command, so the action that ran (e.g. the query - * an expectation read) carries the error; broadcasts the swap so live mode - * highlights it too. Returns false when no eligible action is found. + * Mark the most recent user action of a test as failed — for framework + * failures that aren't captured as their own command. Broadcasts the swap so + * live mode highlights it too. Returns false when no eligible action is found, + * OR when that most-recent action already carries an error: the failure is + * then already represented (e.g. an expect matcher captured as its own row via + * afterAssertion), so it must not bleed onto an earlier *passing* action. */ failLastAction(testUid: string | undefined, error: SerializedError): boolean { for (let i = this.commandsLog.length - 1; i >= 0; i--) { const command = this.commandsLog[i] - if (command.error || !mapCommandToAction(command.command)) { + if (!mapCommandToAction(command.command)) { continue } if (testUid && command.testUid !== testUid) { continue } + // First (most-recent) action for this test. If it's already failed, the + // failure is captured — stop rather than marking an earlier passing one. + if (command.error) { + return false + } command.error = error this.sendReplaceCommand(command.timestamp, command) return true @@ -432,7 +445,8 @@ export abstract class SessionCapturerBase { if ( !clean || this.isInternalStreamLine(clean) || - SPINNER_RE.test(clean) + SPINNER_RE.test(clean) || + !this.#terminalThrottle.shouldEmit(clean) ) { continue } diff --git a/packages/core/src/terminal-throttle.ts b/packages/core/src/terminal-throttle.ts new file mode 100644 index 00000000..017cbf00 --- /dev/null +++ b/packages/core/src/terminal-throttle.ts @@ -0,0 +1,70 @@ +/** + * Rate-limits high-frequency identical terminal (stdout/stderr) lines so a + * polling framework that reprints the same line every ~100ms doesn't flood the + * trace's console lane. The motivating case: WDIO's logger writes a + * `COMMAND`/`RESULT` frame for every WebDriver command, so an `expect` that + * polls for its full 10s timeout emits hundreds of near-identical lines. + * + * Only terminal-source capture goes through this — user `console.*` is + * captured as `source: 'test'` and never throttled, so real user output is + * untouched. Distinct lines always pass immediately; a repeat is emitted at + * most once per window. + */ + +/** + * A repeated identical terminal line is emitted at most once per this window + * (ms). Sized so a 100ms poll collapses ~10:1 while a human-paced reprint of + * the same line (>1s apart) still shows every time. + */ +export const TERMINAL_REPEAT_WINDOW_MS = 1000 + +/** + * Leading ISO-8601 timestamp emitted by most structured loggers (`@wdio/logger`'s + * `%t %l %n:` template, pino, winston, …). It's the only volatile part of + * otherwise-identical successive log frames, so it's stripped from the throttle + * key — never from the emitted text. + */ +const LEADING_ISO_TIMESTAMP_RE = + /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d\d\d)?Z {1,4}/ + +/** Key a terminal line for repeat-detection: drop a leading ISO timestamp so + * successive log frames of the same message collapse. */ +export function terminalRepeatKey(line: string): string { + return line.replace(LEADING_ISO_TIMESTAMP_RE, '') +} + +export class TerminalLineThrottle { + #lastEmitted = new Map<string, number>() + readonly #windowMs: number + + constructor(windowMs: number = TERMINAL_REPEAT_WINDOW_MS) { + this.#windowMs = windowMs + } + + /** + * True if the line should be emitted, false if it's a within-window repeat of + * a line already emitted. The window is anchored to the last *emit*, not the + * last occurrence, so a sustained stream of one line still emits once per + * window instead of going silent after the first. Expired keys are pruned on + * each emit, so state stays bounded by the number of distinct lines seen + * within the window. + */ + shouldEmit(line: string, now: number = Date.now()): boolean { + const key = terminalRepeatKey(line) + const last = this.#lastEmitted.get(key) + if (last !== undefined && now - last < this.#windowMs) { + return false + } + this.#lastEmitted.set(key, now) + this.#prune(now) + return true + } + + #prune(now: number): void { + for (const [key, ts] of this.#lastEmitted) { + if (now - ts >= this.#windowMs) { + this.#lastEmitted.delete(key) + } + } + } +} diff --git a/packages/core/tests/session-capturer-base.test.ts b/packages/core/tests/session-capturer-base.test.ts index d89455ae..92ee7722 100644 --- a/packages/core/tests/session-capturer-base.test.ts +++ b/packages/core/tests/session-capturer-base.test.ts @@ -152,3 +152,49 @@ describe('sendCommand', () => { expect(cap.upstream.filter((u) => u.scope === 'commands')).toHaveLength(1) }) }) + +describe('failLastAction', () => { + const boom = { name: 'Error', message: 'boom' } + + it('marks the most-recent action of the test when it has no error', () => { + cap.commandsLog.push({ + command: 'expect.toBeExisting', + args: [], + timestamp: 1, + testUid: 't1' + }) + expect(cap.failLastAction('t1', boom)).toBe(true) + expect(cap.commandsLog[0]!.error).toEqual(boom) + }) + + it('does not bleed onto an earlier passing action when the latest one already failed', () => { + // Regression: a failing expect matcher is captured as its own row via + // afterAssertion, so failLastAction must stop at it — NOT stamp the error + // onto the preceding passing assertion (the toBeExisting-shown-red bug). + const matcherErr = { name: 'Error', message: 'to have text' } + cap.commandsLog.push( + { command: 'expect.toBeExisting', args: [], timestamp: 1, testUid: 't1' }, + { + command: 'expect.toHaveText', + args: [], + timestamp: 2, + testUid: 't1', + error: matcherErr + } + ) + expect(cap.failLastAction('t1', boom)).toBe(false) + expect(cap.commandsLog[0]!.error).toBeUndefined() + expect(cap.commandsLog[1]!.error).toEqual(matcherErr) + }) + + it('ignores actions belonging to a different test', () => { + cap.commandsLog.push({ + command: 'expect.toBeExisting', + args: [], + timestamp: 1, + testUid: 'other' + }) + expect(cap.failLastAction('t1', boom)).toBe(false) + expect(cap.commandsLog[0]!.error).toBeUndefined() + }) +}) diff --git a/packages/core/tests/terminal-throttle.test.ts b/packages/core/tests/terminal-throttle.test.ts new file mode 100644 index 00000000..4e26f11b --- /dev/null +++ b/packages/core/tests/terminal-throttle.test.ts @@ -0,0 +1,74 @@ +import { describe, it, expect } from 'vitest' +import { + TERMINAL_REPEAT_WINDOW_MS, + TerminalLineThrottle, + terminalRepeatKey +} from '../src/terminal-throttle.js' + +describe('terminalRepeatKey', () => { + it('strips a leading ISO-8601 timestamp so log frames of the same message match', () => { + const a = '2026-07-09T22:12:38.497Z INFO webdriver: COMMAND getText("#x")' + const b = '2026-07-09T22:12:38.597Z INFO webdriver: COMMAND getText("#x")' + expect(terminalRepeatKey(a)).toBe(terminalRepeatKey(b)) + expect(terminalRepeatKey(a)).toBe('INFO webdriver: COMMAND getText("#x")') + }) + + it('leaves lines without a leading timestamp untouched', () => { + expect(terminalRepeatKey('[TEST] hello')).toBe('[TEST] hello') + }) +}) + +describe('TerminalLineThrottle', () => { + it('emits distinct lines immediately', () => { + const t = new TerminalLineThrottle() + expect(t.shouldEmit('one', 0)).toBe(true) + expect(t.shouldEmit('two', 0)).toBe(true) + expect(t.shouldEmit('three', 0)).toBe(true) + }) + + it('suppresses a within-window repeat but re-emits once the window passes', () => { + const t = new TerminalLineThrottle(1000) + expect(t.shouldEmit('poll', 0)).toBe(true) // first + expect(t.shouldEmit('poll', 100)).toBe(false) // repeat within window + expect(t.shouldEmit('poll', 900)).toBe(false) + expect(t.shouldEmit('poll', 1000)).toBe(true) // window elapsed → re-emit + expect(t.shouldEmit('poll', 1100)).toBe(false) + }) + + it('anchors the window to the last emit, not the last occurrence (a sustained stream still emits ~1/window)', () => { + const t = new TerminalLineThrottle(1000) + let emitted = 0 + // 100ms poll for 10s = 101 occurrences of the same line. + for (let ms = 0; ms <= 10000; ms += 100) { + if (t.shouldEmit('COMMAND getText', ms)) { + emitted++ + } + } + // ~1 per second rather than ~100 total — flood collapsed ~10:1. + expect(emitted).toBe(11) + }) + + it('collapses successive WDIO logger frames of the same command despite changing timestamps', () => { + const t = new TerminalLineThrottle(1000) + const line = (ms: number) => + `2026-07-09T22:12:${String(30 + Math.floor(ms / 1000)).padStart(2, '0')}.${String(ms % 1000).padStart(3, '0')}Z INFO webdriver: COMMAND getText("#x")` + expect(t.shouldEmit(line(0), 0)).toBe(true) + expect(t.shouldEmit(line(100), 100)).toBe(false) + expect(t.shouldEmit(line(200), 200)).toBe(false) + }) + + it('treats different messages independently', () => { + const t = new TerminalLineThrottle(1000) + expect(t.shouldEmit('COMMAND getText', 0)).toBe(true) + expect(t.shouldEmit('RESULT ""', 10)).toBe(true) // interleaved, distinct + expect(t.shouldEmit('COMMAND getText', 100)).toBe(false) // repeat + expect(t.shouldEmit('RESULT ""', 110)).toBe(false) // repeat + }) + + it('defaults to the exported window constant', () => { + const t = new TerminalLineThrottle() + expect(t.shouldEmit('x', 0)).toBe(true) + expect(t.shouldEmit('x', TERMINAL_REPEAT_WINDOW_MS - 1)).toBe(false) + expect(t.shouldEmit('x', TERMINAL_REPEAT_WINDOW_MS)).toBe(true) + }) +}) From 700929620f1464edcd45642b64abf0331ce63781 Mon Sep 17 00:00:00 2001 From: Vishnu Vardhan <vishnu.p@browserstack.com> Date: Fri, 10 Jul 2026 23:24:50 +0530 Subject: [PATCH 35/68] fix(app): real per-action duration and span-based active highlight --- .../src/components/browser/trace-timeline.ts | 20 ++-- .../workbench/actionItems/duration.ts | 20 ++++ .../app/src/components/workbench/actions.ts | 18 +--- .../src/components/workbench/active-entry.ts | 59 ++++++++--- packages/app/tests/active-entry.test.ts | 97 +++++++++++++++---- packages/app/tests/duration.test.ts | 33 +++++++ 6 files changed, 191 insertions(+), 56 deletions(-) diff --git a/packages/app/src/components/browser/trace-timeline.ts b/packages/app/src/components/browser/trace-timeline.ts index 21f7ffce..07cb81a1 100644 --- a/packages/app/src/components/browser/trace-timeline.ts +++ b/packages/app/src/components/browser/trace-timeline.ts @@ -5,7 +5,7 @@ import { consume } from '@lit/context' import type { CommandLog, TracePlayerFrame } from '@wdio/devtools-shared' import { commandContext, framesContext } from '../../controller/context.js' -import { activeTimestampAt } from '../workbench/active-entry.js' +import { activeSpanAt } from '../workbench/active-entry.js' import { KBD } from '../../controller/keyboard.js' import { PLAYER_RESTART_EVENT, @@ -42,7 +42,7 @@ export class TraceTimeline extends Element { #rafId?: number #rafLast = 0 - #activeTimestamp?: number + #activeCommand?: CommandLog #started = false @query('[data-scrub]') scrubEl?: HTMLElement @@ -229,19 +229,15 @@ export class TraceTimeline extends Element { return } const clock = this.#start + this.currentMs - const timestamps = sorted.map((c) => c.timestamp ?? 0) - const activeTs = activeTimestampAt(timestamps, clock) ?? timestamps[0] - if (this.#started && activeTs === this.#activeTimestamp) { + const command = activeSpanAt(sorted, clock) ?? sorted[0] + if (this.#started && command === this.#activeCommand) { return } this.#started = true - this.#activeTimestamp = activeTs - const command = sorted.find((c) => (c.timestamp ?? 0) === activeTs) - if (command) { - window.dispatchEvent( - new CustomEvent('show-command', { detail: { command } }) - ) - } + this.#activeCommand = command + window.dispatchEvent( + new CustomEvent('show-command', { detail: { command } }) + ) } // ─── scrubbing (drag anywhere on the strip) ─────────────────────────────── diff --git a/packages/app/src/components/workbench/actionItems/duration.ts b/packages/app/src/components/workbench/actionItems/duration.ts index 5d2df61f..f0dd5d63 100644 --- a/packages/app/src/components/workbench/actionItems/duration.ts +++ b/packages/app/src/components/workbench/actionItems/duration.ts @@ -1,3 +1,5 @@ +import type { CommandLog, TraceMutation } from '@wdio/devtools-shared' + export type DurationHeat = 'fast' | 'mid' | 'slow' const ONE_SECOND = 1000 @@ -29,6 +31,24 @@ export function durationHeat(ms: number): DurationHeat { return 'fast' } +/** + * True per-action duration: a command's own execution span (`timestamp − + * startTime`) when it has one, else the inter-action `gapFallback`. Prefer the + * span — the gap over-counts idle time before an action, so e.g. an assertion + * whose internal polling commands are suppressed would otherwise report the + * navigation gap that preceded it rather than its own runtime. Used by both the + * flat (live) and grouped (trace-player) action views so they agree. + */ +export function entryDuration( + entry: CommandLog | TraceMutation, + gapFallback: number | undefined +): number | undefined { + if ('command' in entry && entry.startTime !== undefined) { + return entry.timestamp - entry.startTime + } + return gapFallback +} + /** * Per-step duration for each timeline entry: the gap to the next entry. The * final entry has no next, so it falls back to the gap from the previous entry diff --git a/packages/app/src/components/workbench/actions.ts b/packages/app/src/components/workbench/actions.ts index 842f4f56..5e630c8e 100644 --- a/packages/app/src/components/workbench/actions.ts +++ b/packages/app/src/components/workbench/actions.ts @@ -18,8 +18,8 @@ import '../placeholder.js' import './actionItems/command.js' import './actionItems/group.js' import './actionItems/mutation.js' -import { stepDurations } from './actionItems/duration.js' -import { activeTimestampAt } from './active-entry.js' +import { entryDuration, stepDurations } from './actionItems/duration.js' +import { activeSpanAt } from './active-entry.js' import { defaultExpanded, flattenActionTree, @@ -128,12 +128,7 @@ export class DevtoolsActions extends Element { // every timeupdate tick. #onScreencastProgress = (event: Event) => { const { time } = (event as CustomEvent<{ time: number }>).detail - const entries = this.#sortedEntries() - const timestamp = activeTimestampAt( - entries.map((entry) => entry.timestamp), - time - ) - const active = entries.find((entry) => entry.timestamp === timestamp) + const active = activeSpanAt(this.#sortedEntries(), time) if (active === this.activeEntry) { return } @@ -228,10 +223,7 @@ export class DevtoolsActions extends Element { return nothing } // Reconstructed zips carry the real invocation span; gap is the fallback. - const duration = - entry.startTime !== undefined - ? entry.timestamp - entry.startTime - : gaps[row.commandIndex] + const duration = entryDuration(entry, gaps[row.commandIndex]) return html` <wdio-devtools-command-item style=${indent} @@ -257,7 +249,7 @@ export class DevtoolsActions extends Element { const rows = entries.map((entry, index) => { const elapsedTime = entry.timestamp - baselineTimestamp - const duration = durations[index] + const duration = entryDuration(entry, durations[index]) const active = entry === this.activeEntry if ('command' in entry) { diff --git a/packages/app/src/components/workbench/active-entry.ts b/packages/app/src/components/workbench/active-entry.ts index 3cdd2ed2..edbe3eb9 100644 --- a/packages/app/src/components/workbench/active-entry.ts +++ b/packages/app/src/components/workbench/active-entry.ts @@ -1,20 +1,51 @@ +/** Timeline item occupying a span from `startTime` (inclusive) to `timestamp` + * (inclusive). Items without a `startTime` occupy the single point `timestamp`. */ +export interface TimeSpanned { + timestamp: number + startTime?: number +} + /** - * Given action timestamps sorted ascending and a playback time (wall-clock ms), - * return the timestamp of the action that is "current" — the latest one at or - * before `time`. Returns undefined when `time` precedes the first action, so - * nothing is highlighted before the first command runs. + * Given timeline items and a playback time (wall-clock ms), return the item + * that is "current" at `time`. An item's span is `[startTime ?? timestamp, + * timestamp]` and the item is current when `time` falls inside it — so a + * long-running command (e.g. a polling `expect.*` matcher) stays selected for + * its whole duration, not just at completion. When several spans contain `time` + * (nested/overlapping commands), the one that started most recently wins, with + * ties broken toward the tighter span — the most specific action in progress. + * When none contain `time` (a point-like command, or a gap between actions), + * fall back to the latest item that has already ended at or before `time`. + * Returns undefined when `time` precedes every item, so nothing is highlighted + * before the first action runs. */ -export function activeTimestampAt( - sortedTimestamps: number[], +export function activeSpanAt<T extends TimeSpanned>( + items: readonly T[], time: number -): number | undefined { - let active: number | undefined - for (const ts of sortedTimestamps) { - if (ts <= time) { - active = ts - } else { - break +): T | undefined { + let containing: T | undefined + let containingStart = -Infinity + let containingEnd = Infinity + let ended: T | undefined + let endedAt = -Infinity + + for (const item of items) { + const end = item.timestamp + const start = item.startTime ?? end + if ( + start <= time && + time <= end && + (start > containingStart || + (start === containingStart && end < containingEnd)) + ) { + containing = item + containingStart = start + containingEnd = end + } + if (end <= time && end >= endedAt) { + ended = item + endedAt = end } } - return active + + return containing ?? ended } diff --git a/packages/app/tests/active-entry.test.ts b/packages/app/tests/active-entry.test.ts index 9dcf416f..c894f456 100644 --- a/packages/app/tests/active-entry.test.ts +++ b/packages/app/tests/active-entry.test.ts @@ -1,28 +1,91 @@ import { describe, it, expect } from 'vitest' -import { activeTimestampAt } from '../src/components/workbench/active-entry.js' +import { + activeSpanAt, + type TimeSpanned +} from '../src/components/workbench/active-entry.js' -describe('activeTimestampAt', () => { - const stamps = [100, 200, 300, 400] +describe('activeSpanAt', () => { + // Point-like commands (no startTime) — the pre-span behaviour must be intact. + describe('point-like items (no startTime)', () => { + const points: TimeSpanned[] = [ + { timestamp: 100 }, + { timestamp: 200 }, + { timestamp: 300 }, + { timestamp: 400 } + ] - it('returns undefined before the first action', () => { - expect(activeTimestampAt(stamps, 50)).toBeUndefined() - }) + it('returns undefined before the first item', () => { + expect(activeSpanAt(points, 50)).toBeUndefined() + }) - it('returns the action exactly at the playback time', () => { - expect(activeTimestampAt(stamps, 200)).toBe(200) - }) + it('returns the item exactly at the playback time', () => { + expect(activeSpanAt(points, 200)).toBe(points[1]) + }) - it('returns the latest action at or before the playback time', () => { - expect(activeTimestampAt(stamps, 250)).toBe(200) - expect(activeTimestampAt(stamps, 399)).toBe(300) - }) + it('returns the latest item at or before the playback time', () => { + expect(activeSpanAt(points, 250)).toBe(points[1]) + expect(activeSpanAt(points, 399)).toBe(points[2]) + }) - it('clamps to the last action once playback passes it', () => { - expect(activeTimestampAt(stamps, 999)).toBe(400) + it('clamps to the last item once playback passes it', () => { + expect(activeSpanAt(points, 999)).toBe(points[3]) + }) + + it('returns undefined for an empty timeline', () => { + expect(activeSpanAt([], 100)).toBeUndefined() + }) }) - it('returns undefined for an empty timeline', () => { - expect(activeTimestampAt([], 100)).toBeUndefined() + describe('spanned items (startTime → timestamp)', () => { + it('selects a long-running command while the clock is inside its span', () => { + const before = { timestamp: 1000 } + const poll = { startTime: 1000, timestamp: 11000 } + const items: TimeSpanned[] = [before, poll] + // Mid-poll: the span contains the clock, so the poll stays selected even + // though the preceding command ended earlier. + expect(activeSpanAt(items, 5000)).toBe(poll) + expect(activeSpanAt(items, 1500)).toBe(poll) + // At the span end the poll is still selected. + expect(activeSpanAt(items, 11000)).toBe(poll) + }) + + it('picks the most recently started span when several contain the clock', () => { + const outer = { startTime: 100, timestamp: 10000 } + const inner = { startTime: 4000, timestamp: 6000 } + const items: TimeSpanned[] = [outer, inner] + expect(activeSpanAt(items, 5000)).toBe(inner) + // Outside the inner span but still inside the outer one → outer. + expect(activeSpanAt(items, 2000)).toBe(outer) + expect(activeSpanAt(items, 8000)).toBe(outer) + }) + + it('breaks a start-time tie toward the tighter span', () => { + const wide = { startTime: 100, timestamp: 9000 } + const tight = { startTime: 100, timestamp: 3000 } + const items: TimeSpanned[] = [wide, tight] + expect(activeSpanAt(items, 2000)).toBe(tight) + }) + + it('falls back to the last-ended item when no span contains the clock', () => { + const first = { startTime: 100, timestamp: 2000 } + const second = { startTime: 5000, timestamp: 7000 } + const items: TimeSpanned[] = [first, second] + // Clock sits in the gap between the two spans → the last one that ended. + expect(activeSpanAt(items, 3500)).toBe(first) + }) + + it('returns undefined when the clock precedes the first span', () => { + const items: TimeSpanned[] = [{ startTime: 1000, timestamp: 5000 }] + expect(activeSpanAt(items, 500)).toBeUndefined() + }) + + it('prefers a containing span over an already-ended earlier item', () => { + const ended = { timestamp: 1000 } + const running = { startTime: 900, timestamp: 8000 } + const items: TimeSpanned[] = [ended, running] + // Clock is past the point command but inside the running span → running. + expect(activeSpanAt(items, 2000)).toBe(running) + }) }) }) diff --git a/packages/app/tests/duration.test.ts b/packages/app/tests/duration.test.ts index 0bfc90ba..92ab1c87 100644 --- a/packages/app/tests/duration.test.ts +++ b/packages/app/tests/duration.test.ts @@ -3,8 +3,10 @@ import { describe, it, expect } from 'vitest' import { formatDuration, durationHeat, + entryDuration, stepDurations } from '../src/components/workbench/actionItems/duration.js' +import type { CommandLog, TraceMutation } from '@wdio/devtools-shared' describe('formatDuration', () => { it('shows milliseconds under a second', () => { @@ -73,3 +75,34 @@ describe('stepDurations', () => { expect(stepDurations([0, 0, 0, 500])).toEqual([0, 0, 500, 500]) }) }) + +describe('entryDuration', () => { + const cmd = (over: Partial<CommandLog> = {}): CommandLog => ({ + command: 'expect.toExist', + args: [], + timestamp: 10603, + ...over + }) + + it('uses the command execution span (timestamp − startTime) when present', () => { + // Regression: an assertion whose internal polling is suppressed sits after a + // long navigation gap; the row must show its own 603ms runtime, not the gap. + expect(entryDuration(cmd({ startTime: 10000 }), 10650)).toBe(603) + }) + + it('falls back to the inter-action gap when the command has no startTime', () => { + expect(entryDuration(cmd(), 420)).toBe(420) + }) + + it('falls back to the gap for a mutation entry (no startTime field)', () => { + const mutation = { + type: 'childList', + timestamp: 5 + } as unknown as TraceMutation + expect(entryDuration(mutation, 42)).toBe(42) + }) + + it('returns undefined when neither a span nor a gap is available', () => { + expect(entryDuration(cmd(), undefined)).toBeUndefined() + }) +}) From f2a4534645a773d7d6c06a45a9f91154ab713e2d Mon Sep 17 00:00:00 2001 From: Vishnu Vardhan <vishnu.p@browserstack.com> Date: Fri, 10 Jul 2026 23:25:14 +0530 Subject: [PATCH 36/68] fix(app): sidebar icon alignment and resizable-pane drag --- packages/app/src/components/sidebar/test-suite.ts | 11 +++++++---- packages/app/src/utils/DragController.ts | 11 +++++++++++ 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/packages/app/src/components/sidebar/test-suite.ts b/packages/app/src/components/sidebar/test-suite.ts index cf0d43f3..9cc7e0ee 100644 --- a/packages/app/src/components/sidebar/test-suite.ts +++ b/packages/app/src/components/sidebar/test-suite.ts @@ -287,10 +287,11 @@ export class ExplorerTestEntry extends CollapseableEntry { return this.state === TestState.RUNNING } get testStateIcon() { - // Fixed-height box (= the label's line-height) centred so the icon aligns - // with the first line of the title whether it wraps or not — no margin hacks. + // Vertically centred in the row so the status icon lines up with the + // row's action buttons (run/stop), which are also centre-aligned. const box = (inner: unknown) => - html`<span class="w-4 h-[18px] shrink-0 flex items-center justify-center" + html`<span + class="w-4 h-[18px] shrink-0 self-center grid place-items-center" >${inner}</span >` if (this.isRunning) { @@ -407,7 +408,9 @@ export class ExplorerTestEntry extends CollapseableEntry { class="row flex w-full items-start text-sm group/sidebar rounded-md my-0.5 px-1 py-1 cursor-pointer hover:bg-toolbarHoverBackground" > <button - class="flex-none pointer px-2 h-8 ${hasNoChildren ? 'hidden' : ''}" + class="flex-none pointer px-2 h-[18px] flex items-center justify-center ${hasNoChildren + ? 'hidden' + : ''}" @click="${() => this.#toggleEntry()}" > <icon-mdi-menu-down diff --git a/packages/app/src/utils/DragController.ts b/packages/app/src/utils/DragController.ts index d675570f..e74d8d6e 100644 --- a/packages/app/src/utils/DragController.ts +++ b/packages/app/src/utils/DragController.ts @@ -256,6 +256,17 @@ export class DragController implements ReactiveController { // if slider was removed (collapsed) and re-added, re-init pointer tracker if (this.#draggableEl !== draggableEl) { this.#draggableEl = draggableEl as HTMLElement + // The container often doesn't exist at construction (the sidebar renders + // only after a connection), so it must be resolved here too — otherwise + // the tracker attaches but #handleWindowMove bails on a null container + // and the drag silently no-ops. + if (!this.#containerEl) { + const containerEl = await this.#options.getContainerEl() + if (containerEl) { + this.#containerEl = containerEl as HTMLElement + window.onresize = () => this.#adjustPosition() + } + } this.#init() } } From 3a8092b2084346d211466641c4ef90c762a34ba0 Mon Sep 17 00:00:00 2001 From: Vishnu Vardhan <vishnu.p@browserstack.com> Date: Fri, 10 Jul 2026 23:26:04 +0530 Subject: [PATCH 37/68] chore(examples): assertion + retry harnesses, chromedriver bump --- .../nightwatch/tests/assert-capture-check.js | 18 +++++++ examples/nightwatch/tests/login.js | 47 ------------------- examples/nightwatch/tests/sample.js | 21 --------- .../features/step-definitions/steps.ts | 8 ++-- examples/wdio/mocha/wdio.retry.conf.ts | 4 +- packages/nightwatch-devtools/package.json | 2 +- pnpm-lock.yaml | 30 +++++++++--- 7 files changed, 49 insertions(+), 81 deletions(-) create mode 100644 examples/nightwatch/tests/assert-capture-check.js delete mode 100644 examples/nightwatch/tests/login.js delete mode 100644 examples/nightwatch/tests/sample.js diff --git a/examples/nightwatch/tests/assert-capture-check.js b/examples/nightwatch/tests/assert-capture-check.js new file mode 100644 index 00000000..6f8511a3 --- /dev/null +++ b/examples/nightwatch/tests/assert-capture-check.js @@ -0,0 +1,18 @@ +// Verification harness for native-assert trace capture (browser.assert / verify). +// Run `pnpm demo:nightwatch` and inspect the dashboard Actions: the PASSING +// asserts must render green and the FAILING ones RED. If a failing assert shows +// green, the classic-chained capture is mis-reporting failures (the known risk +// in nativeAssertCapture — the wrapper sees the enqueue, not the queued result). +describe('Native assert capture check', function () { + it('renders passing and failing native asserts', async function (browser) { + await browser.url('https://example.com').waitForElementVisible('body', 5000) + + // Soft verify.* first — never aborts the test, so all four always run. + browser.verify.titleContains('Example') // PASS → expect green + browser.verify.titleContains('SOFT_FAIL_ME') // FAIL → expect RED + + // Hard assert.* — the classic-chained/queued path under test. + browser.assert.titleContains('Example') // PASS → expect green + browser.assert.titleContains('HARD_FAIL_ME') // FAIL → expect RED + }) +}) diff --git a/examples/nightwatch/tests/login.js b/examples/nightwatch/tests/login.js deleted file mode 100644 index d8964d8d..00000000 --- a/examples/nightwatch/tests/login.js +++ /dev/null @@ -1,47 +0,0 @@ -describe('The Internet Guinea Pig Website', function () { - afterEach(async function (browser) { - await browser.end() - }) - - it('should log into the secure area with valid credentials', async function (browser) { - console.log('[TEST] Navigating to login page') - await browser - .url('https://the-internet.herokuapp.com/login') - .waitForElementVisible('body') - - console.log('[TEST] Attempting login with username: tomsmith') - await browser - .setValue('#username', 'tomsmith') - .setValue('#password', 'SuperSecretPassword!') - .click('button[type="submit"]') - - console.log( - '[TEST] Verifying flash message: You logged into a secure area!' - ) - await browser - .waitForElementVisible('#flash', 10000) - .assert.textContains('#flash', 'You logged into a secure area!') - - console.log('[TEST] Flash message verified successfully') - }) - - it('should show error with invalid credentials', async function (browser) { - console.log('[TEST] Navigating to login page') - await browser - .url('https://the-internet.herokuapp.com/login') - .waitForElementVisible('body') - - console.log('[TEST] Attempting login with username: foobar') - await browser - .setValue('#username', 'foobar') - .setValue('#password', 'barfoo') - .click('button[type="submit"]') - - console.log('[TEST] Verifying flash message: Your username is invalid!') - await browser - .waitForElementVisible('#flash', 10000) - .assert.textContains('#flash', 'Your username is invalid!') - - console.log('[TEST] Flash message verified successfully') - }) -}) diff --git a/examples/nightwatch/tests/sample.js b/examples/nightwatch/tests/sample.js deleted file mode 100644 index ff2e4b2a..00000000 --- a/examples/nightwatch/tests/sample.js +++ /dev/null @@ -1,21 +0,0 @@ -describe('Sample Nightwatch Test with DevTools', function () { - it('should navigate to example.com and check title', async function (browser) { - await browser - .url('https://example.com') - .waitForElementVisible('body', 5000) - .assert.titleContains('Example') - .assert.visible('h1') - - const result = await browser.getText('h1') - browser.assert.ok(result.includes('Example'), 'H1 contains "Example"') - }) - - it('should perform basic interactions', async function (browser) { - await browser - .url('https://www.google.com') - .waitForElementVisible('body', 5000) - .assert.visible('textarea[name="q"]') - .setValue('textarea[name="q"]', 'WebdriverIO DevTools') - .pause(1000) - }) -}) diff --git a/examples/wdio/cucumber/features/step-definitions/steps.ts b/examples/wdio/cucumber/features/step-definitions/steps.ts index 3367b631..5d02133e 100644 --- a/examples/wdio/cucumber/features/step-definitions/steps.ts +++ b/examples/wdio/cucumber/features/step-definitions/steps.ts @@ -26,9 +26,9 @@ When(/^I login with (\w+) and (.+)$/, async (username, password) => { Then(/^I should see a flash message saying (.*)$/, async (message) => { console.log(`[TEST] Verifying flash message: ${message}`) - const el = await SecurePage.flashAlert - await expect(el).toBeExisting() - await expect(el).toHaveText(expect.stringContaining(message)) + await expect(SecurePage.flashAlert).toBeExisting() + await expect(SecurePage.flashAlert).toHaveText( + expect.stringContaining(message) + ) console.log('[TEST] Flash message verified successfully') - // await browser.pause(15000) }) diff --git a/examples/wdio/mocha/wdio.retry.conf.ts b/examples/wdio/mocha/wdio.retry.conf.ts index 90b87858..d9c873bb 100644 --- a/examples/wdio/mocha/wdio.retry.conf.ts +++ b/examples/wdio/mocha/wdio.retry.conf.ts @@ -42,8 +42,8 @@ export const config: Options.Testrunner = { 'devtools', { mode: 'trace' as const, - traceGranularity: 'spec' as const, - tracePolicy: 'on-first-retry' as const + traceGranularity: 'test' as const, + tracePolicy: 'retain-on-failure' as const } ] ], diff --git a/packages/nightwatch-devtools/package.json b/packages/nightwatch-devtools/package.json index 39ce8788..60f418ca 100644 --- a/packages/nightwatch-devtools/package.json +++ b/packages/nightwatch-devtools/package.json @@ -62,7 +62,7 @@ "@types/ws": "^8.18.1", "@wdio/devtools-core": "workspace:^", "@wdio/devtools-shared": "workspace:^", - "chromedriver": "^148.0.4", + "chromedriver": "^150.0.0", "nightwatch": "^3.16.0", "tsup": "^8.5.1", "typescript": "^6.0.3" diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index a2354ac3..528cbb11 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -109,7 +109,7 @@ importers: version: link:../../packages/nightwatch-devtools nightwatch: specifier: ^3.16.0 - version: 3.16.0(@cucumber/cucumber@13.0.0)(chromedriver@148.0.4) + version: 3.16.0(@cucumber/cucumber@13.0.0)(chromedriver@150.0.1) examples/selenium: dependencies: @@ -422,11 +422,11 @@ importers: specifier: workspace:^ version: link:../shared chromedriver: - specifier: ^148.0.4 - version: 148.0.4 + specifier: ^150.0.0 + version: 150.0.1 nightwatch: specifier: ^3.16.0 - version: 3.16.0(@cucumber/cucumber@13.0.0)(chromedriver@148.0.4) + version: 3.16.0(@cucumber/cucumber@13.0.0)(chromedriver@150.0.1) tsup: specifier: ^8.5.1 version: 8.5.1(@microsoft/api-extractor@7.58.9(@types/node@25.9.3))(jiti@2.7.0)(postcss@8.5.15)(tsx@4.22.4)(typescript@6.0.3)(yaml@2.9.0) @@ -3338,6 +3338,11 @@ packages: engines: {node: '>=22'} hasBin: true + chromedriver@150.0.1: + resolution: {integrity: sha512-vpYskeQFyZGPNMEwc/5fuvfnxzfjljCO1EvwuF0dZGQ8KE0repYQar7eThTlT1IjznwVrirYRgXrRsJts7DRIQ==} + engines: {node: '>=22'} + hasBin: true + chromium-bidi@0.5.8: resolution: {integrity: sha512-blqh+1cEQbHBKmok3rVJkBlBxt9beKBgOsxbFgs7UJcoVbbeZ+K7+6liAsjgpc8l1Xd55cQUy14fXZdGSb4zIw==} peerDependencies: @@ -10656,6 +10661,19 @@ snapshots: - debug - supports-color + chromedriver@150.0.1: + dependencies: + '@testim/chrome-version': 1.1.4 + adm-zip: 0.5.17 + axios: 1.16.1 + compare-versions: 6.1.1 + proxy-agent: 8.0.1 + proxy-from-env: 2.1.0 + tcp-port-used: 1.0.2 + transitivePeerDependencies: + - debug + - supports-color + chromium-bidi@0.5.8(devtools-protocol@0.0.1232444): dependencies: devtools-protocol: 0.0.1232444 @@ -13280,7 +13298,7 @@ snapshots: dependencies: axe-core: 4.12.0 - nightwatch@3.16.0(@cucumber/cucumber@13.0.0)(chromedriver@148.0.4): + nightwatch@3.16.0(@cucumber/cucumber@13.0.0)(chromedriver@150.0.1): dependencies: '@nightwatch/chai': 5.0.3 '@nightwatch/html-reporter-template': 0.3.0 @@ -13318,7 +13336,7 @@ snapshots: uuid: 8.3.2 optionalDependencies: '@cucumber/cucumber': 13.0.0 - chromedriver: 148.0.4 + chromedriver: 150.0.1 transitivePeerDependencies: - bufferutil - canvas From 19d046ac30d294a03c44442a3052f97f9b385f4f Mon Sep 17 00:00:00 2001 From: Vishnu Vardhan <vishnu.p@browserstack.com> Date: Fri, 10 Jul 2026 23:26:25 +0530 Subject: [PATCH 38/68] docs: test-results/ layout, traceGranularity:'test', tracePolicy, and known-debt --- CLAUDE.md | 8 +++++++- README.md | 10 ++++++---- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/CLAUDE.md b/CLAUDE.md index f06ea24d..14fff1a0 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -236,10 +236,16 @@ Documented divergences from the conventions above. They exist today as debt to b - `patchNodeAssert` (via `core/assert-patcher`) is now wired in all three adapters, default-on behind each adapter's `captureAssertions` option (opt out with `captureAssertions: false`). Framework matcher libraries differ: Service additionally taps expect-webdriverio's `afterAssertion` hook so passing+failing matchers render as `expect.*` actions; Selenium (chai/jest) and Nightwatch (`browser.assert`/`verify`) don't yet surface passing framework-matcher assertions as trace actions. - BiDi is auto-attached in Service and Selenium; Nightwatch is opt-in via `bidi: true` and requires `webSocketUrl: true` in capabilities. - Retry-aware trace policies aren't uniform across adapters. Selenium emits one metadata entry per attempt (counter-based `generateStableUid` uid + retry-stable tracker key), so all six policies — including `retain-on-first-failure` (needs the first attempt's outcome) — evaluate correctly. Service and Nightwatch key metadata on the retry-stable uid and overwrite/mutate per attempt, collapsing to the *final* attempt's outcome; the retry-count policies (`retain-on-failure`, `on-first-retry`, `on-all-retries`, `retain-on-failure-and-retries`) are still correct, but `retain-on-first-failure` can't see a failed-then-passed first attempt and under-retains. Proper fix rides with B5 (per-test granularity preserves per-attempt slices); until then all three set `attemptInfoAvailable: true`. +- Nightwatch negated native asserts (`browser.assert.not.*` / `verify.not.*`) produce no trace row: the `not` namespace is a nested Proxy that `wrapAssertionNamespaces` passes through untouched, and the internal command it issues is dropped by the `hasUserSource` gate in `browserProxy`. Positive `assert.*`/`verify.*` still record. +- Eager per-test trace slice (Nightwatch + Selenium) can drop an action snapshot whose fire-and-forget capture hasn't resolved by `afterEach` / scenario end — the slice is written from whatever snapshots exist at flush time. The WDIO service is immune because it awaits each snapshot inline before flushing. +- Nightwatch native asserts populate all at once in live mode and finalize pass/fail in one test-end batch: Nightwatch exposes no per-assertion execution/outcome hook reachable from the plugin (`client.queue.tree` / `client.reporter` aren't on `browser`), so call-time capture streams neutral pending rows and `afterEach` reconciles outcomes. Trace-timeline positions are corrected from `results.commands`. +- Service assertion-suppression self-heal is command-triggered: a swallowed soft-assert (`expect.soft` / user `try/catch`) whose matcher hard-throws leaves `#assertionDepth` stuck until the next top-level user command's stack shows no `expect-webdriverio` frame (heal) or the next `beforeAssertion` (reset). A stuck depth with no following command or assertion before test end persists until the next test's `resetStack()` — benign (no later command in that test to suppress). ### File-size (raw line counts; soft cap is 500 logic lines) -None of the entries below trigger the `max-lines` lint rule after `skipBlankLines`/`skipComments`. They're documented because their raw line count is over 500, and the next substantive change to any of them should still look for an extraction opportunity. +Most entries below don't trigger the `max-lines` lint rule after `skipBlankLines`/`skipComments`; they're documented because their raw line count is over 500, and the next substantive change to any of them should still look for an extraction opportunity. The service plugin is the exception — it's now over the *logic*-line cap. + +- `packages/service/src/index.ts` — over the 500-**logic**-line soft cap (the WDIO plugin god-file: session/screencast/command/assertion/trace-slice wiring in one class). Split candidate; the trace-slice, assertion-capture, and screencast concerns are the natural extraction seams. - `packages/nightwatch-devtools/src/index.ts` (~536 raw). Cucumber/test/run-lifecycle, session-init, event-hub modules already extracted; remainder is the `PluginInternals` accessor bag plus per-method delegators plus the factory. Accept-as-is. - `packages/selenium-devtools/src/index.ts` (~560 raw). Session/test-lifecycle extracted; remainder is the `PluginInternals` accessor bag plus onCommand/onDriverCreated wiring. Same situation as nightwatch. diff --git a/README.md b/README.md index fa87cc71..09f93c2e 100644 --- a/README.md +++ b/README.md @@ -70,7 +70,7 @@ When BiDi is active in Selenium or Nightwatch, the per-command Chrome performanc ### 📦 Trace mode (trace.zip) -Headless capture path — no DevTools UI window opens. At session end the adapter writes a trace artifact next to the user's output directory, suitable for offline replay, AI-agent diffing, or any consumer that prefers a portable artifact over a live UI. +Headless capture path — no DevTools UI window opens. At session end the adapter writes trace artifacts into a `test-results/` folder (created next to the resolved spec/config directory), suitable for offline replay, AI-agent diffing, or any consumer that prefers a portable artifact over a live UI. | Adapter | How to enable | |---|---| @@ -79,7 +79,7 @@ Headless capture path — no DevTools UI window opens. At session end the adapte | **Nightwatch** | `globals: nightwatchDevtools({ mode: 'trace' })` | The trace artifact contains: -- `trace.trace` — NDJSON `context-options` + `before`/`after` action events. When test hooks are available (Mocha's `it()` / Cucumber's `Scenario()`), each test becomes a [`Tracing.tracingGroup`](https://github.com/VibiumDev/vibium/blob/main/docs/explanation/recording-format.md#action-groups-user-defined) span — an open/close `before`/`after` pair with `method: "tracingGroup"` and `params.name` set to the test title. Child actions inside the group carry `parentId` pointing back to the group's `callId`, so timeline viewers render tests as labelled spans wrapping their commands. +- `trace.trace` — NDJSON `context-options` + `before`/`after` action events. When test hooks are available (Mocha's `it()` / Cucumber's `Scenario()`), each test becomes a `Tracing.tracingGroup` span — an open/close `before`/`after` pair with `method: "tracingGroup"` and `params.name` set to the test title. Child actions inside the group carry `parentId` pointing back to the group's `callId`, so timeline viewers render tests as labelled spans wrapping their commands. - `trace.network` — HAR-style network entries derived from the existing capture - `resources/page@<id>-<ts>.jpeg` — screenshot per user-facing action - `resources/elements-page@<id>-<ts>.json` — flat interactable element list extracted by the page-injected scripts in `@wdio/devtools-core/element-scripts` @@ -103,7 +103,7 @@ npx show-trace path/to/trace.zip # in a project that installs an adapter The `show-trace` bin is exposed by each adapter (`@wdio/devtools-service`, `@wdio/nightwatch-devtools`, `@wdio/selenium-devtools`), so `pnpm show-trace <zip>` / `npx show-trace <zip>` work in any project that installs one — no extra dependency. -**Other viewers.** Because the format is unchanged, the same `.zip` also drops into [player.vibium.dev](https://player.vibium.dev) or `npx playwright show-trace <path>`. The format follows the [Vibium recording format](https://github.com/VibiumDev/vibium/blob/main/docs/explanation/recording-format.md) spec — a Playwright-compatible NDJSON schema that the ecosystem already renders. This is the same format [`@wdio/mcp`](https://webdriver.io/docs/mcp) uses for AI-driven session recording. +**Other viewers.** The trace uses a portable NDJSON schema, so the same `.zip` also opens in other compatible trace viewers that read the format. This is the same format [`@wdio/mcp`](https://webdriver.io/docs/mcp) uses for AI-driven session recording. #### Options @@ -111,7 +111,9 @@ The `show-trace` bin is exposed by each adapter (`@wdio/devtools-service`, `@wdi |--------|--------|---------|-------------| | `mode` | `'live'` \| `'trace'` | `'live'` | `'live'` launches the DevTools UI; `'trace'` writes an offline artifact. | | `traceFormat` | `'zip'` \| `'ndjson-directory'` | `'zip'` | Output layout. `'zip'` writes a single archive; `'ndjson-directory'` unpacks into `trace-<id>/`. | -| `traceGranularity` | `'session'` \| `'spec'` | `'session'` | `'session'` writes one trace per worker; `'spec'` writes one trace per spec file — smaller artifacts, easier to navigate. | +| `traceGranularity` | `'session'` \| `'spec'` \| `'test'` | `'session'` | `'session'` writes one trace per worker; `'spec'` one per spec file; `'test'` one per test into its own `<spec>-<title>-<browser>[-retryN]/trace.zip` folder — the smallest, most navigable artifacts, and the best pairing for a retention policy. | +| `tracePolicy` | `'on'` \| `'retain-on-failure'` \| `'retain-on-first-failure'` \| `'on-first-retry'` \| `'on-all-retries'` \| `'retain-on-failure-and-retries'` | `'on'` | Which traces to keep. `'on'` keeps every trace; the rest keep only failing/retried tests — pairs well with `traceGranularity: 'test'`. | +| `captureAssertions` | `boolean` | `true` | Capture assertions as action rows: `node:assert` (all adapters), WebdriverIO `expect(...)` matchers, and Nightwatch `browser.assert`/`verify`. Set `false` to opt out. | WDIO config example: From 98db9226914a2da293db2464ee5187d9ae1d3d3e Mon Sep 17 00:00:00 2001 From: Vishnu Vardhan <vishnu.p@browserstack.com> Date: Mon, 13 Jul 2026 18:39:51 +0530 Subject: [PATCH 39/68] fix(core): bound the pending-snapshot settle in trace finalize --- packages/core/src/trace-finalizer.ts | 55 ++++++++++++++++++++++++++-- 1 file changed, 52 insertions(+), 3 deletions(-) diff --git a/packages/core/src/trace-finalizer.ts b/packages/core/src/trace-finalizer.ts index 415b6289..f0459590 100644 --- a/packages/core/src/trace-finalizer.ts +++ b/packages/core/src/trace-finalizer.ts @@ -79,6 +79,57 @@ const TEST_WITHOUT_BOUNDARIES_WARNING = /** Above this many slices, warn to pair granularity with a retention policy. */ const SLICE_COUNT_WARN_THRESHOLD = 200 +/** Cap on how long finalize waits for still-in-flight snapshot captures. A + * fire-and-forget capture against a tearing-down session can hang forever + * (never resolves nor rejects); blocking the export on it would deadlock the + * whole run (finalize never returns → the runner never tears the session down + * → the capture stays stuck). Past this bound we write whatever snapshots + * resolved in time — the same "flush from what exists" rule slices use. */ +const PENDING_SETTLE_TIMEOUT_MS = 5000 + +/** + * `Promise.allSettled` bounded by a timeout. Resolves when every pending + * capture settles OR the cap elapses, whichever comes first — a stuck capture + * can never hang the export. Returns whether it timed out so the caller can + * warn. The timer is unref'd so it can't itself keep the process alive. + */ +async function settlePending( + pending: Promise<unknown>[], + timeoutMs: number +): Promise<boolean> { + let timer: ReturnType<typeof setTimeout> | undefined + const timedOut = new Promise<true>((resolve) => { + timer = setTimeout(() => resolve(true), timeoutMs) + timer.unref?.() + }) + const settled = Promise.allSettled(pending).then(() => false) + const result = await Promise.race([settled, timedOut]) + if (timer) { + clearTimeout(timer) + } + return result +} + +/** Settle in-flight snapshot captures under the timeout cap, warning if the + * bound elapses. Never throws; never hangs. */ +async function awaitPendingCaptures(ctx: TraceExportContext): Promise<void> { + if (!ctx.awaitPending?.length) { + return + } + const timedOut = await settlePending( + ctx.awaitPending, + PENDING_SETTLE_TIMEOUT_MS + ) + if (timedOut) { + ctx.log?.( + 'warn', + `One or more of ${ctx.awaitPending.length} snapshot capture(s) did not ` + + `settle within ${PENDING_SETTLE_TIMEOUT_MS}ms; writing trace with the ` + + 'snapshots captured so far.' + ) + } +} + function sliceCountWarning(count: number): string { return ( `traceGranularity produced ${count} trace slices. Consider pairing it ` + @@ -269,9 +320,7 @@ export async function finalizeTraceExport( if (ctx.mode !== 'trace') { return [] } - if (ctx.awaitPending?.length) { - await Promise.allSettled(ctx.awaitPending) - } + await awaitPendingCaptures(ctx) const sliced = ctx.granularity === 'spec' || ctx.granularity === 'test' if (sliced && ctx.ranges.length > 0) { if (ctx.ranges.length > SLICE_COUNT_WARN_THRESHOLD) { From e7ddba829f50bc2e2d233a9feda18b51cff85e57 Mon Sep 17 00:00:00 2001 From: Vishnu Vardhan <vishnu.p@browserstack.com> Date: Mon, 13 Jul 2026 18:40:05 +0530 Subject: [PATCH 40/68] fix(core): order trace actions chronologically, not by insertion --- packages/core/src/trace-action-events.ts | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/packages/core/src/trace-action-events.ts b/packages/core/src/trace-action-events.ts index 7cd44eab..29eeb0b2 100644 --- a/packages/core/src/trace-action-events.ts +++ b/packages/core/src/trace-action-events.ts @@ -297,7 +297,18 @@ export function buildActionEvents( snapshotIndex?: FrameSnapshotIndex ): ActionEvent[] { const stream: ActionStream = { events: [], prevEndMs: 0, callCounter: 0 } - for (const cmd of commands) { + // Process in chronological order, not insertion order. Deferred rows (e.g. + // Nightwatch native asserts, finalized in one batch at test-end) are appended + // late but carry their real call-time `startTime`; since pushActionPair floors + // each start at the running prevEndMs, out-of-order input would clamp those + // late rows to the end of the timeline (clustering them after the last real + // command). A stable sort by start time restores true positions and keeps + // equal-time rows in insertion order (each command owns its own before/after + // pair, so pairing is unaffected). + const ordered = [...commands].sort( + (a, b) => (a.startTime ?? a.timestamp) - (b.startTime ?? b.timestamp) + ) + for (const cmd of ordered) { const action = mapCommandToAction(cmd.command) if (!action) { continue From 10afcf87a06f33c2278ce5ad4250dea7d7c9a100 Mon Sep 17 00:00:00 2001 From: Vishnu Vardhan <vishnu.p@browserstack.com> Date: Mon, 13 Jul 2026 18:40:25 +0530 Subject: [PATCH 41/68] fix(core): capture strict-mode node:assert and label asserts from args --- packages/core/src/action-mapping.ts | 12 +++--- packages/core/src/assert-patcher.ts | 44 +++++++++++++++----- packages/core/tests/assert-patcher.test.ts | 22 +++++++++- packages/core/tests/trace-assertions.test.ts | 19 ++++++++- 4 files changed, 79 insertions(+), 18 deletions(-) diff --git a/packages/core/src/action-mapping.ts b/packages/core/src/action-mapping.ts index 368ca8e3..eb31c2b2 100644 --- a/packages/core/src/action-mapping.ts +++ b/packages/core/src/action-mapping.ts @@ -46,8 +46,12 @@ function formatAssertValue(value: unknown): string { : text } -// Prefer normalized actual/expected params (nightwatch collapses them into -// the result); fall back to the first two positional args (node:assert order). +// The label mirrors the CALL the user wrote — its positional args +// (`assert.equal(a, b)`, `titleContains('x')`). The normalized actual/expected +// params drive the result diff, NOT the label: a single-arg assert like +// titleContains passes only the expected, so labelling it with the derived +// actual would misrepresent the call. Fall back to actual/expected only when +// no args survived (reader round-trips that dropped them). function formatAssertTitle( action: TraceAction, args: unknown[], @@ -55,9 +59,7 @@ function formatAssertTitle( command?: string ): string { const values = - params && ('actual' in params || 'expected' in params) - ? [params.actual, params.expected] - : args.slice(0, 2) + args.length > 0 ? args.slice(0, 2) : [params?.actual, params?.expected] const label = values .filter((value) => value !== undefined) .map(formatAssertValue) diff --git a/packages/core/src/assert-patcher.ts b/packages/core/src/assert-patcher.ts index c6b91bf2..c60f21ab 100644 --- a/packages/core/src/assert-patcher.ts +++ b/packages/core/src/assert-patcher.ts @@ -200,6 +200,29 @@ export function matcherAssertionToCommandLog( return entry } +/** Wrap every tracked method on one assert namespace object in place; returns + * how many were patched. Used for both `assert.*` and `assert.strict.*`. */ +function patchAssertNamespace( + nsObj: Record<string | symbol, unknown>, + onCommand: (cmd: CapturedAssert) => void +): number { + let count = 0 + for (const methodName of TRACKED_ASSERT_METHODS) { + const original = nsObj[methodName] + if (typeof original !== 'function') { + continue + } + nsObj[methodName] = makePatchedAssertMethod( + methodName, + nsObj, + original as (...a: unknown[]) => unknown, + onCommand + ) + count++ + } + return count +} + /** * Patch `node:assert` so each tracked method emits a `CapturedAssert` to the * supplied hook. Idempotent across calls (guarded by `ASSERT_PATCHED_SYMBOL`). @@ -239,19 +262,20 @@ export function patchNodeAssert( } assertObj[ASSERT_PATCHED_SYMBOL] = true - for (const methodName of TRACKED_ASSERT_METHODS) { - const original = assertObj[methodName] - if (typeof original !== 'function') { - continue - } - assertObj[methodName] = makePatchedAssertMethod( - methodName, - assertObj, - original as (...a: unknown[]) => unknown, + let patched = patchAssertNamespace(assertObj, onCommand) + // `import { strict as assert }` and `assert.strict.*` reference a separate + // object with its own method identities; patch it too so strict-mode + // assertions are captured the same as the default ones. `assert.strict` is a + // callable (strict-mode `assert()`) with the methods hung off it, so it's a + // function, not a plain object. + const strict = assertObj.strict + if (strict && (typeof strict === 'object' || typeof strict === 'function')) { + patched += patchAssertNamespace( + strict as Record<string | symbol, unknown>, onCommand ) } - log('info', `Patched ${TRACKED_ASSERT_METHODS.length} node:assert method(s)`) + log('info', `Patched ${patched} node:assert method(s)`) return true } diff --git a/packages/core/tests/assert-patcher.test.ts b/packages/core/tests/assert-patcher.test.ts index 38bea721..5eda95da 100644 --- a/packages/core/tests/assert-patcher.test.ts +++ b/packages/core/tests/assert-patcher.test.ts @@ -23,17 +23,24 @@ const USER_FRAME = { } const INTERNAL_FRAME = { filePath: undefined, callSource: 'unknown:0' } -// Snapshot real methods once so each test starts from a fresh assert. +// Snapshot real methods once so each test starts from a fresh assert. Both the +// default namespace and `assert.strict` are patched, so restore both. const ASSERT_MUT = assert as unknown as Record<string | symbol, unknown> +const STRICT_MUT = ( + assert as unknown as { strict: Record<string | symbol, unknown> } +).strict const originals: Record<string, unknown> = {} +const strictOriginals: Record<string, unknown> = {} for (const m of TRACKED_ASSERT_METHODS) { originals[m] = ASSERT_MUT[m] + strictOriginals[m] = STRICT_MUT[m] } beforeEach(() => { delete ASSERT_MUT[ASSERT_PATCHED_SYMBOL] for (const m of TRACKED_ASSERT_METHODS) { ASSERT_MUT[m] = originals[m] + STRICT_MUT[m] = strictOriginals[m] } // Default: every assert looks like it came from user code so captures fire. vi.mocked(getCallSourceFromStack).mockReturnValue(USER_FRAME) @@ -84,6 +91,19 @@ describe('patchNodeAssert', () => { expect(results).toEqual(['passed', undefined]) // first resolved, second rejected }) + // `import { strict as assert }` (and `assert.strict.*`) reference a separate + // object; without patching it, strict-mode assertions were never captured. + it('captures assertions on the strict namespace too', () => { + const captured: CapturedAssert[] = [] + patchNodeAssert((c) => captured.push(c)) + assert.strict.equal(1, 1) + expect(() => assert.strict.equal(1, 2)).toThrow() + expect(captured.map((c) => [c.command, c.result])).toEqual([ + ['assert.equal', 'passed'], + ['assert.equal', undefined] + ]) + }) + // Only assertions that originate in user test code should reach the trace. // Dependency/framework-internal asserts have no user-code frame on the // stack (getCallSourceFromStack yields 'unknown:0') and must be dropped. diff --git a/packages/core/tests/trace-assertions.test.ts b/packages/core/tests/trace-assertions.test.ts index 0d3d4ccd..d28b8d55 100644 --- a/packages/core/tests/trace-assertions.test.ts +++ b/packages/core/tests/trace-assertions.test.ts @@ -67,7 +67,10 @@ describe('formatActionTitle for asserts', () => { ).toBe('assert.strictEqual("a", "b")') }) - it('prefers normalized actual/expected params over positional args', () => { + it('labels from the call args, not the derived actual/expected', () => { + // textContains('#el', 'foo') passes the selector + expected as args; the + // real "actual" ('bar') lives in params for the result diff and must NOT + // leak into the concise label, which mirrors the call the user wrote. expect( formatActionTitle( action, @@ -75,6 +78,17 @@ describe('formatActionTitle for asserts', () => { { actual: 'bar', expected: 'foo' }, 'verify.textContains' ) + ).toBe('verify.textContains("#el", "foo")') + }) + + it('falls back to actual/expected params only when no args survive', () => { + expect( + formatActionTitle( + action, + [], + { actual: 'bar', expected: 'foo' }, + 'verify.textContains' + ) ).toBe('verify.textContains("bar", "foo")') }) @@ -169,7 +183,8 @@ describe('buildActionEvents with assert commands', () => { expected: 'foo', message: 'nope' }) - expect(before.title).toBe('verify.textContains("bar", "foo")') + // Label mirrors the call args; actual/expected stay in params for the diff. + expect(before.title).toBe('verify.textContains("#el", "foo")') // No Error instance on the entry — the failure comes from the collapsed result. expect(afterOf(events, before.callId)?.error).toEqual({ message: 'nope' }) }) From 181b6d5ae718c72371182e6e5dd143d3c324f145 Mon Sep 17 00:00:00 2001 From: Vishnu Vardhan <vishnu.p@browserstack.com> Date: Mon, 13 Jul 2026 18:40:42 +0530 Subject: [PATCH 42/68] fix(nightwatch): emit native asserts at test-end with real outcomes --- .../src/helpers/browserProxy.ts | 10 +- .../src/helpers/nativeAssertions.ts | 162 ++++++++++++++---- packages/nightwatch-devtools/src/index.ts | 2 + .../nightwatch-devtools/src/trace-context.ts | 6 +- .../tests/nativeAssertions.test.ts | 123 ++++++++----- 5 files changed, 219 insertions(+), 84 deletions(-) diff --git a/packages/nightwatch-devtools/src/helpers/browserProxy.ts b/packages/nightwatch-devtools/src/helpers/browserProxy.ts index dff01a1f..21225506 100644 --- a/packages/nightwatch-devtools/src/helpers/browserProxy.ts +++ b/packages/nightwatch-devtools/src/helpers/browserProxy.ts @@ -134,9 +134,12 @@ export class BrowserProxy { }) } - /** Stream a neutral pending row for an explicit assert/verify call the moment - * it's invoked, and buffer the call (with its emitted row) for `afterEach` - * to finalize pass/fail in place. */ + /** Buffer an explicit assert/verify call at invocation time (prebuilding its + * row with args/callSource/screenshot), but do NOT stream it yet. Nightwatch + * exposes no per-assertion result hook, so streaming here would show every + * assert as a neutral (green) row while the test is still Running — before + * its pass/fail is known. `captureNativeAssertions` emits the rows at test-end + * with real outcomes + execution timing instead. */ private emitPendingAssertion(call: NativeAssertCall): void { const testUid = this.getCurrentTest()?.uid const entry = pendingAssertionCommand( @@ -144,7 +147,6 @@ export class BrowserProxy { testUid, latestResolvedScreenshot(this.sessionCapturer) ) - this.sessionCapturer.captureAssertCommand(entry) call.entry = entry this.nativeAssertCalls.push(call) } diff --git a/packages/nightwatch-devtools/src/helpers/nativeAssertions.ts b/packages/nightwatch-devtools/src/helpers/nativeAssertions.ts index 16dd3bd2..1134047d 100644 --- a/packages/nightwatch-devtools/src/helpers/nativeAssertions.ts +++ b/packages/nightwatch-devtools/src/helpers/nativeAssertions.ts @@ -1,19 +1,22 @@ // Native-assertion capture: turns explicit `browser.assert.*` / // `browser.verify.*` calls into concise trace/UI action rows with real args, -// a clickable source location, and pass/fail colour — streamed LIVE. +// a clickable source location, and pass/fail colour. // -// Nightwatch exposes no per-assertion hook, and its test-end -// `currentTest.results` carries no source location for PASSING assertions -// (results.assertions[i].stackTrace is '' on pass) and only stringified args. -// So each explicit call is intercepted at CALL TIME -// (BrowserProxy.wrapAssertionNamespaces): a neutral "pending" row is emitted -// immediately (concise title, real args, callSource) so rows stream in one by -// one like normal commands. At TEST END the pass/fail truth + verbose failure -// message are read from results.assertions and the already-emitted row is -// UPDATED IN PLACE (same stable id) — never re-created, so no duplicates. -// Iterating the recorded calls (not results.assertions) also excludes +// Nightwatch exposes no per-assertion hook. Each explicit call is intercepted +// at CALL TIME (BrowserProxy.wrapAssertionNamespaces) and BUFFERED — concise +// title, real args, callSource, the per-test testUid, and the preceding +// screenshot — but NOT streamed, because streaming a neutral row mid-run would +// render every assert green before its outcome is known. At test-end +// `captureNativeAssertions` reads the pass/fail truth + verbose failure message +// from the results bag and emits each row once, positioned on its real +// execution window (the exporter re-sorts by the buffered call timestamp). +// Correlating the recorded calls (not the raw results) also excludes // Nightwatch's implicit command-generated assertions (e.g. // waitForElementVisible's "element was visible" entry). +// +// The plugin `afterEach` fires once per describe-suite (not per `it`), so the +// finalize reads assertions from `results.testcases` (all tests) — see +// gatherResultAssertions. import logger from '@wdio/logger' import { @@ -96,6 +99,39 @@ interface Outcome { message: string } +/** + * All assertion entries for the suite, in declaration order. Nightwatch's + * plugin `afterEach` fires once per describe-suite (not per `it`), so the flat + * `results.assertions` reflects only the last testcase; the full per-test + * breakdown lives in `results.testcases[title].assertions`. Flattening every + * testcase's entries lets each buffered call correlate to its own test's + * outcome (without this, only the last test's asserts get a pass/fail and the + * rest render neutral). Falls back to the flat list for single-test modules or + * older Nightwatch that doesn't populate `testcases`. + */ +function gatherResultAssertions( + results: + | { + assertions?: NwAssertionEntry[] + testcases?: Record<string, { assertions?: unknown[] }> + } + | undefined +): NwAssertionEntry[] { + const testcases = results?.testcases + if (testcases && typeof testcases === 'object') { + const all: NwAssertionEntry[] = [] + for (const tc of Object.values(testcases)) { + if (Array.isArray(tc?.assertions)) { + all.push(...(tc.assertions as NwAssertionEntry[])) + } + } + if (all.length > 0) { + return all + } + } + return Array.isArray(results?.assertions) ? results.assertions : [] +} + /** * Pair each recorded call with its `results.assertions` entry for pass/fail + * verbose message. Both lists are in call/execution order and each explicit @@ -206,8 +242,55 @@ export function pendingAssertionCommand( return entry } -/** Update one streamed pending row in place: apply pass/fail + verbose error, - * a screenshot, and its real execution window, then re-broadcast by stable id. */ +/** Collapsed pass/fail result core's `collapsedAssertResult` reads. */ +interface CollapsedAssertResult { + passed: boolean + expected?: unknown + actual?: unknown + message?: string +} + +/** Nightwatch failure messages end with `… but got: "<actual>"`. Pull out the + * real observed value: Nightwatch passes only the EXPECTED as an arg, so the + * actual lives in the message (and only on failure). Undefined when absent. + * Uses indexOf + slice (no backtracking-prone regex) on the message tail. */ +function parseActualFromMessage(message: string): string | undefined { + const marker = 'but got:' + const idx = message.lastIndexOf(marker) + if (idx === -1) { + return undefined + } + let rest = message.slice(idx + marker.length).trim() + rest = rest.replace(/ \(\d+ms\)$/, '').trim() // drop trailing "(123ms)" + rest = rest.replace(/^["']/, '').replace(/["']$/, '').trim() // strip quotes + return rest || undefined +} + +/** Build the collapsed `{passed, expected, actual?, message}` result core's + * `buildAssertParams` prefers over the positional `[actual, expected]` arg + * convention — which is wrong for Nightwatch, whose asserts pass only the + * expected value (`titleContains('x')`), never the actual. */ +function collapsedAssertResult( + call: NativeAssertCall, + outcome: Outcome +): CollapsedAssertResult { + const result: CollapsedAssertResult = { + passed: outcome.passed, + expected: call.args.length <= 1 ? call.args[0] : call.args, + message: outcome.message + } + const actual = outcome.passed + ? undefined + : parseActualFromMessage(outcome.message) + if (actual !== undefined) { + result.actual = actual + } + return result +} + +/** Emit one assertion row at test-end: apply pass/fail + verbose error, a + * screenshot, and its real execution window, then send it. Rows are NOT + * streamed at call time, so this is the single emit (no neutral pending row). */ function finalizeAssertionRow( capturer: SessionCapturer, call: NativeAssertCall, @@ -229,32 +312,34 @@ function finalizeAssertionRow( }, testUid ) - entry.result = finalized.result + // Collapsed object result (not the plain 'passed' string) so the trace's + // action params show a true actual-vs-expected diff instead of mislabelling + // Nightwatch's single expected-arg as the actual. + entry.result = collapsedAssertResult(call, outcome) entry.error = finalized.error if (!entry.screenshot && screenshot) { entry.screenshot = screenshot } - // Reposition the row on its REAL execution window (Nightwatch enqueues all - // asserts at once, so the emit/enqueue timestamp clustered them). The row is - // matched for replacement by its stable id, so the old enqueue timestamp is - // what the UI still keys on until this swap lands. - const oldTimestamp = entry.timestamp + // Position the row on its REAL execution window (Nightwatch enqueues all + // asserts at once, so the call-time timestamp clustered them). The row was + // never streamed, so send it now — a single emit with the final outcome. if (timing) { entry.startTime = timing.startTime entry.timestamp = timing.endTime > timing.startTime ? timing.endTime : timing.startTime } - capturer.sendReplaceCommand(oldTimestamp, entry) + capturer.captureAssertCommand(entry) log.info(`[assert] ${entry.title} → ${outcome.passed ? 'pass' : 'fail'}`) } /** - * Finalize the streamed pending rows at test-end: correlate the recorded calls - * with `results.assertions`, then UPDATE each row's `entry` in place with - * pass/fail (`result`) + the verbose failure message (`error`, failures only) - * + a screenshot + its real execution window, and re-broadcast via - * `sendReplaceCommand` keyed on the row's stable id. No new rows are created - * (no duplicates); a call with no matching result is left pending (defensive). + * Emit the native assertion rows at test-end: correlate the recorded calls with + * `results.assertions`, then send each row with pass/fail (`result`) + the + * verbose failure message (`error`, failures only) + a screenshot + its real + * execution window. Rows are buffered (not streamed) at call time — Nightwatch + * has no per-assertion result hook, so streaming them during the run would show + * every assert as a neutral (green) row before its outcome is known. A call with + * no matching result is skipped (defensive). */ export async function captureNativeAssertions( capturer: SessionCapturer, @@ -269,11 +354,13 @@ export async function captureNativeAssertions( // Boundary cast: `results` is Nightwatch's loosely-typed per-test bag; we read // only the assertions + commands arrays whose shapes are documented above. const results = currentTest?.results as - | { assertions?: NwAssertionEntry[]; commands?: NwCommandEntry[] } + | { + assertions?: NwAssertionEntry[] + commands?: NwCommandEntry[] + testcases?: Record<string, { assertions?: unknown[] }> + } | undefined - const assertions = Array.isArray(results?.assertions) - ? results.assertions - : [] + const assertions = gatherResultAssertions(results) const outcomes = correlate(calls, assertions) const timings = assertCommandTimings( Array.isArray(results?.commands) ? results.commands : [], @@ -283,9 +370,11 @@ export async function captureNativeAssertions( const screenshot = await resolveAssertionScreenshot(capturer, browser) calls.forEach((call, index) => { + if (!call.entry) { + return + } const outcome = outcomes[index] - // Leave an unmatched (or never-emitted) row in its last state. - if (call.entry && outcome) { + if (outcome) { finalizeAssertionRow( capturer, call, @@ -294,6 +383,15 @@ export async function captureNativeAssertions( screenshot, testUid ) + return + } + // No execution outcome correlated to this call (defensive): emit the + // buffered row in its neutral state so the assertion still appears — never + // dropped, never mis-coloured as passed/failed. Rows are only buffered at + // call time, so without this an uncorrelated assert would vanish. + if (!call.entry.screenshot && screenshot) { + call.entry.screenshot = screenshot } + capturer.captureAssertCommand(call.entry) }) } diff --git a/packages/nightwatch-devtools/src/index.ts b/packages/nightwatch-devtools/src/index.ts index db5cefc0..53b1a3dd 100644 --- a/packages/nightwatch-devtools/src/index.ts +++ b/packages/nightwatch-devtools/src/index.ts @@ -589,6 +589,8 @@ class NightwatchDevToolsPlugin { ranges: this.#specRanges, flushed: this.#flushedSpecs, configPath: this.#configPath, + testFilePath: + this.browserProxy?.getCurrentTestFullPath?.() ?? undefined, log: (level, msg) => log[level](msg) }, sessionId diff --git a/packages/nightwatch-devtools/src/trace-context.ts b/packages/nightwatch-devtools/src/trace-context.ts index 1e451ac7..359737b9 100644 --- a/packages/nightwatch-devtools/src/trace-context.ts +++ b/packages/nightwatch-devtools/src/trace-context.ts @@ -31,6 +31,7 @@ export interface TraceContextInput { ranges: SpecRange[] flushed: Set<string> configPath: string | undefined + testFilePath: string | undefined log: (level: 'info' | 'warn', msg: string) => void } @@ -50,7 +51,10 @@ export function buildTraceContext( ranges: input.ranges, flushed: input.flushed, resolveOutputDir: () => - resolveAdapterOutputDir({ configPath: input.configPath }), + resolveAdapterOutputDir({ + testFilePath: input.testFilePath, + configPath: input.configPath + }), awaitPending: input.capturer.snapshotCaptures, // Nightwatch feeds real per-test attempt numbers via TestAttemptTracker // (B4), so retry-aware policies use per-test attempts, not the fallback. diff --git a/packages/nightwatch-devtools/tests/nativeAssertions.test.ts b/packages/nightwatch-devtools/tests/nativeAssertions.test.ts index d904dbde..352875f2 100644 --- a/packages/nightwatch-devtools/tests/nativeAssertions.test.ts +++ b/packages/nightwatch-devtools/tests/nativeAssertions.test.ts @@ -56,21 +56,20 @@ function makeFakeCapturer( const browser = {} as unknown as NightwatchBrowser -/** Mimic BrowserProxy.emitPendingAssertion: stream a neutral pending row at - * call time and attach it to the call for later finalization. */ +/** Mimic BrowserProxy.emitPendingAssertion: build the row and buffer it on the + * call, but do NOT stream it — rows are emitted at test-end by + * captureNativeAssertions once their pass/fail is known. */ function emitPending( capturer: SessionCapturer, calls: NativeAssertCall[], testUid: string | undefined ) { for (const call of calls) { - const entry = pendingAssertionCommand( + call.entry = pendingAssertionCommand( call, testUid, latestResolvedScreenshot(capturer) ) - capturer.captureAssertCommand(entry) - call.entry = entry } } @@ -104,8 +103,8 @@ function call( return { prefix, method, args, callSource, timestamp: clock++ } } -describe('captureNativeAssertions (live pending → finalize)', () => { - it('streams neutral pending rows at call time, then finalizes pass/fail in place with a stable id and no duplicates', async () => { +describe('captureNativeAssertions (buffer → finalize at test-end)', () => { + it('buffers rows at call time (nothing streamed), then emits pass/fail once at test-end with no duplicates', async () => { // Preceding real commands: the last with a resolved screenshot is the DOM // the assertions evaluated against — reused for the pending rows. const precedingCommands: CommandLog[] = [ @@ -120,7 +119,6 @@ describe('captureNativeAssertions (live pending → finalize)', () => { const { capturer, sent, - replaced, commandsLog, captureAssertCommand, sendReplaceCommand, @@ -133,23 +131,23 @@ describe('captureNativeAssertions (live pending → finalize)', () => { call('assert', 'titleContains', ['HARD_FAIL_ME'], 'spec.js:16') ] - // 1) CALL TIME: each call streams one neutral pending row. + // 1) CALL TIME: rows are buffered, NOT streamed — nothing is sent, so no + // assert shows as a neutral (green) row before its outcome is known. emitPending(capturer, calls, 'test-uid') - expect(captureAssertCommand).toHaveBeenCalledTimes(4) - expect(sent).toHaveLength(4) - for (const row of sent) { + expect(captureAssertCommand).not.toHaveBeenCalled() + expect(sent).toHaveLength(0) + const buffered = calls.map((c) => c.entry!) + for (const row of buffered) { expect(row.result).toBeUndefined() // neutral — not green expect(row.error).toBeUndefined() // neutral — not red expect(row.testUid).toBe('test-uid') expect(row.screenshot).toBe('BODY_SHOT') // reused preceding DOM - expect(typeof (row as { id?: number }).id).toBe('number') } - expect(sent[0].title).toBe("verify.titleContains('Example')") - expect(sent[0].command).toBe('verify.titleContains') - expect(sent[0].args).toEqual(['Example']) - expect(sent[0].callSource).toBe('spec.js:11') - expect(sent[0].timestamp).toBe(sent[0].startTime) - const pendingIds = sent.map((r) => (r as { id?: number }).id) + expect(buffered[0].title).toBe("verify.titleContains('Example')") + expect(buffered[0].command).toBe('verify.titleContains') + expect(buffered[0].args).toEqual(['Example']) + expect(buffered[0].callSource).toBe('spec.js:11') + expect(buffered[0].timestamp).toBe(buffered[0].startTime) // 2) TEST END: results.assertions includes the implicit waitForElementVisible // assertion (first entry) which must NOT create/finalize a row. @@ -168,46 +166,45 @@ describe('captureNativeAssertions (live pending → finalize)', () => { calls ) - // Each row updated exactly once, in place — no new rows, no duplicates. + // Rows emitted exactly once at test-end — no call-time stream, no replace. expect(captureAssertCommand).toHaveBeenCalledTimes(4) - expect(sendReplaceCommand).toHaveBeenCalledTimes(4) + expect(sendReplaceCommand).not.toHaveBeenCalled() + expect(sent).toHaveLength(4) expect(commandsLog).toHaveLength(2 + 4) expect(takeScreenshotViaHttp).not.toHaveBeenCalled() - // Same row objects, same stable ids (updated, not recreated). const finalized = calls.map((c) => c.entry!) - expect(finalized.map((r) => (r as { id?: number }).id)).toEqual(pendingIds) - // Pass/fail applied; failures carry the verbose message as error. - expect(finalized[0].result).toBe('passed') + // Pass/fail applied as a collapsed { passed, expected, actual? } result; + // failures also carry the verbose message as error. + expect(finalized[0].result).toMatchObject({ passed: true }) expect(finalized[0].error).toBeUndefined() - expect(finalized[1].result).toBeUndefined() + expect(finalized[1].result).toMatchObject({ passed: false }) expect((finalized[1].error as { message: string }).message).toContain( 'SOFT_FAIL_ME' ) - expect(finalized[2].result).toBe('passed') // duplicate 'Example' → 2nd entry + expect(finalized[2].result).toMatchObject({ passed: true }) // 2nd 'Example' expect(finalized[3].error).toBeDefined() // Labels/args/callSource preserved from the pending row. expect(finalized[3].title).toBe("assert.titleContains('HARD_FAIL_ME')") expect(finalized[3].callSource).toBe('spec.js:16') - // With no results.commands timing, rows keep their enqueue timestamp, so - // the replace is keyed on that same timestamp. - replaced.forEach(({ oldTimestamp, command }) => { - expect(oldTimestamp).toBe(command.timestamp) + // With no results.commands timing, rows keep their call-time timestamp. + finalized.forEach((row) => { + expect(row.timestamp).toBe(row.startTime) }) }) it('repositions each row on its real execution window from results.commands (not enqueue time)', async () => { - const { capturer, sent, replaced } = makeFakeCapturer() + const { capturer, sent } = makeFakeCapturer() const calls = [ call('verify', 'titleContains', ['Example']), call('assert', 'titleContains', ['SOFT_FAIL_ME']) ] emitPending(capturer, calls, 'uid') - // Both enqueued ~together (clock++), clustered. - const enqueue = sent.map((r) => r.timestamp) + // Buffered ~together (clock++), clustered at call time — nothing sent yet. + expect(sent).toHaveLength(0) // Nightwatch ran them ~29ms apart; results.commands carries the real window. const assertions = [ @@ -234,10 +231,10 @@ describe('captureNativeAssertions (live pending → finalize)', () => { expect(finalized[1].startTime).toBe(6100) expect(finalized[1].timestamp).toBe(6132) expect(finalized[1].timestamp - finalized[0].timestamp).toBeGreaterThan(50) - // The replace is keyed on the ORIGINAL enqueue timestamp (stable id also - // matches), while the row now carries its real execution timestamp. - expect(replaced[0].oldTimestamp).toBe(enqueue[0]) - expect(replaced[0].command.timestamp).toBe(6029) + // Rows are sent (not replaced) at test-end, each carrying its real window. + expect(sent).toHaveLength(2) + expect(sent[0].timestamp).toBe(6029) + expect(sent[1].timestamp).toBe(6132) }) it('finalizes with a fresh screenshot when no preceding one has resolved yet', async () => { @@ -246,13 +243,13 @@ describe('captureNativeAssertions (live pending → finalize)', () => { const pending: CommandLog[] = [ { command: 'waitForElementVisible', args: ['body'], timestamp: 2 } ] - const { capturer, sent, takeScreenshotViaHttp } = makeFakeCapturer( + const { capturer, takeScreenshotViaHttp } = makeFakeCapturer( pending, 'FRESH_SHOT' ) const calls = [call('verify', 'titleContains', ['Example'])] emitPending(capturer, calls, 'uid') - expect(sent[0].screenshot).toBeUndefined() + expect(calls[0].entry!.screenshot).toBeUndefined() await captureNativeAssertions( capturer, @@ -265,15 +262,15 @@ describe('captureNativeAssertions (live pending → finalize)', () => { ) expect(takeScreenshotViaHttp).toHaveBeenCalledTimes(1) expect(calls[0].entry!.screenshot).toBe('FRESH_SHOT') - expect(calls[0].entry!.result).toBe('passed') + expect(calls[0].entry!.result).toMatchObject({ passed: true }) }) it('preserves real multi-arg assertions (no faked args)', async () => { - const { capturer, sent, replaced } = makeFakeCapturer() + const { capturer, sent } = makeFakeCapturer() const calls = [call('assert', 'containsText', ['#btn', 'Save'])] emitPending(capturer, calls, 'uid') - expect(sent[0].args).toEqual(['#btn', 'Save']) - expect(sent[0].title).toBe("assert.containsText('#btn', 'Save')") + expect(calls[0].entry!.args).toEqual(['#btn', 'Save']) + expect(calls[0].entry!.title).toBe("assert.containsText('#btn', 'Save')") await captureNativeAssertions( capturer, @@ -284,17 +281,17 @@ describe('captureNativeAssertions (live pending → finalize)', () => { 'uid', calls ) - expect(replaced).toHaveLength(1) + expect(sent).toHaveLength(1) expect(calls[0].entry!.error).toBeDefined() }) it('falls back to positional correlation when args are not literals', async () => { - const { capturer, sent } = makeFakeCapturer() + const { capturer } = makeFakeCapturer() // Element-object arg won't substring-match; positional order still pairs // the single call with the single explicit assertion outcome. const calls = [call('assert', 'elementPresent', [{ selector: 'body' }])] emitPending(capturer, calls, 'uid') - expect(sent[0].title).toBe('assert.elementPresent(…)') + expect(calls[0].entry!.title).toBe('assert.elementPresent(…)') await captureNativeAssertions( capturer, @@ -324,6 +321,38 @@ describe('captureNativeAssertions (live pending → finalize)', () => { expect(calls[0].entry!.error).toBeUndefined() }) + it('surfaces a real actual-vs-expected from the failure message (not the expected-only arg)', async () => { + const { capturer, sent } = makeFakeCapturer() + const calls = [call('assert', 'titleContains', ['This Is Not The Title'])] + emitPending(capturer, calls, 'uid') + + await captureNativeAssertions( + capturer, + browser, + currentTestWith([ + failing( + "Testing if the page title contains 'This Is Not The Title' in " + + '5000ms - expected "contains \'This Is Not The Title\'" but got: ' + + '"Example Domain"' + ) + ]), + 'uid', + calls + ) + + // Nightwatch passes only the EXPECTED as an arg; the real actual lives in + // the "but got: …" clause of the message. The collapsed result must carry + // the true diff, not mislabel the expected arg as the actual. + const result = sent[0].result as { + passed: boolean + expected: unknown + actual: unknown + } + expect(result.passed).toBe(false) + expect(result.expected).toBe('This Is Not The Title') + expect(result.actual).toBe('Example Domain') + }) + it('is a no-op when there are no recorded calls (implicit assertions ignored)', async () => { const { capturer, sendReplaceCommand, takeScreenshotViaHttp } = makeFakeCapturer() From 80916f61a162d9a6c66987d93898617a375fa00b Mon Sep 17 00:00:00 2001 From: Vishnu Vardhan <vishnu.p@browserstack.com> Date: Mon, 13 Jul 2026 18:40:57 +0530 Subject: [PATCH 43/68] fix(service): keep expect matchers to a single row --- packages/service/src/index.ts | 58 +++++++++++++++++++--------- packages/service/tests/index.test.ts | 52 +++++++++++++++++++------ 2 files changed, 79 insertions(+), 31 deletions(-) diff --git a/packages/service/src/index.ts b/packages/service/src/index.ts index 411d75d7..2696d71a 100644 --- a/packages/service/src/index.ts +++ b/packages/service/src/index.ts @@ -110,6 +110,14 @@ export default class DevToolsHookService implements Services.ServiceInstance { * matching Playwright and the Nightwatch native-assert behaviour. */ #assertionDepth = 0 + /** True once the current matcher has issued an internal command whose stack + * shows an expect-webdriverio frame. Gates the stuck-depth self-heal: the + * matcher's FIRST internal command (an element re-lookup) traces to the + * user's expect() line but its sync stack has no matcher frame yet, which + * would otherwise be misread as "matcher ended" and reset the depth mid-run + * — capturing every later matcher command as a duplicate row. */ + #matcherStarted = false + /** Matcher start timestamps (stack, paired with #assertionDepth) so a captured * assertion spans [matcher start → end] — its poll duration — instead of * collapsing to a zero-width point at completion, which left the screencast @@ -464,6 +472,7 @@ export default class DevToolsHookService implements Services.ServiceInstance { this.#assertionCallSources = [] } this.#assertionDepth++ + this.#matcherStarted = false this.#assertionStartTimes.push(Date.now()) // Capture the user's expect() call site now — see #assertionCallSources. this.#assertionCallSources.push( @@ -480,6 +489,7 @@ export default class DevToolsHookService implements Services.ServiceInstance { if (this.#assertionDepth > 0) { this.#assertionDepth-- } + this.#matcherStarted = false if (this.#options.captureAssertions === false) { return } @@ -546,6 +556,7 @@ export default class DevToolsHookService implements Services.ServiceInstance { private resetStack() { this.#commandStack = [] this.#assertionDepth = 0 + this.#matcherStarted = false this.#assertionStartTimes = [] this.#assertionCallSources = [] this.#sessionCapturer.resetLastSelector() @@ -569,6 +580,30 @@ export default class DevToolsHookService implements Services.ServiceInstance { } } + /** Maintain the expect-matcher suppression window for an incoming command. + * `inMatcher` marks the matcher as started (so its trailing internal + * commands stay suppressed); otherwise self-heal a depth left stuck by a + * matcher that hard-threw (skipping afterAssertion) — but only once the + * matcher has actually started, so its own leading element re-lookup (no + * matcher frame on the sync stack yet) can't reset the depth mid-run. */ + #trackAssertionWindow(inMatcher: boolean, hasSource: boolean): void { + if (inMatcher) { + this.#matcherStarted = true + return + } + if ( + hasSource && + this.#commandStack.length === 0 && + this.#assertionDepth > 0 && + this.#matcherStarted + ) { + this.#assertionDepth = 0 + this.#assertionStartTimes = [] + this.#assertionCallSources = [] + this.#matcherStarted = false + } + } + async beforeCommand(command: string, args: string[]) { if (!this.#browser) { return @@ -589,25 +624,10 @@ export default class DevToolsHookService implements Services.ServiceInstance { Error.stackTraceLimit = 20 const stack = parse(new Error('')).reverse() const source = stack.find((frame) => isUserSpecFile(frame.getFileName())) - // #assertionDepth > 0 → we believe we're inside an expect matcher; its - // internal commands trace back to the user's expect() line but must not - // become their own rows (only the expect.<matcher> assertion should). - // expect-webdriverio doesn't wrap afterAssertion in try/finally, though, so - // a matcher whose internal command hard-throws skips it and leaves the depth - // stuck ≥1 — which would then suppress every later user command. When a - // top-level user command arrives with the depth stuck but no live - // expect-webdriverio frame on the stack, the matcher already ended: self-heal - // the leftover depth + parallel stacks so this command captures normally. - if (source && this.#commandStack.length === 0 && this.#assertionDepth > 0) { - const inMatcher = stack.some((frame) => - frame.getFileName()?.includes('expect-webdriverio') - ) - if (!inMatcher) { - this.#assertionDepth = 0 - this.#assertionStartTimes = [] - this.#assertionCallSources = [] - } - } + const inMatcher = + this.#assertionDepth > 0 && + stack.some((frame) => frame.getFileName()?.includes('expect-webdriverio')) + this.#trackAssertionWindow(inMatcher, Boolean(source)) if ( source && this.#commandStack.length === 0 && diff --git a/packages/service/tests/index.test.ts b/packages/service/tests/index.test.ts index d46921e0..459ba326 100644 --- a/packages/service/tests/index.test.ts +++ b/packages/service/tests/index.test.ts @@ -2,14 +2,26 @@ import { describe, it, expect, vi, beforeEach } from 'vitest' import type * as DevtoolsCore from '@wdio/devtools-core' import DevToolsHookService from '../src/index.js' -const fakeFrame = { - getFileName: () => '/test/specs/fake.spec.ts', - getLineNumber: () => 1, - getColumnNumber: () => 1 -} +// Controllable stack: `frames` defaults to a single user-spec frame (commands +// read as top-level). A test can splice in `matcherFrame` to simulate a command +// issued from inside an expect-webdriverio matcher. +const stackMock = vi.hoisted(() => { + const userFrame = { + getFileName: () => '/test/specs/fake.spec.ts', + getLineNumber: () => 1, + getColumnNumber: () => 1 + } + const matcherFrame = { + getFileName: () => + '/node_modules/expect-webdriverio/lib/matchers/toHaveText.js', + getLineNumber: () => 1, + getColumnNumber: () => 1 + } + return { frames: [userFrame], userFrame, matcherFrame } +}) // Create mock instance that will be returned by SessionCapturer constructor vi.mock('stack-trace', () => ({ - parse: () => [fakeFrame] + parse: () => stackMock.frames })) const mockSessionCapturerInstance = { afterCommand: vi.fn(), @@ -303,6 +315,7 @@ describe('DevtoolsService - assertion suppression self-heal', () => { beforeEach(async () => { vi.clearAllMocks() + stackMock.frames = [stackMock.userFrame] service = new DevToolsHookService() await service.before({} as any, [], mockBrowser) vi.clearAllMocks() @@ -312,18 +325,33 @@ describe('DevtoolsService - assertion suppression self-heal', () => { // whose internal command hard-throws runs beforeAssertion but skips // afterAssertion, leaving #assertionDepth stuck ≥ 1. Without the self-heal // that stuck depth suppresses every later user command in the test. - it('captures a top-level command after a matcher throw left the assertion depth stuck', async () => { - service.beforeAssertion() - service.beforeAssertion() // depth now 2, no matching afterAssertion - - // The mocked stack is a user-spec frame with no expect-webdriverio frame, - // so the command is recognised as top-level and the leftover depth resets. + it('captures a top-level command after a matcher that started then threw left the assertion depth stuck', async () => { + service.beforeAssertion() // depth 1 + // The matcher issues an internal command (stack shows an expect-webdriverio + // frame) → marks the matcher started, then hard-throws (afterAssertion + // skipped), leaving the depth stuck. + stackMock.frames = [stackMock.userFrame, stackMock.matcherFrame] + service.beforeCommand('getText' as any, ['#el']) + // A later top-level user command with no matcher frame self-heals the depth. + stackMock.frames = [stackMock.userFrame] service.beforeCommand('click' as any, ['.button']) await service.afterCommand('click' as any, ['.button'], undefined) expect(capturedCommands()).toEqual(['click']) }) + it('keeps suppressing the matcher’s own leading command (no premature self-heal)', async () => { + service.beforeAssertion() // depth 1 + // The matcher's FIRST internal command is an element re-lookup whose sync + // stack has no expect-webdriverio frame yet. It must NOT reset the depth — + // otherwise the subsequent matcher commands would surface as duplicate rows. + stackMock.frames = [stackMock.userFrame] + service.beforeCommand('$' as any, ['#el']) + await service.afterCommand('$' as any, ['#el'], undefined) + + expect(capturedCommands()).toEqual([]) + }) + it('a fresh beforeAssertion resets a stuck depth so the window stays balanced', async () => { service.beforeAssertion() service.beforeAssertion() // leftover depth from a prior throw From 26e79a2b9b1af673c330b375d1d5501a05c85a79 Mon Sep 17 00:00:00 2001 From: Vishnu Vardhan <vishnu.p@browserstack.com> Date: Mon, 13 Jul 2026 18:41:16 +0530 Subject: [PATCH 44/68] fix(app): render assertions consistently across frameworks --- .../app/src/components/browser/snapshot.ts | 22 ++++++- .../workbench/actionItems/category.ts | 9 ++- .../workbench/actionItems/command.ts | 25 +++++++- .../components/workbench/compare/markers.ts | 8 +++ .../workbench/compare/renderDetailBlock.ts | 60 +++++++++++++++---- .../app/src/components/workbench/errors.ts | 19 ++++-- .../components/workbench/errors/collect.ts | 22 ++++++- 7 files changed, 138 insertions(+), 27 deletions(-) diff --git a/packages/app/src/components/browser/snapshot.ts b/packages/app/src/components/browser/snapshot.ts index ce9ae709..9fe5f7b4 100644 --- a/packages/app/src/components/browser/snapshot.ts +++ b/packages/app/src/components/browser/snapshot.ts @@ -249,7 +249,7 @@ export class DevtoolsBrowser extends Element { } async #renderCommandScreenshot(command?: CommandLog) { - this.#screenshotData = command?.screenshot ?? null + this.#screenshotData = this.#screenshotForCommand(command) // Follow the selected command's page in the address bar — commands carry no // URL, so resolve it from the navigation active at the command's time. if (command) { @@ -466,6 +466,26 @@ export class DevtoolsBrowser extends Element { this.requestUpdate() } + /** Screenshot for the selected command. Assertions (and other snapshot-less + * commands) carry none, so fall back to the nearest PRECEDING command's frame + * — the page state the assertion observed — instead of a blank preview. */ + #screenshotForCommand(command?: CommandLog): string | null { + if (!command) { + return null + } + if (command.screenshot) { + return command.screenshot + } + const cmds = this.commands ?? [] + const idx = cmds.indexOf(command) + for (let i = (idx === -1 ? cmds.length : idx) - 1; i >= 0; i--) { + if (cmds[i].screenshot) { + return cmds[i].screenshot! + } + } + return null + } + /** Latest screenshot from any command — auto-updates the preview as tests run. */ get #latestAutoScreenshot(): string | null { if (!this.commands?.length) { diff --git a/packages/app/src/components/workbench/actionItems/category.ts b/packages/app/src/components/workbench/actionItems/category.ts index e40861a8..28b25db9 100644 --- a/packages/app/src/components/workbench/actionItems/category.ts +++ b/packages/app/src/components/workbench/actionItems/category.ts @@ -78,6 +78,11 @@ const QUERY = new Set([ 'findElements' ]) +/** assert.* (node:assert), expect.* (expect-webdriverio), verify.* (Nightwatch) + * — assertion rows across every framework, so they get the check icon + green + * like the WDIO state matchers above instead of the generic fallback. */ +const ASSERTION_PREFIX_RE = /^(?:assert|expect|verify)\./ + /** Group a command by intent so the timeline can colour it consistently. */ export function commandCategory(command: string): ActionCategory { if (NAVIGATION.has(command)) { @@ -86,7 +91,7 @@ export function commandCategory(command: string): ActionCategory { if (INPUT.has(command)) { return 'input' } - if (ASSERTION.has(command)) { + if (ASSERTION.has(command) || ASSERTION_PREFIX_RE.test(command)) { return 'assertion' } if (QUERY.has(command)) { @@ -133,7 +138,7 @@ export function commandIcon(command: string): ActionIcon { if (INPUT.has(command)) { return 'click' } - if (ASSERTION.has(command)) { + if (ASSERTION.has(command) || ASSERTION_PREFIX_RE.test(command)) { return 'assert' } if (QUERY.has(command)) { diff --git a/packages/app/src/components/workbench/actionItems/command.ts b/packages/app/src/components/workbench/actionItems/command.ts index 1a596f56..02631b0d 100644 --- a/packages/app/src/components/workbench/actionItems/command.ts +++ b/packages/app/src/components/workbench/actionItems/command.ts @@ -15,11 +15,20 @@ import '~icons/mdi/target.js' import '~icons/mdi/keyboard-outline.js' import '~icons/mdi/cursor-default-click-outline.js' import '~icons/mdi/check-circle-outline.js' +import '~icons/mdi/close-circle-outline.js' import '~icons/mdi/text.js' import '~icons/mdi/code-tags.js' const SOURCE_COMPONENT = 'wdio-devtools-command-item' +function capitalizeAssertLabel(label: string): string { + return label.replace( + /^(assert|expect|verify)\./, + (_m, prefix: string) => + prefix.charAt(0).toUpperCase() + prefix.slice(1) + '.' + ) +} + const CATEGORY_COLOR: Record<ActionCategory, string> = { navigation: 'text-chartsBlue', input: 'text-chartsPurple', @@ -48,8 +57,18 @@ export class CommandItem extends ActionItem { } #renderIcon(command: string): TemplateResult { - const cls = `${ICON_CLASS} ${CATEGORY_COLOR[commandCategory(command)]}` - switch (commandIcon(command)) { + // Failed commands render red (matching the label); a failed assertion also + // swaps its green ✓-circle for a red ✗-circle so it never reads as passed. + const cls = `${ICON_CLASS} ${ + this.failed ? 'text-chartsRed' : CATEGORY_COLOR[commandCategory(command)] + }` + const icon = commandIcon(command) + if (this.failed && icon === 'assert') { + return html`<icon-mdi-close-circle-outline + class="${cls}" + ></icon-mdi-close-circle-outline>` + } + switch (icon) { case 'navigate': return html`<icon-mdi-arrow-top-right class="${cls}" @@ -93,7 +112,7 @@ export class CommandItem extends ActionItem { class="text-[12.5px] flex-wrap text-left break-all ${this.failed ? 'text-chartsRed' : ''}" - >${entry.title ?? entry.command}</code + >${capitalizeAssertLabel(entry.title ?? entry.command)}</code > ${this.renderTime()} </button> diff --git a/packages/app/src/components/workbench/compare/markers.ts b/packages/app/src/components/workbench/compare/markers.ts index ccec430b..18945b4f 100644 --- a/packages/app/src/components/workbench/compare/markers.ts +++ b/packages/app/src/components/workbench/compare/markers.ts @@ -75,6 +75,14 @@ function renderStatusMarker( >✗ in failed step</span >` } + // A command that itself errored is a failure even when its step state didn't + // resolve — e.g. the rerun/latest side, whose live step state isn't tracked + // the way the baseline's PreservedStep is. Without this it shows a green ✓. + if (cmd.error?.message) { + return html`<span class="marker error" title="Failed: ${cmd.error.message}" + >✗ failed</span + >` + } if (step?.state === 'passed') { return html`<span class="marker ok" diff --git a/packages/app/src/components/workbench/compare/renderDetailBlock.ts b/packages/app/src/components/workbench/compare/renderDetailBlock.ts index 1c4c03ed..81dfcd80 100644 --- a/packages/app/src/components/workbench/compare/renderDetailBlock.ts +++ b/packages/app/src/components/workbench/compare/renderDetailBlock.ts @@ -10,9 +10,35 @@ import type { PreservedAttempt, PreservedStep } from '@wdio/devtools-shared' -import { safeJson } from './compareUtils.js' +import { cleanErrorMessage, safeJson } from './compareUtils.js' import { computeDetailBlockData } from './stepResolution.js' +/** Assertion commands carry `[actual, expected]` in args (mirrors the Errors + * tab). Surfacing those instead of the raw node:assert message keeps the + * detail free of the "+ actual - expected" diff noise + ANSI. */ +const ASSERTION_CMD_RE = /^(?:assert|expect|verify)\./ + +function assertionArgs( + cmd: CommandLog +): { actual: unknown; expected: unknown } | undefined { + if (!ASSERTION_CMD_RE.test(cmd.command)) { + return undefined + } + // Prefer a collapsed { actual, expected } result (Nightwatch native asserts) + // over positional args, which mislabel a single expected-only arg as actual. + const result = cmd.result + if (result && typeof result === 'object') { + const r = result as { actual?: unknown; expected?: unknown } + if ('actual' in r || 'expected' in r) { + return { actual: r.actual, expected: r.expected } + } + } + if ((cmd.args?.length ?? 0) < 2) { + return undefined + } + return { actual: cmd.args![0], expected: cmd.args![1] } +} + /** Hooks the detail-block renderers need to reach component state. */ export interface DetailBlockCtx { baseline: PreservedAttempt | undefined @@ -102,22 +128,32 @@ export function renderDetailBlock( ctx.findStepFor(cmd, side), allCmdsThisSide ) + const assertVals = assertionArgs(cmd) return html` <div class="detail-block"> <h4>${label} · ${cmd.command}</h4> ${renderDetailStepBanner(data.step, data.stepText)} <pre>args: ${data.argsStr}</pre> - ${cmd.error - ? html`<pre style="color:var(--vscode-charts-red,#f48771);"> -error: ${cmd.error.message || String(cmd.error)}</pre - >` - : html`<pre>result: ${data.resultStr}</pre>`} - ${renderExpectedActualAssertion( - data.expected, - data.actual, - data.assertionMessage, - data.fallbackExpected - )} + ${assertVals + ? renderExpectedActualAssertion( + assertVals.expected, + assertVals.actual, + undefined, + undefined + ) + : html` + ${cmd.error + ? html`<pre style="color:var(--vscode-charts-red,#f48771);"> +error: ${cleanErrorMessage(cmd.error.message || String(cmd.error))}</pre + >` + : html`<pre>result: ${data.resultStr}</pre>`} + ${renderExpectedActualAssertion( + data.expected, + data.actual, + data.assertionMessage, + data.fallbackExpected + )} + `} ${cmd.screenshot ? html`<img src="${cmd.screenshot.startsWith('data:') diff --git a/packages/app/src/components/workbench/errors.ts b/packages/app/src/components/workbench/errors.ts index f4470005..ef497850 100644 --- a/packages/app/src/components/workbench/errors.ts +++ b/packages/app/src/components/workbench/errors.ts @@ -16,6 +16,11 @@ function shortSource(callSource: string): string { return callSource.split(/[\\/]/).slice(-3).join('/') } +/** Show assertion values readably: quote strings, stringify everything else. */ +function fmtValue(value: unknown): string { + return typeof value === 'string' ? `'${value}'` : String(value) +} + @customElement(COMPONENT) export class DevtoolsErrors extends Element { @consume({ context: commandContext, subscribe: true }) @@ -155,13 +160,13 @@ export class DevtoolsErrors extends Element { return nothing } return html`<div class="error-diff"> + ${error.actual !== undefined + ? html`<span class="label">Actual</span + ><span class="received">${fmtValue(error.actual)}</span>` + : nothing} ${error.expected !== undefined ? html`<span class="label">Expected</span - ><span class="expected">${error.expected}</span>` - : nothing} - ${error.actual !== undefined - ? html`<span class="label">Received</span - ><span class="received">${error.actual}</span>` + ><span class="expected">${fmtValue(error.expected)}</span>` : nothing} </div>` } @@ -178,7 +183,9 @@ export class DevtoolsErrors extends Element { @${shortSource(error.callSource)} </button>` : nothing} - <div class="error-title">${error.message}</div> + ${error.expected === undefined && error.actual === undefined + ? html`<div class="error-title">${error.message}</div>` + : nothing} ${this.#renderDiff(error)} ${error.stack ? html`<details class="error-stack"> diff --git a/packages/app/src/components/workbench/errors/collect.ts b/packages/app/src/components/workbench/errors/collect.ts index ca7429e1..f28d8065 100644 --- a/packages/app/src/components/workbench/errors/collect.ts +++ b/packages/app/src/components/workbench/errors/collect.ts @@ -50,13 +50,29 @@ function displayValue(value: unknown): string | undefined { } } -/** Assertion commands carry `[actual, expected]` in args (see the exporter's - * Assert params + synthesizeExpectFailure). */ +/** Actual/expected for an assertion row. Prefer a collapsed `{actual, expected}` + * result (Nightwatch native asserts, and any framework that surfaces a + * structured result) over the positional `[actual, expected]` arg convention — + * the latter is wrong for asserts that pass only an expected value + * (`titleContains('x')`), which would mislabel the expected as the actual. */ function assertionValues(command: CommandLog): { actual?: string expected?: string } { - if (!ASSERTION_COMMAND_RE.test(command.command) || command.args.length < 2) { + if (!ASSERTION_COMMAND_RE.test(command.command)) { + return {} + } + const result = command.result + if (result && typeof result === 'object') { + const r = result as { actual?: unknown; expected?: unknown } + if ('actual' in r || 'expected' in r) { + return { + actual: displayValue(r.actual), + expected: displayValue(r.expected) + } + } + } + if (command.args.length < 2) { return {} } return { From 6d066d5d8aaf671e91876f0640d0324b2d9f149e Mon Sep 17 00:00:00 2001 From: Vishnu Vardhan <vishnu.p@browserstack.com> Date: Mon, 13 Jul 2026 18:41:31 +0530 Subject: [PATCH 45/68] fix(backend): escape test names in grep-style rerun filters --- packages/backend/src/runner.ts | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/packages/backend/src/runner.ts b/packages/backend/src/runner.ts index da1104b5..95b3a14f 100644 --- a/packages/backend/src/runner.ts +++ b/packages/backend/src/runner.ts @@ -40,6 +40,14 @@ function stripNameTestNameSlot(template: string): string { return template.slice(0, leftEdge) + template.slice(idx + NAME_SLOT.length) } +// Grep-style rerun filters (mocha --grep, jest/vitest -t, cucumber --name) treat +// the value as a REGEX, so a test name containing (), [], ., etc. matches +// nothing unless escaped. The slot is double-quoted in the template, so the +// added backslashes survive shell parsing and reach the runner as literals. +function escapeRerunName(name: string): string { + return name.replace(/[.*+?^${}()|[\]\\]/g, '\\$&') +} + class TestRunner { #child?: ChildProcess #lastPayload?: RunnerRequestBody @@ -203,7 +211,7 @@ class TestRunner { return `${stripped} ${shellQuote([featureSpec])}` } const name = payload.label || payload.fullTitle || '' - return template.replace(/\{\{testName\}\}/g, name) + return template.replace(/\{\{testName\}\}/g, escapeRerunName(name)) } #parseGenericCommand(command: string): { file: string; args: string[] } { From 918c830e35d1769c95de8e84713b9b4f06cbedea Mon Sep 17 00:00:00 2001 From: Vishnu Vardhan <vishnu.p@browserstack.com> Date: Mon, 13 Jul 2026 18:41:45 +0530 Subject: [PATCH 46/68] fix(selenium): read snapshot state via unpatched driver; forward tracePolicy --- .../selenium-devtools/src/action-snapshot.ts | 30 +++++++++++-------- .../selenium-devtools/src/driverPatcher.ts | 10 +++++++ packages/selenium-devtools/src/index.ts | 5 ++++ packages/selenium-devtools/src/types.ts | 2 ++ 4 files changed, 34 insertions(+), 13 deletions(-) diff --git a/packages/selenium-devtools/src/action-snapshot.ts b/packages/selenium-devtools/src/action-snapshot.ts index 7644ae1d..83a81d3e 100644 --- a/packages/selenium-devtools/src/action-snapshot.ts +++ b/packages/selenium-devtools/src/action-snapshot.ts @@ -1,33 +1,37 @@ // Selenium adapter: wires SeleniumDriverLike into core's captureActionSnapshot. +// URL/title/screenshot/script are read through the UNPATCHED driver originals +// (getDriverOriginals) so the snapshot's own reads don't record as commands. +// getCurrentUrl/getTitle map to page.* actions, so capturing them would make +// every snapshot trigger another snapshot — a feedback loop that bloats the +// trace (observed: thousands of getUrl/getTitle actions in one run). import { captureActionSnapshot as coreCapture } from '@wdio/devtools-core' import type { ActionSnapshot } from '@wdio/devtools-shared' +import { getDriverOriginals } from './driverPatcher.js' import type { SeleniumDriverLike } from './types.js' -interface DriverWithUrl extends SeleniumDriverLike { - getCurrentUrl?: () => Promise<string> - getTitle?: () => Promise<string> -} - export function captureActionSnapshot( driver: SeleniumDriverLike, command: string ): Promise<ActionSnapshot | null> { - const d = driver as DriverWithUrl + const orig = getDriverOriginals() return coreCapture({ command, - runScript: (src) => driver.executeScript(`return (${src})`), + runScript: (src) => + orig.executeScript + ? orig.executeScript(driver, `return (${src})`) + : driver.executeScript(`return (${src})`), takeScreenshot: () => - d.takeScreenshot - ? d.takeScreenshot().catch(() => undefined) + orig.takeScreenshot + ? orig.takeScreenshot(driver).catch(() => undefined) : Promise.resolve(undefined), getUrl: () => - d.getCurrentUrl - ? d.getCurrentUrl().catch(() => undefined) + orig.getCurrentUrl + ? orig.getCurrentUrl(driver).catch(() => undefined) : Promise.resolve(undefined), getTitle: () => - d.getTitle - ? d.getTitle().catch(() => undefined) + orig.getTitle + ? orig.getTitle(driver).catch(() => undefined) : Promise.resolve(undefined) }) } diff --git a/packages/selenium-devtools/src/driverPatcher.ts b/packages/selenium-devtools/src/driverPatcher.ts index d4b4a1c5..d1aab68f 100644 --- a/packages/selenium-devtools/src/driverPatcher.ts +++ b/packages/selenium-devtools/src/driverPatcher.ts @@ -217,6 +217,16 @@ function stashDriverOriginals(driverProto: Patchable): void { originals.manage = (driver) => orig.call(driver) as ReturnType<typeof orig> & object } + const url = driverProto.getCurrentUrl + if (typeof url === 'function') { + const orig = url as (this: unknown) => unknown + originals.getCurrentUrl = (driver) => orig.call(driver) as Promise<string> + } + const title = driverProto.getTitle + if (typeof title === 'function') { + const orig = title as (this: unknown) => unknown + originals.getTitle = (driver) => orig.call(driver) as Promise<string> + } } // Lets onBeforeQuit flush async cleanup before runners that `process.exit()` diff --git a/packages/selenium-devtools/src/index.ts b/packages/selenium-devtools/src/index.ts index 7289cd81..4c4b2406 100644 --- a/packages/selenium-devtools/src/index.ts +++ b/packages/selenium-devtools/src/index.ts @@ -64,6 +64,7 @@ import { type ScreencastOptions, type TraceFormat, type TraceGranularity, + type TraceRetentionPolicy, type SeleniumDriverLike, type TestStats } from './types.js' @@ -277,6 +278,7 @@ class SeleniumDevToolsPlugin { mode?: DevToolsMode traceFormat?: TraceFormat traceGranularity?: TraceGranularity + tracePolicy?: TraceRetentionPolicy } = {} ) { if ('rerunCommand' in opts) { @@ -301,6 +303,9 @@ class SeleniumDevToolsPlugin { if (opts.traceGranularity) { this.#options.traceGranularity = opts.traceGranularity } + if (opts.tracePolicy) { + this.#options.tracePolicy = opts.tracePolicy + } if (opts.screencast) { if (this.#options.mode === 'trace' && opts.screencast.enabled) { log.warn('trace mode: ignoring screencast option (live-mode feature)') diff --git a/packages/selenium-devtools/src/types.ts b/packages/selenium-devtools/src/types.ts index af764d69..e7aceb8f 100644 --- a/packages/selenium-devtools/src/types.ts +++ b/packages/selenium-devtools/src/types.ts @@ -101,6 +101,8 @@ export interface DriverOriginals { ...args: unknown[] ) => Promise<unknown> manage?: (driver: SeleniumDriverLike) => unknown + getCurrentUrl?: (driver: SeleniumDriverLike) => Promise<string> + getTitle?: (driver: SeleniumDriverLike) => Promise<string> } // Unwrapped WebElement methods for internal enrichment paths. From 7da55c01714729e2fb299312d78e6c0ae9c101c8 Mon Sep 17 00:00:00 2001 From: Vishnu Vardhan <vishnu.p@browserstack.com> Date: Mon, 13 Jul 2026 18:41:59 +0530 Subject: [PATCH 47/68] chore(selenium): bump chromedriver to ^150 for Chrome 150 --- packages/selenium-devtools/package.json | 2 +- pnpm-lock.yaml | 22 ++-------------------- 2 files changed, 3 insertions(+), 21 deletions(-) diff --git a/packages/selenium-devtools/package.json b/packages/selenium-devtools/package.json index 24a88ea5..8c049df6 100644 --- a/packages/selenium-devtools/package.json +++ b/packages/selenium-devtools/package.json @@ -65,7 +65,7 @@ "@types/ws": "^8.18.1", "@wdio/devtools-core": "workspace:^", "@wdio/devtools-shared": "workspace:^", - "chromedriver": "^148.0.4", + "chromedriver": "^150.0.0", "jest": "^30.4.2", "mocha": "^11.7.6", "selenium-webdriver": "^4.44.0", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 528cbb11..a1de3557 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -499,8 +499,8 @@ importers: specifier: workspace:^ version: link:../shared chromedriver: - specifier: ^148.0.4 - version: 148.0.4 + specifier: ^150.0.0 + version: 150.0.1 jest: specifier: ^30.4.2 version: 30.4.2(@types/node@25.9.3)(ts-node@10.9.2(@types/node@25.9.3)(typescript@6.0.3)) @@ -3333,11 +3333,6 @@ packages: engines: {node: '>=12.13.0'} hasBin: true - chromedriver@148.0.4: - resolution: {integrity: sha512-3UyptFDG4YF1Pyv3fzn95s1CN4K3zCpHSmE6g+6J4f2u9KxxOYzrwN2GApVyM2z02hlbSqzo9Ajn2hMi7LnvCw==} - engines: {node: '>=22'} - hasBin: true - chromedriver@150.0.1: resolution: {integrity: sha512-vpYskeQFyZGPNMEwc/5fuvfnxzfjljCO1EvwuF0dZGQ8KE0repYQar7eThTlT1IjznwVrirYRgXrRsJts7DRIQ==} engines: {node: '>=22'} @@ -10648,19 +10643,6 @@ snapshots: transitivePeerDependencies: - supports-color - chromedriver@148.0.4: - dependencies: - '@testim/chrome-version': 1.1.4 - adm-zip: 0.5.17 - axios: 1.16.1 - compare-versions: 6.1.1 - proxy-agent: 8.0.1 - proxy-from-env: 2.1.0 - tcp-port-used: 1.0.2 - transitivePeerDependencies: - - debug - - supports-color - chromedriver@150.0.1: dependencies: '@testim/chrome-version': 1.1.4 From b4522a73b951006c8eca0c4dfb0dac056f9de57d Mon Sep 17 00:00:00 2001 From: Vishnu Vardhan <vishnu.p@browserstack.com> Date: Mon, 13 Jul 2026 18:42:17 +0530 Subject: [PATCH 48/68] chore(examples): live/trace config-ladder harnesses + retry scripts --- examples/nightwatch/nightwatch.conf.cjs | 22 +++++---- .../nightwatch/tests/assert-capture-check.js | 18 ------- examples/nightwatch/tests/smoke-test.js | 48 +++++++++++++++++++ examples/selenium/mocha-test/test/example.js | 40 +++++++++++++++- examples/wdio/cucumber/wdio.conf.ts | 2 +- examples/wdio/mocha/wdio.conf.ts | 11 +++-- package.json | 1 + packages/nightwatch-devtools/package.json | 1 + 8 files changed, 110 insertions(+), 33 deletions(-) delete mode 100644 examples/nightwatch/tests/assert-capture-check.js create mode 100644 examples/nightwatch/tests/smoke-test.js diff --git a/examples/nightwatch/nightwatch.conf.cjs b/examples/nightwatch/nightwatch.conf.cjs index 2a969097..3ec31930 100644 --- a/examples/nightwatch/nightwatch.conf.cjs +++ b/examples/nightwatch/nightwatch.conf.cjs @@ -37,16 +37,22 @@ module.exports = { }, 'goog:loggingPrefs': { performance: 'ALL' } }, - // Simple configuration - just call the function to get globals. - // - screencast: polling-mode .webm written to cwd as - // nightwatch-video-<sessionId>.webm. - // - bidi: opt-in WebDriver BiDi capture for console + network. When - // attached, the per-command Chrome perf-log network path is gated - // off to avoid duplicate entries. + // bidi: opt-in WebDriver BiDi capture for console + network. When + // attached, the per-command Chrome perf-log network path is gated off to + // avoid duplicate entries. globals: nightwatchDevtools({ port: 3000, - mode: 'live', - screencast: { enabled: true, pollIntervalMs: 200 }, + // ── Config ladder — change ONLY this block per rung ─────────────── + // 1 live: mode: 'live' + // 2 trace: mode: 'trace' + // 3 per-test: mode: 'trace', traceGranularity: 'test' + // 4 fail: mode: 'trace', traceGranularity: 'test', tracePolicy: 'retain-on-failure' + // 5 retry: mode: 'trace', traceGranularity: 'test', tracePolicy: 'on-first-retry' + // (rung 5 needs retries → run `pnpm demo:nightwatch:retry`) + // NOTE: the BDD describe/it interface fires the plugin's beforeEach once + // per module (no per-`it` hook), so traceGranularity:'test' collapses to + // a single session-scoped slice here. See CLAUDE.md § Known debt. + mode: 'trace', bidi: true }) } diff --git a/examples/nightwatch/tests/assert-capture-check.js b/examples/nightwatch/tests/assert-capture-check.js deleted file mode 100644 index 6f8511a3..00000000 --- a/examples/nightwatch/tests/assert-capture-check.js +++ /dev/null @@ -1,18 +0,0 @@ -// Verification harness for native-assert trace capture (browser.assert / verify). -// Run `pnpm demo:nightwatch` and inspect the dashboard Actions: the PASSING -// asserts must render green and the FAILING ones RED. If a failing assert shows -// green, the classic-chained capture is mis-reporting failures (the known risk -// in nativeAssertCapture — the wrapper sees the enqueue, not the queued result). -describe('Native assert capture check', function () { - it('renders passing and failing native asserts', async function (browser) { - await browser.url('https://example.com').waitForElementVisible('body', 5000) - - // Soft verify.* first — never aborts the test, so all four always run. - browser.verify.titleContains('Example') // PASS → expect green - browser.verify.titleContains('SOFT_FAIL_ME') // FAIL → expect RED - - // Hard assert.* — the classic-chained/queued path under test. - browser.assert.titleContains('Example') // PASS → expect green - browser.assert.titleContains('HARD_FAIL_ME') // FAIL → expect RED - }) -}) diff --git a/examples/nightwatch/tests/smoke-test.js b/examples/nightwatch/tests/smoke-test.js new file mode 100644 index 00000000..3cdca005 --- /dev/null +++ b/examples/nightwatch/tests/smoke-test.js @@ -0,0 +1,48 @@ +/** + * Example + config-sweep harness for @wdio/nightwatch-devtools. + * + * Walk the live/trace ladder by editing ONLY the mode/traceGranularity/ + * tracePolicy block in ../nightwatch.conf.cjs. The suite carries a passing + * pair, an always-failing test (retain-on-failure target), and a flaky + * fail-then-pass test (on-first-retry / attempt-capture target). + * + * Native asserts (browser.assert.*) double as the assertion-capture check: + * the passing ones must render green ✓, the failing one red ✗. + * + * Run from repo root: + * pnpm demo:nightwatch (rungs 1-4) + * pnpm demo:nightwatch:retry (rung 5 — adds --retries 1) + */ + +// Survives Nightwatch's testcase retry so the flaky test fails once, then passes. +let flakyAttempts = 0 + +describe('nightwatch-devtools smoke test', function () { + it('loads example.com and reads the heading', async function (browser) { + await browser.url('https://example.com') + await browser.waitForElementVisible('body', 5000) + browser.assert.titleContains('Example') + }) + + it('navigates and reads the page title', async function (browser) { + await browser.url('https://example.org') + await browser.waitForElementVisible('body', 5000) + browser.assert.titleContains('Example') + }) + + it('fails on a wrong title (retain-on-failure target)', async function (browser) { + await browser.url('https://example.com') + await browser.waitForElementVisible('body', 5000) + browser.assert.titleContains('This Is Not The Title') + }) + + it('flaky: fails the first attempt, then passes (retry target)', async function (browser) { + await browser.url('https://example.com') + await browser.waitForElementVisible('body', 5000) + flakyAttempts += 1 + if (flakyAttempts === 1) { + throw new Error('intentional first-attempt failure — should retry') + } + browser.assert.titleContains('Example') + }) +}) diff --git a/examples/selenium/mocha-test/test/example.js b/examples/selenium/mocha-test/test/example.js index be2a79e4..26360598 100644 --- a/examples/selenium/mocha-test/test/example.js +++ b/examples/selenium/mocha-test/test/example.js @@ -1,5 +1,10 @@ /** - * Smoke test for @wdio/selenium-devtools. + * Example + config-sweep harness for @wdio/selenium-devtools (Mocha runner). + * + * Walk the live/trace ladder by editing ONLY the DevTools.configure({...}) + * block below. The suite carries a passing pair, an always-failing test (for + * retain-on-failure), and a flaky fail-then-pass test with a per-test retry + * (for on-first-retry / attempt capture). * * Run from the package root: pnpm example:mocha */ @@ -8,11 +13,22 @@ import { strict as assert } from 'node:assert' import { Builder, By, until } from 'selenium-webdriver' import { DevTools } from '@wdio/selenium-devtools' +// ── Config ladder — change this block per rung ────────────────────────────── +// 1 live: { mode: 'live' } +// 2 trace: { mode: 'trace' } +// 3 per-test:{ mode: 'trace', traceGranularity: 'test' } +// 4 fail: { mode: 'trace', traceGranularity: 'test', tracePolicy: 'retain-on-failure' } +// 5 retry: { mode: 'trace', traceGranularity: 'test', tracePolicy: 'on-first-retry' } DevTools.configure({ - screencast: { enabled: true, quality: 70, maxWidth: 1280, maxHeight: 720 }, + mode: 'trace', + // traceGranularity: 'test', + // tracePolicy: 'on-first-retry', headless: true }) +// Survives Mocha's in-process retry so the flaky test fails once, then passes. +let flakyAttempts = 0 + describe('selenium-devtools smoke test', function () { let driver @@ -40,4 +56,24 @@ describe('selenium-devtools smoke test', function () { const title = await driver.getTitle() assert.match(title, /Example/i) }) + + it('fails on a wrong heading (retain-on-failure target)', async function () { + await driver.get('https://example.com') + await driver.sleep(1000) + const heading = await driver.wait(until.elementLocated(By.css('h1')), 10000) + const text = await heading.getText() + assert.equal(text, 'This Is Not The Heading') + }) + + it('flaky: fails the first attempt, then passes (retry target)', async function () { + this.retries(1) + await driver.get('https://example.com') + await driver.sleep(1000) + flakyAttempts += 1 + if (flakyAttempts === 1) { + throw new Error('intentional first-attempt failure — should retry') + } + const title = await driver.getTitle() + assert.match(title, /Example/i) + }) }) diff --git a/examples/wdio/cucumber/wdio.conf.ts b/examples/wdio/cucumber/wdio.conf.ts index 4c535acc..e1a82294 100644 --- a/examples/wdio/cucumber/wdio.conf.ts +++ b/examples/wdio/cucumber/wdio.conf.ts @@ -133,7 +133,7 @@ export const config: Options.Testrunner = { [ 'devtools', { - mode: 'live' as const, + mode: 'trace' as const, screencast: { enabled: true, pollIntervalMs: 200 } } ] diff --git a/examples/wdio/mocha/wdio.conf.ts b/examples/wdio/mocha/wdio.conf.ts index 966226f9..c7680f7d 100644 --- a/examples/wdio/mocha/wdio.conf.ts +++ b/examples/wdio/mocha/wdio.conf.ts @@ -39,10 +39,13 @@ export const config: Options.Testrunner = { [ 'devtools', { - mode: 'live' as const, - traceGranularity: 'spec' as const - // tracePolicy: 'retain-on-failure' as const - // screencast: { enabled: true, pollIntervalMs: 200 } + // ── Config ladder — change ONLY this block per rung ────────────── + // 1 live: mode: 'live' + // 2 trace: mode: 'trace' + // 3 per-test: mode: 'trace', traceGranularity: 'test' + // 4 fail: mode: 'trace', traceGranularity: 'test', tracePolicy: 'retain-on-failure' + // 5 retry: use `pnpm demo:wdio:retry` (adds retries:1 + on-first-retry) + mode: 'live' as const } ] ], diff --git a/package.json b/package.json index 45fba2f4..5c25c3e6 100644 --- a/package.json +++ b/package.json @@ -7,6 +7,7 @@ "demo:wdio:mocha": "wdio run ./examples/wdio/mocha/wdio.conf.ts", "demo:wdio:retry": "wdio run ./examples/wdio/mocha/wdio.retry.conf.ts", "demo:nightwatch": "pnpm --filter @wdio/nightwatch-devtools example", + "demo:nightwatch:retry": "pnpm --filter @wdio/nightwatch-devtools example:retry", "demo:selenium": "pnpm --filter @wdio/selenium-devtools example", "dev": "pnpm --parallel dev", "preview": "pnpm --parallel preview", diff --git a/packages/nightwatch-devtools/package.json b/packages/nightwatch-devtools/package.json index 60f418ca..34986e02 100644 --- a/packages/nightwatch-devtools/package.json +++ b/packages/nightwatch-devtools/package.json @@ -34,6 +34,7 @@ "clean": "rm -rf dist", "lint": "eslint .", "example": "nightwatch -c ../../examples/nightwatch/nightwatch.conf.cjs", + "example:retry": "nightwatch -c ../../examples/nightwatch/nightwatch.conf.cjs --retries 1", "prepublishOnly": "pnpm build" }, "keywords": [ From 47aa59eec37b9a8b6f51e71e684ca098eca5aa53 Mon Sep 17 00:00:00 2001 From: Vishnu Vardhan <vishnu.p@browserstack.com> Date: Mon, 13 Jul 2026 18:42:32 +0530 Subject: [PATCH 49/68] docs: record Nightwatch BDD per-test and WDIO assert-capture debt --- CLAUDE.md | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/CLAUDE.md b/CLAUDE.md index 14fff1a0..1f33103e 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -238,8 +238,11 @@ Documented divergences from the conventions above. They exist today as debt to b - Retry-aware trace policies aren't uniform across adapters. Selenium emits one metadata entry per attempt (counter-based `generateStableUid` uid + retry-stable tracker key), so all six policies — including `retain-on-first-failure` (needs the first attempt's outcome) — evaluate correctly. Service and Nightwatch key metadata on the retry-stable uid and overwrite/mutate per attempt, collapsing to the *final* attempt's outcome; the retry-count policies (`retain-on-failure`, `on-first-retry`, `on-all-retries`, `retain-on-failure-and-retries`) are still correct, but `retain-on-first-failure` can't see a failed-then-passed first attempt and under-retains. Proper fix rides with B5 (per-test granularity preserves per-attempt slices); until then all three set `attemptInfoAvailable: true`. - Nightwatch negated native asserts (`browser.assert.not.*` / `verify.not.*`) produce no trace row: the `not` namespace is a nested Proxy that `wrapAssertionNamespaces` passes through untouched, and the internal command it issues is dropped by the `hasUserSource` gate in `browserProxy`. Positive `assert.*`/`verify.*` still record. - Eager per-test trace slice (Nightwatch + Selenium) can drop an action snapshot whose fire-and-forget capture hasn't resolved by `afterEach` / scenario end — the slice is written from whatever snapshots exist at flush time. The WDIO service is immune because it awaits each snapshot inline before flushing. -- Nightwatch native asserts populate all at once in live mode and finalize pass/fail in one test-end batch: Nightwatch exposes no per-assertion execution/outcome hook reachable from the plugin (`client.queue.tree` / `client.reporter` aren't on `browser`), so call-time capture streams neutral pending rows and `afterEach` reconciles outcomes. Trace-timeline positions are corrected from `results.commands`. -- Service assertion-suppression self-heal is command-triggered: a swallowed soft-assert (`expect.soft` / user `try/catch`) whose matcher hard-throws leaves `#assertionDepth` stuck until the next top-level user command's stack shows no `expect-webdriverio` frame (heal) or the next `beforeAssertion` (reset). A stuck depth with no following command or assertion before test end persists until the next test's `resetStack()` — benign (no later command in that test to suppress). +- Nightwatch native asserts are buffered at call time and emitted in one batch at test-end (Nightwatch exposes no per-assertion execution/outcome hook reachable from the plugin — `client.queue.tree` / `client.reporter` aren't on `browser`). Streaming mid-run would flash every assert green before its outcome is known, so rows are held and reconciled against `results.testcases[*].assertions` at `afterEach` (the flat `results.assertions` only reflects the last testcase). The exporter re-sorts commands by timestamp (`buildActionEvents`) so the batched rows land at their real timeline positions rather than clustering at flush time. `actual` is parsed from the failure message (`but got: …`, failures only) into a collapsed `{passed, expected, actual}` result; `expected` is the assertion arg — the label still mirrors the call args, not the derived values. +- Nightwatch's BDD `describe/it` interface fires the plugin's global `beforeEach`/`afterEach` **once per module** (with an empty `currentTest.name`), not per `it` — Nightwatch runs the individual `it`s internally with no per-testcase hook or event reachable from the plugin (the lib emits only transport-level events). So `traceGranularity:'test'` records a single slice boundary (the first test) and collapses to one session-scoped slice; retry-aware/per-test retention (`tracePolicy`) degrades to session scope for this interface. Per-test slicing works for adapters/styles that expose per-test hooks (WDIO mocha/cucumber, Selenium mocha; likely Nightwatch's exports-object style, unverified). Session-granularity trace is unaffected. +- Service assertion-suppression self-heal is command-triggered and gated on `#matcherStarted`: a swallowed soft-assert (`expect.soft` / user `try/catch`) whose matcher hard-throws leaves `#assertionDepth` stuck; the next top-level user command with no `expect-webdriverio` frame heals it — but only once the matcher has issued at least one internal (ewdio-framed) command, because the matcher's own FIRST command (an element re-lookup) also lacks a matcher frame on its sync stack and would otherwise reset the depth mid-run (duplicating every later matcher command). A stuck depth with no following command before test end persists until the next `beforeAssertion`/`resetStack()` — benign. +- Service captures the assertion **target's** element resolution as its own row. `expect(page.el).toHaveText(x)` evaluates `page.el` → `$('#sel')`/`$$` as the ARGUMENT to `expect()`, before `beforeAssertion` fires, so `#assertionDepth` is 0 and the `$`/`$$`/`isExisting` lands as a top-level command alongside the `expect.*` row. The in-window suppression can't reach it (it runs pre-window). Proper fix: coalesce a trailing element-query into the imminent assertion, or detect query-commands whose only consumer is the next matcher. +- Service `expect.*` rows can resolve `callSource` to the service bundle (`dist/index.js`) instead of the user's `expect()` line. `#assertionCallSources` captures the spec frame in `beforeAssertion`, but the matcher runs async, so in some cases the parallel stack doesn't hold a user frame and the fallback resolves into the service dist. The failure's own error stack still points at the spec. ### File-size (raw line counts; soft cap is 500 logic lines) From 83345d23ac03e3853e01665dd72074033f869187 Mon Sep 17 00:00:00 2001 From: Vishnu Vardhan <vishnu.p@browserstack.com> Date: Mon, 13 Jul 2026 19:28:07 +0530 Subject: [PATCH 50/68] refactor: dedupe assert-result type into shared; drop unused trace defaults --- packages/core/src/trace-action-events.ts | 18 +++++------ .../src/helpers/nativeAssertions.ts | 9 +----- packages/nightwatch-devtools/src/types.ts | 1 + packages/shared/src/types.ts | 30 ++++++++----------- 4 files changed, 23 insertions(+), 35 deletions(-) diff --git a/packages/core/src/trace-action-events.ts b/packages/core/src/trace-action-events.ts index 29eeb0b2..114233d7 100644 --- a/packages/core/src/trace-action-events.ts +++ b/packages/core/src/trace-action-events.ts @@ -1,7 +1,11 @@ // Builds the before/after action events of the exported trace stream, // including tracingGroup test boundaries and frame-snapshot ref stamping. -import type { CommandLog, TestMetadataMap } from '@wdio/devtools-shared' +import type { + CollapsedAssertResult, + CommandLog, + TestMetadataMap +} from '@wdio/devtools-shared' import { ASSERT_ACTION_CLASS, formatActionTitle, @@ -75,15 +79,9 @@ interface ActionStream { groupCallId?: string } -// Nightwatch built-in assertions collapse {passed, actual, expected, message} -// into the command result on failure — surface those over positional args. -interface CollapsedAssertResult { - passed?: unknown - actual?: unknown - expected?: unknown - message?: unknown -} - +// An adapter may attach a normalized CollapsedAssertResult (see shared) to an +// assertion command — prefer its actual/expected over the positional args, +// which are only correct for node:assert-style `[actual, expected]` calls. function collapsedAssertResult( result: unknown ): CollapsedAssertResult | undefined { diff --git a/packages/nightwatch-devtools/src/helpers/nativeAssertions.ts b/packages/nightwatch-devtools/src/helpers/nativeAssertions.ts index 1134047d..b86beba2 100644 --- a/packages/nightwatch-devtools/src/helpers/nativeAssertions.ts +++ b/packages/nightwatch-devtools/src/helpers/nativeAssertions.ts @@ -26,6 +26,7 @@ import { } from '@wdio/devtools-core' import type { SessionCapturer } from '../session.js' import type { + CollapsedAssertResult, CommandLog, NativeAssertCall, NightwatchBrowser, @@ -242,14 +243,6 @@ export function pendingAssertionCommand( return entry } -/** Collapsed pass/fail result core's `collapsedAssertResult` reads. */ -interface CollapsedAssertResult { - passed: boolean - expected?: unknown - actual?: unknown - message?: string -} - /** Nightwatch failure messages end with `… but got: "<actual>"`. Pull out the * real observed value: Nightwatch passes only the EXPECTED as an arg, so the * actual lives in the message (and only on failure). Undefined when absent. diff --git a/packages/nightwatch-devtools/src/types.ts b/packages/nightwatch-devtools/src/types.ts index 203dbbdb..d4c3f01e 100644 --- a/packages/nightwatch-devtools/src/types.ts +++ b/packages/nightwatch-devtools/src/types.ts @@ -3,6 +3,7 @@ export { TraceType, type ActionSnapshot, + type CollapsedAssertResult, type CommandLog, type ConsoleLog, type DevToolsMode, diff --git a/packages/shared/src/types.ts b/packages/shared/src/types.ts index f90db146..1e2c7033 100644 --- a/packages/shared/src/types.ts +++ b/packages/shared/src/types.ts @@ -49,10 +49,6 @@ export type TraceRetentionPolicy = | 'on-all-retries' | 'retain-on-failure-and-retries' -/** Video retention — video has no separate mode switch, so `off` is valid - * (and the default). */ -export type TraceVideoPolicy = 'off' | TraceRetentionPolicy - /** One node in a test's ancestor chain, outermost first. */ export interface TestAncestor { uid: string @@ -73,19 +69,19 @@ export interface TestMetadataEntry { * title + specFile for Tracing.tracingGroup events in trace output. */ export type TestMetadataMap = Map<string, TestMetadataEntry> -/** Defaults for trace-mode options when not specified by the user. */ -export const TRACE_DEFAULTS = { - mode: 'live', - traceFormat: 'zip', - traceGranularity: 'session', - tracePolicy: 'on', - video: 'off' -} as const satisfies { - mode: DevToolsMode - traceFormat: TraceFormat - traceGranularity: TraceGranularity - tracePolicy: TraceRetentionPolicy - video: TraceVideoPolicy +/** + * Normalized assertion result an adapter may attach to `CommandLog.result` for + * an assertion command. The trace exporter's assert-param builder prefers this + * over the positional `[actual, expected]` arg convention — correct for + * frameworks whose asserts pass only an expected value (a matcher like + * `titleContains('x')`), where args[0] is the expected, not the actual. + * Cross-package contract: adapters produce it, core's exporter consumes it. + */ +export interface CollapsedAssertResult { + passed: boolean + actual?: unknown + expected?: unknown + message?: string } /** From 42a6d886322eec2b2a6d56e7ab7b58099450a8cb Mon Sep 17 00:00:00 2001 From: Vishnu Vardhan <vishnu.p@browserstack.com> Date: Mon, 13 Jul 2026 19:28:31 +0530 Subject: [PATCH 51/68] refactor(core): one findFlushableRange for the three adapters' slice flush --- packages/core/src/spec-trace-helpers.ts | 24 ++++++++++++++ .../core/tests/spec-trace-helpers.test.ts | 32 +++++++++++++++++++ .../nightwatch-devtools/src/trace-slices.ts | 3 +- .../src/session-lifecycle.ts | 3 +- packages/service/src/trace-slices.ts | 18 ++--------- .../service/tests/trace-granularity.test.ts | 29 ----------------- 6 files changed, 62 insertions(+), 47 deletions(-) diff --git a/packages/core/src/spec-trace-helpers.ts b/packages/core/src/spec-trace-helpers.ts index 2eb69298..6de04bf8 100644 --- a/packages/core/src/spec-trace-helpers.ts +++ b/packages/core/src/spec-trace-helpers.ts @@ -36,6 +36,30 @@ export interface SpecRange { snapshotCount: number } +/** + * The slice range to eager-flush for a just-ended test. When the caller knows + * the test's uid (e.g. WDIO's `afterTest`), reverse-scan for it — retries push + * a new range under the same uid, and the next test's boundary may already be + * recorded, so the last range isn't reliably this test's. When the uid isn't + * independently known (Nightwatch/Selenium discover it from the range itself), + * the last recorded range is the just-ended test's. Undefined when there is no + * range to flush. One helper for what the three adapters each open-coded. + */ +export function findFlushableRange( + ranges: readonly SpecRange[], + testUid?: string +): SpecRange | undefined { + if (testUid !== undefined) { + for (let i = ranges.length - 1; i >= 0; i--) { + if (ranges[i]!.testUid === testUid) { + return ranges[i] + } + } + return undefined + } + return ranges[ranges.length - 1] +} + // ─── Spec name sanitization ─────────────────────────────────────────────────── /** diff --git a/packages/core/tests/spec-trace-helpers.test.ts b/packages/core/tests/spec-trace-helpers.test.ts index 95257afe..27f4f694 100644 --- a/packages/core/tests/spec-trace-helpers.test.ts +++ b/packages/core/tests/spec-trace-helpers.test.ts @@ -8,6 +8,7 @@ import { buildTestSliceFolder, buildTestSliceSessionId, filterTestMetadataBySpec, + findFlushableRange, filterTestMetadataByUid, recordSliceBoundary, recordSpecBoundary, @@ -335,3 +336,34 @@ describe('recordSliceBoundary / recordSpecBoundary (spec granularity)', () => { expect(ctx.specRanges[0]!.key).toBe('/a.js') }) }) + +describe('findFlushableRange', () => { + const mk = (key: string, testUid?: string): SpecRange => ({ + specFile: 'f', + key, + testUid, + commandStartIdx: 0, + consoleStartIdx: 0, + networkStartIdx: 0, + mutationStartIdx: 0, + traceLogStartIdx: 0, + snapshotCount: 0 + }) + + it('reverse-scans for the given testUid (latest retry attempt wins)', () => { + const ranges = [mk('a', 'a'), mk('b', 'b'), mk('b-retry1', 'b')] + expect(findFlushableRange(ranges, 'b')?.key).toBe('b-retry1') + expect(findFlushableRange(ranges, 'a')?.key).toBe('a') + }) + + it('returns undefined when no range matches the testUid', () => { + expect(findFlushableRange([mk('spec.ts', undefined)], 'x')).toBeUndefined() + expect(findFlushableRange([], 'x')).toBeUndefined() + }) + + it('falls back to the last recorded range when no testUid is given', () => { + const ranges = [mk('a', 'a'), mk('b', 'b')] + expect(findFlushableRange(ranges)?.key).toBe('b') + expect(findFlushableRange([])).toBeUndefined() + }) +}) diff --git a/packages/nightwatch-devtools/src/trace-slices.ts b/packages/nightwatch-devtools/src/trace-slices.ts index bdd0cf0d..c69f8fe7 100644 --- a/packages/nightwatch-devtools/src/trace-slices.ts +++ b/packages/nightwatch-devtools/src/trace-slices.ts @@ -12,6 +12,7 @@ */ import { + findFlushableRange, recordSliceBoundary, recordSpecBoundary, type SpecBoundaryContext, @@ -93,7 +94,7 @@ export function flushTestSlice(ctx: TestSliceCtx): void { if (!sliceActive(ctx)) { return } - const range = ctx.specRanges[ctx.specRanges.length - 1] + const range = findFlushableRange(ctx.specRanges) if (!range) { return } diff --git a/packages/selenium-devtools/src/session-lifecycle.ts b/packages/selenium-devtools/src/session-lifecycle.ts index 856d05ee..7716210b 100644 --- a/packages/selenium-devtools/src/session-lifecycle.ts +++ b/packages/selenium-devtools/src/session-lifecycle.ts @@ -14,6 +14,7 @@ import { errorMessage, finalizeScreencast, finalizeTraceExport, + findFlushableRange, flushRangeLogged, recordSliceBoundary as coreRecordSliceBoundary, recordSpecBoundary as coreRecordSpecBoundary, @@ -425,7 +426,7 @@ export function flushCurrentTestTrace(ctx: SessionLifecycleCtx): void { return } const sessionId = ctx.sessionCapturer.metadata?.sessionId - const currentRange = ctx.specRanges[ctx.specRanges.length - 1] + const currentRange = findFlushableRange(ctx.specRanges) if (!sessionId || currentRange?.testUid === undefined) { return } diff --git a/packages/service/src/trace-slices.ts b/packages/service/src/trace-slices.ts index fcc36962..5da66806 100644 --- a/packages/service/src/trace-slices.ts +++ b/packages/service/src/trace-slices.ts @@ -3,26 +3,12 @@ // selection and flush I/O are unit-testable and the god-file stays lean. import { + findFlushableRange, flushRangeLogged, type SpecRange, type TraceExportContext } from '@wdio/devtools-core' -/** The range for the test that just ended is the most recent slice recorded - * under this base testUid — retries push a new range under the same testUid, - * so reverse-scanning finds the attempt whose afterTest is now firing. */ -export function findCurrentTestRange( - ranges: readonly SpecRange[], - testUid: string -): SpecRange | undefined { - for (let i = ranges.length - 1; i >= 0; i--) { - if (ranges[i]!.testUid === testUid) { - return ranges[i] - } - } - return undefined -} - /** Fire-and-forget flush of the previous unflushed slice at a boundary change * (spec granularity, or a test slice whose eager flush was missed). Errors are * logged, never thrown, so a failed flush can't abort the next test. */ @@ -41,7 +27,7 @@ export async function flushTestSlice( ranges: readonly SpecRange[], testUid: string ): Promise<void> { - const range = findCurrentTestRange(ranges, testUid) + const range = findFlushableRange(ranges, testUid) if (!range) { return } diff --git a/packages/service/tests/trace-granularity.test.ts b/packages/service/tests/trace-granularity.test.ts index c43b6b66..66a3b971 100644 --- a/packages/service/tests/trace-granularity.test.ts +++ b/packages/service/tests/trace-granularity.test.ts @@ -1,7 +1,6 @@ import { describe, it, expect, vi, beforeEach } from 'vitest' import type * as DevtoolsCore from '@wdio/devtools-core' import { deterministicUid, type SpecRange } from '@wdio/devtools-core' -import { findCurrentTestRange } from '../src/trace-slices.js' // Records the key/state observed at each per-slice flush and replays the real // dedupe (flushed.add) so recordSliceBoundary's prev-slice logic behaves as in @@ -87,34 +86,6 @@ vi.mock('@wdio/devtools-core', async (importOriginal) => { // Imported after the mocks are declared so the mocked core module is used. const { default: DevToolsHookService } = await import('../src/index.js') -describe('findCurrentTestRange', () => { - const mk = (key: string, testUid?: string): SpecRange => ({ - specFile: 'f', - key, - testUid, - commandStartIdx: 0, - consoleStartIdx: 0, - networkStartIdx: 0, - mutationStartIdx: 0, - traceLogStartIdx: 0, - snapshotCount: 0 - }) - - it('returns the most recent range recorded under the base testUid', () => { - const ranges = [mk('a', 'a'), mk('b', 'b'), mk('b-retry1', 'b')] - // A retry pushes a new range under the same testUid; the latest one wins. - expect(findCurrentTestRange(ranges, 'b')?.key).toBe('b-retry1') - expect(findCurrentTestRange(ranges, 'a')?.key).toBe('a') - }) - - it('returns undefined when no range matches (spec/session slices)', () => { - expect( - findCurrentTestRange([mk('spec.ts', undefined)], 'x') - ).toBeUndefined() - expect(findCurrentTestRange([], 'x')).toBeUndefined() - }) -}) - describe('DevtoolsService - trace granularity slicing', () => { const file = '/proj/specs/login.spec.ts' const mockBrowser = { From ac211b2a626bc28df87fd8eb2fa6055426ab2f2a Mon Sep 17 00:00:00 2001 From: Vishnu Vardhan <vishnu.p@browserstack.com> Date: Tue, 14 Jul 2026 02:02:26 +0530 Subject: [PATCH 52/68] refactor(service): rebuild expect-matcher capture around before/afterAssertion --- packages/service/src/assert-capture.ts | 108 +++++---- packages/service/src/index.ts | 222 +++++++++--------- packages/service/src/session.ts | 44 ++++ packages/service/tests/assert-capture.test.ts | 73 ++---- packages/service/tests/assertion-rows.test.ts | 189 ++++++++++----- packages/service/tests/index.test.ts | 78 ------ packages/service/tests/session.test.ts | 106 +++++++++ 7 files changed, 481 insertions(+), 339 deletions(-) diff --git a/packages/service/src/assert-capture.ts b/packages/service/src/assert-capture.ts index 49725ec8..a62b4cff 100644 --- a/packages/service/src/assert-capture.ts +++ b/packages/service/src/assert-capture.ts @@ -9,42 +9,45 @@ import { stripAnsi } from '@wdio/devtools-core' import type { CommandLog, SerializedError } from '@wdio/devtools-shared' -import { parse } from 'stack-trace' -import { - resolveCallSourceFromFrame, - resolveFilePathFromFrame -} from './call-source.js' -import { isUserSpecFile } from './utils.js' import type { SessionCapturer } from './session.js' const log = logger('@wdio/devtools-service:assert-capture') /** - * Capture the user's `expect()` call site from the SYNCHRONOUS stack at matcher - * entry (call from `beforeAssertion`). The matcher then runs async, so this is - * the only point a user frame is still on the stack — reading it in - * `afterAssertion` resolves to the service bundle instead. Mirrors - * `beforeCommand`'s resolver (`parse(new Error()).reverse()` → first user-spec - * frame → `resolveCallSourceFromFrame`) so assertion rows share regular - * commands' Source-tab behaviour. Also loads that file's source via - * `captureSource` so the tab renders. Returns `undefined` when no user frame is - * present (row falls back to no callSource, exactly as before this fix). + * The WDIO element/browser query commands an expect-webdriverio matcher issues + * to read the value it asserts on (`toHaveText`→`getText`, `toExist`→ + * `isExisting`, …). The matcher's read is captured as a normal command; on + * `afterAssertion` the synthesized `expect.*` row is coalesced into it (it + * already carries the correct callSource, screenshot, and timeline position), + * so only one row remains — no timing/stack heuristics. Not exhaustive: an + * unlisted matcher just leaves its read visible plus the assertion row. */ -export function resolveAssertionCallSource( - captureSource: (filePath: string) => void -): string | undefined { - Error.stackTraceLimit = 20 - const frame = parse(new Error('')) - .reverse() - .find((f) => isUserSpecFile(f.getFileName())) - if (!frame) { - return undefined - } - const filePath = resolveFilePathFromFrame(frame) - if (filePath) { - captureSource(filePath) - } - return resolveCallSourceFromFrame(frame) +const MATCHER_READ_COMMANDS = new Set([ + 'getText', + 'getHTML', + 'isExisting', + 'isDisplayed', + 'isDisplayedInViewport', + 'getValue', + 'getAttribute', + 'getProperty', + 'getComputedRole', + 'getComputedLabel', + 'isEnabled', + 'isClickable', + 'isSelected', + 'isFocused', + 'getSize', + 'getLocation', + 'getCSSProperty', + 'getTagName', + 'getUrl', + 'getTitle' +]) + +/** True when a command is a matcher's value-read (see MATCHER_READ_COMMANDS). */ +export function isMatcherReadCommand(command: string): boolean { + return MATCHER_READ_COMMANDS.has(command) } /** @@ -131,33 +134,48 @@ export interface ExpectAssertion { result: { pass?: boolean; result?: boolean; message?: () => string } } +/** `expect.stringContaining(x)` / `objectContaining` etc. are jest asymmetric + * matchers — objects carrying a `sample` payload and an `asymmetricMatch` + * method. Surface the payload so a row reads `toHaveText("x")` instead of + * `toHaveText({"sample":"x"})`; non-matchers pass through unchanged. */ +function unwrapAsymmetricMatcher(value: unknown): unknown { + if ( + value !== null && + typeof value === 'object' && + 'sample' in value && + typeof (value as { asymmetricMatch?: unknown }).asymmetricMatch === + 'function' + ) { + return (value as { sample: unknown }).sample + } + return value +} + /** * Adapt expect-webdriverio's afterAssertion params to the shared matcher * converter. Framework-specific extraction only (matcher name, expectedValue → - * args, the runtime `pass` vs typed `result` flag); the actual CommandLog - * shaping lives once in core's `matcherAssertionToCommandLog`. `callSource` is - * the user's `expect()` call site captured in `beforeAssertion` (the matcher - * runs async, so afterAssertion's own stack no longer holds a user frame) — it - * makes the row's Source tab point at the spec, not the service bundle. + * args, the runtime `pass` vs typed `result` flag); the CommandLog shaping + * lives once in core's `matcherAssertionToCommandLog`. The row's callSource + + * screenshot come from the matcher's read command it's coalesced into (see + * `coalesceAssertionIntoLastRead`), not from a stack walk here. */ export function expectAssertionToCommandLog( params: ExpectAssertion, - testUid: string | undefined, - callSource?: string + testUid: string | undefined ): CommandLog { const { matcherName, expectedValue, result } = params + const rawArgs = + expectedValue === undefined + ? [] + : Array.isArray(expectedValue) + ? expectedValue + : [expectedValue] return matcherAssertionToCommandLog( { method: matcherName, - args: - expectedValue === undefined - ? [] - : Array.isArray(expectedValue) - ? expectedValue - : [expectedValue], + args: rawArgs.map(unwrapAsymmetricMatcher), passed: result.pass ?? result.result ?? false, - message: result.message, - callSource + message: result.message }, testUid ) diff --git a/packages/service/src/index.ts b/packages/service/src/index.ts index 2696d71a..7d9d0419 100644 --- a/packages/service/src/index.ts +++ b/packages/service/src/index.ts @@ -15,7 +15,7 @@ import { import { captureExpectFailure, expectAssertionToCommandLog, - resolveAssertionCallSource, + isMatcherReadCommand, wireAssertCapture, type ExpectAssertion } from './assert-capture.js' @@ -104,35 +104,23 @@ export default class DevToolsHookService implements Services.ServiceInstance { */ #commandStack: CommandFrame[] = [] - /** Depth of the current expect-webdriverio matcher evaluation. While > 0, the - * matcher's internal WebDriver commands (getText/isExisting polling) are - * suppressed so only the `expect.<matcher>` assertion row is captured — - * matching Playwright and the Nightwatch native-assert behaviour. */ - #assertionDepth = 0 - - /** True once the current matcher has issued an internal command whose stack - * shows an expect-webdriverio frame. Gates the stuck-depth self-heal: the - * matcher's FIRST internal command (an element re-lookup) traces to the - * user's expect() line but its sync stack has no matcher frame yet, which - * would otherwise be misread as "matcher ended" and reset the depth mid-run - * — capturing every later matcher command as a duplicate row. */ - #matcherStarted = false - - /** Matcher start timestamps (stack, paired with #assertionDepth) so a captured - * assertion spans [matcher start → end] — its poll duration — instead of - * collapsing to a zero-width point at completion, which left the screencast - * tracking the preceding command during the poll. */ - #assertionStartTimes: number[] = [] - - /** User `expect()` call sites (stack, paired with #assertionStartTimes), - * captured in beforeAssertion while a user frame is still synchronous. The - * matcher runs async, so afterAssertion's own stack resolves to the service - * bundle — this parallel stack lets each row keep its spec-file callSource. */ - #assertionCallSources: (string | undefined)[] = [] - /** Current test UID, set in beforeTest(), used by afterCommand() to tag commands. */ #currentTestUid?: string + /** expect-webdriverio matcher nesting depth. Aliases fire before/afterAssertion + * twice (toBeChecked→toBeSelected), so only the outermost pair owns the row. */ + #assertionDepth = 0 + + /** Matcher armed at beforeAssertion, cleared at its afterAssertion. It survives + * to test end only when the matcher hard-threw (element never resolved, so + * waitUntil rethrew and afterAssertion never fired) — then it's synthesized as + * a failing expect.<matcher> row instead of leaving a raw read. */ + #pendingAssertion?: { + matcherName: string + expectedValue?: unknown + testUid?: string + } + /** Map of testUid → metadata for trace group events and per-spec partitioning. */ #testMetadata: TestMetadataMap = new Map() @@ -400,7 +388,54 @@ export default class DevToolsHookService implements Services.ServiceInstance { _scenario?: unknown, result?: { error?: unknown } ) { - this.#captureExpectFailure(result?.error) + this.#handleAssertionOutcome(result?.error) + } + + /** Route a test/step failure to assertion capture. A matcher that hard-threw + * (element never resolved) left an armed #pendingAssertion because + * afterAssertion never fired — synthesize its failing expect row. Any other + * failure just marks the last action with the error. */ + #handleAssertionOutcome(error: unknown): void { + if (this.#options.captureAssertions === false) { + return + } + if (this.#pendingAssertion) { + this.#finalizePendingAssertion(error) + return + } + this.#captureExpectFailure(error) + } + + /** Synthesize the failing expect.<matcher> row for a hard-thrown matcher: fold + * it into the throwing read (relabel `getText`→`expect.toHaveText`, keeping + * the error) so the assertion renders consistently whether or not the element + * resolved; fall back to a fresh row when there is no read to fold. */ + #finalizePendingAssertion(error: unknown): void { + const pending = this.#pendingAssertion + this.#pendingAssertion = undefined + this.#assertionDepth = 0 + if (!pending) { + return + } + const message = errorMessage(error) || `${pending.matcherName} failed` + const entry = expectAssertionToCommandLog( + { + matcherName: pending.matcherName, + expectedValue: pending.expectedValue, + result: { pass: false, message: () => message } + }, + pending.testUid + ) + if ( + this.#sessionCapturer.coalesceAssertionIntoLastRead( + entry, + isMatcherReadCommand, + true + ) + ) { + return + } + this.#sessionCapturer.captureAssertCommand(entry) } /** Mark the failing action from a matcher error (afterStep for Cucumber, @@ -446,7 +481,7 @@ export default class DevToolsHookService implements Services.ServiceInstance { _context?: unknown, result?: TestOutcomeResult ) { - this.#captureExpectFailure(result?.error) + this.#handleAssertionOutcome(result?.error) const testTitle = test?.fullTitle || test?.title const uid = testTitle ? testMetadataUid(test?.file, testTitle) : undefined if (uid) { @@ -459,52 +494,63 @@ export default class DevToolsHookService implements Services.ServiceInstance { } } - /** expect-webdriverio fires this before each matcher evaluates. The matcher's - * internal polling commands run inside the before→after window and must not - * surface as their own rows — only the `expect.<matcher>` assertion should. */ - beforeAssertion(): void { - // Matchers don't nest, so any residual depth here is a prior matcher whose - // afterAssertion was skipped by a hard throw. Reset before pushing so the - // suppression window can't stay stuck open across assertions. - if (this.#assertionDepth > 0) { - this.#assertionDepth = 0 - this.#assertionStartTimes = [] - this.#assertionCallSources = [] + /** + * expect-webdriverio fires this after each matcher evaluates, with the matcher + * name + pass/fail + expected value. The matcher's value-read (getText / + * isExisting / …) was captured as a normal command; fold this assertion into + * that read so one `expect.<matcher>` row remains — inheriting the read's real + * callSource, screenshot, and timeline position. Deterministic and anchored to + * this reliable hook: no `#assertionDepth`, no stack-frame detection. + */ + /** + * expect-webdriverio fires this at the START of every matcher, before it polls + * — so it fires even for a matcher that later hard-throws (element never + * resolved), unlike afterAssertion. Arm the pending matcher here so test-end + * can synthesize its expect row if afterAssertion never comes. Depth-counted: + * aliases (toBeChecked→toBeSelected) fire this twice, so only the outermost + * arms the row. + */ + beforeAssertion(params: { + matcherName: string + expectedValue?: unknown + }): void { + if (this.#options.captureAssertions === false) { + return + } + if (this.#assertionDepth === 0) { + this.#pendingAssertion = { + matcherName: params.matcherName, + expectedValue: params.expectedValue, + testUid: this.#currentTestUid + } } this.#assertionDepth++ - this.#matcherStarted = false - this.#assertionStartTimes.push(Date.now()) - // Capture the user's expect() call site now — see #assertionCallSources. - this.#assertionCallSources.push( - resolveAssertionCallSource( - (file) => void this.#sessionCapturer.captureSource(file) - ) - ) } async afterAssertion(params: ExpectAssertion): Promise<void> { - // Decrement first (even when capture is off) so the window stays balanced. - const startTime = this.#assertionStartTimes.pop() - const callSource = this.#assertionCallSources.pop() - if (this.#assertionDepth > 0) { - this.#assertionDepth-- - } - this.#matcherStarted = false if (this.#options.captureAssertions === false) { return } - const entry = expectAssertionToCommandLog( - params, - this.#currentTestUid, - callSource - ) - // Span the matcher's poll window so the row's duration is real and the - // screencast tracks the assertion (not the preceding command) during it. - if (startTime !== undefined) { - entry.startTime = startTime + this.#assertionDepth = Math.max(0, this.#assertionDepth - 1) + // Inner matcher of a nested pair (toBeChecked→toBeSelected): the outer + // afterAssertion owns the row. + if (this.#assertionDepth > 0) { + return } - // The suppressed matcher-internal command carried the DOM screenshot; - // capture one here so the assertion row's Snapshot panel isn't blank. + // Reached afterAssertion → the matcher resolved (pass or value-fail), so no + // hard-throw synthesis is needed at test end. + this.#pendingAssertion = undefined + const entry = expectAssertionToCommandLog(params, this.#currentTestUid) + if ( + this.#sessionCapturer.coalesceAssertionIntoLastRead( + entry, + isMatcherReadCommand + ) + ) { + return + } + // No matcher read to fold into (a value matcher like toBe(x), or the read + // hard-threw): emit a fresh row with its own screenshot + trace snapshot. if (this.#browser && !isNativeMobile(this.#browser)) { try { entry.screenshot = await this.#browser.takeScreenshot() @@ -513,9 +559,6 @@ export default class DevToolsHookService implements Services.ServiceInstance { log.debug(`assertion screenshot skipped: ${errorMessage(err)}`) } } - // Trace mode: push a DOM action-snapshot stamped at this row's timestamp so - // the trace player's Snapshot tab renders it, exactly like a regular - // command's post-action snapshot (captureActionResult). if (this.#options.mode === 'trace' && this.#browser) { await pushActionSnapshotAt( this.#browser, @@ -556,9 +599,7 @@ export default class DevToolsHookService implements Services.ServiceInstance { private resetStack() { this.#commandStack = [] this.#assertionDepth = 0 - this.#matcherStarted = false - this.#assertionStartTimes = [] - this.#assertionCallSources = [] + this.#pendingAssertion = undefined this.#sessionCapturer.resetLastSelector() this.#sessionCapturer.resetRetryTracker() } @@ -580,30 +621,6 @@ export default class DevToolsHookService implements Services.ServiceInstance { } } - /** Maintain the expect-matcher suppression window for an incoming command. - * `inMatcher` marks the matcher as started (so its trailing internal - * commands stay suppressed); otherwise self-heal a depth left stuck by a - * matcher that hard-threw (skipping afterAssertion) — but only once the - * matcher has actually started, so its own leading element re-lookup (no - * matcher frame on the sync stack yet) can't reset the depth mid-run. */ - #trackAssertionWindow(inMatcher: boolean, hasSource: boolean): void { - if (inMatcher) { - this.#matcherStarted = true - return - } - if ( - hasSource && - this.#commandStack.length === 0 && - this.#assertionDepth > 0 && - this.#matcherStarted - ) { - this.#assertionDepth = 0 - this.#assertionStartTimes = [] - this.#assertionCallSources = [] - this.#matcherStarted = false - } - } - async beforeCommand(command: string, args: string[]) { if (!this.#browser) { return @@ -624,15 +641,10 @@ export default class DevToolsHookService implements Services.ServiceInstance { Error.stackTraceLimit = 20 const stack = parse(new Error('')).reverse() const source = stack.find((frame) => isUserSpecFile(frame.getFileName())) - const inMatcher = - this.#assertionDepth > 0 && - stack.some((frame) => frame.getFileName()?.includes('expect-webdriverio')) - this.#trackAssertionWindow(inMatcher, Boolean(source)) - if ( - source && - this.#commandStack.length === 0 && - this.#assertionDepth === 0 - ) { + // A matcher's value-read (getText/isExisting) is captured normally like any + // command; afterAssertion later folds it into the expect.<matcher> row (see + // coalesceAssertionIntoLastRead) — no suppression window needed here. + if (source && this.#commandStack.length === 0) { this.#pushTopLevelCommandFrame( command, resolveCallSourceFromFrame(source) diff --git a/packages/service/src/session.ts b/packages/service/src/session.ts index 58a90a24..6e8b2b9a 100644 --- a/packages/service/src/session.ts +++ b/packages/service/src/session.ts @@ -254,6 +254,50 @@ export class SessionCapturer extends SessionCapturerBase { this.#captureOrReplace(entry) } + /** + * Fold an expect-matcher assertion into the matcher's value-read command when + * that read is the most recent captured command (per `isRead`). The read + * already carries the correct callSource, screenshot, and timeline position — + * the DOM the matcher evaluated — so replace it in place with the assertion + * row: one row, no duplicate, and no timing/stack heuristics. WDIO's + * RetryTracker already collapses a matcher's repeated polls to that one read. + * Returns false when the last command isn't a matcher read (a value matcher), + * so the caller emits a fresh assertion row instead. + * + * `foldErrored` folds even when the read carries an error — used by the + * hard-throw path (element never resolved, so afterAssertion never fired and + * the read threw): relabel the throwing read as the failing expect row rather + * than leave a raw `getText`. The normal path keeps the guard so a value + * matcher can't accidentally swallow an unrelated errored command. + */ + coalesceAssertionIntoLastRead( + entry: CommandLog, + isRead: (command: string) => boolean, + foldErrored = false + ): boolean { + const log = this.commandsLog as (CommandLog & { _id?: number })[] + const last = log[log.length - 1] + if (!last || !isRead(last.command) || (last.error && !foldErrored)) { + return false + } + // Inherit the read's `_id` (local dedup bookkeeping) and timestamp, but do + // NOT stamp a public `id`: WDIO replaces by timestamp (like #captureOrReplace), + // and `commandCounter` resets per worker/spec, so a bare `id` collides across + // specs and the app's id-first replaceCommand would swap the wrong row. + const merged: CommandLog & { _id?: number } = { + ...entry, + _id: last._id, + timestamp: last.timestamp, + startTime: last.startTime, + callSource: entry.callSource ?? last.callSource, + screenshot: entry.screenshot ?? last.screenshot, + error: entry.error ?? last.error + } + log[log.length - 1] = merged + this.sendReplaceCommand(last.timestamp, merged) + return true + } + /** * Run the shared Performance API capture script and attach the result to * the given CommandLog entry. Same `CAPTURE_PERFORMANCE_SCRIPT` + diff --git a/packages/service/tests/assert-capture.test.ts b/packages/service/tests/assert-capture.test.ts index 534945e3..e88f3dca 100644 --- a/packages/service/tests/assert-capture.test.ts +++ b/packages/service/tests/assert-capture.test.ts @@ -7,30 +7,11 @@ import { import { captureExpectFailure, expectAssertionToCommandLog, - resolveAssertionCallSource, toCommandError, wireAssertCapture } from '../src/assert-capture.js' import type { SessionCapturer } from '../src/session.js' -const stackFrames = vi.hoisted(() => ({ - value: [] as Array<{ - getFileName: () => string | null - getLineNumber: () => number | null - getColumnNumber: () => number | null - }> -})) - -// Only resolveAssertionCallSource reads 'stack-trace'; node:assert capture uses -// core's stacktrace-parser path, so mocking here doesn't affect those tests. -vi.mock('stack-trace', () => ({ parse: () => stackFrames.value })) - -const frame = (file: string | null, line = 1, column = 1) => ({ - getFileName: () => file, - getLineNumber: () => line, - getColumnNumber: () => column -}) - describe('toCommandError', () => { it('normalizes a plain Error object (ANSI stripped)', () => { // Matcher errors are skipped now (afterAssertion owns them); a plain @@ -178,6 +159,23 @@ describe('expectAssertionToCommandLog', () => { expect(entry.error).toBeUndefined() }) + it('unwraps a jest asymmetric matcher (stringContaining) to its payload', () => { + const entry = expectAssertionToCommandLog( + { + matcherName: 'toHaveText', + // expect.stringContaining('You logged in') shape. + expectedValue: { + sample: 'You logged in', + asymmetricMatch: () => true + }, + result: { pass: true, message: () => 'ok' } + }, + 'uid-2' + ) + // Label reads toHaveText("You logged in"), not toHaveText({"sample":…}). + expect(entry.args).toEqual(['You logged in']) + }) + it('captures a failing matcher with its ANSI-stripped message as the error', () => { const entry = expectAssertionToCommandLog( { @@ -215,41 +213,4 @@ describe('expectAssertionToCommandLog', () => { ) expect(entry).toMatchObject({ command: 'expect.toBeClickable', args: [] }) }) - - it('forwards the captured callSource onto the assertion row', () => { - const entry = expectAssertionToCommandLog( - { matcherName: 'toExist', result: { pass: true } }, - 'uid-1', - '/proj/specs/login.e2e.ts:30:7' - ) - expect(entry.callSource).toBe('/proj/specs/login.e2e.ts:30:7') - }) -}) - -describe('resolveAssertionCallSource', () => { - it('returns the outermost user-spec frame and loads its source', () => { - // innermost → outermost: service bundle, expect-webdriverio matcher, the - // user spec, then node internals. The user spec must win, not the bundle. - stackFrames.value = [ - frame('/repo/packages/service/dist/index.js', 4045, 12), - frame('/repo/node_modules/expect-webdriverio/lib/matchers.js', 9, 3), - frame('/proj/specs/login.e2e.ts', 30, 7), - frame('node:internal/process/task_queues', 95, 5) - ] - const captured: string[] = [] - const callSource = resolveAssertionCallSource((f) => captured.push(f)) - expect(callSource).toBe('/proj/specs/login.e2e.ts:30:7') - expect(callSource).not.toContain('/dist/') - expect(captured).toEqual(['/proj/specs/login.e2e.ts']) - }) - - it('returns undefined and loads nothing when only dependency frames exist', () => { - stackFrames.value = [ - frame('/repo/node_modules/expect-webdriverio/lib/matchers.js', 9, 3), - frame('node:internal/process/task_queues', 95, 5) - ] - const captured: string[] = [] - expect(resolveAssertionCallSource((f) => captured.push(f))).toBeUndefined() - expect(captured).toEqual([]) - }) }) diff --git a/packages/service/tests/assertion-rows.test.ts b/packages/service/tests/assertion-rows.test.ts index 5a5b09ac..ddd216b2 100644 --- a/packages/service/tests/assertion-rows.test.ts +++ b/packages/service/tests/assertion-rows.test.ts @@ -5,18 +5,14 @@ import { TRACKED_ASSERT_METHODS } from '@wdio/devtools-core' -// Controlled synchronous stack for the beforeAssertion call-source walk. -const stackFrames = vi.hoisted(() => ({ - value: [] as Array<{ - getFileName: () => string | null - getLineNumber: () => number | null - getColumnNumber: () => number | null - }> -})) -vi.mock('stack-trace', () => ({ parse: () => stackFrames.value })) - +// Mock the capturer: the service routes each expect matcher through +// coalesceAssertionIntoLastRead (fold into the matcher's read command) or, when +// there's no read to fold into, captureAssertCommand (a fresh row). These tests +// assert that routing; the fold itself is a capturer unit test (session.test). const capturer = vi.hoisted(() => ({ captureAssertCommand: vi.fn(), + coalesceAssertionIntoLastRead: vi.fn(), + failLastAction: vi.fn(), captureSource: vi.fn().mockResolvedValue(undefined), injectScript: vi.fn().mockResolvedValue(undefined), sendUpstream: vi.fn(), @@ -42,13 +38,6 @@ vi.mock('../src/action-snapshot.js', () => ({ })) import DevToolsHookService from '../src/index.js' -import type { ExpectAssertion } from '../src/assert-capture.js' - -const userFrame = { - getFileName: () => '/proj/specs/login.e2e.ts', - getLineNumber: () => 30, - getColumnNumber: () => 7 -} const mockBrowser = { isBidi: true, @@ -76,82 +65,172 @@ afterAll(() => { } }) -const capturedEntry = () => capturer.captureAssertCommand.mock.calls[0]![0] - describe('DevtoolsService — expect.* assertion rows', () => { beforeEach(() => { vi.clearAllMocks() - stackFrames.value = [userFrame] }) - it('trace mode: user-spec callSource, spec source loaded, DOM snapshot pushed', async () => { + it('folds the assertion into the matcher read (no fresh row or snapshot)', async () => { + capturer.coalesceAssertionIntoLastRead.mockReturnValue(true) const service = new DevToolsHookService({ mode: 'trace' }) await service.before({} as never, [], mockBrowser) - service.beforeAssertion() - const params: ExpectAssertion = { - matcherName: 'toExist', - result: { pass: true, message: () => 'ok' } - } - await service.afterAssertion(params) - - const entry = capturedEntry() - expect(entry.command).toBe('expect.toExist') - // The regression: callSource must point at the user's spec, not the - // service bundle (…/service/dist/index.js). - expect(entry.callSource).toBe('/proj/specs/login.e2e.ts:30:7') - expect(entry.callSource).not.toContain('/dist/') - // Source of that file is loaded so the Source tab can render it. - expect(capturer.captureSource).toHaveBeenCalledWith( - '/proj/specs/login.e2e.ts' - ) - // Live-mode screenshot kept for the CommandLog. + await service.afterAssertion({ + matcherName: 'toHaveText', + expectedValue: 'Hi', + result: { pass: false, message: () => 'nope' } + }) + + // Routed through the fold: the matcher's read becomes the expect row (it + // already carries the correct callSource + screenshot + position). + const [entry, isRead] = + capturer.coalesceAssertionIntoLastRead.mock.calls[0]! + expect(entry.command).toBe('expect.toHaveText') + expect(entry.error).toMatchObject({ message: 'nope' }) + expect(isRead('getText')).toBe(true) + expect(isRead('click')).toBe(false) + // No duplicate fresh row, no fresh screenshot/snapshot. + expect(capturer.captureAssertCommand).not.toHaveBeenCalled() + expect(pushActionSnapshotAt).not.toHaveBeenCalled() + }) + + it('trace fallback: fresh row + screenshot + DOM snapshot when there is no read to fold', async () => { + capturer.coalesceAssertionIntoLastRead.mockReturnValue(false) + const service = new DevToolsHookService({ mode: 'trace' }) + await service.before({} as never, [], mockBrowser) + + await service.afterAssertion({ + matcherName: 'toBe', + expectedValue: 1, + result: { pass: true } + }) + + const entry = capturer.captureAssertCommand.mock.calls[0]![0] + expect(entry.command).toBe('expect.toBe') expect(entry.screenshot).toBe('SHOT') - // Trace-player Snapshot tab: a DOM snapshot stamped at the row timestamp. expect(pushActionSnapshotAt).toHaveBeenCalledWith( mockBrowser, - 'expect.toExist', + 'expect.toBe', entry.timestamp, expect.any(Array) ) - // WDIO reconciles rows by timestamp (like every regular WDIO command), - // so assertion rows carry no public id — parity with regular commands. - expect(entry.id).toBeUndefined() }) - it('live mode: keeps the screenshot but pushes no DOM snapshot', async () => { + it('live fallback: keeps the screenshot but pushes no DOM snapshot', async () => { + capturer.coalesceAssertionIntoLastRead.mockReturnValue(false) const service = new DevToolsHookService({ mode: 'live' }) await service.before({} as never, [], mockBrowser) - service.beforeAssertion() await service.afterAssertion({ - matcherName: 'toHaveText', - expectedValue: 'Hi', - result: { pass: false, message: () => 'nope' } + matcherName: 'toBe', + expectedValue: 1, + result: { pass: true } }) - const entry = capturedEntry() - expect(entry.command).toBe('expect.toHaveText') - expect(entry.callSource).toBe('/proj/specs/login.e2e.ts:30:7') - expect(entry.error).toMatchObject({ message: 'nope' }) + const entry = capturer.captureAssertCommand.mock.calls[0]![0] expect(entry.screenshot).toBe('SHOT') expect(pushActionSnapshotAt).not.toHaveBeenCalled() }) - it('captureAssertions: false suppresses the row but keeps the window balanced', async () => { + it('captureAssertions: false emits nothing (no fold, no fresh row)', async () => { const service = new DevToolsHookService({ mode: 'trace', captureAssertions: false }) await service.before({} as never, [], mockBrowser) - service.beforeAssertion() await service.afterAssertion({ matcherName: 'toExist', result: { pass: true } }) + expect(capturer.coalesceAssertionIntoLastRead).not.toHaveBeenCalled() expect(capturer.captureAssertCommand).not.toHaveBeenCalled() expect(pushActionSnapshotAt).not.toHaveBeenCalled() }) + + it('afterAssertion clears the armed matcher so test end does not re-synthesize', async () => { + capturer.coalesceAssertionIntoLastRead.mockReturnValue(true) + const service = new DevToolsHookService({ mode: 'live' }) + await service.before({} as never, [], mockBrowser) + + service.beforeAssertion({ matcherName: 'toHaveText', expectedValue: 'Hi' }) + await service.afterAssertion({ + matcherName: 'toHaveText', + expectedValue: 'Hi', + // a real matcher failure carries matcherResult, so failLastAction skips it + result: { pass: false, message: () => 'nope' } + }) + // afterAssertion fired → pending cleared; test end must not fold again. + await service.afterTest({ file: '/s.ts' } as never, undefined, { + error: Object.assign(new Error('nope'), { + matcherResult: { pass: false } + }) + } as never) + + expect(capturer.coalesceAssertionIntoLastRead).toHaveBeenCalledTimes(1) + expect(capturer.captureAssertCommand).not.toHaveBeenCalled() + }) + + it('hard-throw (no afterAssertion): folds the throwing read into a failing expect row', async () => { + capturer.coalesceAssertionIntoLastRead.mockReturnValue(true) + const service = new DevToolsHookService({ mode: 'live' }) + await service.before({} as never, [], mockBrowser) + + // Matcher armed, then getText hard-threw → afterAssertion never fires. + service.beforeAssertion({ + matcherName: 'toHaveText', + expectedValue: 'Your username is invalid!' + }) + await service.afterTest({ file: '/s.ts' } as never, undefined, { + error: new Error('element ("#flash") still not existing') + } as never) + + const [entry, , foldErrored] = + capturer.coalesceAssertionIntoLastRead.mock.calls[0]! + expect(entry.command).toBe('expect.toHaveText') + expect(entry.args).toEqual(['Your username is invalid!']) + expect(entry.error.message).toContain('still not existing') + expect(foldErrored).toBe(true) // folds even though the read carries an error + expect(capturer.captureAssertCommand).not.toHaveBeenCalled() + }) + + it('hard-throw with no read to fold: emits a fresh failing expect row', async () => { + capturer.coalesceAssertionIntoLastRead.mockReturnValue(false) + const service = new DevToolsHookService({ mode: 'live' }) + await service.before({} as never, [], mockBrowser) + + service.beforeAssertion({ matcherName: 'toBeDisplayed' }) + await service.afterTest({ file: '/s.ts' } as never, undefined, { + error: new Error('element not found') + } as never) + + const entry = capturer.captureAssertCommand.mock.calls[0]![0] + expect(entry.command).toBe('expect.toBeDisplayed') + expect(entry.error.message).toContain('element not found') + }) + + it('nested matcher aliases fold once (toBeChecked→toBeSelected)', async () => { + capturer.coalesceAssertionIntoLastRead.mockReturnValue(true) + const service = new DevToolsHookService({ mode: 'live' }) + await service.before({} as never, [], mockBrowser) + + // toBeChecked delegates to toBeSelected — before/after fire twice, nested. + service.beforeAssertion({ matcherName: 'toBeChecked' }) + service.beforeAssertion({ matcherName: 'toBeSelected' }) + await service.afterAssertion({ + matcherName: 'toBeSelected', + result: { pass: true } + }) + await service.afterAssertion({ + matcherName: 'toBeChecked', + result: { pass: true } + }) + + // Only the outer afterAssertion emits — one row, labelled by the alias. + expect(capturer.coalesceAssertionIntoLastRead).toHaveBeenCalledTimes(1) + expect( + capturer.coalesceAssertionIntoLastRead.mock.calls[0]![0].command + ).toBe('expect.toBeChecked') + }) }) diff --git a/packages/service/tests/index.test.ts b/packages/service/tests/index.test.ts index 459ba326..21617248 100644 --- a/packages/service/tests/index.test.ts +++ b/packages/service/tests/index.test.ts @@ -292,81 +292,3 @@ describe('DevtoolsService - Screencast Integration', () => { expect(mockScreencastRecorder.start).toHaveBeenCalledWith(mockBrowser) }) }) - -describe('DevtoolsService - assertion suppression self-heal', () => { - let service: DevToolsHookService - const mockBrowser = { - isBidi: true, - sessionId: 'heal-session', - scriptAddPreloadScript: vi.fn().mockResolvedValue(undefined), - takeScreenshot: vi.fn().mockResolvedValue('screenshot'), - execute: vi.fn().mockResolvedValue({ - width: 1200, - height: 800, - offsetLeft: 0, - offsetTop: 0 - }), - on: vi.fn(), - emit: vi.fn() - } as any - - const capturedCommands = () => - mockSessionCapturerInstance.afterCommand.mock.calls.map((call) => call[1]) - - beforeEach(async () => { - vi.clearAllMocks() - stackMock.frames = [stackMock.userFrame] - service = new DevToolsHookService() - await service.before({} as any, [], mockBrowser) - vi.clearAllMocks() - }) - - // expect-webdriverio doesn't wrap afterAssertion in try/finally, so a matcher - // whose internal command hard-throws runs beforeAssertion but skips - // afterAssertion, leaving #assertionDepth stuck ≥ 1. Without the self-heal - // that stuck depth suppresses every later user command in the test. - it('captures a top-level command after a matcher that started then threw left the assertion depth stuck', async () => { - service.beforeAssertion() // depth 1 - // The matcher issues an internal command (stack shows an expect-webdriverio - // frame) → marks the matcher started, then hard-throws (afterAssertion - // skipped), leaving the depth stuck. - stackMock.frames = [stackMock.userFrame, stackMock.matcherFrame] - service.beforeCommand('getText' as any, ['#el']) - // A later top-level user command with no matcher frame self-heals the depth. - stackMock.frames = [stackMock.userFrame] - service.beforeCommand('click' as any, ['.button']) - await service.afterCommand('click' as any, ['.button'], undefined) - - expect(capturedCommands()).toEqual(['click']) - }) - - it('keeps suppressing the matcher’s own leading command (no premature self-heal)', async () => { - service.beforeAssertion() // depth 1 - // The matcher's FIRST internal command is an element re-lookup whose sync - // stack has no expect-webdriverio frame yet. It must NOT reset the depth — - // otherwise the subsequent matcher commands would surface as duplicate rows. - stackMock.frames = [stackMock.userFrame] - service.beforeCommand('$' as any, ['#el']) - await service.afterCommand('$' as any, ['#el'], undefined) - - expect(capturedCommands()).toEqual([]) - }) - - it('a fresh beforeAssertion resets a stuck depth so the window stays balanced', async () => { - service.beforeAssertion() - service.beforeAssertion() // leftover depth from a prior throw - - // A new matcher starts (resetting the stuck depth) and completes normally. - service.beforeAssertion() - await service.afterAssertion({ - matcherName: 'toBeDisplayed', - result: { pass: true } - } as any) - - // Depth is balanced again, so the next top-level command is captured. - service.beforeCommand('setValue' as any, ['.input', 'hi']) - await service.afterCommand('setValue' as any, ['.input', 'hi'], undefined) - - expect(capturedCommands()).toEqual(['setValue']) - }) -}) diff --git a/packages/service/tests/session.test.ts b/packages/service/tests/session.test.ts index 43f9853b..1f1185f2 100644 --- a/packages/service/tests/session.test.ts +++ b/packages/service/tests/session.test.ts @@ -769,4 +769,110 @@ describe('SessionCapturer', () => { expect(capturer.commandsLog[0].testUid).toBeUndefined() }) }) + + describe('coalesceAssertionIntoLastRead', () => { + const isRead = (c: string) => c === 'getText' + + it('folds the assertion into the trailing matcher read, in place', () => { + const capturer = new SessionCapturer() + capturer.commandsLog.push({ + command: 'getText', + args: [], + timestamp: 100, + startTime: 90, + callSource: '/spec.ts:13:5', + screenshot: 'READ_SHOT', + _id: 7 + } as never) + + const folded = capturer.coalesceAssertionIntoLastRead( + { + command: 'expect.toHaveText', + args: ['x'], + timestamp: 999, + result: 'passed' + } as never, + isRead + ) + + expect(folded).toBe(true) + expect(capturer.commandsLog).toHaveLength(1) + const row = capturer.commandsLog[0] as Record<string, unknown> + expect(row.command).toBe('expect.toHaveText') // became the assertion + expect(row.callSource).toBe('/spec.ts:13:5') // inherited from the read + expect(row.screenshot).toBe('READ_SHOT') // inherited from the read + expect(row.timestamp).toBe(100) // kept the read's timeline position + expect(row._id).toBe(7) // local dedup bookkeeping preserved + // No public `id`: WDIO replaces by timestamp, and commandCounter resets + // per worker/spec, so a bare id would collide across specs and the app's + // id-first replaceCommand would swap the wrong row. + expect(row.id).toBeUndefined() + }) + + it('returns false and leaves the log untouched when the last command is not a matcher read', () => { + const capturer = new SessionCapturer() + capturer.commandsLog.push({ + command: 'click', + args: [], + timestamp: 100 + } as never) + + const folded = capturer.coalesceAssertionIntoLastRead( + { command: 'expect.toExist', args: [], timestamp: 999 } as never, + isRead + ) + + expect(folded).toBe(false) + expect(capturer.commandsLog).toHaveLength(1) + expect((capturer.commandsLog[0] as Record<string, unknown>).command).toBe( + 'click' + ) + }) + + it('returns false when the trailing read hard-threw (carries an error)', () => { + const capturer = new SessionCapturer() + capturer.commandsLog.push({ + command: 'getText', + args: [], + timestamp: 100, + error: { message: 'element not found' } + } as never) + + expect( + capturer.coalesceAssertionIntoLastRead( + { command: 'expect.toHaveText', args: [], timestamp: 999 } as never, + isRead + ) + ).toBe(false) + }) + + it('foldErrored=true folds a throwing read, keeping its error (hard-throw)', () => { + const capturer = new SessionCapturer() + capturer.commandsLog.push({ + command: 'getText', + args: [], + timestamp: 100, + callSource: '/spec.ts:22:5', + error: { message: 'element not found' }, + _id: 3 + } as never) + + const folded = capturer.coalesceAssertionIntoLastRead( + { command: 'expect.toHaveText', args: ['x'], timestamp: 999 } as never, + isRead, + true + ) + + expect(folded).toBe(true) + expect(capturer.commandsLog).toHaveLength(1) + const row = capturer.commandsLog[0] as Record<string, unknown> + expect(row.command).toBe('expect.toHaveText') // relabelled from the read + expect(row.callSource).toBe('/spec.ts:22:5') // inherited from the read + expect(row.timestamp).toBe(100) // kept the read's timeline position + expect((row.error as { message: string }).message).toBe( + 'element not found' + ) // the throw's error carries through + expect(row.id).toBeUndefined() // still no cross-spec-colliding public id + }) + }) }) From 1cf0bcc7c7d085d48b5cb33f798b34df7c22d846 Mon Sep 17 00:00:00 2001 From: Vishnu Vardhan <vishnu.p@browserstack.com> Date: Tue, 14 Jul 2026 02:02:42 +0530 Subject: [PATCH 53/68] fix(service): exclude the devtools bundle from user-frame detection --- packages/service/src/utils.ts | 33 +++++++++++++++++++++++++--- packages/service/tests/utils.test.ts | 16 ++++++++++++++ 2 files changed, 46 insertions(+), 3 deletions(-) diff --git a/packages/service/src/utils.ts b/packages/service/src/utils.ts index dc58e737..ebb7c94a 100644 --- a/packages/service/src/utils.ts +++ b/packages/service/src/utils.ts @@ -2,6 +2,9 @@ // (AST parsing, source mapping, cucumber step-def lookup) lives in // utils/source-mapping.ts and utils/step-defs.ts. +import path from 'node:path' +import { fileURLToPath } from 'node:url' + export { setCurrentSpecFile, findTestLocations, @@ -11,8 +14,17 @@ export { } from './utils/source-mapping.js' export { findStepDefinitionLocation } from './utils/step-defs.js' -/** A spec file owned by the user — excludes node-builtins and node_modules, - * but keeps WDIO's expect helpers (callers may want to step into those). */ +/** The service's own bundle directory. Stack frames from here are the service's + * instrumentation, not user code — a normal install has the service under + * node_modules (already excluded below), but a monorepo/linked setup puts the + * built service outside node_modules, so exclude it explicitly. */ +const SELF_DIR = path + .dirname(fileURLToPath(import.meta.url)) + .replace(/\\/g, '/') + +/** A spec file owned by the user — excludes node-builtins, node_modules, and + * the service's own bundle, but keeps WDIO's expect helpers (callers may want + * to step into those). */ export function isUserSpecFile(file?: string | null): boolean { if (!file) { return false @@ -20,10 +32,25 @@ export function isUserSpecFile(file?: string | null): boolean { if (file.startsWith('node:')) { return false } - const normalized = file.replace(/\\/g, '/') + // ESM stack frames report a file:// URL; SELF_DIR is a plain path, so decode + // to a plain path first or the self-bundle exclusion below silently no-ops. + // Use fileURLToPath (not `new URL().pathname`) so the Windows drive-letter is + // normalized the same way SELF_DIR is — otherwise `/C:/…` vs `C:/…` mismatch. + let normalized = file + if (normalized.startsWith('file://')) { + try { + normalized = fileURLToPath(normalized) + } catch { + /* keep the raw value */ + } + } + normalized = normalized.replace(/\\/g, '/') if (normalized.includes('/@wdio/expect-webdriverio/')) { return true } + if (normalized.startsWith(SELF_DIR)) { + return false + } return !normalized.includes('/node_modules/') } diff --git a/packages/service/tests/utils.test.ts b/packages/service/tests/utils.test.ts index bb9ffb76..2583e2cb 100644 --- a/packages/service/tests/utils.test.ts +++ b/packages/service/tests/utils.test.ts @@ -1,4 +1,5 @@ import { describe, it, expect, vi, beforeEach } from 'vitest' +import { fileURLToPath } from 'node:url' import { getBrowserObject, isUserSpecFile, @@ -82,5 +83,20 @@ describe('service utils', () => { ).toBe(false) expect(isUserSpecFile('C:\\proj\\test\\login.spec.ts')).toBe(true) }) + + it('decodes file:// URLs (incl. percent-encoding) before matching', () => { + expect(isUserSpecFile('file:///proj/test/login%20spec.ts')).toBe(true) + expect(isUserSpecFile('file:///proj/node_modules/lib/index.js')).toBe( + false + ) + }) + + it("excludes the service's own bundle dir, plain and as a file:// frame", () => { + // SELF_DIR is the dir this module resolves from; at test time that's + // packages/service/src. A frame from there is instrumentation, not a spec. + const selfDir = fileURLToPath(new URL('../src/', import.meta.url)) + expect(isUserSpecFile(`${selfDir}index.js`)).toBe(false) + expect(isUserSpecFile(`file://${selfDir}session.js`)).toBe(false) + }) }) }) From 03f5f77ce349513d20b6a1d4dc415075941ef424 Mon Sep 17 00:00:00 2001 From: Vishnu Vardhan <vishnu.p@browserstack.com> Date: Tue, 14 Jul 2026 15:01:28 +0530 Subject: [PATCH 54/68] fix(nightwatch): record negated native asserts (assert.not.* / verify.not.*) --- .../src/helpers/browserProxy.ts | 64 +++++++---- .../tests/browserProxy.test.ts | 100 +++++++++++++++++- 2 files changed, 142 insertions(+), 22 deletions(-) diff --git a/packages/nightwatch-devtools/src/helpers/browserProxy.ts b/packages/nightwatch-devtools/src/helpers/browserProxy.ts index 21225506..1d41c4fb 100644 --- a/packages/nightwatch-devtools/src/helpers/browserProxy.ts +++ b/packages/nightwatch-devtools/src/helpers/browserProxy.ts @@ -109,28 +109,52 @@ export class BrowserProxy { if (!original || typeof original !== 'object') { return } - b[prefix] = new Proxy(original as object, { - get: (target, name, receiver) => { - const orig = Reflect.get(target, name, receiver) - // `not` (negation Proxy) and non-method props pass through untouched. - if (typeof orig !== 'function' || typeof name !== 'string') { - return orig - } - return (...args: unknown[]) => { - const callInfo = getCallSourceFromStack() - if (callInfo.filePath !== undefined) { - this.emitPendingAssertion({ - prefix, - method: name, - args, - callSource: callInfo.callSource, - timestamp: Date.now() - }) - } - return (orig as (...a: unknown[]) => unknown)(...args) + b[prefix] = this.recordingNamespaceProxy(original, prefix, []) + }) + } + + /** + * Recording Proxy over one assert/verify namespace. A function property + * becomes a call-time recorder keyed by its full dotted path + * (`titleContains`, `not.titleContains`); a nested namespace object recurses + * through the SAME wrapper — Nightwatch exposes `assert.not` as its own Proxy, + * so negated asserts are recorded via the identical mechanism as positive + * ones instead of a parallel path. The recorder buffers a pending row, then + * delegates to the ORIGINAL method so Nightwatch's queue, chaining, and + * abort/negate semantics stay byte-for-byte unchanged. Non-method, + * non-namespace props pass through untouched. + */ + private recordingNamespaceProxy( + target: object, + prefix: 'assert' | 'verify', + path: readonly string[] + ): object { + return new Proxy(target, { + get: (t, name, receiver) => { + const orig = Reflect.get(t, name, receiver) + if (typeof name !== 'string') { + return orig + } + if (orig !== null && typeof orig === 'object') { + return this.recordingNamespaceProxy(orig, prefix, [...path, name]) + } + if (typeof orig !== 'function') { + return orig + } + return (...args: unknown[]) => { + const callInfo = getCallSourceFromStack() + if (callInfo.filePath !== undefined) { + this.emitPendingAssertion({ + prefix, + method: [...path, name].join('.'), + args, + callSource: callInfo.callSource, + timestamp: Date.now() + }) } + return (orig as (...a: unknown[]) => unknown)(...args) } - }) + } }) } diff --git a/packages/nightwatch-devtools/tests/browserProxy.test.ts b/packages/nightwatch-devtools/tests/browserProxy.test.ts index d69ffcf3..d6b7605d 100644 --- a/packages/nightwatch-devtools/tests/browserProxy.test.ts +++ b/packages/nightwatch-devtools/tests/browserProxy.test.ts @@ -1,5 +1,9 @@ import { describe, it, expect, vi, beforeEach } from 'vitest' -import type { CommandLog, NightwatchBrowser } from '../src/types.js' +import type { + CommandLog, + NightwatchBrowser, + NightwatchCurrentTest +} from '../src/types.js' // browserProxy resolves the caller's source via this helper; stub it so each // test can decide whether the command looks user-issued or framework-internal. @@ -10,6 +14,7 @@ const { getCallSourceFromStack } = vi.hoisted(() => ({ vi.mock('../src/helpers/utils.js', () => ({ getCallSourceFromStack })) import { BrowserProxy } from '../src/helpers/browserProxy.js' +import { captureNativeAssertions } from '../src/helpers/nativeAssertions.js' import type { SessionCapturer } from '../src/session.js' import type { TestManager } from '../src/helpers/testManager.js' @@ -39,16 +44,24 @@ function makeCapturer() { return true } ) + const captureAssertCommand = vi.fn( + (entry: CommandLog & { _id?: number; id?: number }) => { + entry._id = counter++ + entry.id = entry._id + commandsLog.push(entry) + } + ) const capturer = { commandsLog, captureCommand, + captureAssertCommand, replaceCommand: vi.fn(), sendCommand: vi.fn(), sendReplaceCommand: vi.fn(), takeScreenshotViaHttp: vi.fn(async () => null), captureTrace: vi.fn(async () => {}) } as unknown as SessionCapturer - return { capturer, commandsLog, captureCommand } + return { capturer, commandsLog, captureCommand, captureAssertCommand } } function makeTestManager() { @@ -166,3 +179,86 @@ describe('BrowserProxy captureAssertions gating', () => { expect(browser.verify).not.toBe(originalVerify) }) }) + +describe('BrowserProxy negated native assertions (assert.not.* / verify.not.*)', () => { + beforeEach(() => { + getCallSourceFromStack.mockReset() + }) + + /** Browser whose assert/verify mirror Nightwatch's structure: a positive + * namespace plus a nested `not` namespace object (Nightwatch exposes the + * negation as its own Proxy). Each leaf method records so the test can assert + * the wrapper still delegates to the real negated method. */ + function makeNegatableAssertBrowser() { + const record: string[] = [] + const ns = (label: string) => ({ + titleContains: vi.fn(() => { + record.push(`${label}titleContains`) + }) + }) + const browser = { + assert: { ...ns(''), not: ns('not.') }, + verify: { ...ns(''), not: ns('not.') } + } as unknown as NightwatchBrowser + return { browser, record } + } + + it('buffers a negated assert with a negation-reflecting label and finalizes its outcome', async () => { + const { capturer, commandsLog } = makeCapturer() + getCallSourceFromStack.mockReturnValue({ + filePath: '/tests/spec.js', + callSource: '/tests/spec.js:9' + }) + const proxy = new BrowserProxy(capturer, makeTestManager(), () => ({ + uid: 't1' + })) + const { browser, record } = makeNegatableAssertBrowser() + proxy.wrapBrowserCommands(browser) + + // browser.assert.not.titleContains('Example') — a negated call from user code. + ;( + browser as unknown as { + assert: { not: { titleContains: (a: unknown) => unknown } } + } + ).assert.not.titleContains('Example') + + // Delegated to the ORIGINAL negated method (Nightwatch semantics unchanged). + expect(record).toEqual(['not.titleContains']) + + // Buffered exactly one recorded call, keyed by its full dotted path — so the + // negation is captured through the SAME mechanism as positive asserts. + const calls = proxy.drainNativeAssertCalls() + expect(calls).toHaveLength(1) + expect(calls[0].prefix).toBe('assert') + expect(calls[0].method).toBe('not.titleContains') + expect(calls[0].args).toEqual(['Example']) + expect(calls[0].callSource).toBe('/tests/spec.js:9') + expect(calls[0].entry?.command).toBe('assert.not.titleContains') + expect(calls[0].entry?.title).toBe("assert.not.titleContains('Example')") + + // Reconciled at test-end through the same finalize path as positive asserts: + // a failing negated entry yields one row with the negated label + fail outcome. + const results = { + assertions: [ + { + message: "Testing if the page title doesn't contain 'Example'", + fullMsg: "Testing if the page title doesn't contain 'Example'", + failure: + "Testing if the page title doesn't contain 'Example' — failed" + } + ], + commands: [] + } + const currentTest = { + name: 't', + results + } as unknown as NightwatchCurrentTest + await captureNativeAssertions(capturer, browser, currentTest, 't1', calls) + + const row = commandsLog[commandsLog.length - 1] + expect(row.command).toBe('assert.not.titleContains') + expect(row.title).toBe("assert.not.titleContains('Example')") + expect(row.result).toMatchObject({ passed: false }) + expect(row.error).toBeDefined() + }) +}) From 8781afb905325407eb167cfb806cabbc76e575c3 Mon Sep 17 00:00:00 2001 From: Vishnu Vardhan <vishnu.p@browserstack.com> Date: Tue, 14 Jul 2026 15:06:05 +0530 Subject: [PATCH 55/68] refactor(service): extract assertion capture into AssertionTracker --- packages/service/src/assertion-tracker.ts | 187 ++++++++++++++++++++++ packages/service/src/index.ts | 171 +++----------------- 2 files changed, 205 insertions(+), 153 deletions(-) create mode 100644 packages/service/src/assertion-tracker.ts diff --git a/packages/service/src/assertion-tracker.ts b/packages/service/src/assertion-tracker.ts new file mode 100644 index 00000000..b0ab2f3d --- /dev/null +++ b/packages/service/src/assertion-tracker.ts @@ -0,0 +1,187 @@ +// Owns the expect-webdriverio matcher lifecycle for the WDIO worker: the matcher +// nesting depth and the pending-matcher state machine that turns a matcher call +// into a single trace row. Pure CommandLog shaping lives in ./assert-capture. + +import logger from '@wdio/logger' +import { errorMessage } from '@wdio/devtools-core' +import type { ActionSnapshot } from '@wdio/devtools-shared' +import { + captureExpectFailure, + expectAssertionToCommandLog, + isMatcherReadCommand, + type ExpectAssertion +} from './assert-capture.js' +import { pushActionSnapshotAt } from './action-snapshot.js' +import { isNativeMobile } from './mobile.js' +import type { SessionCapturer } from './session.js' +import type { ServiceOptions } from './types.js' + +const log = logger('@wdio/devtools-service:assertion-tracker') + +/** Live accessors into the owning service's state. The capturer is replaced in + * before() and the browser/test uid change per test, so each is read lazily. */ +export interface AssertionTrackerContext { + getCapturer: () => SessionCapturer + getBrowser: () => WebdriverIO.Browser | undefined + getTestUid: () => string | undefined + options: ServiceOptions + actionSnapshots: ActionSnapshot[] +} + +interface PendingAssertion { + matcherName: string + expectedValue?: unknown + testUid?: string +} + +export class AssertionTracker { + #ctx: AssertionTrackerContext + + /** expect-webdriverio matcher nesting depth. Aliases fire before/afterAssertion + * twice (toBeChecked→toBeSelected), so only the outermost pair owns the row. */ + #assertionDepth = 0 + + /** Matcher armed at beforeAssertion, cleared at its afterAssertion. It survives + * to test end only when the matcher hard-threw (element never resolved, so + * waitUntil rethrew and afterAssertion never fired) — then it's synthesized as + * a failing expect.<matcher> row instead of leaving a raw read. */ + #pendingAssertion?: PendingAssertion + + constructor(ctx: AssertionTrackerContext) { + this.#ctx = ctx + } + + /** Clear per-test matcher state; called from the service's resetStack. */ + reset(): void { + this.#assertionDepth = 0 + this.#pendingAssertion = undefined + } + + /** + * expect-webdriverio fires this at the START of every matcher, before it polls + * — so it fires even for a matcher that later hard-throws (element never + * resolved), unlike afterAssertion. Arm the pending matcher here so test-end + * can synthesize its expect row if afterAssertion never comes. Depth-counted: + * aliases (toBeChecked→toBeSelected) fire this twice, so only the outermost + * arms the row. + */ + beforeAssertion(params: { + matcherName: string + expectedValue?: unknown + }): void { + if (this.#ctx.options.captureAssertions === false) { + return + } + if (this.#assertionDepth === 0) { + this.#pendingAssertion = { + matcherName: params.matcherName, + expectedValue: params.expectedValue, + testUid: this.#ctx.getTestUid() + } + } + this.#assertionDepth++ + } + + /** + * expect-webdriverio fires this after each matcher evaluates, with the matcher + * name + pass/fail + expected value. The matcher's value-read (getText / + * isExisting / …) was captured as a normal command; fold this assertion into + * that read so one `expect.<matcher>` row remains — inheriting the read's real + * callSource, screenshot, and timeline position. Deterministic and anchored to + * this reliable hook: no `#assertionDepth`, no stack-frame detection. + */ + async afterAssertion(params: ExpectAssertion): Promise<void> { + if (this.#ctx.options.captureAssertions === false) { + return + } + this.#assertionDepth = Math.max(0, this.#assertionDepth - 1) + // Inner matcher of a nested pair (toBeChecked→toBeSelected): the outer + // afterAssertion owns the row. + if (this.#assertionDepth > 0) { + return + } + // Reached afterAssertion → the matcher resolved (pass or value-fail), so no + // hard-throw synthesis is needed at test end. + this.#pendingAssertion = undefined + const capturer = this.#ctx.getCapturer() + const entry = expectAssertionToCommandLog(params, this.#ctx.getTestUid()) + if (capturer.coalesceAssertionIntoLastRead(entry, isMatcherReadCommand)) { + return + } + // No matcher read to fold into (a value matcher like toBe(x), or the read + // hard-threw): emit a fresh row with its own screenshot + trace snapshot. + const browser = this.#ctx.getBrowser() + if (browser && !isNativeMobile(browser)) { + try { + entry.screenshot = await browser.takeScreenshot() + } catch (err) { + // best-effort: a missing screenshot must not fail the assertion hook + log.debug(`assertion screenshot skipped: ${errorMessage(err)}`) + } + } + if (this.#ctx.options.mode === 'trace' && browser) { + await pushActionSnapshotAt( + browser, + entry.command, + entry.timestamp, + this.#ctx.actionSnapshots + ) + } + capturer.captureAssertCommand(entry) + } + + /** Route a test/step failure to assertion capture. A matcher that hard-threw + * (element never resolved) left an armed pendingAssertion because + * afterAssertion never fired — synthesize its failing expect row. Any other + * failure just marks the last action with the error. */ + handleOutcome(error: unknown): void { + if (this.#ctx.options.captureAssertions === false) { + return + } + if (this.#pendingAssertion) { + this.#finalizePendingAssertion(error) + return + } + this.#captureExpectFailure(error) + } + + /** Mark the failing action from a matcher error (afterStep for Cucumber, + * afterTest for Mocha route here). */ + #captureExpectFailure(error: unknown): void { + captureExpectFailure( + this.#ctx.getCapturer(), + this.#ctx.getTestUid(), + error, + this.#ctx.options.captureAssertions !== false + ) + } + + /** Synthesize the failing expect.<matcher> row for a hard-thrown matcher: fold + * it into the throwing read (relabel `getText`→`expect.toHaveText`, keeping + * the error) so the assertion renders consistently whether or not the element + * resolved; fall back to a fresh row when there is no read to fold. */ + #finalizePendingAssertion(error: unknown): void { + const pending = this.#pendingAssertion + this.#pendingAssertion = undefined + this.#assertionDepth = 0 + if (!pending) { + return + } + const message = errorMessage(error) || `${pending.matcherName} failed` + const entry = expectAssertionToCommandLog( + { + matcherName: pending.matcherName, + expectedValue: pending.expectedValue, + result: { pass: false, message: () => message } + }, + pending.testUid + ) + const capturer = this.#ctx.getCapturer() + if ( + capturer.coalesceAssertionIntoLastRead(entry, isMatcherReadCommand, true) + ) { + return + } + capturer.captureAssertCommand(entry) + } +} diff --git a/packages/service/src/index.ts b/packages/service/src/index.ts index 7d9d0419..5b4ee689 100644 --- a/packages/service/src/index.ts +++ b/packages/service/src/index.ts @@ -12,13 +12,8 @@ import { type SpecRange, type TraceExportContext } from '@wdio/devtools-core' -import { - captureExpectFailure, - expectAssertionToCommandLog, - isMatcherReadCommand, - wireAssertCapture, - type ExpectAssertion -} from './assert-capture.js' +import { wireAssertCapture, type ExpectAssertion } from './assert-capture.js' +import { AssertionTracker } from './assertion-tracker.js' import { cucumberScenarioUid, resolveTestAttempt, @@ -30,8 +25,7 @@ import { resolveCallSourceFromFrame } from './call-source.js' import { flushPrevSlice, flushTestSlice } from './trace-slices.js' import { captureActionResult, - captureActionSnapshot, - pushActionSnapshotAt + captureActionSnapshot } from './action-snapshot.js' import { dedupeSnapshotsByTimestamp, @@ -80,9 +74,17 @@ export default class DevToolsHookService implements Services.ServiceInstance { #screencastOptions?: ScreencastOptions #options: ServiceOptions #actionSnapshots: ActionSnapshot[] = [] + #assertionTracker: AssertionTracker constructor(serviceOptions: ServiceOptions = {}) { this.#options = serviceOptions + this.#assertionTracker = new AssertionTracker({ + getCapturer: () => this.#sessionCapturer, + getBrowser: () => this.#browser, + getTestUid: () => this.#currentTestUid, + options: this.#options, + actionSnapshots: this.#actionSnapshots + }) const policyWarning = tracePolicyModeWarning( serviceOptions.tracePolicy, serviceOptions.mode @@ -107,20 +109,6 @@ export default class DevToolsHookService implements Services.ServiceInstance { /** Current test UID, set in beforeTest(), used by afterCommand() to tag commands. */ #currentTestUid?: string - /** expect-webdriverio matcher nesting depth. Aliases fire before/afterAssertion - * twice (toBeChecked→toBeSelected), so only the outermost pair owns the row. */ - #assertionDepth = 0 - - /** Matcher armed at beforeAssertion, cleared at its afterAssertion. It survives - * to test end only when the matcher hard-threw (element never resolved, so - * waitUntil rethrew and afterAssertion never fired) — then it's synthesized as - * a failing expect.<matcher> row instead of leaving a raw read. */ - #pendingAssertion?: { - matcherName: string - expectedValue?: unknown - testUid?: string - } - /** Map of testUid → metadata for trace group events and per-spec partitioning. */ #testMetadata: TestMetadataMap = new Map() @@ -388,65 +376,7 @@ export default class DevToolsHookService implements Services.ServiceInstance { _scenario?: unknown, result?: { error?: unknown } ) { - this.#handleAssertionOutcome(result?.error) - } - - /** Route a test/step failure to assertion capture. A matcher that hard-threw - * (element never resolved) left an armed #pendingAssertion because - * afterAssertion never fired — synthesize its failing expect row. Any other - * failure just marks the last action with the error. */ - #handleAssertionOutcome(error: unknown): void { - if (this.#options.captureAssertions === false) { - return - } - if (this.#pendingAssertion) { - this.#finalizePendingAssertion(error) - return - } - this.#captureExpectFailure(error) - } - - /** Synthesize the failing expect.<matcher> row for a hard-thrown matcher: fold - * it into the throwing read (relabel `getText`→`expect.toHaveText`, keeping - * the error) so the assertion renders consistently whether or not the element - * resolved; fall back to a fresh row when there is no read to fold. */ - #finalizePendingAssertion(error: unknown): void { - const pending = this.#pendingAssertion - this.#pendingAssertion = undefined - this.#assertionDepth = 0 - if (!pending) { - return - } - const message = errorMessage(error) || `${pending.matcherName} failed` - const entry = expectAssertionToCommandLog( - { - matcherName: pending.matcherName, - expectedValue: pending.expectedValue, - result: { pass: false, message: () => message } - }, - pending.testUid - ) - if ( - this.#sessionCapturer.coalesceAssertionIntoLastRead( - entry, - isMatcherReadCommand, - true - ) - ) { - return - } - this.#sessionCapturer.captureAssertCommand(entry) - } - - /** Mark the failing action from a matcher error (afterStep for Cucumber, - * afterTest for Mocha route here). */ - #captureExpectFailure(error: unknown): void { - captureExpectFailure( - this.#sessionCapturer, - this.#currentTestUid, - error, - this.#options.captureAssertions !== false - ) + this.#assertionTracker.handleOutcome(result?.error) } /** Stamp final state + the resolved 0-based attempt onto the test's metadata @@ -481,7 +411,7 @@ export default class DevToolsHookService implements Services.ServiceInstance { _context?: unknown, result?: TestOutcomeResult ) { - this.#handleAssertionOutcome(result?.error) + this.#assertionTracker.handleOutcome(result?.error) const testTitle = test?.fullTitle || test?.title const uid = testTitle ? testMetadataUid(test?.file, testTitle) : undefined if (uid) { @@ -494,80 +424,16 @@ export default class DevToolsHookService implements Services.ServiceInstance { } } - /** - * expect-webdriverio fires this after each matcher evaluates, with the matcher - * name + pass/fail + expected value. The matcher's value-read (getText / - * isExisting / …) was captured as a normal command; fold this assertion into - * that read so one `expect.<matcher>` row remains — inheriting the read's real - * callSource, screenshot, and timeline position. Deterministic and anchored to - * this reliable hook: no `#assertionDepth`, no stack-frame detection. - */ - /** - * expect-webdriverio fires this at the START of every matcher, before it polls - * — so it fires even for a matcher that later hard-throws (element never - * resolved), unlike afterAssertion. Arm the pending matcher here so test-end - * can synthesize its expect row if afterAssertion never comes. Depth-counted: - * aliases (toBeChecked→toBeSelected) fire this twice, so only the outermost - * arms the row. - */ + /** expect-webdriverio matcher hooks — delegated to the assertion tracker. */ beforeAssertion(params: { matcherName: string expectedValue?: unknown }): void { - if (this.#options.captureAssertions === false) { - return - } - if (this.#assertionDepth === 0) { - this.#pendingAssertion = { - matcherName: params.matcherName, - expectedValue: params.expectedValue, - testUid: this.#currentTestUid - } - } - this.#assertionDepth++ + this.#assertionTracker.beforeAssertion(params) } - async afterAssertion(params: ExpectAssertion): Promise<void> { - if (this.#options.captureAssertions === false) { - return - } - this.#assertionDepth = Math.max(0, this.#assertionDepth - 1) - // Inner matcher of a nested pair (toBeChecked→toBeSelected): the outer - // afterAssertion owns the row. - if (this.#assertionDepth > 0) { - return - } - // Reached afterAssertion → the matcher resolved (pass or value-fail), so no - // hard-throw synthesis is needed at test end. - this.#pendingAssertion = undefined - const entry = expectAssertionToCommandLog(params, this.#currentTestUid) - if ( - this.#sessionCapturer.coalesceAssertionIntoLastRead( - entry, - isMatcherReadCommand - ) - ) { - return - } - // No matcher read to fold into (a value matcher like toBe(x), or the read - // hard-threw): emit a fresh row with its own screenshot + trace snapshot. - if (this.#browser && !isNativeMobile(this.#browser)) { - try { - entry.screenshot = await this.#browser.takeScreenshot() - } catch (err) { - // best-effort: a missing screenshot must not fail the assertion hook - log.debug(`assertion screenshot skipped: ${errorMessage(err)}`) - } - } - if (this.#options.mode === 'trace' && this.#browser) { - await pushActionSnapshotAt( - this.#browser, - entry.command, - entry.timestamp, - this.#actionSnapshots - ) - } - this.#sessionCapturer.captureAssertCommand(entry) + afterAssertion(params: ExpectAssertion): Promise<void> { + return this.#assertionTracker.afterAssertion(params) } async #finalizePerScenario() { @@ -598,8 +464,7 @@ export default class DevToolsHookService implements Services.ServiceInstance { private resetStack() { this.#commandStack = [] - this.#assertionDepth = 0 - this.#pendingAssertion = undefined + this.#assertionTracker.reset() this.#sessionCapturer.resetLastSelector() this.#sessionCapturer.resetRetryTracker() } From b5d6aded6e21526f94d69c2bc4e857e2cc3341c9 Mon Sep 17 00:00:00 2001 From: Vishnu Vardhan <vishnu.p@browserstack.com> Date: Tue, 14 Jul 2026 16:37:17 +0530 Subject: [PATCH 56/68] docs: sync WDIO assert-capture debt to the fold model; drop fixed entries --- CLAUDE.md | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/CLAUDE.md b/CLAUDE.md index 1f33103e..007cc486 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -233,22 +233,19 @@ Documented divergences from the conventions above. They exist today as debt to b ### Architecture - `replaceCommand` has two semantics — Selenium mutates in place (preserves `_id`/`id` for chained calls); Nightwatch splices and reissues. Both call the same `core/suite-helpers` factories; the storage strategy stays adapter-specific because runner integrations differ. Could be unified by parameterizing the policy if the divergence ever causes a real problem. -- `patchNodeAssert` (via `core/assert-patcher`) is now wired in all three adapters, default-on behind each adapter's `captureAssertions` option (opt out with `captureAssertions: false`). Framework matcher libraries differ: Service additionally taps expect-webdriverio's `afterAssertion` hook so passing+failing matchers render as `expect.*` actions; Selenium (chai/jest) and Nightwatch (`browser.assert`/`verify`) don't yet surface passing framework-matcher assertions as trace actions. +- `patchNodeAssert` (via `core/assert-patcher`) is now wired in all three adapters, default-on behind each adapter's `captureAssertions` option (opt out with `captureAssertions: false`). Framework matcher libraries differ: Service taps expect-webdriverio's `beforeAssertion`/`afterAssertion` hooks so passing+failing matchers render as `expect.*` actions (mechanism in the assert-capture entry below); Nightwatch native `assert`/`verify` and Selenium's `node:assert` also surface passing+failing rows via their reconcile/patch paths. The remaining gap is Selenium's jest-style `expect()` (and chai): jest/vitest expose no pass+fail assertion hook, so only failing matchers surface there. - BiDi is auto-attached in Service and Selenium; Nightwatch is opt-in via `bidi: true` and requires `webSocketUrl: true` in capabilities. - Retry-aware trace policies aren't uniform across adapters. Selenium emits one metadata entry per attempt (counter-based `generateStableUid` uid + retry-stable tracker key), so all six policies — including `retain-on-first-failure` (needs the first attempt's outcome) — evaluate correctly. Service and Nightwatch key metadata on the retry-stable uid and overwrite/mutate per attempt, collapsing to the *final* attempt's outcome; the retry-count policies (`retain-on-failure`, `on-first-retry`, `on-all-retries`, `retain-on-failure-and-retries`) are still correct, but `retain-on-first-failure` can't see a failed-then-passed first attempt and under-retains. Proper fix rides with B5 (per-test granularity preserves per-attempt slices); until then all three set `attemptInfoAvailable: true`. -- Nightwatch negated native asserts (`browser.assert.not.*` / `verify.not.*`) produce no trace row: the `not` namespace is a nested Proxy that `wrapAssertionNamespaces` passes through untouched, and the internal command it issues is dropped by the `hasUserSource` gate in `browserProxy`. Positive `assert.*`/`verify.*` still record. - Eager per-test trace slice (Nightwatch + Selenium) can drop an action snapshot whose fire-and-forget capture hasn't resolved by `afterEach` / scenario end — the slice is written from whatever snapshots exist at flush time. The WDIO service is immune because it awaits each snapshot inline before flushing. - Nightwatch native asserts are buffered at call time and emitted in one batch at test-end (Nightwatch exposes no per-assertion execution/outcome hook reachable from the plugin — `client.queue.tree` / `client.reporter` aren't on `browser`). Streaming mid-run would flash every assert green before its outcome is known, so rows are held and reconciled against `results.testcases[*].assertions` at `afterEach` (the flat `results.assertions` only reflects the last testcase). The exporter re-sorts commands by timestamp (`buildActionEvents`) so the batched rows land at their real timeline positions rather than clustering at flush time. `actual` is parsed from the failure message (`but got: …`, failures only) into a collapsed `{passed, expected, actual}` result; `expected` is the assertion arg — the label still mirrors the call args, not the derived values. - Nightwatch's BDD `describe/it` interface fires the plugin's global `beforeEach`/`afterEach` **once per module** (with an empty `currentTest.name`), not per `it` — Nightwatch runs the individual `it`s internally with no per-testcase hook or event reachable from the plugin (the lib emits only transport-level events). So `traceGranularity:'test'` records a single slice boundary (the first test) and collapses to one session-scoped slice; retry-aware/per-test retention (`tracePolicy`) degrades to session scope for this interface. Per-test slicing works for adapters/styles that expose per-test hooks (WDIO mocha/cucumber, Selenium mocha; likely Nightwatch's exports-object style, unverified). Session-granularity trace is unaffected. -- Service assertion-suppression self-heal is command-triggered and gated on `#matcherStarted`: a swallowed soft-assert (`expect.soft` / user `try/catch`) whose matcher hard-throws leaves `#assertionDepth` stuck; the next top-level user command with no `expect-webdriverio` frame heals it — but only once the matcher has issued at least one internal (ewdio-framed) command, because the matcher's own FIRST command (an element re-lookup) also lacks a matcher frame on its sync stack and would otherwise reset the depth mid-run (duplicating every later matcher command). A stuck depth with no following command before test end persists until the next `beforeAssertion`/`resetStack()` — benign. -- Service captures the assertion **target's** element resolution as its own row. `expect(page.el).toHaveText(x)` evaluates `page.el` → `$('#sel')`/`$$` as the ARGUMENT to `expect()`, before `beforeAssertion` fires, so `#assertionDepth` is 0 and the `$`/`$$`/`isExisting` lands as a top-level command alongside the `expect.*` row. The in-window suppression can't reach it (it runs pre-window). Proper fix: coalesce a trailing element-query into the imminent assertion, or detect query-commands whose only consumer is the next matcher. -- Service `expect.*` rows can resolve `callSource` to the service bundle (`dist/index.js`) instead of the user's `expect()` line. `#assertionCallSources` captures the spec frame in `beforeAssertion`, but the matcher runs async, so in some cases the parallel stack doesn't hold a user frame and the fallback resolves into the service dist. The failure's own error stack still points at the spec. +- Service renders expect-webdriverio matchers as single `expect.<matcher>` rows by **folding**, not stack/depth suppression (the old `#assertionDepth`/`#matcherStarted`/self-heal machinery is gone). The matcher's value-read (`toHaveText`→`getText`, `toExist`→`isExisting`, …) is captured as a normal command; `afterAssertion` then coalesces the synthesized `expect.*` row into that read in place — inheriting its callSource, screenshot, and timeline position — and the fold replaces **by timestamp, never a public `id`**: `id` is the per-worker `commandCounter`, which resets per spec, so stamping one lets the app's id-first `replaceCommand` swap a same-id row from another spec (duplicate rows + a fold from another spec vanishing, in multi-spec live mode). `beforeAssertion` arms the pending matcher (depth-counted so aliases like `toBeChecked`→`toBeSelected` fold once); a matcher that **hard-throws** — element never resolves, so expect-webdriverio's `waitUntil` rethrows and `afterAssertion` never fires — is synthesized at `afterTest`/`afterStep` from the throwing read, so a failing assertion renders as `expect.<matcher>` whether or not the element existed. Two limits: its error is then the read's (`Can't call getText on … element wasn't found`), not an assertion-phrased message; and `MATCHER_READ_COMMANDS` is a hand-maintained allowlist, so a matcher whose read isn't listed leaves its raw read visible alongside the `expect.*` row. Plain-value jest matchers (`expect(x).toBe(y)`) don't fire the ewdio hooks, so they aren't captured as rows. ### File-size (raw line counts; soft cap is 500 logic lines) Most entries below don't trigger the `max-lines` lint rule after `skipBlankLines`/`skipComments`; they're documented because their raw line count is over 500, and the next substantive change to any of them should still look for an extraction opportunity. The service plugin is the exception — it's now over the *logic*-line cap. -- `packages/service/src/index.ts` — over the 500-**logic**-line soft cap (the WDIO plugin god-file: session/screencast/command/assertion/trace-slice wiring in one class). Split candidate; the trace-slice, assertion-capture, and screencast concerns are the natural extraction seams. +- `packages/service/src/index.ts` — over the 500-**logic**-line soft cap and ~620 raw (the WDIO plugin god-file: session/screencast/command/assertion/trace-slice wiring in one class). The assertion-capture block (`beforeAssertion`/`afterAssertion`/`#handleAssertionOutcome`/`#finalizePendingAssertion`, plus the `#assertionDepth`/`#pendingAssertion` state) is now the largest self-contained seam and the prime extraction candidate; trace-slice and screencast are the next two. - `packages/nightwatch-devtools/src/index.ts` (~536 raw). Cucumber/test/run-lifecycle, session-init, event-hub modules already extracted; remainder is the `PluginInternals` accessor bag plus per-method delegators plus the factory. Accept-as-is. - `packages/selenium-devtools/src/index.ts` (~560 raw). Session/test-lifecycle extracted; remainder is the `PluginInternals` accessor bag plus onCommand/onDriverCreated wiring. Same situation as nightwatch. From 35ec983947eb575625c969666d7b2de682c25ce3 Mon Sep 17 00:00:00 2001 From: Vishnu Vardhan <vishnu.p@browserstack.com> Date: Tue, 14 Jul 2026 17:35:59 +0530 Subject: [PATCH 57/68] feat(core): per-attempt outcome ledger + group-by-test retention --- packages/core/src/attempt-tracker.ts | 97 +++++++++++++++++---- packages/core/src/trace-finalizer.ts | 32 ++++++- packages/core/src/trace-retention.ts | 60 +++++++++++-- packages/core/tests/attempt-tracker.test.ts | 75 ++++++++++++++++ packages/core/tests/trace-finalizer.test.ts | 44 ++++++++++ packages/core/tests/trace-retention.test.ts | 44 ++++++++++ 6 files changed, 326 insertions(+), 26 deletions(-) diff --git a/packages/core/src/attempt-tracker.ts b/packages/core/src/attempt-tracker.ts index e4915584..60bbdfa4 100644 --- a/packages/core/src/attempt-tracker.ts +++ b/packages/core/src/attempt-tracker.ts @@ -1,29 +1,71 @@ +import type { TestStatus } from '@wdio/devtools-shared' +import type { TestOutcome } from './trace-retention.js' + /** - * Framework-agnostic per-test attempt counting. Every supported runner - * re-enters its per-test start hook when a test is retried, so recording the - * same uid again yields an incremented attempt number (0-based: the first run - * is attempt 0, the first retry is attempt 1). This is the primary, - * runner-independent retry signal feeding `TestOutcome.attempt` for the - * retry-aware trace policies (see trace-retention.ts). + * Framework-agnostic per-test attempt ledger. Every supported runner re-enters + * its per-test start hook when a test is retried, so recording the same uid + * again appends a new attempt slot (0-based: first run is attempt 0, first retry + * is attempt 1). Once the outcome is known the adapter stamps it onto the slot. + * This is the primary, runner-independent retry signal feeding the retry-aware + * trace policies — the finalizer reads the scoped views to evaluate retention + * per test/spec/session with real per-attempt outcomes (see trace-retention.ts). */ -export class TestAttemptTracker { - #attempts = new Map<string, number>() +export interface RetryOutcomeView { + /** Every attempt of every test — for session-scope retention. */ + all(): TestOutcome[] + /** Every attempt of the tests recorded against `specFile` — for spec scope. */ + forSpec(specFile: string): TestOutcome[] + /** A single test's attempts; pass `attempt` to scope to one attempt's slice. */ + forTest(uid: string, attempt?: number): TestOutcome[] +} + +export class TestAttemptTracker implements RetryOutcomeView { + #ledger = new Map<string, { specFile?: string; attempts: TestOutcome[] }>() #sawRetry = false - /** Record a starting test; returns its attempt number (0 first, +1 per rerun). */ - recordStart(uid: string): number { - const prior = this.#attempts.get(uid) - const attempt = prior === undefined ? 0 : prior + 1 - this.#attempts.set(uid, attempt) + /** Record a starting test; returns its attempt number (0 first, +1 per rerun). + * `specFile` (optional) enables spec-scoped retention lookups. */ + recordStart(uid: string, specFile?: string): number { + const entry = this.#ledger.get(uid) + const attempt = entry ? entry.attempts.length : 0 if (attempt > 0) { this.#sawRetry = true } + const slot: TestOutcome = { uid, attempt } + if (entry) { + entry.attempts.push(slot) + if (specFile !== undefined) { + entry.specFile = specFile + } + } else { + this.#ledger.set(uid, { specFile, attempts: [slot] }) + } return attempt } + /** Stamp the resolved state onto uid's most recent attempt slot once the + * outcome is known. `attempt` overrides the slot's number when the adapter + * resolved a more authoritative value (e.g. WDIO's result.retries). */ + recordOutcome( + uid: string, + state: TestStatus | undefined, + attempt?: number + ): void { + const attempts = this.#ledger.get(uid)?.attempts + const slot = attempts?.[attempts.length - 1] + if (!slot) { + return + } + slot.state = state + if (attempt !== undefined) { + slot.attempt = attempt + } + } + /** Latest attempt recorded for `uid`, or undefined if it never started. */ attemptFor(uid: string): number | undefined { - return this.#attempts.get(uid) + const entry = this.#ledger.get(uid) + return entry ? entry.attempts.length - 1 : undefined } /** True once any test has started more than once (a retry occurred). */ @@ -31,9 +73,34 @@ export class TestAttemptTracker { return this.#sawRetry } + all(): TestOutcome[] { + const out: TestOutcome[] = [] + for (const entry of this.#ledger.values()) { + out.push(...entry.attempts) + } + return out + } + + forSpec(specFile: string): TestOutcome[] { + const out: TestOutcome[] = [] + for (const entry of this.#ledger.values()) { + if (entry.specFile === specFile) { + out.push(...entry.attempts) + } + } + return out + } + + forTest(uid: string, attempt?: number): TestOutcome[] { + const attempts = this.#ledger.get(uid)?.attempts ?? [] + return attempt === undefined + ? attempts + : attempts.filter((a) => a.attempt === attempt) + } + /** Clear all state — used at session boundaries and reuse-mode reconnects. */ reset(): void { - this.#attempts.clear() + this.#ledger.clear() this.#sawRetry = false } } diff --git a/packages/core/src/trace-finalizer.ts b/packages/core/src/trace-finalizer.ts index f0459590..a07286af 100644 --- a/packages/core/src/trace-finalizer.ts +++ b/packages/core/src/trace-finalizer.ts @@ -23,6 +23,7 @@ import { type SpecRange } from './spec-trace-helpers.js' import { shouldRetainTrace, type TestOutcome } from './trace-retention.js' +import type { RetryOutcomeView } from './attempt-tracker.js' import { writeTraceZip, type TraceCapturer } from './trace-exporter.js' /** One artifact produced (or, when `retained` is false, decided-against) by a @@ -45,6 +46,13 @@ export interface TraceExportContext { /** True when the adapter fed real per-test attempt numbers (B4); retry-aware * policies degrade to retain-on-failure when this is false. */ attemptInfoAvailable?: boolean + /** Per-attempt outcome ledger. When present, retention is evaluated against + * real per-attempt outcomes (scoped per session/spec/test) instead of the + * collapsed final-attempt state in `testMetadata` — this is what lets + * retain-on-first-failure see a failed-then-passed first attempt and stops + * retain-on-failure over-retaining it. Absent → falls back to `testMetadata` + * (unchanged behavior). */ + outcomes?: RetryOutcomeView granularity?: TraceGranularity format?: TraceFormat capturer: TraceCapturer @@ -153,14 +161,22 @@ function toOutcomes(metadata: TestMetadataMap): TestOutcome[] { */ function shouldRetain( ctx: TraceExportContext, - metadata: TestMetadataMap + metadata: TestMetadataMap, + ledgerOutcomes?: TestOutcome[] ): boolean { return shouldRetainTrace(ctx.policy, { - outcomes: toOutcomes(metadata), + outcomes: ledgerOutcomes ?? toOutcomes(metadata), attemptInfoAvailable: ctx.attemptInfoAvailable ?? false }).retain } +/** Attempt number encoded in a test-slice key (`<uid>-retry<n>`); 0 when the + * key is the base uid (the first attempt / no retry suffix). */ +function attemptFromKey(key: string): number { + const match = /-retry(\d+)$/.exec(key) + return match ? Number(match[1]) : 0 +} + /** * Policy-aware single-range flush: dedupes via `ctx.flushed` on the slice * `key`, applies the retention decision, and delegates the byte-level @@ -182,13 +198,21 @@ export async function flushRangeTrace( const sliceMetadata = isTestSlice ? filterTestMetadataByUid(ctx.testMetadata, range.testUid!) : filterTestMetadataBySpec(ctx.testMetadata, range.specFile) + // Scope the per-attempt ledger to this slice: a test slice sees only its own + // attempt (so a passing retry's slice isn't retained on first-failure), a spec + // slice sees every attempt of its tests. + const sliceOutcomes = ctx.outcomes + ? isTestSlice + ? ctx.outcomes.forTest(range.testUid!, attemptFromKey(range.key)) + : ctx.outcomes.forSpec(range.specFile) + : undefined const artifact: TraceArtifact = { kind: 'trace', path: '', scope: isTestSlice ? 'test' : 'spec', key: range.key, testUids: Array.from(sliceMetadata.keys()), - retained: shouldRetain(ctx, sliceMetadata) + retained: shouldRetain(ctx, sliceMetadata, sliceOutcomes) } if (!artifact.retained) { ctx.onArtifact?.(artifact) @@ -255,7 +279,7 @@ async function writeSessionTrace( path: '', scope: 'session', testUids: Array.from(ctx.testMetadata.keys()), - retained: shouldRetain(ctx, ctx.testMetadata) + retained: shouldRetain(ctx, ctx.testMetadata, ctx.outcomes?.all()) } if (!artifact.retained) { ctx.onArtifact?.(artifact) diff --git a/packages/core/src/trace-retention.ts b/packages/core/src/trace-retention.ts index 6688dfcb..cdc35013 100644 --- a/packages/core/src/trace-retention.ts +++ b/packages/core/src/trace-retention.ts @@ -23,6 +23,10 @@ const KNOWN_POLICIES = new Set<TraceRetentionPolicy>([ ]) export interface TestOutcome { + /** Retry-stable test identity. Outcomes sharing a uid are one test's attempts + * (attempt 0, 1, …); an outcome with no uid is its own single-attempt group, + * so a flat/uid-less feed evaluates exactly as one-outcome-per-test. */ + uid?: string state?: TestStatus attempt?: number } @@ -56,26 +60,68 @@ export function shouldRetainTrace( if (!input.attemptInfoAvailable && policy !== 'retain-on-failure') { return { retain: anyFailed, degradedToFailure: true } } + // Group a test's attempts so failure policies key on the RIGHT attempt: the + // *final* attempt (retain-on-failure — a fail-then-pass ends passed) vs the + // *first* attempt (retain-on-first-failure). A uid-less feed makes every + // outcome its own group, so this reduces to the flat one-outcome-per-test + // logic and is byte-identical for callers that don't supply per-attempt uids. + const groups = groupByTest(outcomes) switch (policy) { case 'retain-on-failure': - return { retain: anyFailed } + return { retain: groups.some((g) => finalAttempt(g).state === 'failed') } case 'retain-on-first-failure': return { - retain: outcomes.some( - (o) => o.state === 'failed' && (o.attempt ?? 0) === 0 - ) + retain: groups.some((g) => firstAttempt(g)?.state === 'failed') } case 'on-first-retry': - return { retain: outcomes.some((o) => o.attempt === 1) } + return { retain: groups.some((g) => g.some((o) => o.attempt === 1)) } case 'on-all-retries': - return { retain: outcomes.some((o) => (o.attempt ?? 0) >= 1) } + return { retain: groups.some((g) => g.some((o) => attemptOf(o) >= 1)) } case 'retain-on-failure-and-retries': return { - retain: anyFailed || outcomes.some((o) => (o.attempt ?? 0) >= 1) + retain: groups.some( + (g) => + finalAttempt(g).state === 'failed' || + g.some((o) => attemptOf(o) >= 1) + ) } } } +const attemptOf = (o: TestOutcome): number => o.attempt ?? 0 + +/** Group outcomes by `uid`; a uid-less outcome becomes its own singleton group + * (preserving flat, one-outcome-per-test evaluation for callers without uids). */ +function groupByTest(outcomes: TestOutcome[]): TestOutcome[][] { + const byUid = new Map<string, TestOutcome[]>() + const groups: TestOutcome[][] = [] + for (const outcome of outcomes) { + if (outcome.uid === undefined) { + groups.push([outcome]) + continue + } + const existing = byUid.get(outcome.uid) + if (existing) { + existing.push(outcome) + } else { + const group = [outcome] + byUid.set(outcome.uid, group) + groups.push(group) + } + } + return groups +} + +/** The highest-numbered attempt's outcome — the test's final result. */ +function finalAttempt(group: TestOutcome[]): TestOutcome { + return group.reduce((best, o) => (attemptOf(o) >= attemptOf(best) ? o : best)) +} + +/** The attempt-0 outcome, if the group recorded one. */ +function firstAttempt(group: TestOutcome[]): TestOutcome | undefined { + return group.find((o) => attemptOf(o) === 0) +} + /** * Warning text when a retention policy is configured outside trace mode, where * it has no effect (the finalizer no-ops in live mode). Returns undefined when diff --git a/packages/core/tests/attempt-tracker.test.ts b/packages/core/tests/attempt-tracker.test.ts index 5d9a44b4..012fbaf7 100644 --- a/packages/core/tests/attempt-tracker.test.ts +++ b/packages/core/tests/attempt-tracker.test.ts @@ -43,4 +43,79 @@ describe('TestAttemptTracker', () => { expect(t.attemptFor('a')).toBeUndefined() expect(t.recordStart('a')).toBe(0) }) + + describe('outcome ledger', () => { + it('records per-attempt outcomes with uid + attempt for a retried test', () => { + const t = new TestAttemptTracker() + t.recordStart('a', 'login.spec.ts') + t.recordOutcome('a', 'failed') + t.recordStart('a', 'login.spec.ts') + t.recordOutcome('a', 'passed') + expect(t.forTest('a')).toEqual([ + { uid: 'a', attempt: 0, state: 'failed' }, + { uid: 'a', attempt: 1, state: 'passed' } + ]) + }) + + it('recordOutcome stamps only the latest attempt slot', () => { + const t = new TestAttemptTracker() + t.recordStart('a') + t.recordOutcome('a', 'passed') + t.recordStart('a') + t.recordOutcome('a', 'failed') + expect(t.forTest('a').map((o) => o.state)).toEqual(['passed', 'failed']) + }) + + it('recordOutcome can override the slot attempt (authoritative retry #)', () => { + const t = new TestAttemptTracker() + t.recordStart('a') + t.recordOutcome('a', 'failed', 3) + expect(t.forTest('a')).toEqual([ + { uid: 'a', attempt: 3, state: 'failed' } + ]) + }) + + it('forTest(uid, attempt) scopes to one attempt (per-slice retention)', () => { + const t = new TestAttemptTracker() + t.recordStart('a') + t.recordOutcome('a', 'failed') + t.recordStart('a') + t.recordOutcome('a', 'passed') + expect(t.forTest('a', 0)).toEqual([ + { uid: 'a', attempt: 0, state: 'failed' } + ]) + expect(t.forTest('a', 1)).toEqual([ + { uid: 'a', attempt: 1, state: 'passed' } + ]) + }) + + it('all() and forSpec() gather attempts across tests', () => { + const t = new TestAttemptTracker() + t.recordStart('a', 'one.spec.ts') + t.recordOutcome('a', 'failed') + t.recordStart('a', 'one.spec.ts') + t.recordOutcome('a', 'passed') + t.recordStart('b', 'two.spec.ts') + t.recordOutcome('b', 'passed') + expect(t.all()).toHaveLength(3) + expect(t.forSpec('one.spec.ts').map((o) => o.uid)).toEqual(['a', 'a']) + expect(t.forSpec('two.spec.ts')).toEqual([ + { uid: 'b', attempt: 0, state: 'passed' } + ]) + }) + + it('recordOutcome is a safe no-op for an unstarted uid', () => { + const t = new TestAttemptTracker() + expect(() => t.recordOutcome('ghost', 'failed')).not.toThrow() + expect(t.forTest('ghost')).toEqual([]) + }) + + it('reset clears the ledger too', () => { + const t = new TestAttemptTracker() + t.recordStart('a') + t.recordOutcome('a', 'failed') + t.reset() + expect(t.all()).toEqual([]) + }) + }) }) diff --git a/packages/core/tests/trace-finalizer.test.ts b/packages/core/tests/trace-finalizer.test.ts index 6d2b5013..a73df5d8 100644 --- a/packages/core/tests/trace-finalizer.test.ts +++ b/packages/core/tests/trace-finalizer.test.ts @@ -7,6 +7,7 @@ import { buildTestSliceFolder, finalizeTraceExport, flushRangeTrace, + TestAttemptTracker, type SpecRange, type TraceArtifact, type TraceCapturer, @@ -374,6 +375,49 @@ describe('finalizeTraceExport', () => { ) }) + // The ledger fix: collapsed testMetadata carries only the final (passed) + // attempt, so a metadata-only feed can't see the failed first attempt. The + // per-attempt ledger supplies both, so the failure policies key correctly. + function failThenPassLedger(): TestAttemptTracker { + const ledger = new TestAttemptTracker() + ledger.recordStart('u1', '/a.js') + ledger.recordOutcome('u1', 'failed') + ledger.recordStart('u1', '/a.js') + ledger.recordOutcome('u1', 'passed') + return ledger + } + const finalPassedMeta = meta([ + ['u1', { title: 'T', specFile: '/a.js', state: 'passed', attempt: 1 }] + ]) + + it('retain-on-first-failure retains a fail-then-pass via the ledger', async () => { + const result = await finalizeTraceExport( + baseCtx({ + policy: 'retain-on-first-failure', + attemptInfoAvailable: true, + outcomes: failThenPassLedger(), + testMetadata: finalPassedMeta + }) + ) + expect(result[0]!.retained).toBe(true) + expect(await exists(path.join(outputDir, 'trace-abcd1234.zip'))).toBe( + true + ) + }) + + it('retain-on-failure does NOT retain a fail-then-pass (final attempt passed)', async () => { + const result = await finalizeTraceExport( + baseCtx({ + policy: 'retain-on-failure', + attemptInfoAvailable: true, + outcomes: failThenPassLedger(), + testMetadata: finalPassedMeta + }) + ) + expect(result[0]!.retained).toBe(false) + expect(result[0]!.path).toBe('') + }) + // Session slice = OR over every test: one failure keeps the whole trace. it('session slice retains when ANY test failed under retain-on-failure', async () => { const result = await finalizeTraceExport( diff --git a/packages/core/tests/trace-retention.test.ts b/packages/core/tests/trace-retention.test.ts index f2e34f82..00982ea2 100644 --- a/packages/core/tests/trace-retention.test.ts +++ b/packages/core/tests/trace-retention.test.ts @@ -216,6 +216,50 @@ describe('shouldRetainTrace unknown policy (fail open)', () => { }) }) +describe('shouldRetainTrace groups a test’s attempts by uid', () => { + // One test, failed then passed on retry. + const failThenPass: TestOutcome[] = [ + { uid: 't', state: 'failed', attempt: 0 }, + { uid: 't', state: 'passed', attempt: 1 } + ] + // One test, passed then failed on retry. + const passThenFail: TestOutcome[] = [ + { uid: 't', state: 'passed', attempt: 0 }, + { uid: 't', state: 'failed', attempt: 1 } + ] + + it('retain-on-failure keys on the FINAL attempt, not any attempt', () => { + // Ends passed → not retained (flat any-failed logic over-retained here). + expect(retain('retain-on-failure', failThenPass)).toBe(false) + // Ends failed → retained. + expect(retain('retain-on-failure', passThenFail)).toBe(true) + }) + + it('retain-on-first-failure keys on attempt 0', () => { + // First attempt failed → retained even though it later passed. + expect(retain('retain-on-first-failure', failThenPass)).toBe(true) + // First attempt passed → not retained. + expect(retain('retain-on-first-failure', passThenFail)).toBe(false) + }) + + it('treats distinct uids as independent groups', () => { + const mixed: TestOutcome[] = [ + { uid: 'a', state: 'failed', attempt: 0 }, + { uid: 'a', state: 'passed', attempt: 1 }, + { uid: 'b', state: 'passed', attempt: 0 } + ] + // a ends passed, b passed → nothing to retain on final-attempt failure. + expect(retain('retain-on-failure', mixed)).toBe(false) + // a's first attempt failed → retained. + expect(retain('retain-on-first-failure', mixed)).toBe(true) + }) + + it('retry-count policies see the grouped attempts', () => { + expect(retain('on-first-retry', failThenPass)).toBe(true) + expect(retain('on-all-retries', passThenFail)).toBe(true) + }) +}) + describe('tracePolicyModeWarning', () => { it('warns when a policy is set outside trace mode', () => { expect(tracePolicyModeWarning('retain-on-failure', 'live')).toMatch( From 80b32f9bfe85766b4c39fceff9bdd5501bc28969 Mon Sep 17 00:00:00 2001 From: Vishnu Vardhan <vishnu.p@browserstack.com> Date: Tue, 14 Jul 2026 17:36:16 +0530 Subject: [PATCH 58/68] fix(service): feed the retention ledger for correct retry-aware policies --- packages/service/src/index.ts | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/packages/service/src/index.ts b/packages/service/src/index.ts index 5b4ee689..95c1edbd 100644 --- a/packages/service/src/index.ts +++ b/packages/service/src/index.ts @@ -185,6 +185,7 @@ export default class DevToolsHookService implements Services.ServiceInstance { capabilities: browser.capabilities, testMetadata: this.#testMetadata, attemptInfoAvailable: true, + outcomes: this.#attemptTracker, ranges: this.#specRanges, flushed: this.#flushedSpecs, resolveOutputDir: () => this.#outputDir, @@ -337,7 +338,7 @@ export default class DevToolsHookService implements Services.ServiceInstance { // ── Test identity for command tagging ── if (uid && scenarioName && featureFile) { this.#currentTestUid = uid - this.#attemptTracker.recordStart(uid) + this.#attemptTracker.recordStart(uid, featureFile) this.#testMetadata.set(uid, { title: scenarioName, specFile: featureFile @@ -361,7 +362,7 @@ export default class DevToolsHookService implements Services.ServiceInstance { if (uid && testTitle) { this.#currentTestUid = uid - this.#attemptTracker.recordStart(uid) + this.#attemptTracker.recordStart(uid, test?.file) this.#testMetadata.set(uid, { title: testTitle, specFile: test?.file ?? '' @@ -385,6 +386,13 @@ export default class DevToolsHookService implements Services.ServiceInstance { const fallback = this.#attemptTracker.attemptFor(uid) ?? 0 const attempt = resolveTestAttempt(result, fallback) stampTestState(this.#testMetadata, uid, result, attempt) + // Feed the per-attempt ledger so session/spec retention sees this attempt's + // real outcome, not just the final state that overwrites #testMetadata. + this.#attemptTracker.recordOutcome( + uid, + this.#testMetadata.get(uid)?.state, + attempt + ) } async afterScenario( From 6a70ca3aef657327183367f1d0b5d4cf617123ba Mon Sep 17 00:00:00 2001 From: Vishnu Vardhan <vishnu.p@browserstack.com> Date: Tue, 14 Jul 2026 17:45:52 +0530 Subject: [PATCH 59/68] fix(core): fall back to metadata when a scoped retention ledger view is empty --- packages/core/src/trace-finalizer.ts | 5 ++++- packages/core/tests/trace-finalizer.test.ts | 21 +++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/packages/core/src/trace-finalizer.ts b/packages/core/src/trace-finalizer.ts index a07286af..d69a2898 100644 --- a/packages/core/src/trace-finalizer.ts +++ b/packages/core/src/trace-finalizer.ts @@ -164,8 +164,11 @@ function shouldRetain( metadata: TestMetadataMap, ledgerOutcomes?: TestOutcome[] ): boolean { + // An empty scoped ledger view (adapter fed outcomes but not for this scope) + // falls back to metadata rather than fail-opening the evaluator into retaining + // everything — only a genuinely empty metadata slice fails open. return shouldRetainTrace(ctx.policy, { - outcomes: ledgerOutcomes ?? toOutcomes(metadata), + outcomes: ledgerOutcomes?.length ? ledgerOutcomes : toOutcomes(metadata), attemptInfoAvailable: ctx.attemptInfoAvailable ?? false }).retain } diff --git a/packages/core/tests/trace-finalizer.test.ts b/packages/core/tests/trace-finalizer.test.ts index a73df5d8..d25e30b8 100644 --- a/packages/core/tests/trace-finalizer.test.ts +++ b/packages/core/tests/trace-finalizer.test.ts @@ -418,6 +418,27 @@ describe('finalizeTraceExport', () => { expect(result[0]!.path).toBe('') }) + it('falls back to metadata when the scoped ledger view is empty', async () => { + // outcomes present but this scope's view is empty (e.g. an adapter that + // didn't feed this scope): must use metadata, not fail-open into retaining + // a passing test. + const emptyView = { all: () => [], forSpec: () => [], forTest: () => [] } + const result = await finalizeTraceExport( + baseCtx({ + policy: 'retain-on-failure', + attemptInfoAvailable: true, + outcomes: emptyView, + testMetadata: meta([ + [ + 'u1', + { title: 'T', specFile: '/a.js', state: 'passed', attempt: 0 } + ] + ]) + }) + ) + expect(result[0]!.retained).toBe(false) + }) + // Session slice = OR over every test: one failure keeps the whole trace. it('session slice retains when ANY test failed under retain-on-failure', async () => { const result = await finalizeTraceExport( From 305c6ea1a42b812bced4aedf0205fffdd3589665 Mon Sep 17 00:00:00 2001 From: Vishnu Vardhan <vishnu.p@browserstack.com> Date: Tue, 14 Jul 2026 19:11:52 +0530 Subject: [PATCH 60/68] fix(core): infer the failed attempt from a retry in the outcome ledger (message given earlier) --- packages/core/src/attempt-tracker.ts | 9 +++++++-- packages/core/tests/attempt-tracker.test.ts | 20 +++++++++++++++++--- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/packages/core/src/attempt-tracker.ts b/packages/core/src/attempt-tracker.ts index 60bbdfa4..6f6f8bc1 100644 --- a/packages/core/src/attempt-tracker.ts +++ b/packages/core/src/attempt-tracker.ts @@ -27,10 +27,15 @@ export class TestAttemptTracker implements RetryOutcomeView { * `specFile` (optional) enables spec-scoped retention lookups. */ recordStart(uid: string, specFile?: string): number { const entry = this.#ledger.get(uid) - const attempt = entry ? entry.attempts.length : 0 - if (attempt > 0) { + if (entry && entry.attempts.length > 0) { this.#sawRetry = true + // A retry only follows a failure. Some runners (e.g. Mocha through a + // --require plugin) can't observe the failed attempt's state from their + // outcome hook and report it as passed — the retry starting IS the + // reliable failure signal, so stamp the prior attempt failed here. + entry.attempts[entry.attempts.length - 1].state = 'failed' } + const attempt = entry ? entry.attempts.length : 0 const slot: TestOutcome = { uid, attempt } if (entry) { entry.attempts.push(slot) diff --git a/packages/core/tests/attempt-tracker.test.ts b/packages/core/tests/attempt-tracker.test.ts index 012fbaf7..845e6f8b 100644 --- a/packages/core/tests/attempt-tracker.test.ts +++ b/packages/core/tests/attempt-tracker.test.ts @@ -57,13 +57,27 @@ describe('TestAttemptTracker', () => { ]) }) - it('recordOutcome stamps only the latest attempt slot', () => { + it('recordOutcome stamps the latest slot', () => { const t = new TestAttemptTracker() t.recordStart('a') t.recordOutcome('a', 'passed') + expect(t.forTest('a')).toEqual([ + { uid: 'a', attempt: 0, state: 'passed' } + ]) + }) + + it('a retry stamps the prior attempt failed (swallowed-failure runners)', () => { + // Mocha via a --require plugin reports the retried attempt as passed; the + // retry starting is the reliable failure signal, so attempt 0 is corrected. + const t = new TestAttemptTracker() t.recordStart('a') - t.recordOutcome('a', 'failed') - expect(t.forTest('a').map((o) => o.state)).toEqual(['passed', 'failed']) + t.recordOutcome('a', 'passed') // outcome hook couldn't see the failure + t.recordStart('a') // retry ⟹ attempt 0 must have failed + t.recordOutcome('a', 'passed') // attempt 1 genuinely passed + expect(t.forTest('a')).toEqual([ + { uid: 'a', attempt: 0, state: 'failed' }, + { uid: 'a', attempt: 1, state: 'passed' } + ]) }) it('recordOutcome can override the slot attempt (authoritative retry #)', () => { From 1e8bf170848f59ac02bcadeea5bb4e4016006643 Mon Sep 17 00:00:00 2001 From: Vishnu Vardhan <vishnu.p@browserstack.com> Date: Tue, 14 Jul 2026 19:12:25 +0530 Subject: [PATCH 61/68] fix(nightwatch): feed the retention ledger for correct retry-aware policies --- .../src/cucumber-lifecycle.ts | 8 ++- .../src/helpers/cucumberScenarioBuilder.ts | 8 +-- .../src/helpers/testManager.ts | 7 ++- packages/nightwatch-devtools/src/index.ts | 6 +- .../src/plugin-internals.ts | 10 +++- .../nightwatch-devtools/src/session-init.ts | 8 ++- .../nightwatch-devtools/src/test-lifecycle.ts | 4 +- .../nightwatch-devtools/src/trace-context.ts | 3 + .../tests/attemptTracking.test.ts | 55 ++++++++++++++++++- 9 files changed, 93 insertions(+), 16 deletions(-) diff --git a/packages/nightwatch-devtools/src/cucumber-lifecycle.ts b/packages/nightwatch-devtools/src/cucumber-lifecycle.ts index 44197c0e..46684570 100644 --- a/packages/nightwatch-devtools/src/cucumber-lifecycle.ts +++ b/packages/nightwatch-devtools/src/cucumber-lifecycle.ts @@ -61,7 +61,8 @@ export interface CucumberLifecycleCtx extends TestSliceCtx { setCurrentStep(s: unknown): void getCurrentStep(): unknown setCurrentTest(t: unknown): void - recordAttempt(uid: string): number + recordAttempt(uid: string, specFile?: string): number + recordOutcome(uid: string, state: TestStats['state']): void } type MutStep = { @@ -182,7 +183,7 @@ export async function initCucumberScenario( stepKeywords, scenarioLine, parentFeatureSuiteUid: featureSuite.uid, - recordAttempt: (uid) => ctx.recordAttempt(uid) + recordAttempt: (uid, specFile) => ctx.recordAttempt(uid, specFile) }) attachScenarioToFeature(ctx, featureSuite, scenarioSuite) // The scenario is the `test` unit; its steps are the leaf metadata entries. @@ -211,6 +212,9 @@ export async function finalizeCucumberScenario( scenario.state = scenarioState scenario.end = now scenario._duration = duration + // Stamp this attempt's real outcome so spec/session retention doesn't + // collapse to the retry-stable suite's last-overwritten state. + ctx.recordOutcome(scenario.uid, scenarioState) closeOpenSteps(scenario, scenarioState, now) const featureUri: string = pickle?.uri ?? 'unknown.feature' diff --git a/packages/nightwatch-devtools/src/helpers/cucumberScenarioBuilder.ts b/packages/nightwatch-devtools/src/helpers/cucumberScenarioBuilder.ts index b60d5ad4..af617f91 100644 --- a/packages/nightwatch-devtools/src/helpers/cucumberScenarioBuilder.ts +++ b/packages/nightwatch-devtools/src/helpers/cucumberScenarioBuilder.ts @@ -21,9 +21,9 @@ export interface CucumberScenarioBuildInput { scenarioLine: number /** Parent feature-suite uid — scenarios nest under this. */ parentFeatureSuiteUid: string - /** Records the scenario start under its (retry-stable) uid and returns the - * 0-based attempt number stamped on every step. Omitted → attempt 0. */ - recordAttempt?: (scenarioUid: string) => number + /** Records the scenario start under its (retry-stable) uid + feature file and + * returns the 0-based attempt number stamped on every step. Omitted → 0. */ + recordAttempt?: (scenarioUid: string, specFile?: string) => number } /** @@ -101,7 +101,7 @@ export function buildCucumberScenarioSuite( featureUri, `scenario:${scenarioName}:${scenarioLine}` ) - const attempt = input.recordAttempt?.(scenarioUid) ?? 0 + const attempt = input.recordAttempt?.(scenarioUid, featureUri) ?? 0 const scenarioSuite: SuiteStats = { uid: scenarioUid, cid: DEFAULTS.CID, diff --git a/packages/nightwatch-devtools/src/helpers/testManager.ts b/packages/nightwatch-devtools/src/helpers/testManager.ts index 0edc5ead..2b98c5d3 100644 --- a/packages/nightwatch-devtools/src/helpers/testManager.ts +++ b/packages/nightwatch-devtools/src/helpers/testManager.ts @@ -13,7 +13,11 @@ export class TestManager { private processedTests = new Map<string, Set<string>>() private lastKnownTestName: string | null = null - constructor(private testReporter: TestReporter) {} + constructor( + private testReporter: TestReporter, + /** Stamps a test's resolved terminal state onto the retry ledger. */ + private recordOutcome?: (uid: string, state: TestStats['state']) => void + ) {} /** * Update test state and report to UI @@ -42,6 +46,7 @@ export class TestManager { if (state !== TEST_STATE.RUNNING) { this.testReporter.onTestEnd(test) + this.recordOutcome?.(test.uid, state) } } diff --git a/packages/nightwatch-devtools/src/index.ts b/packages/nightwatch-devtools/src/index.ts index 53b1a3dd..a4084321 100644 --- a/packages/nightwatch-devtools/src/index.ts +++ b/packages/nightwatch-devtools/src/index.ts @@ -299,7 +299,10 @@ class NightwatchDevToolsPlugin { self.suiteManager.clearExecutionData() self.#attemptTracker.reset() }, - recordAttempt: (uid) => self.#attemptTracker.recordStart(uid), + recordAttempt: (uid, specFile) => + self.#attemptTracker.recordStart(uid, specFile), + recordOutcome: (uid, state) => + self.#attemptTracker.recordOutcome(uid, state), attemptFor: (uid) => self.#attemptTracker.attemptFor(uid), buildMetadataOptions: () => self.#buildMetadataOptions(), ensureSessionInitialized: (b) => self.#ensureSessionInitialized(b), @@ -586,6 +589,7 @@ class NightwatchDevToolsPlugin { format: this.options.traceFormat, capturer: this.sessionCapturer, suites: this.suiteManager.getAllSuites().values(), + outcomes: this.#attemptTracker, ranges: this.#specRanges, flushed: this.#flushedSpecs, configPath: this.#configPath, diff --git a/packages/nightwatch-devtools/src/plugin-internals.ts b/packages/nightwatch-devtools/src/plugin-internals.ts index 1753d09c..4279d360 100644 --- a/packages/nightwatch-devtools/src/plugin-internals.ts +++ b/packages/nightwatch-devtools/src/plugin-internals.ts @@ -77,9 +77,13 @@ export interface PluginInternals { setCucumberRunner(v: boolean): void getRerunLabel(): string | undefined - /** Records a test/scenario start under its retry-stable uid; returns the - * 0-based attempt number (0 first run, +1 per rerun). */ - recordAttempt(uid: string): number + /** Records a test/scenario start under its retry-stable uid; `specFile` + * (when known) enables spec-scoped retention. Returns the 0-based attempt + * number (0 first run, +1 per rerun). */ + recordAttempt(uid: string, specFile?: string): number + /** Stamps the resolved terminal state onto uid's latest attempt slot so the + * retry-aware retention policies see real per-attempt outcomes. */ + recordOutcome(uid: string, state: TestStats['state']): void /** Latest attempt recorded for `uid`, or undefined if it never started. */ attemptFor(uid: string): number | undefined diff --git a/packages/nightwatch-devtools/src/session-init.ts b/packages/nightwatch-devtools/src/session-init.ts index e9d1004a..8e485a21 100644 --- a/packages/nightwatch-devtools/src/session-init.ts +++ b/packages/nightwatch-devtools/src/session-init.ts @@ -28,7 +28,8 @@ import type { DevToolsMode, NightwatchBrowser, ScreencastOptions, - SuiteStats + SuiteStats, + TestStats } from './types.js' const log = logger('@wdio/nightwatch-devtools:session-init') @@ -59,6 +60,7 @@ export interface SessionInitCtx { getCurrentScenarioSuite(): SuiteStats | null buildMetadataOptions(): unknown attemptFor(uid: string): number | undefined + recordOutcome(uid: string, state: TestStats['state']): void } async function handleSessionChange( @@ -94,7 +96,9 @@ function initReporterChain(ctx: SessionInitCtx): void { }, (uid) => ctx.attemptFor(uid) ) - ctx.testManager = new TestManager(ctx.testReporter) + ctx.testManager = new TestManager(ctx.testReporter, (uid, state) => + ctx.recordOutcome(uid, state) + ) ctx.suiteManager = new SuiteManager(ctx.testReporter) ctx.browserProxy = new BrowserProxy( ctx.sessionCapturer, diff --git a/packages/nightwatch-devtools/src/test-lifecycle.ts b/packages/nightwatch-devtools/src/test-lifecycle.ts index 7192c12a..e8e0c60d 100644 --- a/packages/nightwatch-devtools/src/test-lifecycle.ts +++ b/packages/nightwatch-devtools/src/test-lifecycle.ts @@ -40,7 +40,7 @@ export interface TestLifecycleCtx extends TestSliceCtx { incrementCount(state: TestStats['state']): void testIcon(state: TestStats['state']): string setCurrentTest(t: unknown): void - recordAttempt(uid: string): number + recordAttempt(uid: string, specFile?: string): number } interface SuiteMetadata { @@ -126,7 +126,7 @@ export async function startNextTest( const test = ctx.testManager.findTestInSuite(currentSuite, currentTestName) if (test) { // Nightwatch has no per-test retry index; the tracker is the retry signal. - test.retries = ctx.recordAttempt(test.uid) + test.retries = ctx.recordAttempt(test.uid, specFile ?? undefined) if (specFile) { recordTestSliceBoundary(ctx, specFile, test.uid) } diff --git a/packages/nightwatch-devtools/src/trace-context.ts b/packages/nightwatch-devtools/src/trace-context.ts index 359737b9..bf217fb2 100644 --- a/packages/nightwatch-devtools/src/trace-context.ts +++ b/packages/nightwatch-devtools/src/trace-context.ts @@ -9,6 +9,7 @@ import { collectSuiteTestMetadata, resolveAdapterOutputDir, + type RetryOutcomeView, type SpecRange, type TraceExportContext } from '@wdio/devtools-core' @@ -28,6 +29,7 @@ export interface TraceContextInput { format: TraceFormat capturer: SessionCapturer suites: Iterable<SuiteStats> + outcomes?: RetryOutcomeView ranges: SpecRange[] flushed: Set<string> configPath: string | undefined @@ -48,6 +50,7 @@ export function buildTraceContext( actionSnapshots: input.capturer.actionSnapshots, sessionId, testMetadata: collectSuiteTestMetadata(input.suites), + outcomes: input.outcomes, ranges: input.ranges, flushed: input.flushed, resolveOutputDir: () => diff --git a/packages/nightwatch-devtools/tests/attemptTracking.test.ts b/packages/nightwatch-devtools/tests/attemptTracking.test.ts index 30df38e2..7d5eacd3 100644 --- a/packages/nightwatch-devtools/tests/attemptTracking.test.ts +++ b/packages/nightwatch-devtools/tests/attemptTracking.test.ts @@ -10,7 +10,9 @@ import { TEST_STATE } from '../src/constants.js' import type { SessionCapturer } from '../src/session.js' import type { SuiteStats, TestStats } from '../src/types.js' -function scenarioInput(recordAttempt: (uid: string) => number) { +function scenarioInput( + recordAttempt: (uid: string, specFile?: string) => number +) { return { featureUri: 'login.feature', scenarioName: 'Valid login', @@ -71,6 +73,25 @@ describe('cucumber scenario retry attempt', () => { true ) }) + + it('feeds the ledger per attempt with specFile so forSpec sees real outcomes', () => { + const tracker = new TestAttemptTracker() + const record = (uid: string, specFile?: string) => + tracker.recordStart(uid, specFile) + + // Attempt 0 fails, then the retry (same uid) passes — each attempt's real + // outcome must survive, not collapse to the retry-stable suite's last state. + const first = buildCucumberScenarioSuite(scenarioInput(record)) + tracker.recordOutcome(first.uid, TEST_STATE.FAILED as TestStats['state']) + const retried = buildCucumberScenarioSuite(scenarioInput(record)) + tracker.recordOutcome(retried.uid, TEST_STATE.PASSED as TestStats['state']) + + expect(first.uid).toBe(retried.uid) + expect(tracker.forSpec('login.feature')).toEqual([ + { uid: first.uid, attempt: 0, state: 'failed' }, + { uid: first.uid, attempt: 1, state: 'passed' } + ]) + }) }) describe('regular test retry attempt via startNextTest', () => { @@ -131,4 +152,36 @@ describe('buildTraceContext', () => { expect(ctx.sessionId).toBe('session-1') expect(ctx.testMetadata.get('t1')?.attempt).toBe(2) }) + + it('exposes the attempt-outcome ledger on the trace context', () => { + const capturer = { + actionSnapshots: [], + snapshotCaptures: [] + } as unknown as SessionCapturer + const tracker = new TestAttemptTracker() + tracker.recordStart('t1', '/spec.ts') + tracker.recordOutcome('t1', TEST_STATE.FAILED as TestStats['state']) + + const ctx = buildTraceContext( + { + mode: 'trace', + policy: 'retain-on-failure', + granularity: 'spec', + format: 'zip', + capturer, + suites: [], + outcomes: tracker, + ranges: [], + flushed: new Set(), + configPath: undefined, + log: () => {} + }, + 'session-1' + ) + + expect(ctx.outcomes).toBe(tracker) + expect(ctx.outcomes?.forSpec('/spec.ts')).toEqual([ + { uid: 't1', attempt: 0, state: 'failed' } + ]) + }) }) From 8542175220930c4b9357de8c398820149a623e36 Mon Sep 17 00:00:00 2001 From: Vishnu Vardhan <vishnu.p@browserstack.com> Date: Tue, 14 Jul 2026 19:12:42 +0530 Subject: [PATCH 62/68] fix(selenium): feed the retention ledger for correct retry-aware policies --- .../src/helpers/testManager.ts | 33 +++++++- .../src/session-lifecycle.ts | 5 ++ .../tests/attempt-capture.test.ts | 81 ++++++++++++++++++- 3 files changed, 115 insertions(+), 4 deletions(-) diff --git a/packages/selenium-devtools/src/helpers/testManager.ts b/packages/selenium-devtools/src/helpers/testManager.ts index 70fcb14b..09c741c7 100644 --- a/packages/selenium-devtools/src/helpers/testManager.ts +++ b/packages/selenium-devtools/src/helpers/testManager.ts @@ -2,7 +2,8 @@ import logger from '@wdio/logger' import { createTestStats, stampTestEnd, - TestAttemptTracker + TestAttemptTracker, + type RetryOutcomeView } from '@wdio/devtools-core' import { DEFAULTS, TEST_STATE } from '../constants.js' import type { SuiteStats, TestStats } from '../types.js' @@ -23,9 +24,14 @@ export class TestManager { #currentTest: TestStats | null = null #lastMarkedTest: TestStats | null = null #mode: 'session' | 'marked' = 'session' - // Per-test attempt counter. Persists for the whole in-process session so + // Per-test attempt ledger. Persists for the whole in-process session so // same-process retries (Mocha/Jest/etc re-entering the start hook) accumulate. + // Retry-stable-keyed so a test's attempts group under one entry; the trace + // finalizer reads it (attemptOutcomes) for retry-aware retention. #attemptTracker = new TestAttemptTracker() + // Retry-stable uid of the in-progress marked test, so endCurrent can stamp + // this attempt's outcome onto the same ledger entry recordStart opened. + #currentRetryStableUid: string | undefined /** Set true the first time the user calls startMarkedTest. Once true we * never auto-create the synthetic session test — orphan commands attach * to the most-recently-marked test instead. */ @@ -134,9 +140,12 @@ export class TestManager { // deterministicUid is retry-stable (no counter), so re-entering with the // same logical test increments the heuristic. Mocha supplies an // authoritative per-test retry index via opts.attempt; prefer it. + const retryStableUid = deterministicUid(file, signature) const heuristicAttempt = this.#attemptTracker.recordStart( - deterministicUid(file, signature) + retryStableUid, + file ) + this.#currentRetryStableUid = retryStableUid test.retries = opts.attempt ?? heuristicAttempt log.info( `Started marked test "${name}" (callSource: ${opts.callSource || 'n/a'})` @@ -155,6 +164,18 @@ export class TestManager { } test.state = state stampTestEnd(test) + // Stamp this attempt's real outcome onto its retry-stable ledger slot so a + // fail-then-pass groups under one test: retain-on-failure keys on the final + // (passed) attempt and stops over-retaining, retain-on-first-failure on + // attempt 0. Skipped for the synthetic session test (no recordStart). + if (this.#currentRetryStableUid !== undefined) { + this.#attemptTracker.recordOutcome( + this.#currentRetryStableUid, + state, + test.retries + ) + this.#currentRetryStableUid = undefined + } this.testReporter.onTestEnd(test) this.#currentTest = null } @@ -163,6 +184,12 @@ export class TestManager { return this.#currentTest } + /** Read-only per-attempt outcome ledger for the trace finalizer's retry-aware + * retention evaluation (retry-stable-keyed, so a test's attempts group). */ + get attemptOutcomes(): RetryOutcomeView { + return this.#attemptTracker + } + /** Called when the driver session is closing (process exit / quit). */ finalizeSession(): void { if (this.#currentTest) { diff --git a/packages/selenium-devtools/src/session-lifecycle.ts b/packages/selenium-devtools/src/session-lifecycle.ts index 7716210b..ca4bab76 100644 --- a/packages/selenium-devtools/src/session-lifecycle.ts +++ b/packages/selenium-devtools/src/session-lifecycle.ts @@ -315,6 +315,11 @@ export function buildTraceExportContext( // TestStats.retries carries the per-test attempt (Mocha authoritative, // other runners heuristic), so retry-aware policies can trust it. attemptInfoAvailable: true, + // Per-attempt outcome ledger, retry-stable-keyed so a test's attempts + // group as one test. Without it, session/spec retention reads the + // per-attempt suite-node metadata — which sees a fail-then-pass as two + // separate one-shot tests and over-retains it under retain-on-failure. + outcomes: ctx.testManager?.attemptOutcomes, ranges: ctx.specRanges, flushed: ctx.flushedSpecs, resolveOutputDir: (range) => diff --git a/packages/selenium-devtools/tests/attempt-capture.test.ts b/packages/selenium-devtools/tests/attempt-capture.test.ts index 2f2840ff..173acfc8 100644 --- a/packages/selenium-devtools/tests/attempt-capture.test.ts +++ b/packages/selenium-devtools/tests/attempt-capture.test.ts @@ -1,5 +1,8 @@ import { describe, it, expect, vi } from 'vitest' -import { collectSuiteTestMetadata } from '@wdio/devtools-core' +import { + collectSuiteTestMetadata, + shouldRetainTrace +} from '@wdio/devtools-core' import { resetSignatureCounters } from '../src/helpers/utils.js' import { TestManager } from '../src/helpers/testManager.js' @@ -94,3 +97,79 @@ describe('retry/attempt capture', () => { } }) }) + +describe('retry outcome ledger feeds retry-aware retention', () => { + it('groups a fail-then-pass retry under one retry-stable uid', () => { + const { mgr } = makeManager() + + const first = mgr.startMarkedTest('flaky') + mgr.endCurrent('failed') + const retry = mgr.startMarkedTest('flaky') + mgr.endCurrent('passed') + + // Selenium gives each attempt its own suite-node uid… + expect(first.uid).not.toBe(retry.uid) + + // …but the ledger records both attempts under ONE retry-stable uid. + const ledger = mgr.attemptOutcomes.all() + expect(ledger).toHaveLength(2) + expect(ledger[0]).toMatchObject({ attempt: 0, state: 'failed' }) + expect(ledger[1]).toMatchObject({ attempt: 1, state: 'passed' }) + expect(ledger[0].uid).toBe(ledger[1].uid) + }) + + it('exposes outcomes on the ctx so retain-on-failure drops a fail-then-pass but retain-on-first-failure keeps it', () => { + const { mgr, suiteManager } = makeManager() + + mgr.startMarkedTest('flaky') + mgr.endCurrent('failed') + mgr.startMarkedTest('flaky') + mgr.endCurrent('passed') + + const capturer = new SessionCapturer() + try { + const ctx = { + options: { + mode: 'trace', + traceGranularity: 'session', + traceFormat: 'zip' + }, + testManager: mgr, + suiteManager, + actionSnapshots: [], + specRanges: [], + flushedSpecs: new Set<string>(), + traceFlushes: [], + snapshotCaptures: [] + } as unknown as SessionLifecycleCtx + + const traceCtx = buildTraceExportContext( + ctx, + capturer, + 'sess-1', + '/spec.ts' + ) + expect(traceCtx.outcomes).toBe(mgr.attemptOutcomes) + + const outcomes = [...traceCtx.outcomes!.all()] + const retainOnFailure = shouldRetainTrace('retain-on-failure', { + outcomes, + attemptInfoAvailable: true + }) + const retainOnFirstFailure = shouldRetainTrace( + 'retain-on-first-failure', + { + outcomes, + attemptInfoAvailable: true + } + ) + + // Final attempt passed → retain-on-failure drops it (no over-retention). + expect(retainOnFailure.retain).toBe(false) + // Attempt 0 failed → retain-on-first-failure still keeps it. + expect(retainOnFirstFailure.retain).toBe(true) + } finally { + capturer.cleanup() + } + }) +}) From 941ab026de0ea45e0d12aa866986693f6cd484da Mon Sep 17 00:00:00 2001 From: Vishnu Vardhan <vishnu.p@browserstack.com> Date: Tue, 14 Jul 2026 19:12:57 +0530 Subject: [PATCH 63/68] docs: retry-aware retention scope + assert-capture debt sync --- CLAUDE.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/CLAUDE.md b/CLAUDE.md index 007cc486..59009622 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -235,7 +235,10 @@ Documented divergences from the conventions above. They exist today as debt to b - `replaceCommand` has two semantics — Selenium mutates in place (preserves `_id`/`id` for chained calls); Nightwatch splices and reissues. Both call the same `core/suite-helpers` factories; the storage strategy stays adapter-specific because runner integrations differ. Could be unified by parameterizing the policy if the divergence ever causes a real problem. - `patchNodeAssert` (via `core/assert-patcher`) is now wired in all three adapters, default-on behind each adapter's `captureAssertions` option (opt out with `captureAssertions: false`). Framework matcher libraries differ: Service taps expect-webdriverio's `beforeAssertion`/`afterAssertion` hooks so passing+failing matchers render as `expect.*` actions (mechanism in the assert-capture entry below); Nightwatch native `assert`/`verify` and Selenium's `node:assert` also surface passing+failing rows via their reconcile/patch paths. The remaining gap is Selenium's jest-style `expect()` (and chai): jest/vitest expose no pass+fail assertion hook, so only failing matchers surface there. - BiDi is auto-attached in Service and Selenium; Nightwatch is opt-in via `bidi: true` and requires `webSocketUrl: true` in capabilities. -- Retry-aware trace policies aren't uniform across adapters. Selenium emits one metadata entry per attempt (counter-based `generateStableUid` uid + retry-stable tracker key), so all six policies — including `retain-on-first-failure` (needs the first attempt's outcome) — evaluate correctly. Service and Nightwatch key metadata on the retry-stable uid and overwrite/mutate per attempt, collapsing to the *final* attempt's outcome; the retry-count policies (`retain-on-failure`, `on-first-retry`, `on-all-retries`, `retain-on-failure-and-retries`) are still correct, but `retain-on-first-failure` can't see a failed-then-passed first attempt and under-retains. Proper fix rides with B5 (per-test granularity preserves per-attempt slices); until then all three set `attemptInfoAvailable: true`. +- Retry-aware trace policies share one mechanism: adapters feed a per-attempt **outcome ledger** (`core/attempt-tracker.ts` `TestAttemptTracker.recordStart(uid, specFile)` + `recordOutcome(uid, state, attempt)`) keyed by the **retry-stable** uid, and the finalizer reads the scoped views (`all`/`forSpec`/`forTest`) so `trace-retention.ts` evaluates **group-by-test** — `retain-on-failure` keys on each test's *final* attempt (no over-retaining a fail-then-pass), `retain-on-first-failure` on *attempt 0*. `recordStart` on a second attempt stamps the prior attempt `failed` (a retry only follows a failure), which corrects runners that swallow the intermediate failure — e.g. Mocha via a `--require` plugin never surfaces the retried attempt's failure, so the ledger would otherwise see `[passed, passed]`. An empty scoped view falls back to `testMetadata` (never fail-open-retains). + - **WDIO + Selenium: verified end-to-end** (manual fail-then-pass runs: retained under `retain-on-first-failure`, dropped under `retain-on-failure`). + - **Nightwatch: `retain-on-failure` works; the other retry-aware policies degrade.** Its `--retries` re-runs the testcase *internally* without re-firing the plugin's per-test hooks, and the per-testcase results carry no attempt/retry field (retries live only in undocumented, version-varying Nightwatch internals — `suiteRetries.testRetriesCount` / `reporter.testResults.retryTest`), so the ledger sees only the final attempt for the `describe/it` and exports-object interfaces. Cucumber scenarios expose per-scenario hooks, so the feed captures their attempts. Not cleanly fixable without depending on those internals. + - WDIO `specFileRetries` spawns a fresh worker per retry, so cross-process attempts aren't in the (process-scoped) ledger. - Eager per-test trace slice (Nightwatch + Selenium) can drop an action snapshot whose fire-and-forget capture hasn't resolved by `afterEach` / scenario end — the slice is written from whatever snapshots exist at flush time. The WDIO service is immune because it awaits each snapshot inline before flushing. - Nightwatch native asserts are buffered at call time and emitted in one batch at test-end (Nightwatch exposes no per-assertion execution/outcome hook reachable from the plugin — `client.queue.tree` / `client.reporter` aren't on `browser`). Streaming mid-run would flash every assert green before its outcome is known, so rows are held and reconciled against `results.testcases[*].assertions` at `afterEach` (the flat `results.assertions` only reflects the last testcase). The exporter re-sorts commands by timestamp (`buildActionEvents`) so the batched rows land at their real timeline positions rather than clustering at flush time. `actual` is parsed from the failure message (`but got: …`, failures only) into a collapsed `{passed, expected, actual}` result; `expected` is the assertion arg — the label still mirrors the call args, not the derived values. - Nightwatch's BDD `describe/it` interface fires the plugin's global `beforeEach`/`afterEach` **once per module** (with an empty `currentTest.name`), not per `it` — Nightwatch runs the individual `it`s internally with no per-testcase hook or event reachable from the plugin (the lib emits only transport-level events). So `traceGranularity:'test'` records a single slice boundary (the first test) and collapses to one session-scoped slice; retry-aware/per-test retention (`tracePolicy`) degrades to session scope for this interface. Per-test slicing works for adapters/styles that expose per-test hooks (WDIO mocha/cucumber, Selenium mocha; likely Nightwatch's exports-object style, unverified). Session-granularity trace is unaffected. From 7c18690c174a333f18b2f6ad603361b8805e32fd Mon Sep 17 00:00:00 2001 From: Vishnu Vardhan <vishnu.p@browserstack.com> Date: Wed, 15 Jul 2026 02:16:32 +0530 Subject: [PATCH 64/68] feat(core): artifacts manifest + WDIO Allure trace/video attach --- packages/core/src/artifacts-manifest.ts | 81 ++++++++++++++ packages/core/src/index.ts | 1 + packages/core/src/trace-finalizer.ts | 67 +++++++++++- .../core/tests/artifacts-manifest.test.ts | 103 ++++++++++++++++++ packages/core/tests/trace-finalizer.test.ts | 64 +++++++++++ packages/nightwatch-devtools/src/index.ts | 16 ++- .../nightwatch-devtools/src/trace-context.ts | 8 +- .../tests/attemptTracking.test.ts | 4 + packages/selenium-devtools/src/index.ts | 9 +- .../selenium-devtools/src/plugin-internals.ts | 4 +- .../src/session-lifecycle.ts | 8 +- packages/service/package.json | 6 + packages/service/src/allure.ts | 90 +++++++++++++++ packages/service/src/index.ts | 40 +++++-- packages/service/src/trace-slices.ts | 10 +- packages/service/tests/allure.test.ts | 91 ++++++++++++++++ 16 files changed, 578 insertions(+), 24 deletions(-) create mode 100644 packages/core/src/artifacts-manifest.ts create mode 100644 packages/core/tests/artifacts-manifest.test.ts create mode 100644 packages/service/src/allure.ts create mode 100644 packages/service/tests/allure.test.ts diff --git a/packages/core/src/artifacts-manifest.ts b/packages/core/src/artifacts-manifest.ts new file mode 100644 index 00000000..0853e401 --- /dev/null +++ b/packages/core/src/artifacts-manifest.ts @@ -0,0 +1,81 @@ +/** + * The generic artifacts manifest: a single JSON side-file + * (`devtools-artifacts-<sessionId>.json`) written next to the trace/video + * artifacts at end-of-run. It enumerates every artifact a trace-mode session + * produced — including the ones a retention policy decided against + * (`retained: false`) — alongside each test's final state and attempt. Any + * ecosystem reporter (Allure, a CI collector, a custom dashboard) can read this + * one file to discover what to attach and how each test fared, instead of + * re-deriving it from framework internals. The WDIO Allure glue consumes it + * directly; Selenium/Nightwatch document reader recipes against it. + */ + +import fs from 'node:fs/promises' +import path from 'node:path' +import type { + TestMetadataEntry, + TestMetadataMap, + TraceFormat +} from '@wdio/devtools-shared' +import type { TraceArtifact } from './trace-finalizer.js' + +/** One test's outcome as recorded at export time. Field types track + * TestMetadataEntry in shared; adds the keying uid, drops trace-only ancestry. */ +export interface ArtifactManifestTest extends Pick< + TestMetadataEntry, + 'title' | 'specFile' | 'state' | 'attempt' +> { + uid: string +} + +export interface ArtifactsManifest { + sessionId: string + format: TraceFormat + artifacts: TraceArtifact[] + tests: ArtifactManifestTest[] +} + +/** Deterministic manifest filename for a session (used by writers and readers). */ +export function artifactsManifestFilename(sessionId: string): string { + return `devtools-artifacts-${sessionId}.json` +} + +/** Assemble the manifest from the artifacts collected across the run and the + * test-metadata map. Pure — the write is separate so it stays unit-testable. */ +export function buildArtifactsManifest(input: { + sessionId: string + format: TraceFormat + artifacts: readonly TraceArtifact[] + testMetadata: TestMetadataMap +}): ArtifactsManifest { + const tests: ArtifactManifestTest[] = [] + for (const [uid, meta] of input.testMetadata) { + tests.push({ + uid, + title: meta.title, + specFile: meta.specFile, + state: meta.state, + attempt: meta.attempt + }) + } + return { + sessionId: input.sessionId, + format: input.format, + artifacts: [...input.artifacts], + tests + } +} + +/** Write the manifest to `outputDir`, returning its absolute path. */ +export async function writeArtifactsManifest( + outputDir: string, + manifest: ArtifactsManifest +): Promise<string> { + await fs.mkdir(outputDir, { recursive: true }) + const filePath = path.join( + outputDir, + artifactsManifestFilename(manifest.sessionId) + ) + await fs.writeFile(filePath, JSON.stringify(manifest, null, 2), 'utf8') + return filePath +} diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index 89c6c301..9d37bbf0 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -3,6 +3,7 @@ export * from './action-mapping.js' export * from './action-snapshot.js' +export * from './artifacts-manifest.js' export * from './attempt-tracker.js' export * from './with-timeout.js' export * from './assert-patcher.js' diff --git a/packages/core/src/trace-finalizer.ts b/packages/core/src/trace-finalizer.ts index d69a2898..9cb57af2 100644 --- a/packages/core/src/trace-finalizer.ts +++ b/packages/core/src/trace-finalizer.ts @@ -15,6 +15,10 @@ import type { TraceRetentionPolicy } from '@wdio/devtools-shared' import { errorMessage } from './error.js' +import { + buildArtifactsManifest, + writeArtifactsManifest +} from './artifacts-manifest.js' import { filterTestMetadataBySpec, filterTestMetadataByUid, @@ -72,6 +76,15 @@ export interface TraceExportContext { awaitPending?: Promise<unknown>[] log?: (level: 'info' | 'warn', msg: string) => void onArtifact?: (a: TraceArtifact) => void + /** When true, finalize writes a `devtools-artifacts-<sessionId>.json` manifest + * enumerating every artifact (retained or not) plus per-test states, for + * ecosystem reporters (Allure, CI collectors) to consume. */ + emitManifest?: boolean + /** Every artifact seen via `onArtifact` across the run (including eager + * mid-run test-slice flushes, which the end-of-run fan-out dedupes away). + * Pass the same list the adapter accumulates so the manifest is complete; + * finalize falls back to its own fan-out results when this is omitted. */ + collectedArtifacts?: readonly TraceArtifact[] } const SPEC_WITHOUT_BOUNDARIES_WARNING = @@ -336,10 +349,11 @@ async function flushAllRanges( } /** - * Entry point for the after/end-of-run hook. No-op outside trace mode. Awaits - * any pending snapshot captures, then fans out to per-spec, per-test, or - * session writes. `spec`/`test` granularity with no recorded boundaries warns - * and falls back to a single session-level trace. + * Entry point for the after/end-of-run hook. No-op outside trace mode. Settles + * in-flight captures and eager flushes, then fans out to per-spec, per-test, or + * session writes and finally the artifacts manifest. `spec`/`test` granularity + * with no recorded boundaries warns and falls back to a single session-level + * trace. */ export async function finalizeTraceExport( ctx: TraceExportContext @@ -347,7 +361,22 @@ export async function finalizeTraceExport( if (ctx.mode !== 'trace') { return [] } + // Settle in-flight snapshot captures AND the adapters' tracked fire-and-forget + // eager flushes (Selenium/Nightwatch put both in awaitPending) before writing + // anything: every eager-flushed range is a synchronous dedupe-hit in the + // fan-out below, so this is the one place that awaits those eager-flush + // promises. Without it the last eager write can race teardown and its artifact + // can miss the manifest. A no-op for WDIO (awaits eager flushes inline). await awaitPendingCaptures(ctx) + const artifacts = await fanOutTraceWrites(ctx) + await maybeWriteManifest(ctx, artifacts) + return artifacts +} + +/** Session vs per-slice fan-out (with the no-boundaries fallback). */ +async function fanOutTraceWrites( + ctx: TraceExportContext +): Promise<TraceArtifact[]> { const sliced = ctx.granularity === 'spec' || ctx.granularity === 'test' if (sliced && ctx.ranges.length > 0) { if (ctx.ranges.length > SLICE_COUNT_WARN_THRESHOLD) { @@ -363,3 +392,33 @@ export async function finalizeTraceExport( const artifact = await safely(ctx, () => writeSessionTrace(ctx)) return artifact ? [artifact] : [] } + +/** Write the artifacts manifest when enabled, preferring the adapter's full + * collected list (includes eager slices) over this pass's fan-out results. + * Never throws — a manifest failure must not abort the run. */ +async function maybeWriteManifest( + ctx: TraceExportContext, + fanOutArtifacts: readonly TraceArtifact[] +): Promise<void> { + if (!ctx.emitManifest) { + return + } + const artifacts = ctx.collectedArtifacts ?? fanOutArtifacts + try { + const path = await writeArtifactsManifest( + ctx.resolveOutputDir(), + buildArtifactsManifest({ + sessionId: ctx.sessionId, + format: ctx.format ?? 'zip', + artifacts, + testMetadata: ctx.testMetadata + }) + ) + ctx.log?.('info', `Artifacts manifest written to ${path}`) + } catch (err) { + ctx.log?.( + 'warn', + `Failed to write artifacts manifest: ${errorMessage(err)}` + ) + } +} diff --git a/packages/core/tests/artifacts-manifest.test.ts b/packages/core/tests/artifacts-manifest.test.ts new file mode 100644 index 00000000..0c7e969b --- /dev/null +++ b/packages/core/tests/artifacts-manifest.test.ts @@ -0,0 +1,103 @@ +import { describe, it, expect, afterEach } from 'vitest' +import { rm, readFile, mkdtemp } from 'node:fs/promises' +import { tmpdir } from 'node:os' +import { join } from 'node:path' +import { + buildArtifactsManifest, + writeArtifactsManifest, + artifactsManifestFilename, + type ArtifactsManifest +} from '../src/artifacts-manifest.js' +import type { TraceArtifact } from '../src/trace-finalizer.js' +import type { TestMetadataMap } from '@wdio/devtools-shared' + +function metadata(): TestMetadataMap { + return new Map([ + [ + 'u1', + { title: 'passes', specFile: '/a.spec.ts', state: 'passed', attempt: 0 } + ], + [ + 'u2', + { title: 'fails', specFile: '/a.spec.ts', state: 'failed', attempt: 1 } + ] + ]) +} + +const artifacts: TraceArtifact[] = [ + { + kind: 'trace', + path: '/out/u1.zip', + scope: 'test', + key: 'u1', + testUids: ['u1'], + retained: false + }, + { + kind: 'trace', + path: '/out/u2.zip', + scope: 'test', + key: 'u2', + testUids: ['u2'], + retained: true + } +] + +describe('artifacts manifest', () => { + const dirs: string[] = [] + afterEach(async () => { + await Promise.all( + dirs.splice(0).map((d) => rm(d, { recursive: true, force: true })) + ) + }) + + it('buildArtifactsManifest carries every artifact (retained + not) and per-test states', () => { + const m = buildArtifactsManifest({ + sessionId: 'sess-1', + format: 'zip', + artifacts, + testMetadata: metadata() + }) + expect(m.sessionId).toBe('sess-1') + expect(m.format).toBe('zip') + expect(m.artifacts).toEqual(artifacts) + expect(m.artifacts).not.toBe(artifacts) // copied, not aliased + expect(m.tests).toEqual([ + { + uid: 'u1', + title: 'passes', + specFile: '/a.spec.ts', + state: 'passed', + attempt: 0 + }, + { + uid: 'u2', + title: 'fails', + specFile: '/a.spec.ts', + state: 'failed', + attempt: 1 + } + ]) + }) + + it('filename is deterministic per session', () => { + expect(artifactsManifestFilename('abc')).toBe('devtools-artifacts-abc.json') + }) + + it('writeArtifactsManifest round-trips the JSON to disk', async () => { + const dir = await mkdtemp(join(tmpdir(), 'manifest-')) + dirs.push(dir) + const manifest = buildArtifactsManifest({ + sessionId: 'sess-2', + format: 'zip', + artifacts, + testMetadata: metadata() + }) + const filePath = await writeArtifactsManifest(dir, manifest) + expect(filePath).toBe(join(dir, 'devtools-artifacts-sess-2.json')) + const parsed = JSON.parse( + await readFile(filePath, 'utf8') + ) as ArtifactsManifest + expect(parsed).toEqual(manifest) + }) +}) diff --git a/packages/core/tests/trace-finalizer.test.ts b/packages/core/tests/trace-finalizer.test.ts index d25e30b8..70f655fe 100644 --- a/packages/core/tests/trace-finalizer.test.ts +++ b/packages/core/tests/trace-finalizer.test.ts @@ -6,6 +6,7 @@ import { buildSpecSessionId, buildTestSliceFolder, finalizeTraceExport, + flushRangeLogged, flushRangeTrace, TestAttemptTracker, type SpecRange, @@ -315,6 +316,69 @@ describe('finalizeTraceExport', () => { expect(await exists(path.join(outputDir, 'trace-abcd1234.zip'))).toBe(true) }) + describe('artifacts manifest', () => { + const manifestPath = () => + path.join(outputDir, 'devtools-artifacts-abcd1234.json') + + it('is not written unless emitManifest is set', async () => { + await finalizeTraceExport( + baseCtx({ + testMetadata: meta([['u1', { title: 'T1', specFile: '/a.js' }]]) + }) + ) + expect(await exists(manifestPath())).toBe(false) + }) + + it('enumerates collected artifacts and per-test states when enabled', async () => { + await finalizeTraceExport( + baseCtx({ + emitManifest: true, + collectedArtifacts: artifacts, + testMetadata: meta([ + [ + 'u1', + { title: 'T1', specFile: '/a.js', state: 'passed', attempt: 0 } + ] + ]) + }) + ) + const manifest = JSON.parse(await fs.readFile(manifestPath(), 'utf8')) + expect(manifest.sessionId).toBe('abcd1234') + expect(manifest.format).toBe('zip') + // The session write pushed one artifact through onArtifact into the list. + expect(manifest.artifacts).toHaveLength(1) + expect(manifest.artifacts[0].scope).toBe('session') + expect(manifest.tests).toEqual([ + { + uid: 'u1', + title: 'T1', + specFile: '/a.js', + state: 'passed', + attempt: 0 + } + ]) + }) + + it('includes eager-flushed slices the fan-out deduped away', async () => { + const ctx = baseCtx({ + granularity: 'test', + emitManifest: true, + collectedArtifacts: artifacts, + ranges: [testRange('/a.js', 'u1', 0)], + testMetadata: meta([['u1', { title: 'T1', specFile: '/a.js' }]]) + }) + // Eager mid-run flush: writes the slice and records it via onArtifact. + await flushRangeLogged(ctx, ctx.ranges[0]!) + // Finalize: the range is already flushed, so fan-out returns nothing new, + // but the manifest must still list the eager artifact. + await finalizeTraceExport(ctx) + const manifest = JSON.parse(await fs.readFile(manifestPath(), 'utf8')) + expect(manifest.artifacts).toHaveLength(1) + expect(manifest.artifacts[0].scope).toBe('test') + expect(manifest.artifacts[0].key).toBe('u1') + }) + }) + describe('retention wiring', () => { it("writes everything under the default 'on' policy", async () => { const result = await finalizeTraceExport( diff --git a/packages/nightwatch-devtools/src/index.ts b/packages/nightwatch-devtools/src/index.ts index a4084321..fad32b4f 100644 --- a/packages/nightwatch-devtools/src/index.ts +++ b/packages/nightwatch-devtools/src/index.ts @@ -108,6 +108,14 @@ class NightwatchDevToolsPlugin { /** Set of spec files already flushed to disk. */ #flushedSpecs = new Set<string>() + /** Every trace/video artifact seen this run (retained or not), for the + * end-of-run artifacts manifest. Populated via the context's onArtifact. */ + #artifacts: TraceArtifact[] = [] + + /** In-flight fire-and-forget eager/boundary slice flushes, awaited at finalize + * so the last slice's write + its manifest entry land before teardown. */ + #traceFlushes: Promise<unknown>[] = [] + #getRerunLabel() { return process.env[REUSE_ENV.RERUN_ENTRY_TYPE] === 'test' ? process.env[REUSE_ENV.RERUN_LABEL]?.trim() @@ -575,7 +583,11 @@ class NightwatchDevToolsPlugin { if (!sessionId) { return Promise.resolve(undefined) } - return flushRangeLogged(this.#traceContext(sessionId), range) + // Track the promise (before it's added to awaitPending) so finalize awaits + // this fire-and-forget flush — its context snapshot already excludes it. + const flush = flushRangeLogged(this.#traceContext(sessionId), range) + this.#traceFlushes.push(flush) + return flush } /** Assemble the framework-agnostic trace-export context from plugin state. @@ -592,6 +604,8 @@ class NightwatchDevToolsPlugin { outcomes: this.#attemptTracker, ranges: this.#specRanges, flushed: this.#flushedSpecs, + artifacts: this.#artifacts, + traceFlushes: this.#traceFlushes, configPath: this.#configPath, testFilePath: this.browserProxy?.getCurrentTestFullPath?.() ?? undefined, diff --git a/packages/nightwatch-devtools/src/trace-context.ts b/packages/nightwatch-devtools/src/trace-context.ts index bf217fb2..4c4666be 100644 --- a/packages/nightwatch-devtools/src/trace-context.ts +++ b/packages/nightwatch-devtools/src/trace-context.ts @@ -11,6 +11,7 @@ import { resolveAdapterOutputDir, type RetryOutcomeView, type SpecRange, + type TraceArtifact, type TraceExportContext } from '@wdio/devtools-core' import type { SessionCapturer } from './session.js' @@ -32,6 +33,8 @@ export interface TraceContextInput { outcomes?: RetryOutcomeView ranges: SpecRange[] flushed: Set<string> + artifacts: TraceArtifact[] + traceFlushes: Promise<unknown>[] configPath: string | undefined testFilePath: string | undefined log: (level: 'info' | 'warn', msg: string) => void @@ -58,10 +61,13 @@ export function buildTraceContext( testFilePath: input.testFilePath, configPath: input.configPath }), - awaitPending: input.capturer.snapshotCaptures, + awaitPending: [...input.capturer.snapshotCaptures, ...input.traceFlushes], // Nightwatch feeds real per-test attempt numbers via TestAttemptTracker // (B4), so retry-aware policies use per-test attempts, not the fallback. attemptInfoAvailable: true, + emitManifest: true, + collectedArtifacts: input.artifacts, + onArtifact: (a) => input.artifacts.push(a), log: input.log } } diff --git a/packages/nightwatch-devtools/tests/attemptTracking.test.ts b/packages/nightwatch-devtools/tests/attemptTracking.test.ts index 7d5eacd3..fe49f502 100644 --- a/packages/nightwatch-devtools/tests/attemptTracking.test.ts +++ b/packages/nightwatch-devtools/tests/attemptTracking.test.ts @@ -142,6 +142,8 @@ describe('buildTraceContext', () => { suites: [suite], ranges: [], flushed: new Set(), + artifacts: [], + traceFlushes: [], configPath: undefined, log: () => {} }, @@ -173,6 +175,8 @@ describe('buildTraceContext', () => { outcomes: tracker, ranges: [], flushed: new Set(), + artifacts: [], + traceFlushes: [], configPath: undefined, log: () => {} }, diff --git a/packages/selenium-devtools/src/index.ts b/packages/selenium-devtools/src/index.ts index 4c4b2406..91af73f9 100644 --- a/packages/selenium-devtools/src/index.ts +++ b/packages/selenium-devtools/src/index.ts @@ -24,7 +24,7 @@ import { recordTraceBoundary, flushCurrentTestTrace } from './session-lifecycle.js' -import type { SpecRange } from '@wdio/devtools-core' +import type { SpecRange, TraceArtifact } from '@wdio/devtools-core' import { startTest as tmStartTest, endTest as tmEndTest, @@ -127,6 +127,10 @@ class SeleniumDevToolsPlugin { /** In-flight per-test eager flushes (test granularity), awaited at finalize. */ #traceFlushes: Promise<unknown>[] = [] + /** Every trace/video artifact seen this run (retained or not), for the + * end-of-run artifacts manifest. Populated via the context's onArtifact. */ + #artifacts: TraceArtifact[] = [] + constructor(options: DevToolsOptions = {}) { this.#options = { port: options.port ?? 3000, @@ -439,6 +443,9 @@ class SeleniumDevToolsPlugin { get traceFlushes() { return self.#traceFlushes }, + get artifacts() { + return self.#artifacts + }, setFinalized: (v) => { self.#finalized = v }, diff --git a/packages/selenium-devtools/src/plugin-internals.ts b/packages/selenium-devtools/src/plugin-internals.ts index a9672e50..36d1f98e 100644 --- a/packages/selenium-devtools/src/plugin-internals.ts +++ b/packages/selenium-devtools/src/plugin-internals.ts @@ -22,7 +22,7 @@ import type { } from './types.js' import type { RetryTracker } from '@wdio/devtools-core' import type { PendingTestAction, PendingScenario } from './test-management.js' -import type { SpecRange } from '@wdio/devtools-core' +import type { SpecRange, TraceArtifact } from '@wdio/devtools-core' export interface PluginInternals { // Config @@ -69,6 +69,8 @@ export interface PluginInternals { readonly flushedSpecs: Set<string> // In-flight per-test eager flushes (test granularity), awaited at finalize. readonly traceFlushes: Promise<unknown>[] + // Every trace/video artifact seen this run, for the end-of-run manifest. + readonly artifacts: TraceArtifact[] // Plugin-side delegates setFinalized(v: boolean): void diff --git a/packages/selenium-devtools/src/session-lifecycle.ts b/packages/selenium-devtools/src/session-lifecycle.ts index ca4bab76..fabe3895 100644 --- a/packages/selenium-devtools/src/session-lifecycle.ts +++ b/packages/selenium-devtools/src/session-lifecycle.ts @@ -21,6 +21,7 @@ import { resolveAdapterOutputDir, type SpecBoundaryContext, type SpecRange, + type TraceArtifact, type TraceExportContext } from '@wdio/devtools-core' import { TIMING } from './constants.js' @@ -85,6 +86,8 @@ export interface SessionLifecycleCtx { // In-flight per-test eager flushes (test granularity); awaited at finalize // so the last test's write completes before the process exits. readonly traceFlushes: Promise<unknown>[] + // Every trace/video artifact seen this run, for the end-of-run manifest. + readonly artifacts: TraceArtifact[] setFinalized(v: boolean): void ensureBackendStarted(): Promise<void> @@ -327,7 +330,10 @@ export function buildTraceExportContext( testFilePath: range ? range.specFile : testFilePath }), awaitPending: [...ctx.snapshotCaptures, ...ctx.traceFlushes], - log: (level, msg) => log[level](msg) + log: (level, msg) => log[level](msg), + emitManifest: true, + collectedArtifacts: ctx.artifacts, + onArtifact: (a) => ctx.artifacts.push(a) } } diff --git a/packages/service/package.json b/packages/service/package.json index be1a14d8..73eb08a7 100644 --- a/packages/service/package.json +++ b/packages/service/package.json @@ -73,8 +73,14 @@ "vite-plugin-dts": "^5.0.2" }, "peerDependencies": { + "@wdio/allure-reporter": "^9.0.0", "@wdio/protocols": "9.28.0", "devtools": "^8.42.0", "webdriverio": "^9.19.1" + }, + "peerDependenciesMeta": { + "@wdio/allure-reporter": { + "optional": true + } } } diff --git a/packages/service/src/allure.ts b/packages/service/src/allure.ts new file mode 100644 index 00000000..ce0e678d --- /dev/null +++ b/packages/service/src/allure.ts @@ -0,0 +1,90 @@ +// WDIO Allure glue: attach retained trace/video artifacts to the current Allure +// test so a failed run's trace travels with the report. Isolated here so +// index.ts stays free of the optional-dependency dance and the content-type +// convention lives in one place. + +import fs from 'node:fs/promises' +import { basename } from 'node:path' +import logger from '@wdio/logger' +import type { TraceArtifact } from '@wdio/devtools-core' + +const log = logger('@wdio/devtools-service') + +// Attach the trace as a plain zip download — the viewer stays the user's choice +// (our `show-trace`, or any compatible viewer), rather than routing them into a +// third-party viewer Allure would open for a trace-specific content type. Video +// uses video/webm so Allure renders it inline. +const TRACE_CONTENT_TYPE = 'application/zip' +const VIDEO_CONTENT_TYPE = 'video/webm' + +/** The one @wdio/allure-reporter method we use. Typed locally so the optional + * peer dependency never becomes a build-time type dependency. */ +interface AllureReporterModule { + addAttachment(name: string, content: Buffer, type: string): void +} + +// undefined = not yet probed; null = probed and unavailable. +let cachedReporter: AllureReporterModule | null | undefined + +// A non-literal specifier keeps TypeScript and the bundler from resolving this +// optional peer dependency at build time — `import()` of a variable is typed +// `any` and left as a runtime import that no-ops (caught below) when absent. +const ALLURE_REPORTER_SPECIFIER = '@wdio/allure-reporter' + +async function loadAllureReporter(): Promise<AllureReporterModule | null> { + if (cachedReporter !== undefined) { + return cachedReporter + } + try { + const mod = await import(/* @vite-ignore */ ALLURE_REPORTER_SPECIFIER) + const candidate = ( + typeof mod.addAttachment === 'function' ? mod : mod.default + ) as AllureReporterModule | undefined + cachedReporter = + candidate && typeof candidate.addAttachment === 'function' + ? candidate + : null + } catch { + // Optional peer dependency not installed — attaching is a no-op. + cachedReporter = null + } + return cachedReporter +} + +/** + * Attach one retained artifact to the current Allure test. No-op when the + * artifact wasn't retained, has no path yet, or @wdio/allure-reporter isn't + * installed. Must be called from within a per-test hook (afterTest) so Allure + * associates the attachment with the right test. + */ +export async function attachArtifactToAllure( + artifact: TraceArtifact +): Promise<void> { + if (!artifact.retained || !artifact.path) { + return + } + const reporter = await loadAllureReporter() + if (!reporter) { + return + } + try { + // Allure attaches a single file; the ndjson-directory trace format yields a + // directory path, which can't be attached — skip it. + const stat = await fs.stat(artifact.path) + if (!stat.isFile()) { + return + } + const content = await fs.readFile(artifact.path) + const type = + artifact.kind === 'video' ? VIDEO_CONTENT_TYPE : TRACE_CONTENT_TYPE + reporter.addAttachment(basename(artifact.path), content, type) + } catch (err) { + // A missing/unreadable artifact must never reject the test hook. + log.warn(`Allure attach skipped for ${artifact.path}: ${String(err)}`) + } +} + +/** Reset the memoized reporter — test seam only. */ +export function resetAllureReporterCache(): void { + cachedReporter = undefined +} diff --git a/packages/service/src/index.ts b/packages/service/src/index.ts index 95c1edbd..39c61610 100644 --- a/packages/service/src/index.ts +++ b/packages/service/src/index.ts @@ -10,8 +10,10 @@ import { TestAttemptTracker, tracePolicyModeWarning, type SpecRange, + type TraceArtifact, type TraceExportContext } from '@wdio/devtools-core' +import { attachArtifactToAllure } from './allure.js' import { wireAssertCapture, type ExpectAssertion } from './assert-capture.js' import { AssertionTracker } from './assertion-tracker.js' import { @@ -123,6 +125,10 @@ export default class DevToolsHookService implements Services.ServiceInstance { /** Set of spec files already flushed to disk. */ #flushedSpecs = new Set<string>() + /** Every trace/video artifact seen this run (retained or not), for the + * end-of-run artifacts manifest. Populated via the context's onArtifact. */ + #artifacts: TraceArtifact[] = [] + /** Build the boundary context for recordSliceBoundary — the same shape is * needed in both beforeTest and beforeScenario. */ get #boundaryContext() { @@ -156,15 +162,17 @@ export default class DevToolsHookService implements Services.ServiceInstance { /** Eager per-test flush at test end (test granularity only), run after the * outcome is stamped so this attempt's metadata is written before a retry * overwrites it; the end-of-run finalizer then dedupes it via the key set. */ - async #eagerFlushTestSlice(testUid: string): Promise<void> { + async #eagerFlushTestSlice( + testUid: string + ): Promise<TraceArtifact | undefined> { if ( this.#options.traceGranularity !== 'test' || this.#options.mode !== 'trace' || !this.#browser ) { - return + return undefined } - await flushTestSlice( + return flushTestSlice( this.#traceContext(this.#browser), this.#specRanges, testUid @@ -190,7 +198,10 @@ export default class DevToolsHookService implements Services.ServiceInstance { flushed: this.#flushedSpecs, resolveOutputDir: () => this.#outputDir, prepareSnapshots: dedupeSnapshotsByTimestamp, - log: (level, msg) => log[level](msg) + log: (level, msg) => log[level](msg), + emitManifest: true, + collectedArtifacts: this.#artifacts, + onArtifact: (a) => this.#artifacts.push(a) } } @@ -408,10 +419,7 @@ export default class DevToolsHookService implements Services.ServiceInstance { this.#stampOutcome(uid, result) } await this.#finalizePerScenario() - // Flush now so this slice includes the final snapshot and stamped outcome. - if (uid) { - await this.#eagerFlushTestSlice(uid) - } + await this.#eagerFlushAndAttach(uid) } async afterTest( @@ -426,9 +434,19 @@ export default class DevToolsHookService implements Services.ServiceInstance { this.#stampOutcome(uid, result) } await this.#finalizePerScenario() - // Flush now so this slice includes the final snapshot and stamped outcome. - if (uid) { - await this.#eagerFlushTestSlice(uid) + await this.#eagerFlushAndAttach(uid) + } + + /** Flush this test's slice (so it captures the final snapshot + stamped + * outcome), then attach the retained artifact to Allure while the per-test + * hook is still open. No-op outside `test`-granularity trace mode. */ + async #eagerFlushAndAttach(uid: string | undefined): Promise<void> { + if (!uid) { + return + } + const artifact = await this.#eagerFlushTestSlice(uid) + if (artifact) { + await attachArtifactToAllure(artifact) } } diff --git a/packages/service/src/trace-slices.ts b/packages/service/src/trace-slices.ts index 5da66806..24533abb 100644 --- a/packages/service/src/trace-slices.ts +++ b/packages/service/src/trace-slices.ts @@ -6,6 +6,7 @@ import { findFlushableRange, flushRangeLogged, type SpecRange, + type TraceArtifact, type TraceExportContext } from '@wdio/devtools-core' @@ -21,15 +22,16 @@ export function flushPrevSlice( /** Awaited flush of the just-ended test's slice (test granularity), so this * attempt's just-stamped metadata is written before a retry's beforeTest - * overwrites the entry. No-op when the test recorded no range. */ + * overwrites the entry. Returns the produced artifact (for same-hook Allure + * attach); undefined when the test recorded no range. */ export async function flushTestSlice( ctx: TraceExportContext, ranges: readonly SpecRange[], testUid: string -): Promise<void> { +): Promise<TraceArtifact | undefined> { const range = findFlushableRange(ranges, testUid) if (!range) { - return + return undefined } - await flushRangeLogged(ctx, range) + return flushRangeLogged(ctx, range) } diff --git a/packages/service/tests/allure.test.ts b/packages/service/tests/allure.test.ts new file mode 100644 index 00000000..e8305f6a --- /dev/null +++ b/packages/service/tests/allure.test.ts @@ -0,0 +1,91 @@ +import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest' +import { mkdtemp, writeFile, rm } from 'node:fs/promises' +import { tmpdir } from 'node:os' +import { join } from 'node:path' +import type { TraceArtifact } from '@wdio/devtools-core' + +const addAttachment = vi.fn() +vi.mock('@wdio/allure-reporter', () => ({ + addAttachment, + default: { addAttachment } +})) + +import { + attachArtifactToAllure, + resetAllureReporterCache +} from '../src/allure.js' + +function artifact(over: Partial<TraceArtifact> = {}): TraceArtifact { + return { + kind: 'trace', + path: '', + scope: 'test', + testUids: ['u1'], + retained: true, + ...over + } +} + +describe('attachArtifactToAllure', () => { + const dirs: string[] = [] + beforeEach(() => { + addAttachment.mockReset() + resetAllureReporterCache() + }) + afterEach(async () => { + await Promise.all( + dirs.splice(0).map((d) => rm(d, { recursive: true, force: true })) + ) + }) + + async function tempZip(name: string): Promise<string> { + const dir = await mkdtemp(join(tmpdir(), 'allure-')) + dirs.push(dir) + const p = join(dir, name) + await writeFile(p, 'zip-bytes') + return p + } + + it('is a no-op for a non-retained artifact', async () => { + await attachArtifactToAllure(artifact({ retained: false, path: '/x.zip' })) + expect(addAttachment).not.toHaveBeenCalled() + }) + + it('is a no-op for an artifact with no path yet', async () => { + await attachArtifactToAllure(artifact({ path: '' })) + expect(addAttachment).not.toHaveBeenCalled() + }) + + it('attaches a trace zip with the Allure Playwright content type', async () => { + const path = await tempZip('trace-abc.zip') + await attachArtifactToAllure(artifact({ path })) + expect(addAttachment).toHaveBeenCalledOnce() + const [name, content, type] = addAttachment.mock.calls[0]! + expect(name).toBe('trace-abc.zip') + expect(Buffer.isBuffer(content)).toBe(true) + expect(type).toBe('application/zip') + }) + + it('attaches a video artifact as video/webm', async () => { + const path = await tempZip('rec-abc.webm') + await attachArtifactToAllure(artifact({ kind: 'video', path })) + const [, , type] = addAttachment.mock.calls[0]! + expect(type).toBe('video/webm') + }) + + it('skips a directory-format artifact without throwing (ndjson-directory)', async () => { + const dir = await mkdtemp(join(tmpdir(), 'allure-dir-')) + dirs.push(dir) + await expect( + attachArtifactToAllure(artifact({ path: dir })) + ).resolves.toBeUndefined() + expect(addAttachment).not.toHaveBeenCalled() + }) + + it('never rejects when the artifact path is missing', async () => { + await expect( + attachArtifactToAllure(artifact({ path: '/no/such/trace.zip' })) + ).resolves.toBeUndefined() + expect(addAttachment).not.toHaveBeenCalled() + }) +}) From cb555e23cbf05d0e0e1a98fd1e294a03596b7259 Mon Sep 17 00:00:00 2001 From: Vishnu Vardhan <vishnu.p@browserstack.com> Date: Wed, 15 Jul 2026 17:08:22 +0530 Subject: [PATCH 65/68] feat: per-test screenshot + video options with inline Allure attach --- CLAUDE.md | 1 + examples/nightwatch/nightwatch.conf.cjs | 2 + examples/selenium/mocha-test/test/example.js | 4 +- examples/wdio/cucumber/features/login.feature | 2 +- examples/wdio/cucumber/wdio.conf.ts | 26 +- examples/wdio/mocha/wdio.conf.ts | 5 +- package.json | 1 + packages/core/src/artifact-naming.ts | 10 + packages/core/src/index.ts | 3 + packages/core/src/screenshot-artifact.ts | 53 ++++ packages/core/src/trace-finalizer.ts | 5 +- packages/core/src/video-slice.ts | 69 ++++++ packages/core/tests/output-dir.test.ts | 2 +- .../core/tests/screenshot-artifact.test.ts | 53 ++++ packages/core/tests/video-slice.test.ts | 100 ++++++++ packages/service/README.md | 52 +++- packages/service/src/allure.ts | 10 +- packages/service/src/index.ts | 164 +++++++++++-- packages/service/src/screenshot-capture.ts | 67 +++++ packages/service/src/test-metadata.ts | 8 + packages/service/src/video-capture.ts | 80 ++++++ packages/service/tests/allure.test.ts | 9 +- .../service/tests/screenshot-capture.test.ts | 86 +++++++ packages/service/tests/video-capture.test.ts | 75 ++++++ packages/shared/src/types.ts | 19 ++ pnpm-lock.yaml | 231 +++++++++++++++--- 26 files changed, 1065 insertions(+), 72 deletions(-) create mode 100644 packages/core/src/artifact-naming.ts create mode 100644 packages/core/src/screenshot-artifact.ts create mode 100644 packages/core/src/video-slice.ts create mode 100644 packages/core/tests/screenshot-artifact.test.ts create mode 100644 packages/core/tests/video-slice.test.ts create mode 100644 packages/service/src/screenshot-capture.ts create mode 100644 packages/service/src/video-capture.ts create mode 100644 packages/service/tests/screenshot-capture.test.ts create mode 100644 packages/service/tests/video-capture.test.ts diff --git a/CLAUDE.md b/CLAUDE.md index 59009622..5a2d88ce 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -240,6 +240,7 @@ Documented divergences from the conventions above. They exist today as debt to b - **Nightwatch: `retain-on-failure` works; the other retry-aware policies degrade.** Its `--retries` re-runs the testcase *internally* without re-firing the plugin's per-test hooks, and the per-testcase results carry no attempt/retry field (retries live only in undocumented, version-varying Nightwatch internals — `suiteRetries.testRetriesCount` / `reporter.testResults.retryTest`), so the ledger sees only the final attempt for the `describe/it` and exports-object interfaces. Cucumber scenarios expose per-scenario hooks, so the feed captures their attempts. Not cleanly fixable without depending on those internals. - WDIO `specFileRetries` spawns a fresh worker per retry, so cross-process attempts aren't in the (process-scoped) ledger. - Eager per-test trace slice (Nightwatch + Selenium) can drop an action snapshot whose fire-and-forget capture hasn't resolved by `afterEach` / scenario end — the slice is written from whatever snapshots exist at flush time. The WDIO service is immune because it awaits each snapshot inline before flushing. +- The per-test `screenshot` and `video` options live on `BaseDevToolsOptions` (shared) so all three adapters accept them, but only the **WDIO service** reads them today. The capture/slice/encode logic is framework-agnostic (`core/screenshot-artifact.ts`, `core/video-slice.ts`), so Selenium/Nightwatch adoption is wiring-only (capture the base64 + feed `captureAndAttach*`); pending. Both are gated to `traceGranularity:'test'` (per-test inline Allure); coarser granularities keep artifacts in the manifest. Video records the screencast continuously and slices per-test by wall-time — the frame buffer is unbounded for the session (a decimation cap is a noted follow-up), and on non-Chrome the polling recorder issues many `takeScreenshot`s that flood `@wdio/allure-reporter` (pair with `disableWebdriverStepsReporting`). - Nightwatch native asserts are buffered at call time and emitted in one batch at test-end (Nightwatch exposes no per-assertion execution/outcome hook reachable from the plugin — `client.queue.tree` / `client.reporter` aren't on `browser`). Streaming mid-run would flash every assert green before its outcome is known, so rows are held and reconciled against `results.testcases[*].assertions` at `afterEach` (the flat `results.assertions` only reflects the last testcase). The exporter re-sorts commands by timestamp (`buildActionEvents`) so the batched rows land at their real timeline positions rather than clustering at flush time. `actual` is parsed from the failure message (`but got: …`, failures only) into a collapsed `{passed, expected, actual}` result; `expected` is the assertion arg — the label still mirrors the call args, not the derived values. - Nightwatch's BDD `describe/it` interface fires the plugin's global `beforeEach`/`afterEach` **once per module** (with an empty `currentTest.name`), not per `it` — Nightwatch runs the individual `it`s internally with no per-testcase hook or event reachable from the plugin (the lib emits only transport-level events). So `traceGranularity:'test'` records a single slice boundary (the first test) and collapses to one session-scoped slice; retry-aware/per-test retention (`tracePolicy`) degrades to session scope for this interface. Per-test slicing works for adapters/styles that expose per-test hooks (WDIO mocha/cucumber, Selenium mocha; likely Nightwatch's exports-object style, unverified). Session-granularity trace is unaffected. - Service renders expect-webdriverio matchers as single `expect.<matcher>` rows by **folding**, not stack/depth suppression (the old `#assertionDepth`/`#matcherStarted`/self-heal machinery is gone). The matcher's value-read (`toHaveText`→`getText`, `toExist`→`isExisting`, …) is captured as a normal command; `afterAssertion` then coalesces the synthesized `expect.*` row into that read in place — inheriting its callSource, screenshot, and timeline position — and the fold replaces **by timestamp, never a public `id`**: `id` is the per-worker `commandCounter`, which resets per spec, so stamping one lets the app's id-first `replaceCommand` swap a same-id row from another spec (duplicate rows + a fold from another spec vanishing, in multi-spec live mode). `beforeAssertion` arms the pending matcher (depth-counted so aliases like `toBeChecked`→`toBeSelected` fold once); a matcher that **hard-throws** — element never resolves, so expect-webdriverio's `waitUntil` rethrows and `afterAssertion` never fires — is synthesized at `afterTest`/`afterStep` from the throwing read, so a failing assertion renders as `expect.<matcher>` whether or not the element existed. Two limits: its error is then the read's (`Can't call getText on … element wasn't found`), not an assertion-phrased message; and `MATCHER_READ_COMMANDS` is a hand-maintained allowlist, so a matcher whose read isn't listed leaves its raw read visible alongside the `expect.*` row. Plain-value jest matchers (`expect(x).toBe(y)`) don't fire the ewdio hooks, so they aren't captured as rows. diff --git a/examples/nightwatch/nightwatch.conf.cjs b/examples/nightwatch/nightwatch.conf.cjs index 3ec31930..b0d1134d 100644 --- a/examples/nightwatch/nightwatch.conf.cjs +++ b/examples/nightwatch/nightwatch.conf.cjs @@ -53,6 +53,8 @@ module.exports = { // per module (no per-`it` hook), so traceGranularity:'test' collapses to // a single session-scoped slice here. See CLAUDE.md § Known debt. mode: 'trace', + traceGranularity: 'session', + tracePolicy: 'retain-on-first-failure', bidi: true }) } diff --git a/examples/selenium/mocha-test/test/example.js b/examples/selenium/mocha-test/test/example.js index 26360598..25fe5a28 100644 --- a/examples/selenium/mocha-test/test/example.js +++ b/examples/selenium/mocha-test/test/example.js @@ -21,8 +21,8 @@ import { DevTools } from '@wdio/selenium-devtools' // 5 retry: { mode: 'trace', traceGranularity: 'test', tracePolicy: 'on-first-retry' } DevTools.configure({ mode: 'trace', - // traceGranularity: 'test', - // tracePolicy: 'on-first-retry', + traceGranularity: 'session', + tracePolicy: 'retain-on-first-failure', headless: true }) diff --git a/examples/wdio/cucumber/features/login.feature b/examples/wdio/cucumber/features/login.feature index 8b9bcf9a..e6dd0f87 100644 --- a/examples/wdio/cucumber/features/login.feature +++ b/examples/wdio/cucumber/features/login.feature @@ -8,5 +8,5 @@ Feature: The Internet Guinea Pig Website Examples: | username | password | message | - | tomsmith | SuperSecretPassword1! | You logged into a secure area! | + | tomsmith | SuperSecretPassword! | You logged into a secure area! | | foobar | barfoo | Your username is invalid! | diff --git a/examples/wdio/cucumber/wdio.conf.ts b/examples/wdio/cucumber/wdio.conf.ts index e1a82294..75a1f5b9 100644 --- a/examples/wdio/cucumber/wdio.conf.ts +++ b/examples/wdio/cucumber/wdio.conf.ts @@ -69,7 +69,6 @@ export const config: Options.Testrunner = { 'goog:chromeOptions': { args: [ '--headless', - '--disable-gpu', '--remote-allow-origins=*', '--window-size=1600,900' ] @@ -134,7 +133,13 @@ export const config: Options.Testrunner = { 'devtools', { mode: 'trace' as const, - screencast: { enabled: true, pollIntervalMs: 200 } + // tracePolicy: 'retain-on-failure', + traceGranularity: 'test' as const, + // NEW: per-test screenshot + video, attached inline to Allure. + // Both require traceGranularity:'test' (the uniform rule). + screenshot: 'only-on-failure' as const, // 'off' | 'on' | 'only-on-failure' + video: 'on' as const // 'off' | retention policy + // screencast: { enabled: true, pollIntervalMs: 200 } } ] ], @@ -160,7 +165,22 @@ export const config: Options.Testrunner = { // Test reporter for stdout. // The only one supported by default is 'dot' // see also: https://webdriver.io/docs/dot-reporter - reporters: ['spec'], + reporters: [ + 'spec', + [ + 'allure', + { + outputDir: path.resolve(__dirname, 'allure-results'), + // Trace mode issues a takeScreenshot (+ reads) per action to build the + // trace snapshots; @wdio/allure-reporter logs every WebDriver command as + // a step and attaches a screenshot per takeScreenshot, which floods the + // report. Silence both — the trace.zip attachment is unaffected, and the + // gherkin Given/When/Then steps still show. + disableWebdriverStepsReporting: true, + disableWebdriverScreenshotsReporting: true + } + ] + ], // If you are using Cucumber you need to specify the location of your step definitions. cucumberOpts: { diff --git a/examples/wdio/mocha/wdio.conf.ts b/examples/wdio/mocha/wdio.conf.ts index c7680f7d..f6d9e779 100644 --- a/examples/wdio/mocha/wdio.conf.ts +++ b/examples/wdio/mocha/wdio.conf.ts @@ -19,6 +19,7 @@ export const config: Options.Testrunner = { capabilities: [ { browserName: 'chrome', + // browserVersion: '147.0.7727.56', 'goog:chromeOptions': { args: [ '--headless', @@ -45,7 +46,9 @@ export const config: Options.Testrunner = { // 3 per-test: mode: 'trace', traceGranularity: 'test' // 4 fail: mode: 'trace', traceGranularity: 'test', tracePolicy: 'retain-on-failure' // 5 retry: use `pnpm demo:wdio:retry` (adds retries:1 + on-first-retry) - mode: 'live' as const + mode: 'live' as const, + traceGranularity: 'test' as const, + tracePolicy: 'retain-on-failure' as const } ] ], diff --git a/package.json b/package.json index 5c25c3e6..b55ab301 100644 --- a/package.json +++ b/package.json @@ -39,6 +39,7 @@ "@typescript-eslint/utils": "^8.60.1", "@vitest/browser": "^4.1.8", "@vitest/coverage-v8": "^4.1.8", + "@wdio/allure-reporter": "^9.29.1", "autoprefixer": "^10.5.0", "eslint": "^10.4.1", "eslint-config-prettier": "^10.1.8", diff --git a/packages/core/src/artifact-naming.ts b/packages/core/src/artifact-naming.ts new file mode 100644 index 00000000..dcf597b6 --- /dev/null +++ b/packages/core/src/artifact-naming.ts @@ -0,0 +1,10 @@ +/** + * Filename helpers for per-test artifacts (screenshots, videos). Shared so the + * screenshot and video writers slug a test uid identically. + */ + +/** File-safe slug of a value: keep alphanumerics, `_` and `-`; collapse the + * rest to a single `-`; trim leading/trailing dashes. */ +export function fileSlug(value: string): string { + return value.replace(/[^a-zA-Z0-9_-]+/g, '-').replace(/^-+|-+$/g, '') +} diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index 9d37bbf0..e6f4a7ba 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -3,8 +3,11 @@ export * from './action-mapping.js' export * from './action-snapshot.js' +export * from './artifact-naming.js' export * from './artifacts-manifest.js' export * from './attempt-tracker.js' +export * from './screenshot-artifact.js' +export * from './video-slice.js' export * from './with-timeout.js' export * from './assert-patcher.js' export * from './element-snapshot.js' diff --git a/packages/core/src/screenshot-artifact.ts b/packages/core/src/screenshot-artifact.ts new file mode 100644 index 00000000..881ff6e3 --- /dev/null +++ b/packages/core/src/screenshot-artifact.ts @@ -0,0 +1,53 @@ +/** + * Per-test screenshot artifact: the policy decision and the file write, kept + * framework-agnostic so every adapter feeds it the same way (the adapter only + * supplies the base64 capture from its own driver). Mirrors Playwright's + * `screenshot` option — `on` after every test, `only-on-failure` after a + * failing one — and produces a TraceArtifact the manifest and the Allure glue + * consume like any other artifact. + */ + +import fs from 'node:fs/promises' +import path from 'node:path' +import type { TraceScreenshotPolicy } from '@wdio/devtools-shared' +import { fileSlug } from './artifact-naming.js' +import type { TraceArtifact } from './trace-finalizer.js' + +/** Whether a per-test screenshot should be captured for this outcome. */ +export function shouldCaptureScreenshot( + policy: TraceScreenshotPolicy | undefined, + failed: boolean +): boolean { + if (policy === 'on') { + return true + } + if (policy === 'only-on-failure') { + return failed + } + return false +} + +/** + * Write a base64 PNG (from the driver's screenshot command) next to the trace + * output and return the artifact describing it. Retained is always true — a + * screenshot is only ever written when the policy already decided to capture. + */ +export async function writeScreenshotArtifact(input: { + outputDir: string + testUid: string + sessionId: string + base64: string +}): Promise<TraceArtifact> { + await fs.mkdir(input.outputDir, { recursive: true }) + const filename = `screenshot-${fileSlug(input.testUid)}-${input.sessionId.slice(0, 8)}.png` + const filePath = path.join(input.outputDir, filename) + await fs.writeFile(filePath, Buffer.from(input.base64, 'base64')) + return { + kind: 'screenshot', + path: filePath, + scope: 'test', + key: input.testUid, + testUids: [input.testUid], + retained: true + } +} diff --git a/packages/core/src/trace-finalizer.ts b/packages/core/src/trace-finalizer.ts index 9cb57af2..ff2b3b99 100644 --- a/packages/core/src/trace-finalizer.ts +++ b/packages/core/src/trace-finalizer.ts @@ -31,9 +31,10 @@ import type { RetryOutcomeView } from './attempt-tracker.js' import { writeTraceZip, type TraceCapturer } from './trace-exporter.js' /** One artifact produced (or, when `retained` is false, decided-against) by a - * trace-mode finalize pass. */ + * trace-mode run — a trace slice, a screencast video, or a per-test + * screenshot. */ export interface TraceArtifact { - kind: 'trace' | 'video' + kind: 'trace' | 'video' | 'screenshot' path: string scope: 'session' | 'spec' | 'test' /** specFile for spec scope, testUid for test scope. */ diff --git a/packages/core/src/video-slice.ts b/packages/core/src/video-slice.ts new file mode 100644 index 00000000..d20e71bb --- /dev/null +++ b/packages/core/src/video-slice.ts @@ -0,0 +1,69 @@ +/** + * Per-test video: slice the continuous screencast frame buffer to one test's + * wall-clock window and encode that slice to a `.webm`. Framework-agnostic — + * the adapter supplies the recorder's frames and the test's start time; the + * recorder itself (CDP push on Chrome, screenshot polling elsewhere) lives in + * each adapter. Best-effort: too few frames or a missing ffmpeg yields no + * artifact rather than an error, so video never aborts a run. + */ + +import fs from 'node:fs/promises' +import path from 'node:path' +import type { ScreencastFrame } from '@wdio/devtools-shared' +import { fileSlug } from './artifact-naming.js' +import { encodeToVideo } from './video-encoder.js' +import { errorMessage } from './error.js' +import type { TraceArtifact } from './trace-finalizer.js' + +/** Frames captured at or after `startWallTime` (ms) — one test's window, since + * the buffer runs continuously and the next test's frames arrive later. */ +export function sliceFramesFrom( + frames: readonly ScreencastFrame[], + startWallTime: number +): ScreencastFrame[] { + return frames.filter((f) => f.timestamp >= startWallTime) +} + +/** + * Encode a test's frame slice to `<outputDir>/video-<uid>-<session>.webm` and + * return the artifact. Returns undefined when there are too few frames to make + * a video or when encoding fails (e.g. fluent-ffmpeg absent) — video is + * best-effort and must never abort the run. + */ +export async function encodePerTestVideo(input: { + frames: readonly ScreencastFrame[] + outputDir: string + testUid: string + sessionId: string + /** 0-based attempt; a `-retry<n>` suffix for n>0 keeps retries from + * overwriting each other's video (mirrors the trace slice's retry keys). */ + attempt?: number + captureFormat?: 'jpeg' | 'png' + minFrames?: number + onLog?: (level: 'info' | 'warn', message: string) => void +}): Promise<TraceArtifact | undefined> { + const minFrames = input.minFrames ?? 2 + if (input.frames.length < minFrames) { + return undefined + } + await fs.mkdir(input.outputDir, { recursive: true }) + const retrySuffix = input.attempt ? `-retry${input.attempt}` : '' + const filename = `video-${fileSlug(input.testUid)}-${input.sessionId.slice(0, 8)}${retrySuffix}.webm` + const filePath = path.join(input.outputDir, filename) + try { + await encodeToVideo([...input.frames], filePath, { + captureFormat: input.captureFormat + }) + } catch (err) { + input.onLog?.('warn', `Per-test video encode failed: ${errorMessage(err)}`) + return undefined + } + return { + kind: 'video', + path: filePath, + scope: 'test', + key: input.testUid, + testUids: [input.testUid], + retained: true + } +} diff --git a/packages/core/tests/output-dir.test.ts b/packages/core/tests/output-dir.test.ts index f44ba3ef..a3226cfa 100644 --- a/packages/core/tests/output-dir.test.ts +++ b/packages/core/tests/output-dir.test.ts @@ -4,7 +4,7 @@ import path from 'node:path' import { afterEach, beforeEach, describe, expect, it } from 'vitest' import { resolveAdapterOutputDir } from '../src/output-dir.js' -/** Every resolved dir is grouped under this subfolder (Playwright-style). */ +/** Every resolved dir is grouped under this subfolder */ const grouped = (base: string) => path.join(base, 'test-results') describe('resolveAdapterOutputDir', () => { diff --git a/packages/core/tests/screenshot-artifact.test.ts b/packages/core/tests/screenshot-artifact.test.ts new file mode 100644 index 00000000..a24a1efe --- /dev/null +++ b/packages/core/tests/screenshot-artifact.test.ts @@ -0,0 +1,53 @@ +import { describe, it, expect, afterEach } from 'vitest' +import { mkdtemp, readFile, rm } from 'node:fs/promises' +import { tmpdir } from 'node:os' +import { join } from 'node:path' +import { + shouldCaptureScreenshot, + writeScreenshotArtifact +} from '../src/screenshot-artifact.js' + +describe('shouldCaptureScreenshot', () => { + it('captures for `on` regardless of outcome', () => { + expect(shouldCaptureScreenshot('on', false)).toBe(true) + expect(shouldCaptureScreenshot('on', true)).toBe(true) + }) + + it('captures for `only-on-failure` only when failed', () => { + expect(shouldCaptureScreenshot('only-on-failure', true)).toBe(true) + expect(shouldCaptureScreenshot('only-on-failure', false)).toBe(false) + }) + + it('never captures for `off` or undefined', () => { + expect(shouldCaptureScreenshot('off', true)).toBe(false) + expect(shouldCaptureScreenshot(undefined, true)).toBe(false) + }) +}) + +describe('writeScreenshotArtifact', () => { + const dirs: string[] = [] + afterEach(async () => { + await Promise.all( + dirs.splice(0).map((d) => rm(d, { recursive: true, force: true })) + ) + }) + + it('writes the decoded PNG and returns a test-scoped screenshot artifact', async () => { + const dir = await mkdtemp(join(tmpdir(), 'shot-')) + dirs.push(dir) + const base64 = Buffer.from('png-bytes').toString('base64') + const artifact = await writeScreenshotArtifact({ + outputDir: dir, + testUid: 'suite > a test/name', + sessionId: 'abcd1234ef', + base64 + }) + expect(artifact.kind).toBe('screenshot') + expect(artifact.scope).toBe('test') + expect(artifact.key).toBe('suite > a test/name') + expect(artifact.retained).toBe(true) + // Filename is slugged (no spaces/slashes) and session-scoped. + expect(artifact.path).toMatch(/screenshot-suite-a-test-name-abcd1234\.png$/) + expect(await readFile(artifact.path, 'utf8')).toBe('png-bytes') + }) +}) diff --git a/packages/core/tests/video-slice.test.ts b/packages/core/tests/video-slice.test.ts new file mode 100644 index 00000000..c2de429a --- /dev/null +++ b/packages/core/tests/video-slice.test.ts @@ -0,0 +1,100 @@ +import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest' +import { mkdtemp, rm } from 'node:fs/promises' +import { tmpdir } from 'node:os' +import { join } from 'node:path' +import type { ScreencastFrame } from '@wdio/devtools-shared' + +const { encodeToVideo } = vi.hoisted(() => ({ encodeToVideo: vi.fn() })) +vi.mock('../src/video-encoder.js', () => ({ encodeToVideo })) + +import { sliceFramesFrom, encodePerTestVideo } from '../src/video-slice.js' + +const frames = (ts: number[]): ScreencastFrame[] => + ts.map((timestamp) => ({ data: 'AAAA', timestamp })) + +describe('sliceFramesFrom', () => { + it('keeps only frames at or after the window start', () => { + const all = frames([100, 200, 300, 400]) + expect(sliceFramesFrom(all, 250).map((f) => f.timestamp)).toEqual([ + 300, 400 + ]) + }) + + it('returns all frames when start precedes them', () => { + expect(sliceFramesFrom(frames([100, 200]), 0)).toHaveLength(2) + }) +}) + +describe('encodePerTestVideo', () => { + const dirs: string[] = [] + beforeEach(() => encodeToVideo.mockReset()) + afterEach(async () => { + await Promise.all( + dirs.splice(0).map((d) => rm(d, { recursive: true, force: true })) + ) + }) + async function dir() { + const d = await mkdtemp(join(tmpdir(), 'vid-')) + dirs.push(d) + return d + } + + it('returns undefined below the min-frames threshold (no encode)', async () => { + const artifact = await encodePerTestVideo({ + frames: frames([1]), + outputDir: await dir(), + testUid: 'u1', + sessionId: 'sess1234' + }) + expect(artifact).toBeUndefined() + expect(encodeToVideo).not.toHaveBeenCalled() + }) + + it('encodes and returns a test-scoped video artifact', async () => { + const d = await dir() + encodeToVideo.mockResolvedValueOnce(undefined) + const artifact = await encodePerTestVideo({ + frames: frames([1, 2, 3]), + outputDir: d, + testUid: 'my/test', + sessionId: 'sess1234ef' + }) + expect(artifact).toBeDefined() + expect(artifact!.kind).toBe('video') + expect(artifact!.scope).toBe('test') + expect(artifact!.key).toBe('my/test') + expect(artifact!.path).toMatch(/video-my-test-sess1234\.webm$/) + expect(encodeToVideo).toHaveBeenCalledOnce() + }) + + it('suffixes the filename with the attempt so retries do not overwrite', async () => { + encodeToVideo.mockResolvedValue(undefined) + const a0 = await encodePerTestVideo({ + frames: frames([1, 2]), + outputDir: await dir(), + testUid: 'u1', + sessionId: 'sess1234', + attempt: 0 + }) + const a1 = await encodePerTestVideo({ + frames: frames([1, 2]), + outputDir: await dir(), + testUid: 'u1', + sessionId: 'sess1234', + attempt: 1 + }) + expect(a0!.path).toMatch(/video-u1-sess1234\.webm$/) + expect(a1!.path).toMatch(/video-u1-sess1234-retry1\.webm$/) + }) + + it('returns undefined (no throw) when the encoder fails', async () => { + encodeToVideo.mockRejectedValueOnce(new Error('ffmpeg missing')) + const artifact = await encodePerTestVideo({ + frames: frames([1, 2, 3]), + outputDir: await dir(), + testUid: 'u1', + sessionId: 'sess1234' + }) + expect(artifact).toBeUndefined() + }) +}) diff --git a/packages/service/README.md b/packages/service/README.md index d5532798..bfd4002f 100644 --- a/packages/service/README.md +++ b/packages/service/README.md @@ -46,8 +46,58 @@ services: [['devtools', options]] | `port` | `number` | random | Port the DevTools UI server listens on | | `hostname` | `string` | `'localhost'` | Hostname the DevTools UI server binds to | | `devtoolsCapabilities` | `Capabilities` | Chrome 1600×1200 | Capabilities used to open the DevTools UI window | -| `screencast` | `ScreencastOptions` | — | Session video recording (live mode only — see below) | +| `screencast` | `ScreencastOptions` | — | Session video recording (live mode only — see below; for trace mode use `video`) | | `mode` | `'live' \| 'trace'` | `'live'` | `'live'` opens the DevTools UI window; `'trace'` skips the UI and writes a `trace-<sessionId>.zip` at session end. See [Trace mode](../../README.md#-trace-mode-tracezip) | +| `traceGranularity` | `'session' \| 'spec' \| 'test'` | `'session'` | Trace mode only. How traces are partitioned. `'test'` is required for per-test Allure attachments (trace, screenshot, video). | +| `tracePolicy` | `TraceRetentionPolicy` | `'on'` | Trace mode only. Which traces to keep — e.g. `'retain-on-failure'`, `'retain-on-first-failure'`. | +| `screenshot` | `'off' \| 'on' \| 'only-on-failure'` | `'off'` | Trace mode + `traceGranularity: 'test'`. Per-test screenshot, attached inline to Allure (`image/png`). | +| `video` | `'off' \| TraceRetentionPolicy` | `'off'` | Trace mode + `traceGranularity: 'test'`. Per-test screencast video, retained per the given policy, attached inline to Allure (`video/webm`). | + +## Allure integration + +When `@wdio/allure-reporter` is installed, trace-mode artifacts are attached to +the Allure report automatically: + +- **`traceGranularity: 'test'`** — each test's trace (`application/zip`, a + download that opens in `pnpm show-trace`), screenshot (`image/png`, inline) + and video (`video/webm`, inline) attach to that test's card. This is the mode + to use for a per-test Allure report. +- **`traceGranularity: 'session'` / `'spec'`** — a session/spec-spanning + `trace.zip` is written to disk and enumerated in + `devtools-artifacts-<sessionId>.json` (the artifacts manifest, listing every + artifact + each test's state), but it is **not** attached to individual test + cards. + +### Why session/spec traces aren't attached per test + +The reporter's `addAttachment` targets the **currently-running test**. A +session/spec trace is only finalized **after** all its tests have run — by which +point their Allure cards are already closed — so there is no open test to attach +it to. Per-test attachment therefore requires `traceGranularity: 'test'`, where +each slice is written during its own test hook while the card is still open. + +To surface a session/spec trace in Allure anyway, post-process the manifest in +your **own** `onComplete` hook (copying the `trace.zip` into `allure-results/` +and appending it to the result files). This is deliberately left to userland — +baking it into the adapter would couple it to Allure's on-disk result format. + +### Report noise + +In trace mode the service captures a per-action snapshot (a `takeScreenshot` +WebDriver command) to build the trace timeline; `@wdio/allure-reporter` logs +every WebDriver command as a step and attaches a screenshot per `takeScreenshot`. +Silence that flood with the reporter's own options — the `trace.zip` / +screenshot / video attachments are unaffected: + +```ts +reporters: [ + ['allure', { + outputDir: 'allure-results', + disableWebdriverStepsReporting: true, + disableWebdriverScreenshotsReporting: true + }] +] +``` ## Screencast Recording diff --git a/packages/service/src/allure.ts b/packages/service/src/allure.ts index ce0e678d..ce415529 100644 --- a/packages/service/src/allure.ts +++ b/packages/service/src/allure.ts @@ -16,6 +16,13 @@ const log = logger('@wdio/devtools-service') // uses video/webm so Allure renders it inline. const TRACE_CONTENT_TYPE = 'application/zip' const VIDEO_CONTENT_TYPE = 'video/webm' +const SCREENSHOT_CONTENT_TYPE = 'image/png' + +const CONTENT_TYPE_BY_KIND: Record<TraceArtifact['kind'], string> = { + trace: TRACE_CONTENT_TYPE, + video: VIDEO_CONTENT_TYPE, + screenshot: SCREENSHOT_CONTENT_TYPE +} /** The one @wdio/allure-reporter method we use. Typed locally so the optional * peer dependency never becomes a build-time type dependency. */ @@ -75,8 +82,7 @@ export async function attachArtifactToAllure( return } const content = await fs.readFile(artifact.path) - const type = - artifact.kind === 'video' ? VIDEO_CONTENT_TYPE : TRACE_CONTENT_TYPE + const type = CONTENT_TYPE_BY_KIND[artifact.kind] reporter.addAttachment(basename(artifact.path), content, type) } catch (err) { // A missing/unreadable artifact must never reject the test hook. diff --git a/packages/service/src/index.ts b/packages/service/src/index.ts index 39c61610..732ff52e 100644 --- a/packages/service/src/index.ts +++ b/packages/service/src/index.ts @@ -14,10 +14,13 @@ import { type TraceExportContext } from '@wdio/devtools-core' import { attachArtifactToAllure } from './allure.js' +import { captureAndAttachScreenshot } from './screenshot-capture.js' +import { captureAndAttachVideo } from './video-capture.js' import { wireAssertCapture, type ExpectAssertion } from './assert-capture.js' import { AssertionTracker } from './assertion-tracker.js' import { cucumberScenarioUid, + isFailedResult, resolveTestAttempt, stampTestState, testMetadataUid, @@ -33,7 +36,11 @@ import { dedupeSnapshotsByTimestamp, upsertRichestSnapshot } from './snapshot-dedupe.js' -import type { ActionSnapshot, TestMetadataMap } from '@wdio/devtools-shared' +import type { + ActionSnapshot, + ScreencastFrame, + TestMetadataMap +} from '@wdio/devtools-shared' import { SevereServiceError } from 'webdriverio' import type { Services, Capabilities, Options, Reporters } from '@wdio/types' import type { WebDriverCommands } from '@wdio/protocols' @@ -95,11 +102,14 @@ export default class DevToolsHookService implements Services.ServiceInstance { log.warn(policyWarning) } if (serviceOptions.mode === 'trace' && serviceOptions.screencast?.enabled) { - log.warn('trace mode: ignoring screencast option (live-mode feature)') - this.#screencastOptions = undefined - } else { - this.#screencastOptions = serviceOptions.screencast + log.warn( + 'trace mode: `screencast.enabled` is ignored — use `video` to record; ' + + 'the tuning fields (quality/interval) still apply' + ) } + // Tuning is kept for both modes; whether we actually record is decided by + // #shouldRecordScreencast (screencast.enabled in live, `video` in trace). + this.#screencastOptions = serviceOptions.screencast } /** @@ -111,6 +121,19 @@ export default class DevToolsHookService implements Services.ServiceInstance { /** Current test UID, set in beforeTest(), used by afterCommand() to tag commands. */ #currentTestUid?: string + /** Wall-clock ms at the current test's start, set in beforeTest/beforeScenario; + * the lower bound of that test's video frame window (per-test slicing). */ + #currentTestStartWallTime = 0 + + /** Recorder frames snapshotted in onReload before reloadSession replaces the + * recorder — so the ending test's per-test video can still be sliced in + * afterScenario (which runs AFTER the cucumber After hook's reloadSession). */ + #pendingVideoFrames?: { + testUid: string | undefined + startWallTime: number + frames: ScreencastFrame[] + } + /** Map of testUid → metadata for trace group events and per-spec partitioning. */ #testMetadata: TestMetadataMap = new Map() @@ -159,6 +182,34 @@ export default class DevToolsHookService implements Services.ServiceInstance { } } + /** Base64 of the last rendered action snapshot for the current test, skipping + * the end-of-scenario `__final__` frame — that one is captured post-teardown + * and can come back blank when a reloadSession runs before afterScenario. + * Used as the per-test screenshot so it's the real failure-moment frame, + * reload-immune. Scoped to this test's window (>= its start) so a test that + * captured nothing doesn't borrow the previous test's frame. */ + #lastRenderedScreenshot(): string | undefined { + for (let i = this.#actionSnapshots.length - 1; i >= 0; i--) { + const snap = this.#actionSnapshots[i]! + if (snap.timestamp < this.#currentTestStartWallTime) { + return undefined + } + if (snap.command !== '__final__' && snap.screenshot) { + return snap.screenshot + } + } + return undefined + } + + /** Record a screencast this session? Live mode: `screencast.enabled`. Trace + * mode: a non-`off` `video` policy (frames are sliced per test at flush). */ + #shouldRecordScreencast(): boolean { + if (this.#options.mode === 'trace') { + return !!this.#options.video && this.#options.video !== 'off' + } + return !!this.#screencastOptions?.enabled + } + /** Eager per-test flush at test end (test granularity only), run after the * outcome is stamped so this attempt's metadata is written before a retry * overwrites it; the end-of-run finalizer then dedupes it via the key set. */ @@ -254,12 +305,14 @@ export default class DevToolsHookService implements Services.ServiceInstance { } /** - * Start screencast recording if the user has enabled it. - * Options come from the service constructor (services: [['devtools', { screencast: { enabled: true } }]]). - * Failures are non-fatal — a warning is logged and the session continues. + * Start screencast recording when enabled — `screencast.enabled` in live + * mode, or a non-`off` `video` policy in trace mode (per-test slicing at + * flush time). Failures are non-fatal — logged, session continues. */ - if (this.#screencastOptions?.enabled) { - this.#screencastRecorder = new ScreencastRecorder(this.#screencastOptions) + if (this.#shouldRecordScreencast()) { + this.#screencastRecorder = new ScreencastRecorder( + this.#screencastOptions ?? {} + ) await this.#screencastRecorder.start(browser) } @@ -330,6 +383,7 @@ export default class DevToolsHookService implements Services.ServiceInstance { pickle?: { uri?: string; name?: string; astNodeIds?: readonly string[] } }) { this.resetStack() + this.#currentTestStartWallTime = Date.now() const featureFile = world?.pickle?.uri const scenarioName = world?.pickle?.name @@ -360,6 +414,7 @@ export default class DevToolsHookService implements Services.ServiceInstance { /** Mocha/Jasmine hook: the beforeScenario equivalent for file-based specs. */ beforeTest(test?: { file?: string; title?: string; fullTitle?: string }) { this.resetStack() + this.#currentTestStartWallTime = Date.now() // Track test identity for command tagging. Generate a stable UID // from file + title so commands can be partitioned across reruns. @@ -419,7 +474,7 @@ export default class DevToolsHookService implements Services.ServiceInstance { this.#stampOutcome(uid, result) } await this.#finalizePerScenario() - await this.#eagerFlushAndAttach(uid) + await this.#emitTestArtifacts(uid, isFailedResult(result)) } async afterTest( @@ -434,20 +489,60 @@ export default class DevToolsHookService implements Services.ServiceInstance { this.#stampOutcome(uid, result) } await this.#finalizePerScenario() - await this.#eagerFlushAndAttach(uid) + await this.#emitTestArtifacts(uid, isFailedResult(result)) } - /** Flush this test's slice (so it captures the final snapshot + stamped - * outcome), then attach the retained artifact to Allure while the per-test - * hook is still open. No-op outside `test`-granularity trace mode. */ - async #eagerFlushAndAttach(uid: string | undefined): Promise<void> { - if (!uid) { - return - } - const artifact = await this.#eagerFlushTestSlice(uid) - if (artifact) { - await attachArtifactToAllure(artifact) + /** At test end, while the per-test hook is still open: eager-flush this test's + * slice (so it captures the final snapshot + stamped outcome) and attach the + * retained trace to Allure, then capture the per-test screenshot per policy + * and attach it too. Each part no-ops when its feature is off. */ + async #emitTestArtifacts( + uid: string | undefined, + failed: boolean + ): Promise<void> { + if (uid) { + const artifact = await this.#eagerFlushTestSlice(uid) + if (artifact) { + await attachArtifactToAllure(artifact) + } } + await captureAndAttachScreenshot({ + mode: this.#options.mode, + granularity: this.#options.traceGranularity, + policy: this.#options.screenshot, + failed, + screenshotBase64: this.#lastRenderedScreenshot(), + sessionId: this.#browser?.sessionId, + outputDir: this.#outputDir, + testUid: uid, + onArtifact: (a) => this.#artifacts.push(a) + }) + // Authoritative attempt for this test (stamped into metadata by + // #stampOutcome, which ran just before this). Scopes retention + the video + // filename to this attempt so retries don't overwrite each other. + const attempt = uid ? this.#testMetadata.get(uid)?.attempt : undefined + // Prefer frames snapshotted in onReload (reloadSession tears the recorder + // down before this hook); fall back to the live recorder otherwise. + const pending = + this.#pendingVideoFrames?.testUid === uid + ? this.#pendingVideoFrames + : undefined + this.#pendingVideoFrames = undefined + await captureAndAttachVideo({ + mode: this.#options.mode, + granularity: this.#options.traceGranularity, + policy: this.#options.video, + frames: pending?.frames ?? this.#screencastRecorder?.frames, + startWallTime: pending?.startWallTime ?? this.#currentTestStartWallTime, + outcomes: uid ? this.#attemptTracker.forTest(uid, attempt) : [], + attempt, + outputDir: this.#outputDir, + testUid: uid, + sessionId: this.#browser?.sessionId, + captureFormat: this.#screencastOptions?.captureFormat, + onArtifact: (a) => this.#artifacts.push(a), + onLog: (level, msg) => log[level](msg) + }) } /** expect-webdriverio matcher hooks — delegated to the assertion tracker. */ @@ -646,16 +741,30 @@ export default class DevToolsHookService implements Services.ServiceInstance { * on the new session so the second scenario is also covered. */ async onReload(oldSessionId: string, _newSessionId: string) { - if (!this.#screencastOptions?.enabled || !this.#browser) { + if (!this.#shouldRecordScreencast() || !this.#browser) { return } + // Trace mode: the ending test's afterScenario runs AFTER this reload (a + // cucumber `After(() => reloadSession())` is WDIO boilerplate), by which + // point the recorder below has replaced these frames. Snapshot them now, + // keyed to the ending test, so afterScenario can still slice its video. + if (this.#options.mode === 'trace' && this.#screencastRecorder) { + this.#pendingVideoFrames = { + testUid: this.#currentTestUid, + startWallTime: this.#currentTestStartWallTime, + frames: [...this.#screencastRecorder.frames] + } + } + // Finalize the recording from the old session (CDP is already gone, so // stop() will fail gracefully and we encode whatever frames arrived). await this.#finalizeScreencast(oldSessionId) // Start a new recorder for the new session. - this.#screencastRecorder = new ScreencastRecorder(this.#screencastOptions) + this.#screencastRecorder = new ScreencastRecorder( + this.#screencastOptions ?? {} + ) await this.#screencastRecorder.start(this.#browser) } @@ -691,6 +800,13 @@ export default class DevToolsHookService implements Services.ServiceInstance { if (!this.#screencastRecorder) { return } + // Trace mode: the video is emitted per-test (sliced in #emitTestArtifacts), + // and there's no dashboard to receive a session recording — so just stop the + // recorder to release resources; never encode an orphan session-wide webm. + if (this.#options.mode === 'trace') { + await this.#screencastRecorder.stop() + return + } // Skip ghost sessions: browser.reloadSession() creates a new session at // the end of a test run that has no steps — it captures at most a handful // of frames before teardown. Require at least 5 frames so we don't produce diff --git a/packages/service/src/screenshot-capture.ts b/packages/service/src/screenshot-capture.ts new file mode 100644 index 00000000..9aca639c --- /dev/null +++ b/packages/service/src/screenshot-capture.ts @@ -0,0 +1,67 @@ +// Per-test failure/always screenshot for the WDIO adapter: the policy gate and +// the Allure attach. Kept out of index.ts so the god-file stays lean and the +// capture is unit-testable. The policy decision and file write live in core +// (framework-agnostic). +// +// The image is NOT a fresh screenshot taken here — it's the last rendered action +// snapshot the service already captured during the test. A fresh end-of-test +// takeScreenshot() is unreliable: a cucumber `After(() => reloadSession())` hook +// (WDIO boilerplate) runs before the service afterScenario hook and blanks the +// page, so an end-of-test capture comes back empty. The last action snapshot was +// captured mid-command, before any teardown, so it's the real failure-moment +// frame — and reusing it also drops an extra WebDriver command from the report. + +import { + shouldCaptureScreenshot, + writeScreenshotArtifact, + type TraceArtifact +} from '@wdio/devtools-core' +import type { + DevToolsMode, + TraceGranularity, + TraceScreenshotPolicy +} from '@wdio/devtools-shared' +import { attachArtifactToAllure } from './allure.js' + +/** + * Write the per-test screenshot (per the policy) from an already-captured base64 + * frame and attach it to Allure. No-op outside `test`-granularity trace mode, + * without a uid/session/frame, or when the policy declines. Gated to `test` + * granularity so the whole per-test inline story (trace + screenshot + video) is + * one rule; coarser granularities keep their artifacts in the manifest. The + * artifact is handed to `onArtifact` so it lands in the manifest too. + */ +export async function captureAndAttachScreenshot(input: { + mode: DevToolsMode | undefined + granularity: TraceGranularity | undefined + policy: TraceScreenshotPolicy | undefined + failed: boolean + /** Base64 of the last rendered action snapshot (reload-immune, not a fresh + * end-of-test capture). Undefined when the test recorded no snapshot. */ + screenshotBase64: string | undefined + sessionId: string | undefined + outputDir: string + testUid: string | undefined + onArtifact: (artifact: TraceArtifact) => void +}): Promise<void> { + const { mode, granularity, policy, failed, screenshotBase64, sessionId } = + input + if ( + mode !== 'trace' || + granularity !== 'test' || + !screenshotBase64 || + !sessionId || + !input.testUid || + !shouldCaptureScreenshot(policy, failed) + ) { + return + } + const artifact = await writeScreenshotArtifact({ + outputDir: input.outputDir, + testUid: input.testUid, + sessionId, + base64: screenshotBase64 + }) + input.onArtifact(artifact) + await attachArtifactToAllure(artifact) +} diff --git a/packages/service/src/test-metadata.ts b/packages/service/src/test-metadata.ts index dc2babad..5e37ba20 100644 --- a/packages/service/src/test-metadata.ts +++ b/packages/service/src/test-metadata.ts @@ -46,6 +46,14 @@ export function resultToState(result: { return result.passed ? 'passed' : 'failed' } +/** True when a result represents a failed test (not passed, not skipped). + * Absent result → not failed (nothing ran to fail). */ +export function isFailedResult( + result?: Pick<TestOutcomeResult, 'passed' | 'skipped'> +): boolean { + return result ? resultToState(result) === 'failed' : false +} + /** Stamp the final state (and, when known, the 0-based attempt) onto the * metadata entry beforeTest/beforeScenario created, so retention can gate its * trace per attempt. No-op when there's no entry. */ diff --git a/packages/service/src/video-capture.ts b/packages/service/src/video-capture.ts new file mode 100644 index 00000000..ff8b1d06 --- /dev/null +++ b/packages/service/src/video-capture.ts @@ -0,0 +1,80 @@ +// Per-test video for the WDIO adapter: the granularity/policy gate, the +// retention decision, the frame-window slice, the encode, and the Allure +// attach. Kept out of index.ts so the god-file stays lean. The slice + encode +// live in core (framework-agnostic); only the recorder is WDIO-specific. + +import { + encodePerTestVideo, + shouldRetainTrace, + sliceFramesFrom, + type TestOutcome, + type TraceArtifact +} from '@wdio/devtools-core' +import type { + DevToolsMode, + ScreencastFrame, + TraceGranularity, + TraceVideoPolicy +} from '@wdio/devtools-shared' +import { attachArtifactToAllure } from './allure.js' + +/** + * Slice the continuous screencast to this test's window, encode a `.webm`, and + * attach it to Allure — when trace mode + `test` granularity + a non-`off` + * `video` policy that retains this test's outcome. No-op otherwise. The encoded + * artifact is handed to `onArtifact` so it lands in the manifest. + */ +export async function captureAndAttachVideo(input: { + mode: DevToolsMode | undefined + granularity: TraceGranularity | undefined + policy: TraceVideoPolicy | undefined + frames: readonly ScreencastFrame[] | undefined + startWallTime: number + outcomes: TestOutcome[] + attempt: number | undefined + outputDir: string + testUid: string | undefined + sessionId: string | undefined + captureFormat?: 'jpeg' | 'png' + onArtifact: (artifact: TraceArtifact) => void + onLog?: (level: 'info' | 'warn', message: string) => void +}): Promise<void> { + const { mode, granularity, policy, frames, testUid, sessionId } = input + if ( + mode !== 'trace' || + granularity !== 'test' || + !policy || + policy === 'off' || + !frames || + !testUid || + !sessionId || + // No recorded attempt for this uid — the test never started (e.g. a + // skipped/pending test whose afterTest fires without a beforeTest). Skip + // rather than fail-open on empty outcomes, which would slice the PREVIOUS + // test's frames into a video attributed to this one. + input.outcomes.length === 0 + ) { + return + } + const decision = shouldRetainTrace(policy, { + outcomes: input.outcomes, + attemptInfoAvailable: true + }) + if (!decision.retain) { + return + } + const artifact = await encodePerTestVideo({ + frames: sliceFramesFrom(frames, input.startWallTime), + outputDir: input.outputDir, + testUid, + sessionId, + attempt: input.attempt, + captureFormat: input.captureFormat, + onLog: input.onLog + }) + if (!artifact) { + return + } + input.onArtifact(artifact) + await attachArtifactToAllure(artifact) +} diff --git a/packages/service/tests/allure.test.ts b/packages/service/tests/allure.test.ts index e8305f6a..aa269eed 100644 --- a/packages/service/tests/allure.test.ts +++ b/packages/service/tests/allure.test.ts @@ -56,7 +56,7 @@ describe('attachArtifactToAllure', () => { expect(addAttachment).not.toHaveBeenCalled() }) - it('attaches a trace zip with the Allure Playwright content type', async () => { + it('attaches a trace zip as a plain application/zip download', async () => { const path = await tempZip('trace-abc.zip') await attachArtifactToAllure(artifact({ path })) expect(addAttachment).toHaveBeenCalledOnce() @@ -73,6 +73,13 @@ describe('attachArtifactToAllure', () => { expect(type).toBe('video/webm') }) + it('attaches a screenshot artifact as image/png', async () => { + const path = await tempZip('screenshot-u1.png') + await attachArtifactToAllure(artifact({ kind: 'screenshot', path })) + const [, , type] = addAttachment.mock.calls[0]! + expect(type).toBe('image/png') + }) + it('skips a directory-format artifact without throwing (ndjson-directory)', async () => { const dir = await mkdtemp(join(tmpdir(), 'allure-dir-')) dirs.push(dir) diff --git a/packages/service/tests/screenshot-capture.test.ts b/packages/service/tests/screenshot-capture.test.ts new file mode 100644 index 00000000..7c3bdb1d --- /dev/null +++ b/packages/service/tests/screenshot-capture.test.ts @@ -0,0 +1,86 @@ +import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest' +import { mkdtemp, rm, readdir } from 'node:fs/promises' +import { tmpdir } from 'node:os' +import { join } from 'node:path' +import type { TraceArtifact } from '@wdio/devtools-core' + +const addAttachment = vi.fn() +vi.mock('@wdio/allure-reporter', () => ({ + addAttachment, + default: { addAttachment } +})) + +import { captureAndAttachScreenshot } from '../src/screenshot-capture.js' +import { resetAllureReporterCache } from '../src/allure.js' + +const SHOT = Buffer.from('png-bytes').toString('base64') + +describe('captureAndAttachScreenshot', () => { + const dirs: string[] = [] + const collected: TraceArtifact[] = [] + beforeEach(() => { + addAttachment.mockReset() + resetAllureReporterCache() + collected.length = 0 + }) + afterEach(async () => { + await Promise.all( + dirs.splice(0).map((d) => rm(d, { recursive: true, force: true })) + ) + }) + + async function run( + over: Partial<Parameters<typeof captureAndAttachScreenshot>[0]> + ) { + const dir = await mkdtemp(join(tmpdir(), 'shot-cap-')) + dirs.push(dir) + await captureAndAttachScreenshot({ + mode: 'trace', + granularity: 'test', + policy: 'only-on-failure', + failed: true, + screenshotBase64: SHOT, + sessionId: 'sess1234', + outputDir: dir, + testUid: 'u1', + onArtifact: (a) => collected.push(a), + ...over + }) + return { dir } + } + + it('writes + attaches the reused snapshot on failure under only-on-failure', async () => { + const { dir } = await run({}) + expect(collected).toHaveLength(1) + expect(collected[0]!.kind).toBe('screenshot') + expect(addAttachment).toHaveBeenCalledOnce() + expect(await readdir(dir)).toHaveLength(1) + }) + + it('does nothing on a passing test under only-on-failure', async () => { + await run({ failed: false }) + expect(collected).toHaveLength(0) + expect(addAttachment).not.toHaveBeenCalled() + }) + + it('does nothing outside trace mode', async () => { + await run({ mode: 'live', policy: 'on' }) + expect(collected).toHaveLength(0) + }) + + it('does nothing outside test granularity (uniform rule)', async () => { + await run({ granularity: 'session', policy: 'on' }) + expect(collected).toHaveLength(0) + }) + + it('does nothing without a captured frame (no snapshot to reuse)', async () => { + await run({ policy: 'on', screenshotBase64: undefined }) + expect(collected).toHaveLength(0) + }) + + it('does nothing without a sessionId or uid', async () => { + await run({ sessionId: undefined }) + await run({ testUid: undefined }) + expect(collected).toHaveLength(0) + }) +}) diff --git a/packages/service/tests/video-capture.test.ts b/packages/service/tests/video-capture.test.ts new file mode 100644 index 00000000..25c1c404 --- /dev/null +++ b/packages/service/tests/video-capture.test.ts @@ -0,0 +1,75 @@ +import { describe, it, expect, beforeEach } from 'vitest' +import type { ScreencastFrame } from '@wdio/devtools-shared' +import type { TraceArtifact, TestOutcome } from '@wdio/devtools-core' + +// Every case here is designed to no-op BEFORE the ffmpeg encode (failed gate or +// non-retaining policy), so no encoder/reporter mock is needed — the real +// (pure) shouldRetainTrace decides retention. The encode path itself is covered +// by core's video-slice.test.ts. +import { captureAndAttachVideo } from '../src/video-capture.js' + +const frames: ScreencastFrame[] = [ + { data: 'AAAA', timestamp: 10 }, + { data: 'AAAA', timestamp: 20 }, + { data: 'AAAA', timestamp: 30 } +] + +const failedOutcome: TestOutcome[] = [ + { uid: 'u1', attempt: 0, state: 'failed' } +] +const passedOutcome: TestOutcome[] = [ + { uid: 'u1', attempt: 0, state: 'passed' } +] + +describe('captureAndAttachVideo gating', () => { + const collected: TraceArtifact[] = [] + beforeEach(() => { + collected.length = 0 + }) + + const base = { + mode: 'trace' as const, + granularity: 'test' as const, + policy: 'retain-on-failure' as const, + frames, + startWallTime: 0, + outcomes: failedOutcome, + attempt: 0, + outputDir: '/tmp/does-not-encode', + testUid: 'u1', + sessionId: 'sess1234', + onArtifact: (a: TraceArtifact) => collected.push(a) + } + + it('no-ops outside test granularity', async () => { + await captureAndAttachVideo({ ...base, granularity: 'session' }) + expect(collected).toHaveLength(0) + }) + + it('no-ops when the test never started (empty outcomes — no fail-open)', async () => { + await captureAndAttachVideo({ ...base, outcomes: [] }) + expect(collected).toHaveLength(0) + }) + + it('no-ops when video policy is off/undefined', async () => { + await captureAndAttachVideo({ ...base, policy: 'off' }) + await captureAndAttachVideo({ ...base, policy: undefined }) + expect(collected).toHaveLength(0) + }) + + it('no-ops when there are no frames', async () => { + await captureAndAttachVideo({ ...base, frames: undefined }) + expect(collected).toHaveLength(0) + }) + + it('no-ops when the policy does not retain this outcome (passing + retain-on-failure)', async () => { + await captureAndAttachVideo({ ...base, outcomes: passedOutcome }) + expect(collected).toHaveLength(0) + }) + + it('no-ops without a sessionId or uid', async () => { + await captureAndAttachVideo({ ...base, sessionId: undefined }) + await captureAndAttachVideo({ ...base, testUid: undefined }) + expect(collected).toHaveLength(0) + }) +}) diff --git a/packages/shared/src/types.ts b/packages/shared/src/types.ts index 1e2c7033..b360936d 100644 --- a/packages/shared/src/types.ts +++ b/packages/shared/src/types.ts @@ -49,6 +49,17 @@ export type TraceRetentionPolicy = | 'on-all-retries' | 'retain-on-failure-and-retries' +/** Per-test screenshot capture policy, mirroring Playwright's `screenshot` + * option. `only-on-failure` shoots after a failing test; `on` after every + * test; `off` (default) never. Only applies in trace mode. */ +export type TraceScreenshotPolicy = 'off' | 'on' | 'only-on-failure' + +/** Per-test video capture policy. `off` (default) records nothing; any other + * value records the screencast and keeps each test's video slice per the same + * retention semantics as `tracePolicy`. Only applies in trace mode at + * `traceGranularity: 'test'` (the per-test scope videos attach to). */ +export type TraceVideoPolicy = 'off' | TraceRetentionPolicy + /** One node in a test's ancestor chain, outermost first. */ export interface TestAncestor { uid: string @@ -306,6 +317,14 @@ export interface BaseDevToolsOptions { /** Trace retention policy — gates which traces are kept (e.g. * `retain-on-failure`). Default `on` (keep all). Only applies in trace mode. */ tracePolicy?: TraceRetentionPolicy + /** Per-test screenshot capture, attached to the trace artifacts and to Allure + * inline. `off` (default) | `on` | `only-on-failure`. Only applies in trace + * mode. */ + screenshot?: TraceScreenshotPolicy + /** Per-test video (screencast) capture, retained per the given policy and + * attached to Allure inline. `off` (default) or a retention policy. Only + * applies in trace mode at `traceGranularity: 'test'`. */ + video?: TraceVideoPolicy } /** Minimal Cucumber pickle-step shape — only the fields the adapters read. diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index a1de3557..8f03e235 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -41,6 +41,9 @@ importers: '@vitest/coverage-v8': specifier: ^4.1.8 version: 4.1.9(@vitest/browser@4.1.9)(vitest@4.1.9) + '@wdio/allure-reporter': + specifier: ^9.29.1 + version: 9.29.1 autoprefixer: specifier: ^10.5.0 version: 10.5.0(postcss@8.5.15) @@ -152,7 +155,7 @@ importers: version: 9.28.0 expect-webdriverio: specifier: ^5.6.7 - version: 5.6.8(@wdio/globals@9.28.0)(@wdio/logger@9.18.0)(webdriverio@9.28.0(puppeteer-core@21.11.0)) + version: 5.6.8(@wdio/globals@9.28.0)(@wdio/logger@9.29.1)(webdriverio@9.28.0(puppeteer-core@21.11.0)) ts-node: specifier: ^10.9.2 version: 10.9.2(@types/node@25.9.3)(typescript@6.0.3) @@ -368,7 +371,7 @@ importers: version: 6.0.3 vitest: specifier: ^4.0.16 - version: 4.1.9(@types/node@25.9.3)(@vitest/coverage-v8@4.1.9)(happy-dom@20.10.4)(jsdom@24.1.3)(vite@8.0.16(@types/node@25.9.3)(esbuild@0.28.0)(jiti@2.7.0)(tsx@4.22.4)(yaml@2.9.0)) + version: 4.1.9(@types/node@25.9.3)(@vitest/coverage-v8@4.1.9)(happy-dom@20.10.4)(jsdom@24.1.3)(vite@8.0.16(@types/node@25.9.3)(esbuild@0.27.7)(jiti@2.7.0)(tsx@4.22.4)(yaml@2.9.0)) packages/nightwatch-devtools: dependencies: @@ -538,6 +541,9 @@ importers: '@types/yazl': specifier: ^2.4.6 version: 2.4.6 + '@wdio/allure-reporter': + specifier: ^9.0.0 + version: 9.29.1 '@wdio/devtools-backend': specifier: workspace:^ version: link:../backend @@ -2711,6 +2717,10 @@ packages: '@volar/typescript@2.4.28': resolution: {integrity: sha512-Ja6yvWrbis2QtN4ClAKreeUZPVYMARDYZl9LMEv1iQ1QdepB6wn0jTRxA9MftYmYa4DQ4k/DaSZpFPUfxl8giw==} + '@wdio/allure-reporter@9.29.1': + resolution: {integrity: sha512-NroTqEN+l0WHVbqS6CH7sTrFtd3d0mgxjPFvFcbzCER1+GA0chTzFT/GCwl4WvNrbbX6a/z7QWT+UwWdXJQJvQ==} + engines: {node: '>=18.20.0'} + '@wdio/cli@9.28.0': resolution: {integrity: sha512-jEKYdCvZ9ST8YQ4EvyV9lsEoRxhWenplGJppbiH9SKHiwPqrUapi/EE7f6CBDwkWP7NIlzj2PyTe+JRmkXILLw==} engines: {node: '>=18.20.0'} @@ -2751,6 +2761,10 @@ packages: resolution: {integrity: sha512-HdzDrRs+ywAqbXGKqe1i/bLtCv47plz4TvsHFH3j729OooT5VH38ctFn5aLXgECmiAKDkmH/A6kOq2Zh5DIxww==} engines: {node: '>=18.20.0'} + '@wdio/logger@9.29.1': + resolution: {integrity: sha512-0ZAEIo6PNyMIJPlOGkIgyOJUjcd0pC8/QHlVAAe1c91/IcjZ1X+k0yidXHaboJdN7dq1XPUacmhRdtua0U5EZg==} + engines: {node: '>=18.20.0'} + '@wdio/mocha-framework@9.28.0': resolution: {integrity: sha512-UdkgigdyxJu0Rpx2ZRSNuB8u8B5nmLBeRjn5JHYTXBp8Ubi+wgFR1+EdLg8k2z+CtuYe3bNHBw7vPpBYuQMLZw==} engines: {node: '>=18.20.0'} @@ -2769,6 +2783,10 @@ packages: resolution: {integrity: sha512-q9gG6SXNTn/9cKF6EJ+aa5sGZM5HAVNsDZ3YU5B0IHg9ufdBuJgfT0LiAsnehLiceEuivuzPyz85vbDb0SFiVA==} engines: {node: '>=18.20.0'} + '@wdio/reporter@9.29.1': + resolution: {integrity: sha512-CKAcVy9BGwvufokMcl3H+yNcvD11Klku/1BPz8bKSRv7/KzueXR06s1cPbJH3tv9r9zxPErxgdVMpulN8I0qAg==} + engines: {node: '>=18.20.0'} + '@wdio/runner@9.28.0': resolution: {integrity: sha512-i6Zj9IKvHqNrRAuYoj56dhI6dXy5IkAxvsxuMih4R+EHLEihDoIwDRouJ9wOme1ZyHZ0Wpc6XDy8Igf1KnqWvQ==} engines: {node: '>=18.20.0'} @@ -2788,6 +2806,10 @@ packages: resolution: {integrity: sha512-75JPq39gifkPNqOSn5C4/A5ZSyXwF+dGr5jfsCubFN9Lk9dKBXfjdbWueSQNpJg0jmE6dVrbT7+9mnDNnO0HdQ==} engines: {node: '>=18.20.0'} + '@wdio/types@9.29.1': + resolution: {integrity: sha512-jp8jgMv6TS35G96YzHZxw3PVN0Dz6xQ6tnMAicndAJ8Jt9AIXb0ywIse4TjaFywakO3dLoEhlyA3ZtR26vmt+w==} + engines: {node: '>=18.20.0'} + '@wdio/utils@8.41.0': resolution: {integrity: sha512-0TcTjBiax1VxtJQ/iQA0ZyYOSHjjX2ARVmEI0AMo9+AuIq+xBfnY561+v8k9GqOMPKsiH/HrK3xwjx8xCVS03g==} engines: {node: ^16.13 || >=18} @@ -2875,6 +2897,14 @@ packages: ajv@8.20.0: resolution: {integrity: sha512-Thbli+OlOj+iMPYFBVBfJ3OmCAnaSyNn4M1vz9T6Gka5Jt9ba/HIR56joy65tY6kx/FCF5VXNB819Y7/GUrBGA==} + allure-js-commons@3.10.2: + resolution: {integrity: sha512-5nAjUF1iNPSQMwKtEsadclSta8C3L705/k/pIzGb9M6P9krgfRaCyaEHt8pkLlCP9NFj5xk3grjtnAhiLeT+Kg==} + peerDependencies: + allure-playwright: 3.10.2 + peerDependenciesMeta: + allure-playwright: + optional: true + ansi-align@3.0.1: resolution: {integrity: sha512-IOfwwBF5iczOjp/WeY4YxyjqAFMQoZufdQWDd19SEExbVLNXqvpzSJ/M7Za4/sCPmQ0+GRquoA7bGcINcxew6w==} @@ -3310,6 +3340,9 @@ packages: chardet@2.1.1: resolution: {integrity: sha512-PsezH1rqdV9VvyNhxxOW32/d75r01NY7TQCmOqomRo15ZSOKbpTFVsfjghxo6JloQUCGnH4k1LGu0R4yCLlWQQ==} + charenc@0.0.2: + resolution: {integrity: sha512-yrLQ/yVUFXkzg7EDQsPieE/53+0RlaWTs+wBrvW36cyilJ2SaDWfl4Yj7MtLTXleV9uEKefbAGUPv2/iWSooRA==} + check-error@1.0.2: resolution: {integrity: sha512-BrgHpW9NURQgzoNyjfq0Wu6VFO6D7IZEmJNdtgNqpzGG8RuNFHt2jQxWlAs4HMe119chBnv+34syEZtc6IhLtA==} @@ -3546,6 +3579,9 @@ packages: resolution: {integrity: sha512-uV2QOWP2nWzsy2aMp8aRibhi9dlzF5Hgh5SHaB9OiTGEyDTiJJyx0uy51QXdyWbtAHNua4XJzUKca3OzKUd3vA==} engines: {node: '>= 8'} + crypt@0.0.2: + resolution: {integrity: sha512-mCxBlsHFYh9C+HVpiEacem8FEBnMXgU9gy4zmNC+SXAZNB/1idgp/aulFJ4FgCi7GPEVbfyng092GqL2k2rmow==} + css-functions-list@3.3.3: resolution: {integrity: sha512-8HFEBPKhOpJPEPu70wJJetjKta86Gw9+CCyCnB3sui2qQfOvRyqBy4IKLKKAwdMpWb2lHXWk9Wb4Z6AmaUT1Pg==} engines: {node: '>=12'} @@ -3576,6 +3612,9 @@ packages: resolution: {integrity: sha512-2z+rWdzbbSZv6/rhtvzvqeZQHrBaqgogqt85sqFNbabZOuFbCVFb8kPeEtZjiKkbrm395irpNKiYeFeLiQnFPg==} engines: {node: '>=18'} + csv-stringify@6.8.1: + resolution: {integrity: sha512-tZ6X6TKQyQgCo5OptXcyAbfN1pwmoxEqELPQ7KFazNErx7kiVsDK8o+VYRXhfMl4N9vvOOLXuioquR2MeP847A==} + data-uri-to-buffer@4.0.1: resolution: {integrity: sha512-0R9ikRb668HB7QDxT1vkpuUBtqc53YyAwMwGeUFKRojY/NWKvdZ+9UYtRfGmhqNbRkTSVpMbmyhXipFFv2cb/A==} engines: {node: '>= 12'} @@ -4559,6 +4598,9 @@ packages: resolution: {integrity: sha512-Y22oTqIU4uuPgEemfz7NDJz6OeKf12Lsu+QC+s3BVpda64lTiMYCyGwg5ki4vFxkMwQdeZDl2adZoqUgdFuTgQ==} engines: {node: '>=18'} + html-entities@2.6.0: + resolution: {integrity: sha512-kig+rMn/QOVRvr7c86gQ8lWXq+Hkv6CbAH1hLu+RG338StTpE8Z0b44SDVaqVu7HGKf27frdmUYEs9hTUX/cLQ==} + html-escaper@2.0.2: resolution: {integrity: sha512-H2iMtd0I4Mt5eYiapRdIDjp+XzelXQ0tFE4JS7YFwFevXXMmOp9myNrUvCg0D6ws8iqkRPBfKHgbwig1SmlLfg==} @@ -4735,6 +4777,9 @@ packages: resolution: {integrity: sha512-wa56o2/ElJMYqjCjGkXri7it5FbebW5usLw/nPmCMs5DeZ7eziSYZhSmPRn0txqeW4LnAmQQU7FgqLpsEFKM4A==} engines: {node: '>= 0.4'} + is-buffer@1.1.6: + resolution: {integrity: sha512-NcdALwpXkTm5Zvvbk7owOUSvVvBKDgKP5/ewfXEznmQFfs4ZRmanOeKBTjRVjka3QFoN6XJ+9F3USqfHqTaU5w==} + is-builtin-module@5.0.0: resolution: {integrity: sha512-f4RqJKBUe5rQkJ2eJEJBXSticB3hGbN9j0yxxMQFqIW89Jp9WYFtzfTcRlstDKVUTRzSOTLKRfO9vIztenwtxA==} engines: {node: '>=18.20'} @@ -5431,6 +5476,9 @@ packages: mathml-tag-names@4.0.0: resolution: {integrity: sha512-aa6AU2Pcx0VP/XWnh8IGL0SYSgQHDT6Ucror2j2mXeFAlN3ahaNs8EZtG1YiticMkSLj3Gt6VPFfZogt7G5iFQ==} + md5@2.3.0: + resolution: {integrity: sha512-T1GITYmFaKuO91vxyoQMFETst+O71VUPEU3ze5GNzDm0OWdP8v1ziTaAEPUr/3kLsY3Sftgz242A1SetQiDL7g==} + mdn-data@2.27.1: resolution: {integrity: sha512-9Yubnt3e8A0OKwxYSXyhLymGW4sCufcLG6VdiDdUGVkPhpqLxlvP5vl1983gQjJl3tqbrM731mjaZaP68AgosQ==} @@ -7610,7 +7658,7 @@ snapshots: '@babel/types': 7.29.7 '@jridgewell/remapping': 2.3.5 convert-source-map: 2.0.0 - debug: 4.4.3(supports-color@5.5.0) + debug: 4.4.3(supports-color@10.2.2) gensync: 1.0.0-beta.2 json5: 2.2.3 semver: 6.3.1 @@ -7769,7 +7817,7 @@ snapshots: '@babel/parser': 7.29.7 '@babel/template': 7.29.7 '@babel/types': 7.29.7 - debug: 4.4.3(supports-color@5.5.0) + debug: 4.4.3(supports-color@10.2.2) transitivePeerDependencies: - supports-color @@ -8450,7 +8498,7 @@ snapshots: '@eslint/config-array@0.23.5': dependencies: '@eslint/object-schema': 3.0.5 - debug: 4.4.3(supports-color@5.5.0) + debug: 4.4.3(supports-color@10.2.2) minimatch: 10.2.5 transitivePeerDependencies: - supports-color @@ -9134,7 +9182,7 @@ snapshots: '@puppeteer/browsers@2.13.2': dependencies: - debug: 4.4.3(supports-color@5.5.0) + debug: 4.4.3(supports-color@10.2.2) extract-zip: 2.0.1 progress: 2.0.3 proxy-agent: 6.5.0 @@ -9552,7 +9600,7 @@ snapshots: '@typescript-eslint/types': 8.61.1 '@typescript-eslint/typescript-estree': 8.61.1(typescript@6.0.3) '@typescript-eslint/visitor-keys': 8.61.1 - debug: 4.4.3(supports-color@5.5.0) + debug: 4.4.3(supports-color@10.2.2) eslint: 10.5.0(jiti@2.7.0) typescript: 6.0.3 transitivePeerDependencies: @@ -9562,7 +9610,7 @@ snapshots: dependencies: '@typescript-eslint/tsconfig-utils': 8.61.1(typescript@6.0.3) '@typescript-eslint/types': 8.61.1 - debug: 4.4.3(supports-color@5.5.0) + debug: 4.4.3(supports-color@10.2.2) typescript: 6.0.3 transitivePeerDependencies: - supports-color @@ -9581,7 +9629,7 @@ snapshots: '@typescript-eslint/types': 8.61.1 '@typescript-eslint/typescript-estree': 8.61.1(typescript@6.0.3) '@typescript-eslint/utils': 8.61.1(eslint@10.5.0(jiti@2.7.0))(typescript@6.0.3) - debug: 4.4.3(supports-color@5.5.0) + debug: 4.4.3(supports-color@10.2.2) eslint: 10.5.0(jiti@2.7.0) ts-api-utils: 2.5.0(typescript@6.0.3) typescript: 6.0.3 @@ -9596,7 +9644,7 @@ snapshots: '@typescript-eslint/tsconfig-utils': 8.61.1(typescript@6.0.3) '@typescript-eslint/types': 8.61.1 '@typescript-eslint/visitor-keys': 8.61.1 - debug: 4.4.3(supports-color@5.5.0) + debug: 4.4.3(supports-color@10.2.2) minimatch: 10.2.5 semver: 7.8.4 tinyglobby: 0.2.17 @@ -9735,6 +9783,14 @@ snapshots: chai: 6.2.2 tinyrainbow: 3.1.0 + '@vitest/mocker@4.1.9(vite@8.0.16(@types/node@25.9.3)(esbuild@0.27.7)(jiti@2.7.0)(tsx@4.22.4)(yaml@2.9.0))': + dependencies: + '@vitest/spy': 4.1.9 + estree-walker: 3.0.3 + magic-string: 0.30.21 + optionalDependencies: + vite: 8.0.16(@types/node@25.9.3)(esbuild@0.27.7)(jiti@2.7.0)(tsx@4.22.4)(yaml@2.9.0) + '@vitest/mocker@4.1.9(vite@8.0.16(@types/node@25.9.3)(esbuild@0.28.0)(jiti@2.7.0)(tsx@4.22.4)(yaml@2.9.0))': dependencies: '@vitest/spy': 4.1.9 @@ -9789,6 +9845,18 @@ snapshots: path-browserify: 1.0.1 vscode-uri: 3.1.0 + '@wdio/allure-reporter@9.29.1': + dependencies: + '@types/node': 25.9.3 + '@wdio/reporter': 9.29.1 + '@wdio/types': 9.29.1 + allure-js-commons: 3.10.2 + csv-stringify: 6.8.1 + html-entities: 2.6.0 + strip-ansi: 7.2.0 + transitivePeerDependencies: + - allure-playwright + '@wdio/cli@9.28.0(@types/node@25.9.3)(expect-webdriverio@5.6.8)(puppeteer-core@21.11.0)': dependencies: '@vitest/snapshot': 2.1.9 @@ -9878,7 +9946,7 @@ snapshots: '@wdio/globals@9.28.0(expect-webdriverio@5.6.8)(webdriverio@9.28.0(puppeteer-core@21.11.0))': dependencies: - expect-webdriverio: 5.6.8(@wdio/globals@9.28.0)(@wdio/logger@9.18.0)(webdriverio@9.28.0(puppeteer-core@21.11.0)) + expect-webdriverio: 5.6.8(@wdio/globals@9.28.0)(@wdio/logger@9.29.1)(webdriverio@9.28.0(puppeteer-core@21.11.0)) webdriverio: 9.28.0(puppeteer-core@21.11.0) '@wdio/local-runner@9.28.0(@wdio/globals@9.28.0)(webdriverio@9.28.0(puppeteer-core@21.11.0))': @@ -9918,6 +9986,14 @@ snapshots: safe-regex2: 5.1.1 strip-ansi: 7.2.0 + '@wdio/logger@9.29.1': + dependencies: + chalk: 5.6.2 + loglevel: 1.9.2 + loglevel-plugin-prefix: 0.8.4 + safe-regex2: 5.1.1 + strip-ansi: 7.2.0 + '@wdio/mocha-framework@9.28.0': dependencies: '@types/mocha': 10.0.10 @@ -9948,6 +10024,14 @@ snapshots: diff: 8.0.4 object-inspect: 1.13.4 + '@wdio/reporter@9.29.1': + dependencies: + '@types/node': 25.9.3 + '@wdio/logger': 9.29.1 + '@wdio/types': 9.29.1 + diff: 8.0.4 + object-inspect: 1.13.4 + '@wdio/runner@9.28.0(expect-webdriverio@5.6.8)(webdriverio@9.28.0(puppeteer-core@21.11.0))': dependencies: '@types/node': 25.9.3 @@ -9958,7 +10042,7 @@ snapshots: '@wdio/types': 9.28.0 '@wdio/utils': 9.28.0 deepmerge-ts: 7.1.5 - expect-webdriverio: 5.6.8(@wdio/globals@9.28.0)(@wdio/logger@9.18.0)(webdriverio@9.28.0(puppeteer-core@21.11.0)) + expect-webdriverio: 5.6.8(@wdio/globals@9.28.0)(@wdio/logger@9.29.1)(webdriverio@9.28.0(puppeteer-core@21.11.0)) webdriver: 9.28.0 webdriverio: 9.28.0(puppeteer-core@21.11.0) transitivePeerDependencies: @@ -9985,6 +10069,10 @@ snapshots: dependencies: '@types/node': 25.9.3 + '@wdio/types@9.29.1': + dependencies: + '@types/node': 25.9.3 + '@wdio/utils@8.41.0': dependencies: '@puppeteer/browsers': 1.9.1 @@ -10058,7 +10146,7 @@ snapshots: agent-base@6.0.2: dependencies: - debug: 4.4.3(supports-color@5.5.0) + debug: 4.4.3(supports-color@10.2.2) transitivePeerDependencies: - supports-color @@ -10099,6 +10187,10 @@ snapshots: json-schema-traverse: 1.0.0 require-from-string: 2.0.2 + allure-js-commons@3.10.2: + dependencies: + md5: 2.3.0 + ansi-align@3.0.1: dependencies: string-width: 4.2.3 @@ -10593,6 +10685,8 @@ snapshots: chardet@2.1.1: {} + charenc@0.0.2: {} + check-error@1.0.2: {} cheerio-select@2.1.0: @@ -10860,6 +10954,8 @@ snapshots: shebang-command: 2.0.0 which: 2.0.2 + crypt@0.0.2: {} + css-functions-list@3.3.3: {} css-select@5.2.2: @@ -10888,6 +10984,8 @@ snapshots: '@asamuzakjp/css-color': 3.2.0 rrweb-cssom: 0.8.0 + csv-stringify@6.8.1: {} + data-uri-to-buffer@4.0.1: {} data-uri-to-buffer@6.0.2: {} @@ -11489,7 +11587,7 @@ snapshots: '@types/estree': 1.0.9 ajv: 6.15.0 cross-spawn: 7.0.6 - debug: 4.4.3(supports-color@5.5.0) + debug: 4.4.3(supports-color@10.2.2) escape-string-regexp: 4.0.0 eslint-scope: 9.1.2 eslint-visitor-keys: 5.0.1 @@ -11591,6 +11689,16 @@ snapshots: jest-matcher-utils: 30.4.1 webdriverio: 9.28.0(puppeteer-core@21.11.0) + expect-webdriverio@5.6.8(@wdio/globals@9.28.0)(@wdio/logger@9.29.1)(webdriverio@9.28.0(puppeteer-core@21.11.0)): + dependencies: + '@vitest/snapshot': 4.1.9 + '@wdio/globals': 9.28.0(expect-webdriverio@5.6.8)(webdriverio@9.28.0(puppeteer-core@21.11.0)) + '@wdio/logger': 9.29.1 + deep-eql: 5.0.2 + expect: 30.4.1 + jest-matcher-utils: 30.4.1 + webdriverio: 9.28.0(puppeteer-core@21.11.0) + expect@30.4.1: dependencies: '@jest/expect-utils': 30.4.1 @@ -11606,7 +11714,7 @@ snapshots: extract-zip@2.0.1: dependencies: - debug: 4.4.3(supports-color@5.5.0) + debug: 4.4.3(supports-color@10.2.2) get-stream: 5.2.0 yauzl: 2.10.0 optionalDependencies: @@ -11937,7 +12045,7 @@ snapshots: dependencies: basic-ftp: 5.3.1 data-uri-to-buffer: 6.0.2 - debug: 4.4.3(supports-color@5.5.0) + debug: 4.4.3(supports-color@10.2.2) transitivePeerDependencies: - supports-color @@ -11945,7 +12053,7 @@ snapshots: dependencies: basic-ftp: 5.3.1 data-uri-to-buffer: 8.0.0 - debug: 4.4.3(supports-color@5.5.0) + debug: 4.4.3(supports-color@10.2.2) transitivePeerDependencies: - supports-color @@ -12117,6 +12225,8 @@ snapshots: dependencies: whatwg-encoding: 3.1.1 + html-entities@2.6.0: {} + html-escaper@2.0.2: {} html-tags@5.1.0: {} @@ -12141,35 +12251,35 @@ snapshots: http-proxy-agent@7.0.2: dependencies: agent-base: 7.1.4 - debug: 4.4.3(supports-color@5.5.0) + debug: 4.4.3(supports-color@10.2.2) transitivePeerDependencies: - supports-color http-proxy-agent@9.0.0: dependencies: agent-base: 9.0.0 - debug: 4.4.3(supports-color@5.5.0) + debug: 4.4.3(supports-color@10.2.2) transitivePeerDependencies: - supports-color https-proxy-agent@5.0.1: dependencies: agent-base: 6.0.2 - debug: 4.4.3(supports-color@5.5.0) + debug: 4.4.3(supports-color@10.2.2) transitivePeerDependencies: - supports-color https-proxy-agent@7.0.6: dependencies: agent-base: 7.1.4 - debug: 4.4.3(supports-color@5.5.0) + debug: 4.4.3(supports-color@10.2.2) transitivePeerDependencies: - supports-color https-proxy-agent@9.0.0: dependencies: agent-base: 9.0.0 - debug: 4.4.3(supports-color@5.5.0) + debug: 4.4.3(supports-color@10.2.2) transitivePeerDependencies: - supports-color @@ -12290,6 +12400,8 @@ snapshots: call-bound: 1.0.4 has-tostringtag: 1.0.2 + is-buffer@1.1.6: {} + is-builtin-module@5.0.0: dependencies: builtin-modules: 5.2.0 @@ -12466,7 +12578,7 @@ snapshots: istanbul-lib-source-maps@5.0.6: dependencies: '@jridgewell/trace-mapping': 0.3.31 - debug: 4.4.3(supports-color@5.5.0) + debug: 4.4.3(supports-color@10.2.2) istanbul-lib-coverage: 3.2.2 transitivePeerDependencies: - supports-color @@ -12929,7 +13041,7 @@ snapshots: lighthouse-logger@2.0.2: dependencies: - debug: 4.4.3(supports-color@5.5.0) + debug: 4.4.3(supports-color@10.2.2) marky: 1.3.0 transitivePeerDependencies: - supports-color @@ -13131,6 +13243,12 @@ snapshots: mathml-tag-names@4.0.0: {} + md5@2.3.0: + dependencies: + charenc: 0.0.2 + crypt: 0.0.2 + is-buffer: 1.1.6 + mdn-data@2.27.1: {} memorystream@0.3.1: {} @@ -13552,7 +13670,7 @@ snapshots: dependencies: '@tootallnate/quickjs-emscripten': 0.23.0 agent-base: 7.1.4 - debug: 4.4.3(supports-color@5.5.0) + debug: 4.4.3(supports-color@10.2.2) get-uri: 6.0.5 http-proxy-agent: 7.0.2 https-proxy-agent: 7.0.6 @@ -13564,7 +13682,7 @@ snapshots: pac-proxy-agent@9.0.1: dependencies: agent-base: 9.0.0 - debug: 4.4.3(supports-color@5.5.0) + debug: 4.4.3(supports-color@10.2.2) get-uri: 8.0.0 http-proxy-agent: 9.0.0 https-proxy-agent: 9.0.0 @@ -13854,7 +13972,7 @@ snapshots: proxy-agent@6.5.0: dependencies: agent-base: 7.1.4 - debug: 4.4.3(supports-color@5.5.0) + debug: 4.4.3(supports-color@10.2.2) http-proxy-agent: 7.0.2 https-proxy-agent: 7.0.6 lru-cache: 7.18.3 @@ -13867,7 +13985,7 @@ snapshots: proxy-agent@8.0.1: dependencies: agent-base: 9.0.0 - debug: 4.4.3(supports-color@5.5.0) + debug: 4.4.3(supports-color@10.2.2) http-proxy-agent: 9.0.0 https-proxy-agent: 9.0.0 lru-cache: 7.18.3 @@ -14387,7 +14505,7 @@ snapshots: socks-proxy-agent@10.0.0: dependencies: agent-base: 9.0.0 - debug: 4.4.3(supports-color@5.5.0) + debug: 4.4.3(supports-color@10.2.2) socks: 2.8.9 transitivePeerDependencies: - supports-color @@ -14395,7 +14513,7 @@ snapshots: socks-proxy-agent@8.0.5: dependencies: agent-base: 7.1.4 - debug: 4.4.3(supports-color@5.5.0) + debug: 4.4.3(supports-color@10.2.2) socks: 2.8.9 transitivePeerDependencies: - supports-color @@ -14604,7 +14722,7 @@ snapshots: cosmiconfig: 9.0.2(typescript@6.0.3) css-functions-list: 3.3.3 css-tree: 3.2.1 - debug: 4.4.3(supports-color@5.5.0) + debug: 4.4.3(supports-color@10.2.2) fast-glob: 3.3.3 fastest-levenshtein: 1.0.16 file-entry-cache: 11.1.3 @@ -14867,7 +14985,7 @@ snapshots: cac: 6.7.14 chokidar: 4.0.3 consola: 3.4.2 - debug: 4.4.3(supports-color@5.5.0) + debug: 4.4.3(supports-color@10.2.2) esbuild: 0.27.7 fix-dts-default-cjs-exports: 1.0.1 joycon: 3.1.1 @@ -15000,7 +15118,7 @@ snapshots: '@rollup/pluginutils': 5.4.0(rollup@4.62.0) '@volar/typescript': 2.4.28 compare-versions: 6.1.1 - debug: 4.4.3(supports-color@5.5.0) + debug: 4.4.3(supports-color@10.2.2) kolorist: 1.8.0 local-pkg: 1.2.1 magic-string: 0.30.21 @@ -15143,6 +15261,21 @@ snapshots: optionalDependencies: rollup: 4.62.0 + vite@8.0.16(@types/node@25.9.3)(esbuild@0.27.7)(jiti@2.7.0)(tsx@4.22.4)(yaml@2.9.0): + dependencies: + lightningcss: 1.32.0 + picomatch: 4.0.4 + postcss: 8.5.15 + rolldown: 1.0.3 + tinyglobby: 0.2.17 + optionalDependencies: + '@types/node': 25.9.3 + esbuild: 0.27.7 + fsevents: 2.3.3 + jiti: 2.7.0 + tsx: 4.22.4 + yaml: 2.9.0 + vite@8.0.16(@types/node@25.9.3)(esbuild@0.28.0)(jiti@2.7.0)(tsx@4.22.4)(yaml@2.9.0): dependencies: lightningcss: 1.32.0 @@ -15158,6 +15291,36 @@ snapshots: tsx: 4.22.4 yaml: 2.9.0 + vitest@4.1.9(@types/node@25.9.3)(@vitest/coverage-v8@4.1.9)(happy-dom@20.10.4)(jsdom@24.1.3)(vite@8.0.16(@types/node@25.9.3)(esbuild@0.27.7)(jiti@2.7.0)(tsx@4.22.4)(yaml@2.9.0)): + dependencies: + '@vitest/expect': 4.1.9 + '@vitest/mocker': 4.1.9(vite@8.0.16(@types/node@25.9.3)(esbuild@0.27.7)(jiti@2.7.0)(tsx@4.22.4)(yaml@2.9.0)) + '@vitest/pretty-format': 4.1.9 + '@vitest/runner': 4.1.9 + '@vitest/snapshot': 4.1.9 + '@vitest/spy': 4.1.9 + '@vitest/utils': 4.1.9 + es-module-lexer: 2.1.0 + expect-type: 1.3.0 + magic-string: 0.30.21 + obug: 2.1.3 + pathe: 2.0.3 + picomatch: 4.0.4 + std-env: 4.1.0 + tinybench: 2.9.0 + tinyexec: 1.2.4 + tinyglobby: 0.2.17 + tinyrainbow: 3.1.0 + vite: 8.0.16(@types/node@25.9.3)(esbuild@0.27.7)(jiti@2.7.0)(tsx@4.22.4)(yaml@2.9.0) + why-is-node-running: 2.3.0 + optionalDependencies: + '@types/node': 25.9.3 + '@vitest/coverage-v8': 4.1.9(@vitest/browser@4.1.9)(vitest@4.1.9) + happy-dom: 20.10.4 + jsdom: 24.1.3 + transitivePeerDependencies: + - msw + vitest@4.1.9(@types/node@25.9.3)(@vitest/coverage-v8@4.1.9)(happy-dom@20.10.4)(jsdom@24.1.3)(vite@8.0.16(@types/node@25.9.3)(esbuild@0.28.0)(jiti@2.7.0)(tsx@4.22.4)(yaml@2.9.0)): dependencies: '@vitest/expect': 4.1.9 @@ -15200,7 +15363,7 @@ snapshots: dependencies: chalk: 4.1.2 commander: 9.5.0 - debug: 4.4.3(supports-color@5.5.0) + debug: 4.4.3(supports-color@10.2.2) transitivePeerDependencies: - supports-color From 28982ce36595e1eada46a1c2589ca508a5e23c17 Mon Sep 17 00:00:00 2001 From: Vishnu Vardhan <vishnu.p@browserstack.com> Date: Wed, 15 Jul 2026 17:27:41 +0530 Subject: [PATCH 66/68] fix(core): replace polynomial-ReDoS edge-trims with a linear trimChar --- packages/core/src/artifact-naming.ts | 18 +++++++++- packages/core/src/spec-trace-helpers.ts | 20 ++++++----- packages/core/tests/artifact-naming.test.ts | 39 +++++++++++++++++++++ 3 files changed, 67 insertions(+), 10 deletions(-) create mode 100644 packages/core/tests/artifact-naming.test.ts diff --git a/packages/core/src/artifact-naming.ts b/packages/core/src/artifact-naming.ts index dcf597b6..fdb968d7 100644 --- a/packages/core/src/artifact-naming.ts +++ b/packages/core/src/artifact-naming.ts @@ -3,8 +3,24 @@ * screenshot and video writers slug a test uid identically. */ +/** Strip leading and trailing occurrences of `char` via a linear scan. A regex + * like `/^c+|c+$/` is O(n²) on long runs of `c` (the anchored `+` is retried + * at each position) — flagged by CodeQL's js/polynomial-redos — so this avoids + * regex entirely. `char` must be a single character. */ +export function trimChar(value: string, char: string): string { + let start = 0 + let end = value.length + while (start < end && value[start] === char) { + start++ + } + while (end > start && value[end - 1] === char) { + end-- + } + return value.slice(start, end) +} + /** File-safe slug of a value: keep alphanumerics, `_` and `-`; collapse the * rest to a single `-`; trim leading/trailing dashes. */ export function fileSlug(value: string): string { - return value.replace(/[^a-zA-Z0-9_-]+/g, '-').replace(/^-+|-+$/g, '') + return trimChar(value.replace(/[^a-zA-Z0-9_-]+/g, '-'), '-') } diff --git a/packages/core/src/spec-trace-helpers.ts b/packages/core/src/spec-trace-helpers.ts index 6de04bf8..4b37fe80 100644 --- a/packages/core/src/spec-trace-helpers.ts +++ b/packages/core/src/spec-trace-helpers.ts @@ -16,6 +16,7 @@ import type { import type { TraceCapturer } from './trace-exporter.js' import { writeTraceZip } from './trace-exporter.js' import { deterministicUid } from './uid.js' +import { trimChar } from './artifact-naming.js' // ─── SpecRange ──────────────────────────────────────────────────────────────── @@ -68,13 +69,14 @@ export function findFlushableRange( * Falls back to `'unknown-spec'` when the result is empty. */ export function sanitizeSpecName(specFile: string): string { - return ( + const cleaned = trimChar( specFile .replace(/^.*[/\\]/, '') .replace(/\.[^.]+$/, '') - .replace(/[^a-zA-Z0-9_-]/g, '_') - .replace(/^_+|_+$/g, '') || 'unknown-spec' + .replace(/[^a-zA-Z0-9_-]/g, '_'), + '_' ) + return cleaned || 'unknown-spec' } // ─── Spec session ID ────────────────────────────────────────────────────────── @@ -121,12 +123,12 @@ const MAX_SLUG_LENGTH = 60 /** Lowercase, collapse runs of non-alphanumerics to `-`, trim edge dashes, * and cap length (trimming a dash the cut may leave behind). */ function slugify(text: string): string { - return text - .toLowerCase() - .replace(/[^a-z0-9]+/g, '-') - .replace(/^-+|-+$/g, '') - .slice(0, MAX_SLUG_LENGTH) - .replace(/-+$/g, '') + const collapsed = trimChar( + text.toLowerCase().replace(/[^a-z0-9]+/g, '-'), + '-' + ) + // Trim again after the length cap in case it sliced mid-dash-run. + return trimChar(collapsed.slice(0, MAX_SLUG_LENGTH), '-') } /** diff --git a/packages/core/tests/artifact-naming.test.ts b/packages/core/tests/artifact-naming.test.ts new file mode 100644 index 00000000..a8436f00 --- /dev/null +++ b/packages/core/tests/artifact-naming.test.ts @@ -0,0 +1,39 @@ +import { describe, it, expect } from 'vitest' +import { trimChar, fileSlug } from '../src/artifact-naming.js' + +describe('trimChar', () => { + it('strips leading and trailing occurrences only', () => { + expect(trimChar('--a-b--', '-')).toBe('a-b') + expect(trimChar('__x__', '_')).toBe('x') + }) + + it('returns empty for an all-char string', () => { + expect(trimChar('-----', '-')).toBe('') + }) + + it('is a no-op when there is nothing to trim', () => { + expect(trimChar('abc', '-')).toBe('abc') + expect(trimChar('', '-')).toBe('') + }) + + it('handles long runs without catastrophic backtracking', () => { + const long = '-'.repeat(100000) + 'x' + '-'.repeat(100000) + // A polynomial /^-+|-+$/ would stall here; the linear scan returns fast. + expect(trimChar(long, '-')).toBe('x') + }) +}) + +describe('fileSlug', () => { + it('collapses disallowed runs to a single dash and trims edges', () => { + expect(fileSlug('a/b c!!d')).toBe('a-b-c-d') + expect(fileSlug(' spaced ')).toBe('spaced') + }) + + it('keeps alphanumerics, underscore and dash', () => { + expect(fileSlug('Test_Case-1')).toBe('Test_Case-1') + }) + + it('stays fast + correct on a long disallowed run', () => { + expect(fileSlug('a' + ' '.repeat(100000) + 'b')).toBe('a-b') + }) +}) From 8e96fd0dad5990ab1fcabc23a152cfb310d67e1f Mon Sep 17 00:00:00 2001 From: Vishnu Vardhan <vishnu.p@browserstack.com> Date: Wed, 15 Jul 2026 20:31:16 +0530 Subject: [PATCH 67/68] fix: scope screenshot/video options to the WDIO ServiceOptions --- CLAUDE.md | 2 +- packages/service/src/types.ts | 14 +++++++++++++- packages/shared/src/types.ts | 8 -------- 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/CLAUDE.md b/CLAUDE.md index 5a2d88ce..f97151a3 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -240,7 +240,7 @@ Documented divergences from the conventions above. They exist today as debt to b - **Nightwatch: `retain-on-failure` works; the other retry-aware policies degrade.** Its `--retries` re-runs the testcase *internally* without re-firing the plugin's per-test hooks, and the per-testcase results carry no attempt/retry field (retries live only in undocumented, version-varying Nightwatch internals — `suiteRetries.testRetriesCount` / `reporter.testResults.retryTest`), so the ledger sees only the final attempt for the `describe/it` and exports-object interfaces. Cucumber scenarios expose per-scenario hooks, so the feed captures their attempts. Not cleanly fixable without depending on those internals. - WDIO `specFileRetries` spawns a fresh worker per retry, so cross-process attempts aren't in the (process-scoped) ledger. - Eager per-test trace slice (Nightwatch + Selenium) can drop an action snapshot whose fire-and-forget capture hasn't resolved by `afterEach` / scenario end — the slice is written from whatever snapshots exist at flush time. The WDIO service is immune because it awaits each snapshot inline before flushing. -- The per-test `screenshot` and `video` options live on `BaseDevToolsOptions` (shared) so all three adapters accept them, but only the **WDIO service** reads them today. The capture/slice/encode logic is framework-agnostic (`core/screenshot-artifact.ts`, `core/video-slice.ts`), so Selenium/Nightwatch adoption is wiring-only (capture the base64 + feed `captureAndAttach*`); pending. Both are gated to `traceGranularity:'test'` (per-test inline Allure); coarser granularities keep artifacts in the manifest. Video records the screencast continuously and slices per-test by wall-time — the frame buffer is unbounded for the session (a decimation cap is a noted follow-up), and on non-Chrome the polling recorder issues many `takeScreenshot`s that flood `@wdio/allure-reporter` (pair with `disableWebdriverStepsReporting`). +- The per-test `screenshot` and `video` options live on the **WDIO `ServiceOptions` only** — not `BaseDevToolsOptions` — because only the service implements them (an option belongs on an adapter until a second adapter consumes it, mirroring the core-helper rule; putting them on the shared base made them appear available in Selenium/Nightwatch and broke those adapters' `Required<>` option types). The policy *types* (`TraceScreenshotPolicy`/`TraceVideoPolicy`) and the capture/slice/encode logic (`core/screenshot-artifact.ts`, `core/video-slice.ts`) are framework-agnostic, so Selenium/Nightwatch adoption is wiring-only (capture the base64 + feed `captureAndAttach*`) plus adding the option to their own options type; pending. Both are gated to `traceGranularity:'test'` (per-test inline Allure); coarser granularities keep artifacts in the manifest. Video records the screencast continuously and slices per-test by wall-time — the frame buffer is unbounded for the session (a decimation cap is a noted follow-up), and on non-Chrome the polling recorder issues many `takeScreenshot`s that flood `@wdio/allure-reporter` (pair with `disableWebdriverStepsReporting`). - Nightwatch native asserts are buffered at call time and emitted in one batch at test-end (Nightwatch exposes no per-assertion execution/outcome hook reachable from the plugin — `client.queue.tree` / `client.reporter` aren't on `browser`). Streaming mid-run would flash every assert green before its outcome is known, so rows are held and reconciled against `results.testcases[*].assertions` at `afterEach` (the flat `results.assertions` only reflects the last testcase). The exporter re-sorts commands by timestamp (`buildActionEvents`) so the batched rows land at their real timeline positions rather than clustering at flush time. `actual` is parsed from the failure message (`but got: …`, failures only) into a collapsed `{passed, expected, actual}` result; `expected` is the assertion arg — the label still mirrors the call args, not the derived values. - Nightwatch's BDD `describe/it` interface fires the plugin's global `beforeEach`/`afterEach` **once per module** (with an empty `currentTest.name`), not per `it` — Nightwatch runs the individual `it`s internally with no per-testcase hook or event reachable from the plugin (the lib emits only transport-level events). So `traceGranularity:'test'` records a single slice boundary (the first test) and collapses to one session-scoped slice; retry-aware/per-test retention (`tracePolicy`) degrades to session scope for this interface. Per-test slicing works for adapters/styles that expose per-test hooks (WDIO mocha/cucumber, Selenium mocha; likely Nightwatch's exports-object style, unverified). Session-granularity trace is unaffected. - Service renders expect-webdriverio matchers as single `expect.<matcher>` rows by **folding**, not stack/depth suppression (the old `#assertionDepth`/`#matcherStarted`/self-heal machinery is gone). The matcher's value-read (`toHaveText`→`getText`, `toExist`→`isExisting`, …) is captured as a normal command; `afterAssertion` then coalesces the synthesized `expect.*` row into that read in place — inheriting its callSource, screenshot, and timeline position — and the fold replaces **by timestamp, never a public `id`**: `id` is the per-worker `commandCounter`, which resets per spec, so stamping one lets the app's id-first `replaceCommand` swap a same-id row from another spec (duplicate rows + a fold from another spec vanishing, in multi-spec live mode). `beforeAssertion` arms the pending matcher (depth-counted so aliases like `toBeChecked`→`toBeSelected` fold once); a matcher that **hard-throws** — element never resolves, so expect-webdriverio's `waitUntil` rethrows and `afterAssertion` never fires — is synthesized at `afterTest`/`afterStep` from the throwing read, so a failing assertion renders as `expect.<matcher>` whether or not the element existed. Two limits: its error is then the read's (`Can't call getText on … element wasn't found`), not an assertion-phrased message; and `MATCHER_READ_COMMANDS` is a hand-maintained allowlist, so a matcher whose read isn't listed leaves its raw read visible alongside the `expect.*` row. Plain-value jest matchers (`expect(x).toBe(y)`) don't fire the ewdio hooks, so they aren't captured as rows. diff --git a/packages/service/src/types.ts b/packages/service/src/types.ts index ce10a42e..0449c742 100644 --- a/packages/service/src/types.ts +++ b/packages/service/src/types.ts @@ -21,7 +21,11 @@ export { type Viewport } from '@wdio/devtools-shared' -import type { BaseDevToolsOptions } from '@wdio/devtools-shared' +import type { + BaseDevToolsOptions, + TraceScreenshotPolicy, + TraceVideoPolicy +} from '@wdio/devtools-shared' // ScreencastFrame, ScreencastOptions hoisted to @wdio/devtools-shared; re-exported // here for backwards compatibility with existing service-internal imports. @@ -39,6 +43,14 @@ export interface ExtendedCapabilities extends WebdriverIO.Capabilities { } export interface ServiceOptions extends BaseDevToolsOptions { + /** Per-test screenshot capture, attached to the trace artifacts and inline to + * Allure. `off` (default) | `on` | `only-on-failure`. Trace mode + + * `traceGranularity: 'test'` only. WDIO-service-specific for now. */ + screenshot?: TraceScreenshotPolicy + /** Per-test video (screencast) capture, retained per the given policy and + * attached inline to Allure. `off` (default) or a retention policy. Trace + * mode + `traceGranularity: 'test'` only. WDIO-service-specific for now. */ + video?: TraceVideoPolicy /** * capabilities used to launch the devtools application * @default diff --git a/packages/shared/src/types.ts b/packages/shared/src/types.ts index b360936d..583ca3e7 100644 --- a/packages/shared/src/types.ts +++ b/packages/shared/src/types.ts @@ -317,14 +317,6 @@ export interface BaseDevToolsOptions { /** Trace retention policy — gates which traces are kept (e.g. * `retain-on-failure`). Default `on` (keep all). Only applies in trace mode. */ tracePolicy?: TraceRetentionPolicy - /** Per-test screenshot capture, attached to the trace artifacts and to Allure - * inline. `off` (default) | `on` | `only-on-failure`. Only applies in trace - * mode. */ - screenshot?: TraceScreenshotPolicy - /** Per-test video (screencast) capture, retained per the given policy and - * attached to Allure inline. `off` (default) or a retention policy. Only - * applies in trace mode at `traceGranularity: 'test'`. */ - video?: TraceVideoPolicy } /** Minimal Cucumber pickle-step shape — only the fields the adapters read. From 7cc96f6e2f59cad55c949e32d8aa20505ede5ebc Mon Sep 17 00:00:00 2001 From: Vishnu Vardhan <vishnu.p@browserstack.com> Date: Wed, 15 Jul 2026 20:31:38 +0530 Subject: [PATCH 68/68] fix(app): de-duplicate live-mode Errors tab (command + reworded test error) --- .../components/workbench/errors/collect.ts | 49 +++++++++++- packages/app/tests/errors-collect.test.ts | 74 +++++++++++++++++++ 2 files changed, 119 insertions(+), 4 deletions(-) diff --git a/packages/app/src/components/workbench/errors/collect.ts b/packages/app/src/components/workbench/errors/collect.ts index f28d8065..27d7d0ec 100644 --- a/packages/app/src/components/workbench/errors/collect.ts +++ b/packages/app/src/components/workbench/errors/collect.ts @@ -212,24 +212,65 @@ function commandErrors(commands: CommandLog[] | undefined): CollectedError[] { .sort((a, b) => (a.timestamp ?? 0) - (b.timestamp ?? 0)) } +/** Collapse whitespace and drop a leading `…Error:` name prefix so a failed + * test that only re-reports a command failure compares equal despite framework + * wrapping: `@wdio/cucumber-framework` rebuilds a failed step's error from the + * first line of the error's *stack* (`new Error(stack.split('\n')[0])`), which + * prefixes `Error: `, while the command carries the same headline without it. */ +function normalizeMessage(message: string): string { + return message + .replace(/^[A-Za-z]*Error:\s*/, '') + .replace(/\s+/g, ' ') + .trim() +} + +/** True when a command's assertion diff is echoed inside a failed test's raw + * text. Cucumber keeps the full matcher output (`Expected:`/`Received:`) in the + * step error's stack even when its headline was truncated to the first line, so + * matching on both distinctive values catches the duplicate without depending + * on the headline wording — which diverges from the command's. */ +function isAssertionEcho( + command: CollectedError, + test: { message: string; stack?: string } +): boolean { + if (!command.expected || !command.actual) { + return false + } + const haystack = `${test.message}\n${test.stack ?? ''}` + return ( + haystack.includes(command.expected) && haystack.includes(command.actual) + ) +} + /** * Build the Errors-tab list from the live/player contexts. * * Command failures come first (time-ordered) because they carry the clickable - * action; a failed test that only echoes a command's message is dropped so the + * action; a failed test that only echoes a command's failure is dropped so the * same failure isn't listed twice (e.g. a Cucumber `Then` fails as both the - * assertion command and the scenario). + * assertion command and the step). The echo is detected two ways because the + * frameworks reword the test-level message: a normalized-message match (robust + * to the `Error:` prefix Cucumber adds), and — for assertions — the command's + * expected+actual both appearing in the test error's raw text. */ export function collectErrors( commands: CommandLog[] | undefined, suites: Record<string, SuiteStatsFragment>[] | undefined ): CollectedError[] { const fromCommands = commandErrors(commands) - const seenMessages = new Set(fromCommands.map((e) => e.message)) + const seenMessages = new Set( + fromCommands.map((e) => normalizeMessage(e.message)) + ) const fromTests = collectFailedTests(suites).flatMap((test) => { const read = readError(test.error ?? test.errors?.[0]) - if (!read || seenMessages.has(read.message)) { + if (!read) { + return [] + } + if (seenMessages.has(normalizeMessage(read.message))) { + return [] + } + if (fromCommands.some((command) => isAssertionEcho(command, read))) { return [] } return [ diff --git a/packages/app/tests/errors-collect.test.ts b/packages/app/tests/errors-collect.test.ts index c92e3680..0b482fca 100644 --- a/packages/app/tests/errors-collect.test.ts +++ b/packages/app/tests/errors-collect.test.ts @@ -193,6 +193,80 @@ describe('collectErrors', () => { expect(errors[0].command?.command).toBe('expect') }) + it('dedupes a Cucumber assertion listed as both a command and a reworded test error', () => { + // The matcher failure that expect-webdriverio reports: headline + diff. + const matcherMessage = + 'Expect $(`#flash`) to have text\n\n' + + 'Expected: StringContaining "You logged into a secure area!"\n' + + 'Received: "Your username is invalid!"' + const errors = collectErrors( + [ + command({ + command: 'expect.toHaveText', + title: 'expect.toHaveText', + callSource: 'steps.ts:34:3', + error: { name: 'Error', message: matcherMessage }, + timestamp: 50 + }) + ], + suiteMap({ + uid: 'scenario', + state: 'failed', + tests: [ + failedTest({ + uid: 'step', + title: 'Then I should see a flash message', + callSource: 'steps.ts:31', + // Cucumber rebuilds the failed step's error from the first line of + // the error's stack (so the headline gains an `Error:` prefix and + // loses the diff) but keeps the full matcher output in `.stack`. + error: { + name: 'Error', + message: 'Error: Expect to have text', + stack: `Error: ${matcherMessage}\n at World.<anonymous> (/steps.ts:34:3)` + } as unknown as Error + }) + ] + }) + ) + expect(errors).toHaveLength(1) + expect(errors[0].command?.command).toBe('expect.toHaveText') + expect(errors[0].actual).toBe('"Your username is invalid!"') + expect(errors[0].expected).toBe( + 'StringContaining "You logged into a secure area!"' + ) + }) + + it('dedupes a Cucumber command failure whose test error only adds an Error: prefix', () => { + const errors = collectErrors( + [ + command({ + command: 'click', + error: { + name: 'Error', + message: "Can't call click on element, it wasn't found" + }, + timestamp: 5 + }) + ], + suiteMap({ + uid: 'scenario', + state: 'failed', + tests: [ + failedTest({ + uid: 'step', + error: { + name: 'Error', + message: "Error: Can't call click on element, it wasn't found" + } + }) + ] + }) + ) + expect(errors).toHaveLength(1) + expect(errors[0].command?.command).toBe('click') + }) + it('keeps a distinct test failure alongside command failures', () => { const errors = collectErrors( [