Skip to content

Commit 28e6bf8

Browse files
authored
Keep the time spent retrying on a folded command's row (#637)
* Keep the time spent retrying on a folded command's row Each adapter folds a command's repeated polls into one row, and the fold took the last poll's start, so the wait before it fell out of the trace. A WebdriverIO expect matcher's row, folded into its read, started after its ten seconds of retrying and lasted one read. A folded row now keeps the earliest start of the rows it joins, through one rule in core used by the WebdriverIO, Nightwatch and Selenium replace paths, and an expect row starts when its matcher began. Closes #628 * Assert the retried row's first start directly The first and retried rows are one object, since the retry replaces the row in place, so comparing their starts passed whatever the retry wrote. The test now checks the first attempt's own start.
1 parent 22f20aa commit 28e6bf8

13 files changed

Lines changed: 150 additions & 6 deletions

File tree

‎.changeset/retried-row-start.md‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'@wdio/devtools-service': patch
3+
'@wdio/nightwatch-devtools': patch
4+
'@wdio/selenium-devtools': patch
5+
---
6+
7+
Keep the time spent retrying on a command's row. A command that polled several times, folded into one row, now starts at its first poll instead of its last, and a WebdriverIO `expect` matcher's row starts when the matcher began, so its retrying (up to the default 10 s wait) shows on the row instead of as an empty gap before it.

‎packages/core/CLAUDE.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Owns the framework-agnostic capture, reporting and transport logic that the thre
88
- **A poll that failed under a wait that did not fail is not a failure.** Readers use shared `effectiveFailure` (`packages/shared/src/command-failure.ts`), not `commandFailure`. Under a failed wait, its children keep their errors.
99
- **BiDi is auto-attached in Service and Selenium.** Nightwatch is opt-in via `bidi: true` plus `webSocketUrl: true` in capabilities.
1010
- **Stamp a row's `timestamp` at COMPLETION and keep invocation as `startTime`.** The page mutation stream is on real time, so an invocation-stamped row replays the page from before its own effect. `collector.captureCurrentDom` stamps `performance.timeOrigin`, not the drain clock.
11+
- **A row that a retried command or an assertion is folded into keeps the earliest start, `foldedStartTime` (`core/folded-start.ts`).** Every JS adapter's replace path applies it (the WebdriverIO service's retry and matcher folds, Nightwatch's and Selenium's `replaceCommand` callers), so the time spent retrying stays on the row; the end and result are the latest poll's. A WebdriverIO `expect` row starts when its matcher began (`beforeAssertion`).
1112
- **Live mode drains the collector after any command that could move the page.** Not only at navigation: the nav hook is deferred behind injection and a settle. Selenium's drain is serialized on a tail because the driver patcher does not await `onCommand`; an overtaken batch strands every later row.
1213
- **`reattributeDomAnchors` (`packages/trace/src/trace-mutations.ts`) only pulls an anchor born after the last logged command, and only when no later command completed.** Pulling anchors earlier otherwise steals the new page's DOM from rows still on the old one. It is also never pulled earlier than the newest timestamp already in the stream (`floor`).
1314
- **Every per-action snapshot probe is timeout-guarded** (`core/action-snapshot.ts` `probe`; the timeout constant is `SNAPSHOT_DRIVER_PROBE_TIMEOUT_MS` in `core/with-timeout.ts`). Nightwatch's `browser.getCurrentUrl()`/`getTitle()` are queued and hang inside the command hook.

‎packages/core/src/folded-start.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/** The start of a row that a retried command or an assertion is folded into: the earliest start either one knew, so the time spent retrying stays on the row. */
2+
export function foldedStartTime(
3+
first: number | undefined,
4+
latest: number | undefined
5+
): number | undefined {
6+
if (first === undefined) {
7+
return latest
8+
}
9+
return latest === undefined ? first : Math.min(first, latest)
10+
}

‎packages/core/src/index.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ export * from './stack.js'
7676
export * from './terminal-throttle.js'
7777
export * from './error.js'
7878
export * from './final-frame.js'
79+
export * from './folded-start.js'
7980
export * from './finalize-screencast.js'
8081
export * from './driver-activity.js'
8182
export * from './capture-mask.js'
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { describe, expect, it } from 'vitest'
2+
import { foldedStartTime } from '../src/folded-start.js'
3+
4+
describe('foldedStartTime', () => {
5+
it('keeps the first poll start when a later poll replaces the row', () => {
6+
expect(foldedStartTime(1000, 9000)).toBe(1000)
7+
})
8+
9+
it('takes the earlier start whichever row knew it', () => {
10+
expect(foldedStartTime(9000, 1000)).toBe(1000)
11+
})
12+
13+
it('uses whichever start is known', () => {
14+
expect(foldedStartTime(undefined, 9000)).toBe(9000)
15+
expect(foldedStartTime(1000, undefined)).toBe(1000)
16+
expect(foldedStartTime(undefined, undefined)).toBeUndefined()
17+
})
18+
})

‎packages/nightwatch-devtools/src/session.ts‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
collectorDrainExpression,
77
drainCollectorWithRecovery,
88
errorMessage,
9+
foldedStartTime,
910
loadInjectableScript,
1011
NativeNetworkCapture,
1112
pollUntilReady,
@@ -289,13 +290,17 @@ export class SessionCapturer extends SessionCapturerBase {
289290
// Allow the slot to be re-used by a new entry
290291
this.sentCommandIds.delete(oldId)
291292

292-
// The retry keeps the row's public `id`, so the dashboard updates the row it already shows.
293+
// The retry keeps the row's public `id`, so the dashboard updates the row
294+
// it already shows, and its first attempt's start, so the retrying shows.
293295
const entry = this.#commandEntry(
294296
command,
295297
args,
296298
result,
297299
error,
298-
meta,
300+
{
301+
...meta,
302+
startTime: foldedStartTime(previous?.startTime, meta.startTime)
303+
},
299304
previous?.id
300305
)
301306
this.commandsLog.push(entry)

‎packages/nightwatch-devtools/tests/session.test.ts‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,30 @@ describe('SessionCapturer.captureCommand', () => {
124124
}
125125
})
126126

127+
it('keeps a retried row on its first attempt start, so the retrying stays on it', async () => {
128+
const cap = makeCapturer(makeMockBrowser({ sessionId: 'sess-retry' }))
129+
try {
130+
await cap.captureCommand('getText', ['#flash'], 'first', undefined, {
131+
startTime: 1_000,
132+
timestamp: 1_100
133+
})
134+
const firstId = (cap.commandsLog[0] as { _id?: number })._id ?? -1
135+
const { entry } = cap.replaceCommand(
136+
firstId,
137+
'getText',
138+
['#flash'],
139+
'last',
140+
undefined,
141+
{ startTime: 9_000, timestamp: 9_100 }
142+
)
143+
expect(entry.startTime).toBe(1_000)
144+
expect(entry.timestamp).toBe(9_100)
145+
expect(entry.result).toBe('last')
146+
} finally {
147+
cap.cleanup()
148+
}
149+
})
150+
127151
it('sends no timings for a navigation row a retry has replaced', async () => {
128152
const http = await import('node:http')
129153
let reads = 0

‎packages/selenium-devtools/src/helpers/captureOrReplaceCommand.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { RetryTracker, toError } from '@wdio/devtools-core'
1+
import { foldedStartTime, RetryTracker, toError } from '@wdio/devtools-core'
22
import type { SessionCapturer } from '../session.js'
33
import type { CapturedCommand, CommandLog, TestStats } from '../types.js'
44

@@ -114,6 +114,8 @@ function replaceRow(
114114
const previous = capturer.commandsLog.find(
115115
(row) => (row as CommandLog & { _id?: number })._id === targetId
116116
)
117+
// Read before the replacement rewrites the row in place.
118+
const firstStart = previous?.startTime
117119
const replaced = capturer.replaceCommand(
118120
targetId,
119121
cmd.command,
@@ -126,6 +128,8 @@ function replaceRow(
126128
)
127129
const entry = replaced.entry as CommandLog & { _id?: number }
128130
applyCapturedFields(entry, cmd, test.retries)
131+
// A retry keeps its first attempt's start, so the time spent retrying stays on the row.
132+
entry.startTime = foldedStartTime(firstStart, cmd.startTime)
129133
capturer.sendReplaceCommand(replaced.oldTimestamp, entry)
130134
return entry
131135
}

‎packages/selenium-devtools/tests/captureOrReplaceCommand.test.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,9 @@ describe('captureOrReplaceCommand', () => {
7979
expect(retried._id).toBe(first._id)
8080
expect(capturer.commandsLog).toHaveLength(1)
8181
expect(retried.selector).toBe('#username')
82-
expect(retried.startTime).toBe(1_900)
82+
// The row keeps its first attempt's start (950), so the retrying stays on it.
83+
expect(retried.startTime).toBe(950)
84+
expect(retried.timestamp).toBe(2_000)
8385
} finally {
8486
capturer.cleanup()
8587
}

‎packages/service/src/assertion-tracker.ts‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ interface PendingAssertion {
3939
expectedValue?: unknown
4040
testUid?: string
4141
stepUid?: string
42+
/** When the matcher began, before it resolved its element and polled. */
43+
startedAt: number
4244
}
4345

4446
export class AssertionTracker {
@@ -97,7 +99,8 @@ export class AssertionTracker {
9799
matcherName: params.matcherName,
98100
expectedValue: params.expectedValue,
99101
testUid: this.#ctx.getTestUid(),
100-
stepUid: this.#ctx.getStepUid()
102+
stepUid: this.#ctx.getStepUid(),
103+
startedAt: Date.now()
101104
}
102105
// Reset before the matcher resolves its element (WDIO's $ is lazy, so it
103106
// resolves inside the matcher) — only an element resolved during this
@@ -127,10 +130,13 @@ export class AssertionTracker {
127130
}
128131
// Reached afterAssertion → the matcher resolved (pass or value-fail), so no
129132
// hard-throw synthesis is needed at test end.
133+
const startedAt = this.#pendingAssertion?.startedAt
130134
this.#pendingAssertion = undefined
131135
const capturer = this.#ctx.getCapturer()
132136
const entry = expectAssertionToCommandLog(params, this.#ctx.getTestUid())
133137
entry.stepUid = this.#ctx.getStepUid()
138+
// The row spans the matcher from its start, its retries included.
139+
entry.startTime = startedAt ?? entry.startTime
134140
if (capturer.coalesceAssertionIntoLastRead(entry, isMatcherReadCommand)) {
135141
return
136142
}
@@ -207,6 +213,7 @@ export class AssertionTracker {
207213
true
208214
)
209215
entry.stepUid = pending.stepUid
216+
entry.startTime = pending.startedAt
210217
const capturer = this.#ctx.getCapturer()
211218
if (
212219
capturer.coalesceAssertionIntoLastRead(entry, isMatcherReadCommand, true)

0 commit comments

Comments
 (0)