Skip to content

Commit 95e5dca

Browse files
fix(cli): use ordinal filenames for repeated downloads (#44011)
1 parent 489470e commit 95e5dca

3 files changed

Lines changed: 37 additions & 2 deletions

File tree

‎packages/playwright-core/src/tools/backend/context.ts‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ export type RouteEntry = {
9696
export type FilenameTemplate = {
9797
prefix: string;
9898
ext: string;
99+
ordinal?: boolean;
99100
suggestedFilename?: string;
100101
date?: Date;
101102
};
@@ -132,6 +133,7 @@ export class Context {
132133
traces: (TraceInfo & { browserContext: playwrightTypes.BrowserContext })[];
133134
} | undefined;
134135
private _disposables: Disposable[] = [];
136+
private _outputFiles = new Set<string>();
135137

136138
private _webmcpToolsSignature = '';
137139
private _redactUrlParamsRegex: RegExp | undefined;
@@ -250,7 +252,15 @@ export class Context {
250252

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

256266
async startVideoRecording(fileName: string, params: VideoParams) {

‎packages/playwright-core/src/tools/backend/tab.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ export class Tab extends EventEmitter<TabEventsInterface> {
238238

239239
private async _downloadStarted(download: playwright.Download) {
240240
// Do not trust web names.
241-
const outputFile = await this.context.outputFile({ suggestedFilename: sanitizeForFilePath(download.suggestedFilename()), prefix: 'download', ext: 'bin' }, { origin: 'code' });
241+
const outputFile = await this.context.outputFile({ suggestedFilename: sanitizeForFilePath(download.suggestedFilename()), prefix: 'download', ext: 'bin', ordinal: true }, { origin: 'code' });
242242
const entry = {
243243
download,
244244
finished: false,

‎tests/mcp/cli-save-as.spec.ts‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,33 @@
1414
* limitations under the License.
1515
*/
1616

17+
import fs from 'fs/promises';
18+
import path from 'path';
19+
1720
import { test, expect } from './cli-fixtures';
1821

22+
test('downloads with the same suggested filename', {
23+
annotation: { type: 'issue', description: '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/microsoft/playwright-cli/issues/514' },
24+
}, async ({ cli, server }, testInfo) => {
25+
server.setContent('/', `
26+
<a href="/v1" download="same.txt">V1</a>
27+
<a href="/v2" download="same.txt">V2</a>
28+
`, 'text/html');
29+
server.setContent('/v1', 'v1', 'text/plain');
30+
server.setContent('/v2', 'v2', 'text/plain');
31+
32+
await cli('open', server.PREFIX);
33+
for (const [ref, filename] of [['e2', 'same.txt'], ['e3', 'same-1.txt']]) {
34+
let { output } = await cli('click', ref);
35+
await expect.poll(async () => {
36+
output += '\n' + (await cli('snapshot')).output;
37+
return output;
38+
}).toContain(`Downloaded file same.txt to "${path.join('.playwright-cli', filename)}"`);
39+
}
40+
expect(await fs.readFile(testInfo.outputPath('.playwright-cli', 'same.txt'), 'utf8')).toBe('v1');
41+
expect(await fs.readFile(testInfo.outputPath('.playwright-cli', 'same-1.txt'), 'utf8')).toBe('v2');
42+
});
43+
1944
test('screenshot', async ({ cli, server, mcpBrowser }) => {
2045
await cli('open', server.HELLO_WORLD);
2146
const { attachments } = await cli('screenshot');

0 commit comments

Comments
 (0)