Skip to content

Commit ac6e954

Browse files
committed
fix: trace requests should show bodies from resource
1 parent bd4c1f1 commit ac6e954

File tree

4 files changed

+53
-7
lines changed

4 files changed

+53
-7
lines changed

packages/playwright-core/src/tools/trace/installSkill.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import path from 'path';
2222
export async function installSkill() {
2323
const cwd = process.cwd();
2424
const skillSource = path.join(__dirname, 'SKILL.md');
25-
const destDir = path.join(cwd, '.claude', 'playwright-trace');
25+
const destDir = path.join(cwd, '.claude', 'skills', 'playwright-trace');
2626
await fs.promises.mkdir(destDir, { recursive: true });
2727
const destFile = path.join(destDir, 'SKILL.md');
2828
await fs.promises.copyFile(skillSource, destFile);

packages/playwright-core/src/tools/trace/traceRequests.ts

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -114,12 +114,19 @@ export async function traceRequest(requestId: string) {
114114

115115
// Request body
116116
if (r.request.postData) {
117-
console.log('\n Request body');
118-
console.log(` type: ${r.request.postData.mimeType}`);
119-
if (r.request.postData.text) {
120-
const text = r.request.postData.text.length > 2000
121-
? r.request.postData.text.substring(0, 2000) + '...'
122-
: r.request.postData.text;
117+
let body = r.request.postData.text;
118+
const resource = r.request.postData._sha1 ?? r.request.postData._file;
119+
if (resource) {
120+
const blob = await trace.loader.resourceForSha1(resource);
121+
if (blob)
122+
body = await blob.text();
123+
}
124+
125+
if (body) {
126+
console.log('\n Request body');
127+
const text = body.length > 2000
128+
? body.substring(0, 2000) + '...'
129+
: body;
123130
console.log(` ${text}`);
124131
}
125132
}
@@ -131,6 +138,25 @@ export async function traceRequest(requestId: string) {
131138
console.log(` ${h.name}: ${h.value}`);
132139
}
133140

141+
// Response body
142+
{
143+
let body = r.response.content.text;
144+
const resource = r.response.content._sha1 ?? r.response.content._file;
145+
if (resource) {
146+
const blob = await trace.loader.resourceForSha1(resource);
147+
if (blob)
148+
body = await blob.text();
149+
}
150+
151+
if (body) {
152+
console.log('\n Response body');
153+
const text = body.length > 2000
154+
? body.substring(0, 2000) + '...'
155+
: body;
156+
console.log(` ${text}`);
157+
}
158+
}
159+
134160
// Security
135161
if (r._securityDetails) {
136162
console.log('\n Security');

tests/mcp/trace-cli-fixtures.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ export const test = baseTest
6161
</html>
6262
`, 'text/html');
6363

64+
server.setContent('/feedback', JSON.stringify({ received: true }), 'application/json');
65+
6466
// Navigate
6567
await page.goto(server.PREFIX);
6668

@@ -77,6 +79,9 @@ export const test = baseTest
7779
console.error('error message');
7880
});
7981

82+
// Fetch
83+
await page.evaluate(() => fetch('/feedback', { method: 'POST', body: 'What a great product!' }).then(res => res.text()));
84+
8085
// Navigate to another page
8186
await page.locator('a').click();
8287
await page.waitForURL('**/page2');

tests/mcp/trace-cli.spec.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,21 @@ test('trace request shows details', async ({ runTraceCli }) => {
108108
expect(stdout).toContain('Response headers');
109109
});
110110

111+
test('trace request shows request and response body', async ({ runTraceCli }) => {
112+
const { stdout, exitCode } = await runTraceCli(['requests', '--grep', 'feedback']);
113+
expect(exitCode).toBe(0);
114+
expect(stdout).toContain('feedback');
115+
const ordinal = stdout.match(/^\s+(\d+)\.\s/m)![1];
116+
const { stdout: requestOutput, exitCode: requestExitCode } = await runTraceCli(['request', ordinal]);
117+
expect(requestExitCode).toBe(0);
118+
expect(requestOutput).toContain('Request body');
119+
expect(requestOutput).toContain('text/plain');
120+
expect(requestOutput).toContain('What a great product!');
121+
expect(requestOutput).toContain('Response body');
122+
expect(requestOutput).toContain('application/json');
123+
expect(requestOutput).toContain(JSON.stringify({ received: true }));
124+
});
125+
111126
test('trace request with invalid ID', async ({ runTraceCli }) => {
112127
const { stderr, exitCode } = await runTraceCli(['request', '999999']);
113128
expect(exitCode).toBe(1);

0 commit comments

Comments
 (0)