Skip to content

Commit 8684e08

Browse files
Skn0ttli-zhixin
authored andcommitted
cherry-pick(#39121): fix(trace viewer): make paths via stdin work
Co-authored-by: li-zhixin <cruz.liu@developertools.com>
1 parent 97bc385 commit 8684e08

3 files changed

Lines changed: 19 additions & 6 deletions

File tree

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ export type TraceViewerAppOptions = {
5555

5656
const tracesDirMarker = 'traces.dir';
5757

58-
function validateTraceUrl(traceFileOrUrl: string | undefined): string | undefined {
58+
function validateTraceUrlOrPath(traceFileOrUrl: string | undefined): string | undefined {
5959
if (!traceFileOrUrl)
6060
return traceFileOrUrl;
6161

@@ -152,7 +152,7 @@ export async function installRootRedirect(server: HttpServer, traceUrl: string |
152152
}
153153

154154
export async function runTraceViewerApp(traceUrl: string | undefined, browserName: string, options: TraceViewerServerOptions & { headless?: boolean }, exitOnClose?: boolean) {
155-
traceUrl = validateTraceUrl(traceUrl);
155+
traceUrl = validateTraceUrlOrPath(traceUrl);
156156
const server = await startTraceViewerServer(options);
157157
await installRootRedirect(server, traceUrl, options);
158158
const page = await openTraceViewerApp(server.urlPrefix('precise'), browserName, options);
@@ -162,7 +162,7 @@ export async function runTraceViewerApp(traceUrl: string | undefined, browserNam
162162
}
163163

164164
export async function runTraceInBrowser(traceUrl: string | undefined, options: TraceViewerServerOptions) {
165-
traceUrl = validateTraceUrl(traceUrl);
165+
traceUrl = validateTraceUrlOrPath(traceUrl);
166166
const server = await startTraceViewerServer(options);
167167
await installRootRedirect(server, traceUrl, options);
168168
await openTraceInBrowser(server.urlPrefix('human-readable'));
@@ -216,8 +216,8 @@ class StdinServer implements Transport {
216216

217217
constructor() {
218218
process.stdin.on('data', data => {
219-
const url = data.toString().trim();
220-
if (url === this._traceUrl)
219+
const url = validateTraceUrlOrPath(data.toString().trim());
220+
if (!url || url === this._traceUrl)
221221
return;
222222
if (url.endsWith('.json'))
223223
this._pollLoadTrace(url);

packages/playwright-core/src/server/utils/httpServer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ export class HttpServer {
115115
this._port = address.port;
116116
const resolvedHost = address.family === 'IPv4' ? address.address : `[${address.address}]`;
117117
this._urlPrefixPrecise = `http://${resolvedHost}:${address.port}`;
118-
this._urlPrefixHumanReadable = `http://${host}:${address.port}`;
118+
this._urlPrefixHumanReadable = `http://${host ?? 'localhost'}:${address.port}`;
119119
}
120120
}
121121

tests/library/trace-viewer.spec.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2248,3 +2248,16 @@ test('should capture iframe with srcdoc', async ({ page, server, runAndTrace })
22482248
const frame = await traceViewer.snapshotFrame('Evaluate');
22492249
await expect(frame.frameLocator('iframe').getByRole('button')).toHaveText('Hello iframe');
22502250
});
2251+
2252+
test('take trace paths via stdin', async ({ childProcess, page }) => {
2253+
const cliEntrypoint = path.join(__dirname, '../../packages/playwright-core/cli.js');
2254+
const cp = childProcess({ command: ['node', cliEntrypoint, 'show-trace', '--port', '0', '--stdin'] });
2255+
await cp.waitForOutput('Listening on');
2256+
const url = cp.output.match(/Listening on (http:\/\/[^\s]+)/)![1];
2257+
await page.goto(url);
2258+
await expect(page).toHaveTitle('Playwright Trace Viewer');
2259+
cp.write(traceFile);
2260+
await expect(page.locator('.action-title')).toContainText([
2261+
/Create page/,
2262+
]);
2263+
});

0 commit comments

Comments
 (0)