Skip to content

Commit

Permalink
cherry-pick(#31781): chore: show error when opening newer trace with … (
Browse files Browse the repository at this point in the history
#31797)

…old viewer

Reference: microsoft/playwright-java#1617
  • Loading branch information
yury-s authored Jul 22, 2024
1 parent b129aba commit 2f17a27
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 2 deletions.
3 changes: 3 additions & 0 deletions packages/trace-viewer/src/sw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.`);
Expand Down
14 changes: 12 additions & 2 deletions packages/trace-viewer/src/traceModernizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down
Binary file added tests/assets/trace-from-the-future.zip
Binary file not shown.
5 changes: 5 additions & 0 deletions tests/library/trace-viewer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down

0 comments on commit 2f17a27

Please sign in to comment.