Skip to content

Commit 17e9dd9

Browse files
authored
feat(trace): support loading trace from zip (#6551)
1 parent a7ea00d commit 17e9dd9

File tree

2 files changed

+34
-9
lines changed

2 files changed

+34
-9
lines changed

src/cli/cli.ts

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@
1818

1919
/* eslint-disable no-console */
2020

21+
import extract from 'extract-zip';
22+
import fs from 'fs';
23+
import os from 'os';
2124
import path from 'path';
25+
import rimraf from 'rimraf';
2226
import program from 'commander';
23-
import os from 'os';
24-
import fs from 'fs';
2527
import { runDriver, runServer, printApiJson, launchBrowserServer, installBrowsers } from './driver';
26-
import { showTraceViewer } from '../server/trace/viewer/traceViewer';
28+
import { TraceViewer } from '../server/trace/viewer/traceViewer';
2729
import * as playwright from '../..';
2830
import { BrowserContext } from '../client/browserContext';
2931
import { Browser } from '../client/browser';
@@ -475,3 +477,31 @@ function commandWithOpenOptions(command: string, description: string, options: a
475477
.option('--user-agent <ua string>', 'specify user agent string')
476478
.option('--viewport-size <size>', 'specify browser viewport size in pixels, for example "1280, 720"');
477479
}
480+
481+
export async function showTraceViewer(tracePath: string, browserName: string) {
482+
let stat;
483+
try {
484+
stat = fs.statSync(tracePath);
485+
} catch (e) {
486+
console.log(`No such file or directory: ${tracePath}`);
487+
return;
488+
}
489+
490+
if (stat.isDirectory()) {
491+
const traceViewer = new TraceViewer(tracePath, browserName);
492+
await traceViewer.show();
493+
return;
494+
}
495+
496+
const zipFile = tracePath;
497+
const dir = fs.mkdtempSync(path.join(os.tmpdir(), `playwright-trace`));
498+
process.on('exit', () => rimraf.sync(dir));
499+
try {
500+
await extract(zipFile, { dir: dir });
501+
} catch (e) {
502+
console.log(`Invalid trace file: ${zipFile}`);
503+
return;
504+
}
505+
const traceViewer = new TraceViewer(dir, browserName);
506+
await traceViewer.show();
507+
}

src/server/trace/viewer/traceViewer.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import { ProgressController } from '../../progress';
2929

3030
const fsReadFileAsync = util.promisify(fs.readFile.bind(fs));
3131

32-
class TraceViewer {
32+
export class TraceViewer {
3333
private _server: HttpServer;
3434
private _browserName: string;
3535

@@ -144,8 +144,3 @@ class TraceViewer {
144144
await page.mainFrame().goto(internalCallMetadata(), urlPrefix + '/traceviewer/traceViewer/index.html');
145145
}
146146
}
147-
148-
export async function showTraceViewer(traceDir: string, browserName: string) {
149-
const traceViewer = new TraceViewer(traceDir, browserName);
150-
await traceViewer.show();
151-
}

0 commit comments

Comments
 (0)