Skip to content
Merged
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
12 changes: 11 additions & 1 deletion packages/playwright-core/src/tools/backend/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ export type RouteEntry = {
export type FilenameTemplate = {
prefix: string;
ext: string;
ordinal?: boolean;
suggestedFilename?: string;
date?: Date;
};
Expand Down Expand Up @@ -132,6 +133,7 @@ export class Context {
traces: (TraceInfo & { browserContext: playwrightTypes.BrowserContext })[];
} | undefined;
private _disposables: Disposable[] = [];
private _outputFiles = new Set<string>();

private _webmcpToolsSignature = '';
private _redactUrlParamsRegex: RegExp | undefined;
Expand Down Expand Up @@ -250,7 +252,15 @@ export class Context {

async outputFile(template: FilenameTemplate, options: { origin: 'code' | 'llm' }): Promise<string> {
const baseName = template.suggestedFilename || `${template.prefix}-${(template.date ?? new Date()).toISOString().replace(/[:.]/g, '-')}${template.ext ? '.' + template.ext : ''}`;
return await outputFile(this.options, baseName, options);
const resolvedFile = await outputFile(this.options, baseName, options);
if (!template.ordinal)
return resolvedFile;
const { dir, name, ext } = path.parse(resolvedFile);
let fileName = resolvedFile;
for (let ordinal = 1; this._outputFiles.has(fileName) || fs.existsSync(fileName); ++ordinal)
fileName = path.join(dir, `${name}-${ordinal}${ext}`);
this._outputFiles.add(fileName);
return fileName;
}

async startVideoRecording(fileName: string, params: VideoParams) {
Expand Down
2 changes: 1 addition & 1 deletion packages/playwright-core/src/tools/backend/tab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ export class Tab extends EventEmitter<TabEventsInterface> {

private async _downloadStarted(download: playwright.Download) {
// Do not trust web names.
const outputFile = await this.context.outputFile({ suggestedFilename: sanitizeForFilePath(download.suggestedFilename()), prefix: 'download', ext: 'bin' }, { origin: 'code' });
const outputFile = await this.context.outputFile({ suggestedFilename: sanitizeForFilePath(download.suggestedFilename()), prefix: 'download', ext: 'bin', ordinal: true }, { origin: 'code' });
const entry = {
download,
finished: false,
Expand Down
25 changes: 25 additions & 0 deletions tests/mcp/cli-save-as.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,33 @@
* limitations under the License.
*/

import fs from 'fs/promises';
import path from 'path';

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

test('downloads with the same suggested filename', {
annotation: { type: 'issue', description: 'https://github.com/microsoft/playwright-cli/issues/514' },
}, async ({ cli, server }, testInfo) => {
server.setContent('/', `
<a href="/v1" download="same.txt">V1</a>
<a href="/v2" download="same.txt">V2</a>
`, 'text/html');
server.setContent('/v1', 'v1', 'text/plain');
server.setContent('/v2', 'v2', 'text/plain');

await cli('open', server.PREFIX);
for (const [ref, filename] of [['e2', 'same.txt'], ['e3', 'same-1.txt']]) {
let { output } = await cli('click', ref);
await expect.poll(async () => {
output += '\n' + (await cli('snapshot')).output;
return output;
}).toContain(`Downloaded file same.txt to "${path.join('.playwright-cli', filename)}"`);
}
expect(await fs.readFile(testInfo.outputPath('.playwright-cli', 'same.txt'), 'utf8')).toBe('v1');
expect(await fs.readFile(testInfo.outputPath('.playwright-cli', 'same-1.txt'), 'utf8')).toBe('v2');
});

test('screenshot', async ({ cli, server, mcpBrowser }) => {
await cli('open', server.HELLO_WORLD);
const { attachments } = await cli('screenshot');
Expand Down