Skip to content

Commit 49c1f69

Browse files
authored
fix(trace viewer): load trace from a local file (#41263)
1 parent fc893f8 commit 49c1f69

3 files changed

Lines changed: 18 additions & 4 deletions

File tree

‎packages/playwright-core/src/server/trace/viewer/traceViewer.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,13 @@ function traceFileRoots(traceUrl: string | undefined): string[] {
218218
} catch {
219219
}
220220
}
221+
if (traceUrl?.startsWith('file?path=')) {
222+
try {
223+
const url = new URL(traceUrl, 'http://localhost');
224+
return [path.dirname(url.searchParams.get('path')!)];
225+
} catch {
226+
}
227+
}
221228
return [process.cwd()];
222229
}
223230

‎tests/config/traceViewerFixtures.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ type BaseWorkerFixtures = {
3131
};
3232

3333
export type TraceViewerFixtures = {
34-
showTraceViewer: (trace: string | undefined, options?: {host?: string, port?: number, stdin?: boolean}) => Promise<TraceViewerPage>;
34+
showTraceViewer: (trace: string | undefined, options?: {host?: string, port?: number, stdin?: boolean, cwd?: string}) => Promise<TraceViewerPage>;
3535
runAndTrace: (body: () => Promise<void>, optsOverrides?: Parameters<BrowserContext['tracing']['start']>[0]) => Promise<TraceViewerPage>;
3636
};
3737

@@ -153,7 +153,7 @@ export const traceViewerFixtures: Fixtures<TraceViewerFixtures, {}, BaseTestFixt
153153
showTraceViewer: async ({ playwright, childProcess, browserName }, use, testInfo) => {
154154
const browsers: Browser[] = [];
155155
const tracings: any[] = [];
156-
await use(async (trace: string | undefined, { host, port, stdin } = {}) => {
156+
await use(async (trace: string | undefined, { host, port, stdin, cwd } = {}) => {
157157
const command = [
158158
'node',
159159
path.join(__dirname, '../../packages/playwright-core/cli.js'),
@@ -166,7 +166,7 @@ export const traceViewerFixtures: Fixtures<TraceViewerFixtures, {}, BaseTestFixt
166166
command.push('--stdin');
167167
if (trace)
168168
command.push(trace);
169-
const cp = childProcess({ command });
169+
const cp = childProcess({ command, cwd });
170170
await cp.waitForOutput('Listening on');
171171
const browser = await playwright.chromium.launch({
172172
...(browserName === 'chromium' ? {} : { channel: 'chromium' }),

‎tests/library/trace-viewer.spec.ts‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,16 @@ test('should open two trace viewers', async ({ showTraceViewer }, testInfo) => {
106106
});
107107

108108
test('should open trace viewer on specific host', async ({ showTraceViewer }, testInfo) => {
109-
const traceViewer = await showTraceViewer(testInfo.outputPath(), { host: '127.0.0.1' });
109+
const dir = testInfo.outputPath('some-dir');
110+
await fs.promises.mkdir(dir, { recursive: true });
111+
// Run from a random directory to check that file access works.
112+
const traceViewer = await showTraceViewer(traceFile, { host: '127.0.0.1', cwd: dir });
110113
await expect(traceViewer.page).toHaveTitle('Playwright Trace Viewer');
111114
await expect(traceViewer.page).toHaveURL(/127.0.0.1/);
115+
await expect(traceViewer.actionTitles).toContainText([
116+
/Create page/,
117+
/Close page/,
118+
]);
112119
});
113120

114121
test('should show tracing.group in the action list with location', async ({ runAndTrace, page, context }) => {

0 commit comments

Comments
 (0)