Skip to content

Commit 7bcfbd9

Browse files
committed
refactor: Simplification across packages/core and service
- "Un-truncating" selectors in transcript
1 parent 24ef4e0 commit 7bcfbd9

5 files changed

Lines changed: 24 additions & 46 deletions

File tree

packages/core/src/action-mapping.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,8 @@ export function formatActionTitle(
5858
if (firstArg === undefined) {
5959
return `${action.class}.${action.method}()`
6060
}
61-
const label = (
61+
const label =
6262
typeof firstArg === 'object' ? JSON.stringify(firstArg) : String(firstArg)
63-
).slice(0, 80)
6463
return `${action.class}.${action.method}("${label}")`
6564
}
6665

packages/core/src/action-snapshot.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,12 @@ export async function captureActionSnapshot(
5555
const isNativeMobile = !input.runScript && !!input.getPageSource
5656

5757
const [shot, url, title, pageSource, tree, elements] = await Promise.all([
58-
input.takeScreenshot?.().catch(() => null) ?? Promise.resolve(null),
59-
input.getUrl?.().catch(() => undefined) ?? Promise.resolve(undefined),
60-
input.getTitle?.().catch(() => undefined) ?? Promise.resolve(undefined),
58+
input.takeScreenshot?.().catch(() => null),
59+
input.getUrl?.().catch(() => undefined),
60+
input.getTitle?.().catch(() => undefined),
6161
isNativeMobile
62-
? (input.getPageSource?.().catch(() => undefined) ??
63-
Promise.resolve(undefined))
64-
: Promise.resolve(undefined),
62+
? input.getPageSource?.().catch(() => undefined)
63+
: undefined,
6564
runWith<AccessibilityNode[]>(
6665
input.runScript,
6766
accessibilityTreeScript(true),

packages/core/src/trace-exporter.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ import type {
1616
import {
1717
formatActionTitle,
1818
mapCommandToAction,
19-
FILL_METHODS
19+
FILL_METHODS,
20+
type TraceAction
2021
} from './action-mapping.js'
2122
import { networkRequestToHar } from './trace-har.js'
2223
import { buildTraceZip, type TraceZipResource } from './trace-zip-writer.js'
@@ -343,12 +344,15 @@ function generateTranscript(
343344
const wallTimeISO = new Date(startWallTime).toISOString()
344345
const lines: string[] = [`# ${title ?? 'Session'}${wallTimeISO}`, '']
345346

346-
const captured = commands.filter(
347-
(c) => mapCommandToAction(String(c.command)) !== null
348-
)
347+
const captured: { entry: CommandLog; action: TraceAction }[] = []
348+
for (const c of commands) {
349+
const action = mapCommandToAction(String(c.command))
350+
if (action) {
351+
captured.push({ entry: c, action })
352+
}
353+
}
349354

350-
captured.forEach((entry, idx) => {
351-
const action = mapCommandToAction(String(entry.command))!
355+
captured.forEach(({ entry, action }, idx) => {
352356
const label = formatActionTitle(action, entry.args as unknown[])
353357

354358
const rawArgs = entry.args as unknown[]
@@ -357,7 +361,7 @@ function generateTranscript(
357361
if (FILL_METHODS.has(action.method) && rawArgs) {
358362
const valueIdx = rawArgs.length >= 2 ? 1 : 0
359363
if (rawArgs[valueIdx] !== undefined) {
360-
parts.push(`value="${String(rawArgs[valueIdx]).slice(0, 50)}"`)
364+
parts.push(`value="${String(rawArgs[valueIdx])}"`)
361365
}
362366
}
363367

packages/service/src/index.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,6 @@ export default class DevToolsHookService implements Services.ServiceInstance {
7474
*/
7575
#commandStack: CommandFrame[] = []
7676

77-
// This is used to capture the last command signature to avoid duplicate captures
78-
#lastCommandSig: string | null = null
79-
8077
/**
8178
* allows to define the type of data being captured to hint the
8279
* devtools app which data to expect
@@ -239,7 +236,6 @@ export default class DevToolsHookService implements Services.ServiceInstance {
239236
}
240237

241238
private resetStack() {
242-
this.#lastCommandSig = null
243239
this.#commandStack = []
244240
}
245241

@@ -269,20 +265,18 @@ export default class DevToolsHookService implements Services.ServiceInstance {
269265

270266
#pushTopLevelCommandFrame(
271267
command: string,
272-
args: string[],
273268
callSource: string | undefined
274269
): void {
275270
if (INTERNAL_COMMANDS.includes(command)) {
276271
return
277272
}
278-
const cmdSig = JSON.stringify({ command, args, src: callSource })
279-
if (this.#lastCommandSig !== cmdSig) {
273+
const top = this.#commandStack[this.#commandStack.length - 1]
274+
if (!top || top.command !== command || top.callSource !== callSource) {
280275
this.#commandStack.push({
281276
command,
282277
callSource,
283278
startTimestamp: Date.now()
284279
})
285-
this.#lastCommandSig = cmdSig
286280
}
287281
}
288282

@@ -309,7 +303,6 @@ export default class DevToolsHookService implements Services.ServiceInstance {
309303
if (source && this.#commandStack.length === 0) {
310304
this.#pushTopLevelCommandFrame(
311305
command,
312-
args,
313306
this.#resolveCallSourceFromFrame(source)
314307
)
315308

packages/service/src/session.ts

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,10 @@ export class SessionCapturer extends SessionCapturerBase {
205205
!isNativeMobile(browser) &&
206206
PAGE_TRANSITION_COMMANDS.includes(command)
207207
) {
208-
await this.#capturePerformance(browser, commandLogEntry, args)
209-
await this.#captureTrace(browser)
208+
await Promise.all([
209+
this.#capturePerformance(browser, commandLogEntry, args),
210+
this.#captureTrace(browser)
211+
])
210212
}
211213
}
212214

@@ -369,26 +371,7 @@ export class SessionCapturer extends SessionCapturerBase {
369371
try {
370372
const { request, timestamp } = event
371373
const requestId = request.request
372-
const requestHeaders: Record<string, string> = {}
373-
if (request.headers) {
374-
request.headers.forEach(
375-
(h: {
376-
name: string
377-
value: { type?: string; value?: string } | string
378-
}) => {
379-
const name = typeof h.name === 'string' ? h.name.toLowerCase() : ''
380-
const value =
381-
typeof h.value === 'string'
382-
? h.value
383-
: typeof h.value === 'object' && h.value?.value
384-
? h.value.value
385-
: ''
386-
if (name) {
387-
requestHeaders[name] = value
388-
}
389-
}
390-
)
391-
}
374+
const requestHeaders = this.#flattenBidiHeaders(request.headers)
392375

393376
this.#pendingNetworkRequests.set(requestId, {
394377
url: request.url,

0 commit comments

Comments
 (0)