Skip to content
Closed
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
17 changes: 16 additions & 1 deletion packages/playwright-core/src/tools/backend/tab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/

import { EventEmitter } from 'events';
import fs from 'fs';
import path from 'path';
import { types } from 'util';
import debug from 'debug';
import { asLocator } from '@isomorphic/locatorGenerators';
Expand Down Expand Up @@ -238,7 +240,20 @@ 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 suggestedFile = await this.context.outputFile({ suggestedFilename: sanitizeForFilePath(download.suggestedFilename()), prefix: 'download', ext: 'bin' }, { origin: 'code' });

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.

Instead, make outputFile suggest a name w/ next ordinal. I.e. next to prefix and ext options, get an ordinal: boolean and when it is true it'll pick a new name every time.

const ext = path.extname(suggestedFile);
let outputFile = suggestedFile;
for (let ordinal = 1; ; ++ordinal) {
try {
// Reserve the name before saving, including for concurrent downloads.
await fs.promises.writeFile(outputFile, '', { flag: 'wx' });
break;
} catch (e) {
if ((e as NodeJS.ErrnoException).code !== 'EEXIST')
throw e;
}
outputFile = path.join(path.dirname(suggestedFile), `${path.basename(suggestedFile, ext)}-${ordinal}${ext}`);
}
const entry = {
download,
finished: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,10 @@ playwright-cli run-code "async page => {

## File Downloads

Downloads are saved automatically in the output directory using the suggested filename.
If that name already exists, a numeric suffix is added before the extension, such as
`report-1.pdf`. The download event reports the saved path.

```bash
# Handle file download
playwright-cli run-code "async page => {
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 preserve files 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 [link, filename] of [['V1', 'same.txt'], ['V2', 'same-1.txt']]) {
let { output } = await cli('click', `text=${link}`);
await expect.poll(async () => {
output += (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
Loading