Skip to content

Commit

Permalink
fix(electron): allow downloads (#31390)
Browse files Browse the repository at this point in the history
  • Loading branch information
mxschmitt committed Jun 20, 2024
1 parent 1ebd20a commit 6ed3b37
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
7 changes: 6 additions & 1 deletion packages/playwright-core/src/server/browserContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -654,8 +654,13 @@ export function validateBrowserContextOptions(options: channels.BrowserNewContex
throw new Error(`"deviceScaleFactor" option is not supported with null "viewport"`);
if (options.noDefaultViewport && !!options.isMobile)
throw new Error(`"isMobile" option is not supported with null "viewport"`);
if (options.acceptDownloads === undefined)
if (options.acceptDownloads === undefined && browserOptions.name !== 'electron')
options.acceptDownloads = 'accept';
// Electron requires explicit acceptDownloads: true since we wait for
// https://github.com/electron/electron/pull/41718 to be widely shipped.
// In 6-12 months, we can remove this check.
else if (options.acceptDownloads === undefined && browserOptions.name === 'electron')
options.acceptDownloads = 'internal-browser-default';
if (!options.viewport && !options.noDefaultViewport)
options.viewport = { width: 1280, height: 720 };
if (options.recordVideo) {
Expand Down
2 changes: 1 addition & 1 deletion packages/playwright-core/src/server/chromium/crBrowser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ export class CRBrowserContext extends BrowserContext {
override async _initialize() {
assert(!Array.from(this._browser._crPages.values()).some(page => page._browserContext === this));
const promises: Promise<any>[] = [super._initialize()];
if (this._browser.options.name !== 'electron' && this._browser.options.name !== 'clank' && this._options.acceptDownloads !== 'internal-browser-default') {
if (this._browser.options.name !== 'clank' && this._options.acceptDownloads !== 'internal-browser-default') {
promises.push(this._browser._session.send('Browser.setDownloadBehavior', {
behavior: this._options.acceptDownloads === 'accept' ? 'allowAndName' : 'deny',
browserContextId: this._browserContextId,
Expand Down
24 changes: 24 additions & 0 deletions tests/electron/electron-app.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,3 +314,27 @@ test('should return app name / version from manifest', async ({ launchElectronAp
version: '1.0.0'
});
});

test('should report downloads', async ({ launchElectronApp, server }) => {
test.skip(parseInt(require('electron/package.json').version.split('.')[0], 10) < 30, 'Depends on https://github.com/electron/electron/pull/41718');

server.setRoute('/download', (req, res) => {
res.setHeader('Content-Type', 'application/octet-stream');
res.setHeader('Content-Disposition', 'attachment');
res.end(`Hello world`);
});

const app = await launchElectronApp('electron-window-app.js', [], {
acceptDownloads: true,
});
const window = await app.firstWindow();
await window.setContent(`<a href="${server.PREFIX}/download">download</a>`);
const [download] = await Promise.all([
window.waitForEvent('download'),
window.click('a')
]);
const path = await download.path();
expect(fs.existsSync(path)).toBeTruthy();
expect(fs.readFileSync(path).toString()).toBe('Hello world');
await app.close();
});

0 comments on commit 6ed3b37

Please sign in to comment.