Skip to content

Commit ab850af

Browse files
authored
fix: support relative downloadsPath directory for downloads (#6402)
1 parent 5509527 commit ab850af

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

src/server/browserType.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -271,8 +271,10 @@ function copyTestHooks(from: object, to: object) {
271271

272272
function validateLaunchOptions<Options extends types.LaunchOptions>(options: Options): Options {
273273
const { devtools = false } = options;
274-
let { headless = !devtools } = options;
274+
let { headless = !devtools, downloadsPath } = options;
275275
if (debugMode())
276276
headless = false;
277-
return { ...options, devtools, headless };
277+
if (downloadsPath && !path.isAbsolute(downloadsPath))
278+
downloadsPath = path.join(process.cwd(), downloadsPath);
279+
return { ...options, devtools, headless, downloadsPath };
278280
}

tests/downloads-path.spec.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
import { playwrightTest as it, expect } from './config/browserTest';
1818
import fs from 'fs';
19+
import path from 'path';
1920

2021
it.describe('downloads path', () => {
2122
it.beforeEach(async ({server}) => {
@@ -71,6 +72,20 @@ it.describe('downloads path', () => {
7172
await downloadsBrowser.close();
7273
});
7374

75+
it('should report downloads in downloadsPath folder with a relative path', async ({browserType, browserOptions, server}, testInfo) => {
76+
const downloadsBrowser = await browserType.launch({ ...browserOptions, downloadsPath: path.relative(process.cwd(), testInfo.outputPath('')) });
77+
const page = await downloadsBrowser.newPage({ acceptDownloads: true });
78+
await page.setContent(`<a href="${server.PREFIX}/download">download</a>`);
79+
const [ download ] = await Promise.all([
80+
page.waitForEvent('download'),
81+
page.click('a')
82+
]);
83+
const downloadPath = await download.path();
84+
expect(downloadPath.startsWith(testInfo.outputPath(''))).toBeTruthy();
85+
await page.close();
86+
await downloadsBrowser.close();
87+
});
88+
7489
it('should accept downloads in persistent context', async ({launchPersistent, server}, testInfo) => {
7590
const { context, page } = await launchPersistent({ acceptDownloads: true, downloadsPath: testInfo.outputPath('') });
7691
await page.setContent(`<a href="${server.PREFIX}/download">download</a>`);

0 commit comments

Comments
 (0)