Repository navigation
Expand file tree
/
Copy pathtrace-frame-snapshots.test.ts
More file actions
546 lines (506 loc) · 17.4 KB
/
Copy pathtrace-frame-snapshots.test.ts
File metadata and controls
546 lines (506 loc) · 17.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
import { describe, it, expect } from 'vitest'
import { strFromU8, strToU8, unzipSync, zipSync } from 'fflate'
import {
buildActionEvents,
buildImageFrameSnapshots,
buildScreenshotResourceSnapshots,
exportTraceZip,
FrameSnapshotIndex,
screenshotUrl,
type AfterEvent,
type FrameSnapshotEvent,
type FrameSnapshotRef,
type ScreencastFrameEvent,
type ScreenshotResourceEvent
} from '@wdio/devtools-trace'
import { parseTraceZip } from '../src/trace-reader.js'
import {
TraceType,
type ActionSnapshot,
type CommandLog
} from '@wdio/devtools-shared'
function snap(overrides: Partial<ActionSnapshot> = {}): ActionSnapshot {
return {
timestamp: 2000,
command: 'click',
screenshot: 'AAAA',
...overrides
}
}
describe('FrameSnapshotIndex', () => {
it('claims a snapshot by exact command timestamp and names it after@<callId>', () => {
const index = new FrameSnapshotIndex([snap()])
expect(index.claimAfter(2000, 'call@2')).toBe('after@call@2')
expect(index.refs()).toEqual([
{ callId: 'call@2', snapshotName: 'after@call@2', snapshot: snap() }
])
})
it('returns undefined when the index holds no captures at all', () => {
const index = new FrameSnapshotIndex([])
expect(index.claimAfter(1234, 'call@2')).toBeUndefined()
expect(index.refs()).toEqual([])
})
it('shares one capture across actions completing at the same instant', () => {
// Nightwatch emits its native assertion rows in a batch whose execution
// windows collapse onto one instant; consuming left the rest blank.
const index = new FrameSnapshotIndex([snap()])
expect(index.claimAfter(2000, 'call@2')).toBe('after@call@2')
expect(index.claimAfter(2000, 'call@3')).toBe('after@call@3')
expect(index.refs().map((r) => r.callId)).toEqual(['call@2', 'call@3'])
expect(index.refs()[1]!.snapshot).toEqual(snap())
})
it('falls back to the most recent capture before an uncaptured action', () => {
// An assertion reads the page rather than changing it, so it takes no
// capture of its own and inherits the preceding action's.
const index = new FrameSnapshotIndex([
snap(),
snap({ timestamp: 3000, command: 'setValue', screenshot: 'BB' })
])
expect(index.claimAfter(3500, 'call@9')).toBe('after@call@9')
expect(index.refs()[0]!.snapshot.screenshot).toBe('BB')
})
it('still returns nothing for an action preceding every capture', () => {
const index = new FrameSnapshotIndex([snap()])
expect(index.claimAfter(1000, 'call@1')).toBeUndefined()
expect(index.refs()).toEqual([])
})
it('ignores snapshots without a screenshot', () => {
const index = new FrameSnapshotIndex([snap({ screenshot: undefined })])
expect(index.claimAfter(2000, 'call@2')).toBeUndefined()
})
it('keeps the richest capture for duplicate timestamps', () => {
const index = new FrameSnapshotIndex([
snap({ screenshot: 'AA' }),
snap({ screenshot: 'AAAAAAAA' }),
snap({ screenshot: 'AAAA' })
])
index.claimAfter(2000, 'call@2')
expect(index.refs()[0]!.snapshot.screenshot).toBe('AAAAAAAA')
})
it('tracks the last claimed name as the next action before state', () => {
const index = new FrameSnapshotIndex([
snap(),
snap({ timestamp: 3000, command: 'setValue' })
])
expect(index.beforeName()).toBeUndefined()
index.claimAfter(2000, 'call@2')
expect(index.beforeName()).toBe('after@call@2')
index.claimAfter(3000, 'call@3')
expect(index.beforeName()).toBe('after@call@3')
})
it('exposes element rects by timestamp regardless of screenshot', () => {
const index = new FrameSnapshotIndex([
snap({ screenshot: undefined, elements: [{ selector: '#go' }] })
])
expect(index.elementsAt(2000)).toEqual([{ selector: '#go' }])
expect(index.elementsAt(9999)).toBeUndefined()
})
it('keeps the richest element set for duplicate timestamps', () => {
const index = new FrameSnapshotIndex([
snap({ elements: [{ selector: 'a' }] }),
snap({ elements: [{ selector: 'a' }, { selector: 'b' }] })
])
expect(index.elementsAt(2000)).toHaveLength(2)
})
})
describe('input point synthesis (A8)', () => {
const wallTime = 1000
const box = { x: 10, y: 20, width: 40, height: 10 }
function pointOf(commands: CommandLog[], snaps: ActionSnapshot[]) {
const events = buildActionEvents(
commands,
'page@abc',
wallTime,
undefined,
new FrameSnapshotIndex(snaps)
)
const after = events.find((e) => e.type === 'after') as
AfterEvent | undefined
return after?.point
}
it('sets after.point to the matched element rect centre for a pointer action', () => {
const point = pointOf(
[{ command: 'click', args: ['#go'], timestamp: 2000, startTime: 1950 }],
[snap({ elements: [{ selector: '#go', boundingBox: box }] })]
)
expect(point).toEqual({ x: 30, y: 25 })
})
it('resolves the point from the row locator when args carry none', () => {
// Selenium acts through a resolved element handle — the receiver, not an
// argument — so `args` holds no locator and `selector` is the only evidence.
const point = pointOf(
[{ command: 'click', args: [], selector: '#go', timestamp: 2000 }],
[snap({ elements: [{ selector: '#go', boundingBox: box }] })]
)
expect(point).toEqual({ x: 30, y: 25 })
})
it('prefers the row locator over a fill value sitting in args[0]', () => {
const point = pointOf(
[
{
command: 'sendKeys',
args: ['tomsmith'],
selector: '#username',
timestamp: 2000
}
],
[snap({ elements: [{ selector: '#username', boundingBox: box }] })]
)
expect(point).toEqual({ x: 30, y: 25 })
})
it('omits the point when the selector is absent from captured elements', () => {
const point = pointOf(
[{ command: 'click', args: ['#go'], timestamp: 2000, startTime: 1950 }],
[snap({ elements: [{ selector: '#other', boundingBox: box }] })]
)
expect(point).toBeUndefined()
})
it('omits the point for non-pointer commands', () => {
const point = pointOf(
[{ command: 'getText', args: ['#go'], timestamp: 2000, startTime: 1950 }],
[
snap({
command: 'getText',
elements: [{ selector: '#go', boundingBox: box }]
})
]
)
expect(point).toBeUndefined()
})
})
describe('buildImageFrameSnapshots', () => {
const pageId = 'page@abc123'
const wallTime = 1000
const viewport = { width: 800, height: 600 }
function ref(overrides: Partial<ActionSnapshot> = {}): FrameSnapshotRef {
return {
callId: 'call@2',
snapshotName: 'after@call@2',
snapshot: snap(overrides)
}
}
it('emits events with exactly the reference field set', () => {
const [event] = buildImageFrameSnapshots(
[ref()],
pageId,
wallTime,
viewport
)
expect(Object.keys(event!)).toEqual(['type', 'snapshot'])
expect(event!.type).toBe('frame-snapshot')
expect(Object.keys(event!.snapshot).sort()).toEqual(
[
'callId',
'collectionTime',
'doctype',
'frameId',
'frameUrl',
'html',
'isMainFrame',
'pageId',
'resourceOverrides',
'snapshotName',
'timestamp',
'viewport',
'wallTime'
].sort()
)
})
it('references the stored screenshot from an image document in node-array format', () => {
const [event] = buildImageFrameSnapshots(
[ref({ url: 'https://example.test/login' })],
pageId,
wallTime,
viewport
)
expect(event!.snapshot.html).toEqual([
'HTML',
{},
['HEAD', {}, ['BASE', { href: 'https://example.test/login' }]],
[
'BODY',
{ style: 'margin:0' },
[
'IMG',
{
src: 'https://screenshot.wdio-devtools.invalid/action.jpeg',
style: 'display:block;width:100vw;height:100vh;object-fit:contain'
}
]
]
])
})
it('stamps identity, timing, and frame fields from the ref', () => {
const [event] = buildImageFrameSnapshots(
[ref()],
pageId,
wallTime,
viewport
)
const s = event!.snapshot
expect(s.callId).toBe('call@2')
expect(s.snapshotName).toBe('after@call@2')
expect(s.pageId).toBe(pageId)
expect(s.frameId).toBe('frame@abc123')
expect(s.doctype).toBe('html')
expect(s.viewport).toEqual(viewport)
expect(s.timestamp).toBe(1000)
expect(s.wallTime).toBe(2000)
expect(s.collectionTime).toBe(0)
expect(s.resourceOverrides).toEqual([
{
url: 'https://screenshot.wdio-devtools.invalid/action.jpeg',
sha1: 'page@abc123-2000.jpeg'
}
])
expect(s.isMainFrame).toBe(true)
})
it('gives a png screenshot its own url, sniffed from the base64 magic', () => {
const [event] = buildImageFrameSnapshots(
[ref({ screenshot: 'iVBORw0KGgo' })],
pageId,
wallTime,
viewport
)
const html = JSON.stringify(event!.snapshot.html)
expect(html).toContain(
'https://screenshot.wdio-devtools.invalid/action.png'
)
expect(event!.snapshot.resourceOverrides[0]!.url).toBe(
'https://screenshot.wdio-devtools.invalid/action.png'
)
})
it('never inlines the screenshot bytes', () => {
const [event] = buildImageFrameSnapshots(
[ref({ screenshot: 'iVBORw0KGgoAAAANSUhEUg' })],
pageId,
wallTime,
viewport
)
const json = JSON.stringify(event)
expect(json).not.toContain('data:')
expect(json).not.toContain('iVBORw0KGgoAAAANSUhEUg')
})
it('falls back to about:blank when the snapshot has no url', () => {
const [event] = buildImageFrameSnapshots(
[ref()],
pageId,
wallTime,
viewport
)
expect(event!.snapshot.frameUrl).toBe('about:blank')
})
it('clamps timestamps captured before wallTime to zero', () => {
const [event] = buildImageFrameSnapshots(
[ref({ timestamp: 500 })],
pageId,
wallTime,
viewport
)
expect(event!.snapshot.timestamp).toBe(0)
expect(event!.snapshot.wallTime).toBe(500)
})
})
describe('buildScreenshotResourceSnapshots', () => {
const pageId = 'page@abc123'
function ref(callId: string, snapshot: ActionSnapshot): FrameSnapshotRef {
return { callId, snapshotName: `after@${callId}`, snapshot }
}
it('records one response per screenshot url, served from the first stored screenshot', () => {
const events = buildScreenshotResourceSnapshots(
[
ref('call@1', snap({ timestamp: 2000, screenshot: 'iVBORw0KGgo1' })),
ref('call@2', snap({ timestamp: 3000, screenshot: 'iVBORw0KGgo2' })),
ref('call@3', snap({ timestamp: 4000, screenshot: '/9j/4AAQ' }))
],
pageId
)
expect(
events.map((e) => [
e.snapshot.request.url,
e.snapshot.response.content.mimeType,
e.snapshot.response.content._sha1
])
).toEqual([
[
'https://screenshot.wdio-devtools.invalid/action.png',
'image/png',
'page@abc123-2000.png'
],
[
'https://screenshot.wdio-devtools.invalid/action.jpeg',
'image/jpeg',
'page@abc123-4000.jpeg'
]
])
})
it('is a GET recorded before any snapshot, in the snapshot frame', () => {
// The viewer serves only a GET whose time precedes the snapshot, and the
// first snapshot of a trace is commonly at time 0.
const [event] = buildScreenshotResourceSnapshots(
[ref('call@1', snap())],
pageId
)
expect(event!.type).toBe('resource-snapshot')
expect(event!.snapshot.request.method).toBe('GET')
expect(event!.snapshot.response.status).toBe(200)
expect(event!.snapshot._monotonicTime).toBeLessThan(0)
expect(event!.snapshot._frameref).toBe('frame@abc123')
expect(event!.snapshot.pageref).toBe(pageId)
})
it('records nothing when no screenshot is referenced', () => {
expect(buildScreenshotResourceSnapshots([], pageId)).toEqual([])
})
})
describe('a written trace stores each screenshot once', () => {
const PNG =
'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg=='
const JPEG =
'/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAMCAgICAgMCAgIDAwMDBAYEBAQEBAgGBgUGBwkI'
async function written() {
const zip = await exportTraceZip(
{
mutations: [],
logs: [],
consoleLogs: [],
networkRequests: [],
metadata: { type: TraceType.Standalone },
commands: [
{ command: 'url', args: ['https://example.test'], timestamp: 1000 },
{ command: 'click', args: ['#go'], timestamp: 2000 },
{ command: 'getText', args: ['#out'], timestamp: 3000 }
],
sources: {},
actionSnapshots: [
{ timestamp: 1000, command: 'url', screenshot: PNG },
{
timestamp: 2000,
command: 'click',
screenshot: JPEG,
snapshotText: '- button "Go"'
}
]
},
{ sessionId: 'abc12345' }
)
const files = unzipSync(zip)
const events = strFromU8(files['trace.trace']!)
.trim()
.split('\n')
.map((line) => JSON.parse(line) as { type: string })
return { zip, files, events }
}
it('writes no image bytes into the frame-snapshot events', async () => {
const { events } = await written()
const frameSnapshots = events.filter((e) => e.type === 'frame-snapshot')
expect(frameSnapshots).toHaveLength(3)
const json = JSON.stringify(frameSnapshots)
expect(json).not.toContain('data:image')
expect(json).not.toContain(PNG)
expect(json).not.toContain(JPEG)
})
it('points every override at a stored resource the src and a recorded response both name', async () => {
const { files, events } = await written()
const responses = events.filter(
(e) => e.type === 'resource-snapshot'
) as ScreenshotResourceEvent[]
const responseUrls = responses.map((r) => r.snapshot.request.url)
for (const response of responses) {
expect(
files[`resources/${response.snapshot.response.content._sha1}`]
).toBeDefined()
}
const frameSnapshots = events.filter(
(e) => e.type === 'frame-snapshot'
) as FrameSnapshotEvent[]
for (const { snapshot } of frameSnapshots) {
const [override] = snapshot.resourceOverrides
expect(snapshot.resourceOverrides).toHaveLength(1)
expect(files[`resources/${override!.sha1}`]).toBeDefined()
expect(JSON.stringify(snapshot.html)).toContain(
`"src":"${override!.url}"`
)
expect(responseUrls).toContain(override!.url)
}
// The getText row inherits the click's capture, and so its stored file.
expect(
frameSnapshots.map((e) => e.snapshot.resourceOverrides[0]!.sha1)
).toEqual([
'page@abc12345-1000.png',
'page@abc12345-2000.jpeg',
'page@abc12345-2000.jpeg'
])
expect(responseUrls).toEqual([screenshotUrl(PNG), screenshotUrl(JPEG)])
})
it('names each screenshot by its own format in every place that names it', async () => {
const { files, events } = await written()
const stored = Object.keys(files)
.filter((name) => /\.(png|jpeg)$/.test(name))
.sort()
expect(stored).toEqual([
'resources/page@abc12345-1000.png',
'resources/page@abc12345-2000.jpeg'
])
const filmstrip = (
events.filter(
(e) => e.type === 'screencast-frame'
) as ScreencastFrameEvent[]
).map((e) => e.sha1)
const overrides = (
events.filter((e) => e.type === 'frame-snapshot') as FrameSnapshotEvent[]
).map((e) => e.snapshot.resourceOverrides[0]!.sha1)
const responses = (
events.filter(
(e) => e.type === 'resource-snapshot'
) as ScreenshotResourceEvent[]
).map((e) => e.snapshot.response.content)
for (const name of [
...filmstrip,
...overrides,
...responses.map((c) => c._sha1)
]) {
expect(stored).toContain(`resources/${name}`)
}
expect(new Set(filmstrip)).toEqual(new Set(overrides))
expect(responses.map((c) => [c.mimeType, c._sha1])).toEqual([
['image/png', 'page@abc12345-1000.png'],
['image/jpeg', 'page@abc12345-2000.jpeg']
])
})
it('reads back unchanged, with the screenshot responses kept out of the network rows', async () => {
const { zip } = await written()
const data = parseTraceZip(zip)
expect(data.trace.networkRequests).toEqual([])
expect(data.trace.commands.map((c) => c.command)).toEqual([
'url',
'click',
'getText'
])
expect(data.frames.map((f) => f.screenshot)).toEqual([PNG, JPEG])
expect(data.trace.commands[1]!.snapshotText).toBe('- button "Go"')
})
it('still reads a zip written when every screenshot was named .jpeg', async () => {
const { files } = await written()
const legacy: Record<string, Uint8Array> = {}
for (const [name, data] of Object.entries(files)) {
legacy[name.replace(/\.png$/, '.jpeg')] = data
}
legacy['trace.trace'] = strToU8(
strFromU8(files['trace.trace']!).replaceAll(
'page@abc12345-1000.png',
'page@abc12345-1000.jpeg'
)
)
expect(legacy['resources/page@abc12345-1000.jpeg']).toBeDefined()
expect(Object.keys(legacy).some((name) => name.endsWith('.png'))).toBe(
false
)
const data = parseTraceZip(zipSync(legacy))
expect(data.frames.map((f) => f.screenshot)).toEqual([PNG, JPEG])
expect(data.trace.commands.map((c) => c.screenshot)).toEqual([
PNG,
JPEG,
JPEG
])
})
})