Skip to content
Open
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
7 changes: 4 additions & 3 deletions packages/playwright-core/src/tools/trace/traceSnapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,12 @@ 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: any, response: any) => {
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(pageId, searchParams, '/snapshot');
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

response.statusCode = snapshotResponse.status;
snapshotResponse.headers.forEach((value: string, key: string) => response.setHeader(key, value));
snapshotResponse.text().then((text: string) => response.end(text));
Expand All @@ -100,7 +101,7 @@ async function serveTraceSnapshot(storage: SnapshotStorage, loader: TraceLoader,

httpServer.routePrefix('/', (_request: any, response: any) => {
response.statusCode = 302;
response.setHeader('Location', '/snapshot');
response.setHeader('Location', `/snapshot/${pageId}?name=${encodeURIComponent(snapshotKey)}`);
response.end();
return true;
});
Expand Down
26 changes: 24 additions & 2 deletions tests/mcp/trace-cli-fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,27 @@ export const test = baseTest
server.setContent('/page2', `
<html>
<head><title>Page 2</title></head>
<body><h1>Page 2</h1></body>
<body>
<h1>Page 2</h1>
<iframe src="/iframe" id="frame1"></iframe>
</body>
</html>
`, 'text/html');

server.setContent('/iframe', `
<html>
<head><title>Iframe</title></head>
<body>
<p>Iframe content</p>
<iframe src="/iframe-inner" id="frame2"></iframe>
</body>
</html>
`, 'text/html');

server.setContent('/iframe-inner', `
<html>
<head><title>Inner iframe</title></head>
<body><p>Innermost</p></body>
</html>
`, 'text/html');

Expand All @@ -84,7 +104,9 @@ export const test = baseTest

// Navigate to another page
await page.locator('a').click();
await page.waitForURL('**/page2');

// Click into innermost frame
await page.frameLocator('#frame1').frameLocator('#frame2').locator('p').click();

await page.close();
const tmpDir = path.join(workerInfo.project.outputDir, 'pw-trace-cli-' + workerInfo.workerIndex);
Expand Down
10 changes: 10 additions & 0 deletions tests/mcp/trace-cli.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,16 @@ test('trace snapshot --name before', async ({ runTraceCli }) => {
expect(stdout).toBeTruthy();
});

test('trace snapshot resolves inner frames', async ({ runTraceCli }) => {
const { stdout: listOutput } = await runTraceCli(['actions', '--grep', 'Click']);
const ordinals = [...listOutput.matchAll(/^\s+(\d+)\.\s/gm)].map(m => m[1]);
expect(ordinals.length).toBeGreaterThanOrEqual(2);
const anchorClickOrdinal = ordinals[ordinals.length - 1];

const { stdout } = await runTraceCli(['snapshot', '--name', 'after', anchorClickOrdinal]);
expect(stdout).toContain('Innermost');
});

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