Skip to content

Commit 0e7102d

Browse files
committed
chore: show error when opening newer trace with old viewer
Reference: microsoft/playwright-java#1617
1 parent b535139 commit 0e7102d

File tree

4 files changed

+20
-2
lines changed

4 files changed

+20
-2
lines changed

packages/trace-viewer/src/sw.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import { unwrapPopoutUrl } from './snapshotRenderer';
1919
import { SnapshotServer } from './snapshotServer';
2020
import { TraceModel } from './traceModel';
2121
import { FetchTraceModelBackend, ZipTraceModelBackend } from './traceModelBackends';
22+
import { TraceVersionError } from './traceModernizer';
2223

2324
// @ts-ignore
2425
declare const self: ServiceWorkerGlobalScope;
@@ -57,6 +58,8 @@ async function loadTrace(traceUrl: string, traceFileName: string | null, clientI
5758
console.error(error);
5859
if (error?.message?.includes('Cannot find .trace file') && await traceModel.hasEntry('index.html'))
5960
throw new Error('Could not load trace. Did you upload a Playwright HTML report instead? Make sure to extract the archive first and then double-click the index.html file or put it on a web server.');
61+
if (error instanceof TraceVersionError)
62+
throw new Error(`Could not load trace from ${traceFileName || traceUrl}. ${error.message}`);
6063
if (traceFileName)
6164
throw new Error(`Could not load trace from ${traceFileName}. Make sure to upload a valid Playwright trace.`);
6265
throw new Error(`Could not load trace from ${traceUrl}. Make sure a valid Playwright Trace is accessible over this url.`);

packages/trace-viewer/src/traceModernizer.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,15 @@ import type * as traceV6 from './versions/traceV6';
2222
import type { ActionEntry, ContextEntry, PageEntry } from './entries';
2323
import type { SnapshotStorage } from './snapshotStorage';
2424

25+
export class TraceVersionError extends Error {
26+
constructor(message: string) {
27+
super(message);
28+
this.name = 'TraceVersionError';
29+
}
30+
}
31+
32+
const latestVersion: trace.VERSION = 7;
33+
2534
export class TraceModernizer {
2635
private _contextEntry: ContextEntry;
2736
private _snapshotStorage: SnapshotStorage;
@@ -71,6 +80,8 @@ export class TraceModernizer {
7180
const contextEntry = this._contextEntry;
7281
switch (event.type) {
7382
case 'context-options': {
83+
if (event.version > latestVersion)
84+
throw new TraceVersionError('The trace was created by a newer version of Playwright and is not supported by this version of the viewer. Please use latest Playwright to open the trace.');
7485
this._version = event.version;
7586
contextEntry.origin = event.origin;
7687
contextEntry.browserName = event.browserName;
@@ -181,9 +192,8 @@ export class TraceModernizer {
181192
let version = this._version || event.version;
182193
if (version === undefined)
183194
return [event];
184-
const lastVersion: trace.VERSION = 7;
185195
let events = [event];
186-
for (; version < lastVersion; ++version)
196+
for (; version < latestVersion; ++version)
187197
events = (this as any)[`_modernize_${version}_to_${version + 1}`].call(this, events);
188198
return events;
189199
}
385 Bytes
Binary file not shown.

tests/library/trace-viewer.spec.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,11 @@ test('should open simple trace viewer', async ({ showTraceViewer }) => {
120120
]);
121121
});
122122

123+
test('should complain about newer version of trace in old viewer', async ({ showTraceViewer, asset }, testInfo) => {
124+
const traceViewer = await showTraceViewer([asset('trace-from-the-future.zip')]);
125+
await expect(traceViewer.page.getByText('The trace was created by a newer version of Playwright and is not supported by this version of the viewer.')).toBeVisible();
126+
});
127+
123128
test('should contain action info', async ({ showTraceViewer }) => {
124129
const traceViewer = await showTraceViewer([traceFile]);
125130
await traceViewer.selectAction('locator.click');

0 commit comments

Comments
 (0)