Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
17 changes: 11 additions & 6 deletions packages/playwright-core/src/tools/backend/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,13 @@ export class Response {
this._raw = this._json || (options?.raw ?? false);
}

private _computRelativeTo(fileName: string): string {
return path.relative(this._clientWorkspace, fileName);
private _computeRelativeTo(fileName: string): string {
const rel = path.relative(this._clientWorkspace, fileName);
// Prefix bare filenames with `./` so they're not mistaken for living in
// the auto-named `.playwright-cli/` artifact directory.
if (path.dirname(rel) === '.' && !rel.startsWith('.'))
return './' + rel;
return rel;
}

async resolveClientFile(template: FilenameTemplate, title: string): Promise<ResolvedFile> {
Expand All @@ -78,7 +83,7 @@ export class Response {
fileName = await this.resolveClientFilename(template.suggestedFilename);
else
fileName = await this._context.outputFile(template, { origin: 'llm' });
const relativeName = this._computRelativeTo(fileName);
const relativeName = this._computeRelativeTo(fileName);
const printableLink = `- [${title}](${relativeName})`;
return { fileName, relativeName, printableLink };
}
Expand Down Expand Up @@ -113,7 +118,7 @@ export class Response {
}

addFileLink(title: string, fileName: string) {
const relativeName = this._computRelativeTo(fileName);
const relativeName = this._computeRelativeTo(fileName);
this.addTextResult(`- [${title}](${relativeName})`);
}

Expand Down Expand Up @@ -272,7 +277,7 @@ export class Response {
if (event.type === 'download-start')
text.push(`- Downloading file ${event.download.download.suggestedFilename()} ...`);
else if (event.type === 'download-finish')
text.push(`- Downloaded file ${event.download.download.suggestedFilename()} to "${this._computRelativeTo(event.download.outputFile)}"`);
text.push(`- Downloaded file ${event.download.download.suggestedFilename()} to "${this._computeRelativeTo(event.download.outputFile)}"`);
}
}
if (text.length)
Expand All @@ -281,7 +286,7 @@ export class Response {
const pausedDetails = this._context.debugger().pausedDetails();
if (pausedDetails) {
addSection('Paused', [
`- ${pausedDetails.title} at ${this._computRelativeTo(pausedDetails.location.file)}${pausedDetails.location.line ? ':' + pausedDetails.location.line : ''}`,
`- ${pausedDetails.title} at ${this._computeRelativeTo(pausedDetails.location.file)}${pausedDetails.location.line ? ':' + pausedDetails.location.line : ''}`,
'- Use any tools to explore and interact, resume by calling resume/step-over/pause-at',
]);
}
Expand Down
4 changes: 2 additions & 2 deletions tests/mcp/cli-save-as.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ test('screenshot --full-page', async ({ cli, server, mcpBrowser }) => {
test('screenshot --filename', async ({ cli, server, mcpBrowser }) => {
await cli('open', server.HELLO_WORLD);
const { output, attachments } = await cli('screenshot', '--filename=screenshot.png');
expect(output).toContain('[Screenshot of viewport](screenshot.png)');
expect(output).toContain('[Screenshot of viewport](./screenshot.png)');
expect(attachments[0].name).toEqual('Screenshot of viewport');
expect(attachments[0].data).toEqual(expect.any(Buffer));
});
Expand All @@ -58,7 +58,7 @@ test('pdf --filename', async ({ cli, server, mcpBrowser }) => {
test.skip(mcpBrowser !== 'chromium' && mcpBrowser !== 'chrome', 'PDF is only supported in Chromium and Chrome');
await cli('open', server.HELLO_WORLD);
const { output, attachments } = await cli('pdf', '--filename=pdf.pdf');
expect(output).toContain('[Page as pdf](pdf.pdf)');
expect(output).toContain('[Page as pdf](./pdf.pdf)');
expect(attachments[0].name).toEqual('Page as pdf');
expect(attachments[0].data).toEqual(expect.any(Buffer));
});
Loading