From bef87849e32598acde82b6c2d117b89ad19dd569 Mon Sep 17 00:00:00 2001 From: Yury Semikhatsky Date: Mon, 22 Jul 2024 08:16:25 -0700 Subject: [PATCH] chore: show error when opening newer trace with old viewer (#31781) Reference: https://github.com/microsoft/playwright-java/issues/1617 --- packages/trace-viewer/src/sw.ts | 3 +++ packages/trace-viewer/src/traceModernizer.ts | 14 ++++++++++++-- tests/assets/trace-from-the-future.zip | Bin 0 -> 385 bytes tests/library/trace-viewer.spec.ts | 5 +++++ 4 files changed, 20 insertions(+), 2 deletions(-) create mode 100644 tests/assets/trace-from-the-future.zip diff --git a/packages/trace-viewer/src/sw.ts b/packages/trace-viewer/src/sw.ts index 841056f457..14d26c2a36 100644 --- a/packages/trace-viewer/src/sw.ts +++ b/packages/trace-viewer/src/sw.ts @@ -19,6 +19,7 @@ import { unwrapPopoutUrl } from './snapshotRenderer'; import { SnapshotServer } from './snapshotServer'; import { TraceModel } from './traceModel'; import { FetchTraceModelBackend, ZipTraceModelBackend } from './traceModelBackends'; +import { TraceVersionError } from './traceModernizer'; // @ts-ignore declare const self: ServiceWorkerGlobalScope; @@ -57,6 +58,8 @@ async function loadTrace(traceUrl: string, traceFileName: string | null, clientI console.error(error); if (error?.message?.includes('Cannot find .trace file') && await traceModel.hasEntry('index.html')) 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.'); + if (error instanceof TraceVersionError) + throw new Error(`Could not load trace from ${traceFileName || traceUrl}. ${error.message}`); if (traceFileName) throw new Error(`Could not load trace from ${traceFileName}. Make sure to upload a valid Playwright trace.`); throw new Error(`Could not load trace from ${traceUrl}. Make sure a valid Playwright Trace is accessible over this url.`); diff --git a/packages/trace-viewer/src/traceModernizer.ts b/packages/trace-viewer/src/traceModernizer.ts index a2a987318c..e7c65ac41f 100644 --- a/packages/trace-viewer/src/traceModernizer.ts +++ b/packages/trace-viewer/src/traceModernizer.ts @@ -22,6 +22,15 @@ import type * as traceV6 from './versions/traceV6'; import type { ActionEntry, ContextEntry, PageEntry } from './entries'; import type { SnapshotStorage } from './snapshotStorage'; +export class TraceVersionError extends Error { + constructor(message: string) { + super(message); + this.name = 'TraceVersionError'; + } +} + +const latestVersion: trace.VERSION = 7; + export class TraceModernizer { private _contextEntry: ContextEntry; private _snapshotStorage: SnapshotStorage; @@ -71,6 +80,8 @@ export class TraceModernizer { const contextEntry = this._contextEntry; switch (event.type) { case 'context-options': { + if (event.version > latestVersion) + 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.'); this._version = event.version; contextEntry.origin = event.origin; contextEntry.browserName = event.browserName; @@ -181,9 +192,8 @@ export class TraceModernizer { let version = this._version || event.version; if (version === undefined) return [event]; - const lastVersion: trace.VERSION = 7; let events = [event]; - for (; version < lastVersion; ++version) + for (; version < latestVersion; ++version) events = (this as any)[`_modernize_${version}_to_${version + 1}`].call(this, events); return events; } diff --git a/tests/assets/trace-from-the-future.zip b/tests/assets/trace-from-the-future.zip new file mode 100644 index 0000000000000000000000000000000000000000..4100bf93c6305007505b3f54bc6f763d40d08de5 GIT binary patch literal 385 zcmWIWW@Zs#U|`^2keu>4;{4=%o~uBfG9v>6H-ijANl{{QsvejO4dG;9J}5jp4TMW8 zxEUB(z5-Q&HS6{t%xiYwae4ojXTEvpf<}`&T3>@!zFKLYa6{Lk)zqjhSLEY+Q(e1# zGdCVDk7)8aBDm^J#$$*4Ad2Nut=YGfA6I$3ieSXaZ`d27L}rkV#I#9y|s z&uTxrd-*Esgv%{~@s>yDU6j7nxl@cQGv(shX(zJNRX?S_zQCGgefasG9qagx$$jlk z-LS^pGwhJ#p?&|Cyv-{+xA}5#rKaA^l?4U`DNjAl?vHwQGr3Sl*5}`E*<|bF-KBdU zHEnY}yd?jhYv_u3rf>QyLTqw`DkJ3U7z4Z+ndF#pg^dI-lo%L*0l=`N5yV0XDON~G Wp@mX_H!B-RDI*XD0%<9bZUz9Tyo_}K literal 0 HcmV?d00001 diff --git a/tests/library/trace-viewer.spec.ts b/tests/library/trace-viewer.spec.ts index 01e68269ec..1b31c904c1 100644 --- a/tests/library/trace-viewer.spec.ts +++ b/tests/library/trace-viewer.spec.ts @@ -120,6 +120,11 @@ test('should open simple trace viewer', async ({ showTraceViewer }) => { ]); }); +test('should complain about newer version of trace in old viewer', async ({ showTraceViewer, asset }, testInfo) => { + const traceViewer = await showTraceViewer([asset('trace-from-the-future.zip')]); + 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(); +}); + test('should contain action info', async ({ showTraceViewer }) => { const traceViewer = await showTraceViewer([traceFile]); await traceViewer.selectAction('locator.click');