Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 63 additions & 8 deletions packages/playwright-core/src/tools/trace/traceSnapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,34 +87,89 @@ async function serveTraceSnapshot(storage: SnapshotStorage, loader: TraceLoader,
const snapshotServer = new SnapshotServer(storage, sha1 => loader.resourceForSha1(sha1));
const httpServer = new HttpServer();

httpServer.routePrefix('/snapshot/', (request: any, response: any) => {
httpServer.routePrefix('/snapshot/', (request, response) => {
const url = new URL('http://localhost' + request.url!);
const pageOrFrameId = url.pathname.substring('/snapshot/'.length);
const searchParams = url.searchParams;
searchParams.set('name', snapshotKey);
const snapshotResponse = snapshotServer.serveSnapshot(pageOrFrameId, searchParams, url.href);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is very likely going to explode

Copy link
Copy Markdown
Member Author

@Skn0tt Skn0tt May 5, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In what way? Passing pageOrFrameId and url.href is exactly what we do in other callsites. Currently any snapshot with an iframe is displayed as infinite recursion of the main frame.

response.statusCode = snapshotResponse.status;
snapshotResponse.headers.forEach((value: string, key: string) => response.setHeader(key, value));
snapshotResponse.text().then((text: string) => response.end(text));
snapshotResponse.headers.forEach((value, key) => response.setHeader(key, value));
snapshotResponse.text().then(text => response.end(text));
return true;
});

httpServer.routePrefix('/', (_request: any, response: any) => {
response.statusCode = 302;
response.setHeader('Location', `/snapshot/${pageId}?name=${encodeURIComponent(snapshotKey)}`);
response.end();
httpServer.routePath('/__pwsnapshot/sw.js', (_request, response) => {
response.statusCode = 200;
response.setHeader('Content-Type', 'application/javascript');
response.setHeader('Service-Worker-Allowed', '/');
response.end(`
self.addEventListener('install', () => self.skipWaiting());
self.addEventListener('activate', e => e.waitUntil(self.clients.claim()));
self.addEventListener('fetch', e => {
const reqUrl = new URL(e.request.url);
if (reqUrl.origin === self.location.origin)
return;
e.respondWith((async () => {
const client = e.clientId ? await self.clients.get(e.clientId) : null;
if (!client)
return new Response('No client', { status: 500 });
const params = new URLSearchParams({
frame: client.url,
url: e.request.url,
method: e.request.method,
});
return fetch('/__pwsnapshot/resource?' + params.toString());
})());
});
`);
return true;
});

httpServer.routePath('/__pwsnapshot/resource', (request, response) => {
const url = new URL('http://localhost' + request.url!);
const frame = new URL(url.searchParams.get('frame')!);
const snapshotUrl = 'http://localhost' + frame.pathname + frame.search;
const requestUrl = url.searchParams.get('url')!;
const method = url.searchParams.get('method')!;
snapshotServer.serveResource([requestUrl], method, snapshotUrl).then(async resp => {
response.statusCode = resp.status;
resp.headers.forEach((value, key) => response.appendHeader(key, value));
response.end(Buffer.from(await resp.arrayBuffer()));
}).catch(() => {
response.statusCode = 500;
response.end();
});
return true;
});

const startTime = Date.now();
httpServer.routePath('/', (_request, response) => {
response.statusCode = 200;
response.setHeader('Content-Type', 'text/html; charset=utf-8');
response.end(`<!DOCTYPE html><html><body><script>(async () => {
await navigator.serviceWorker.register('/__pwsnapshot/sw.js?v=${startTime}', { scope: '/' });
await navigator.serviceWorker.ready;
if (!navigator.serviceWorker.controller)
await new Promise(r => navigator.serviceWorker.addEventListener('controllerchange', r, { once: true }));
location.replace(${JSON.stringify(`/snapshot/${pageId}?name=${encodeURIComponent(snapshotKey)}`)});
})();</script></body></html>`);
return true;
});

await httpServer.start({ preferredPort: 0 });
return { url: httpServer.urlPrefix('human-readable'), stop: () => httpServer.stop() };
const url = httpServer.urlPrefix('human-readable');

return { url, stop: () => httpServer.stop() };
}

async function runCommandOnSnapshot(server: { url: string, stop: () => Promise<void> }, browserArgs: string[]) {
const browser = await playwright.chromium.launch({ headless: true });
const context = await browser.newContext();

const page = await context.newPage();
await page.goto(server.url);
await page.waitForURL(url => new URL(url).pathname.startsWith('/snapshot/'));

const backend = new BrowserBackend({
snapshot: { mode: 'full' },
Expand Down
12 changes: 11 additions & 1 deletion tests/mcp/trace-cli-fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,22 @@ export const test = baseTest
const page = await context.newPage();
server.setContent('/', `
<html>
<head><title>Test Page</title></head>
<head>
<title>Test Page</title>
<link rel="stylesheet" href="/style.css">
</head>
<body>
<h1>Hello World</h1>
<button id="btn">Click me</button>
<input id="search" type="text" placeholder="Search..." />
<a href="/page2">Go to page 2</a>
<div id="styled">Styled text</div>
</body>
</html>
`, 'text/html');

server.setContent('/style.css', '#styled { color: rgb(255, 0, 0); }', 'text/css');

server.setContent('/page2', `
<html>
<head><title>Page 2</title></head>
Expand Down Expand Up @@ -114,6 +120,10 @@ export const test = baseTest
await context.tracing.stop({ path: tracePath });
await browser.close();

// Drop the recorded routes so trace replay tests can't accidentally fetch sub-resources
// from the still-running test server — they should come from the trace archive.
server.reset();

await use(tracePath);

await fs.promises.rm(tmpDir, { recursive: true, force: true });
Expand Down
20 changes: 19 additions & 1 deletion tests/mcp/trace-cli.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
*/

import fs from 'fs';
import path from 'path';

import { test, expect } from './trace-cli-fixtures';
import path from 'path';

test.skip(({ mcpBrowser }) => mcpBrowser !== 'chrome', 'Chrome-only');

Expand Down Expand Up @@ -185,6 +185,24 @@ test('trace snapshot resolves inner frames', async ({ runTraceCli }) => {
expect(stdout).toContain('Innermost');
});

test('trace snapshot replays sub-resource stylesheets from the archive', async ({ runTraceCli }) => {
// The CLI snapshot HTTP server only handles `/snapshot/...` directly. Sub-resource requests
// (the snapshot HTML's <base href> points at the recorded origin) are intercepted via
// BrowserContext.route in serveTraceSnapshot's decorateContext, mirroring the trace-viewer
// service worker's `snapshotServer.serveResource(...)` logic.
const { stdout: listOutput } = await runTraceCli(['actions', '--grep', 'Click']);
const match = listOutput.match(/^\s+(\d+)\.\s/m);
expect(match).toBeTruthy();
const ordinal = match![1];

const { stdout, exitCode } = await runTraceCli([
'snapshot', '--name', 'before', ordinal,
'--', 'eval', 'el => getComputedStyle(el).color', '#styled',
]);
expect(exitCode).toBe(0);
expect(stdout).toContain('rgb(255, 0, 0)');
});

test('trace screenshot saves image file', async ({ runTraceCli }, testInfo) => {
const { stdout: listOutput } = await runTraceCli(['actions', '--grep', 'Navigate']);
const match = listOutput.match(/^\s+(\d+)\.\s/m);
Expand Down
Loading