Skip to content

Commit e036a9f

Browse files
authored
chore(bidi): add support for browser.setDownloadBehavior (#39311)
1 parent e75321e commit e036a9f

File tree

6 files changed

+20
-33
lines changed

6 files changed

+20
-33
lines changed

packages/playwright-core/src/server/bidi/bidiBrowser.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,13 @@ export class BidiBrowserContext extends BrowserContext {
215215
const promises: Promise<any>[] = [
216216
super._initialize(),
217217
];
218+
const downloadBehavior: bidi.Browser.DownloadBehavior = this._options.acceptDownloads === 'accept' ?
219+
{ type: 'allowed', destinationFolder: this._browser.options.downloadsPath } :
220+
{ type: 'denied' };
221+
promises.push(this._browser._browserSession.send('browser.setDownloadBehavior', {
222+
downloadBehavior,
223+
userContexts: [this._userContextId()],
224+
}));
218225
promises.push(this.doUpdateDefaultViewport());
219226
if (this._options.geolocation)
220227
promises.push(this.setGeolocation(this._options.geolocation));

packages/playwright-core/src/server/bidi/bidiPage.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ export class BidiPage implements PageDelegate {
254254
if (!originPage)
255255
return;
256256

257-
this._browserContext._browser._downloadCreated(originPage, event.navigation, event.url, event.suggestedFilename);
257+
this._browserContext._browser._downloadCreated(originPage, event.navigation, event.url, event.suggestedFilename, event.suggestedFilename);
258258
}
259259

260260
private _onDownloadEnded(event: bidi.BrowsingContext.DownloadEndParams) {

packages/playwright-core/src/server/bidi/third_party/bidiCommands.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ export interface Commands {
5050
};
5151
returnType: Bidi.Browser.RemoveUserContext;
5252
};
53+
'browser.setDownloadBehavior': {
54+
params: Bidi.Browser.SetDownloadBehaviorParameters;
55+
returnType: Bidi.Browser.SetDownloadBehaviorResult;
56+
}
5357

5458
'browsingContext.activate': {
5559
params: Bidi.BrowsingContext.ActivateParameters;

packages/playwright-core/src/server/browser.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,8 @@ export abstract class Browser extends SdkObject {
140140
return this._contextForReuse?.context;
141141
}
142142

143-
_downloadCreated(page: Page, uuid: string, url: string, suggestedFilename?: string) {
144-
const download = new Download(page, this.options.downloadsPath || '', uuid, url, suggestedFilename);
143+
_downloadCreated(page: Page, uuid: string, url: string, suggestedFilename?: string, downloadFilename?: string) {
144+
const download = new Download(page, this.options.downloadsPath || '', uuid, url, suggestedFilename, downloadFilename);
145145
this._downloads.set(uuid, download);
146146
}
147147

packages/playwright-core/src/server/download.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,10 @@ export class Download {
2626
private _page: Page;
2727
private _suggestedFilename: string | undefined;
2828

29-
constructor(page: Page, downloadsPath: string, uuid: string, url: string, suggestedFilename?: string) {
29+
constructor(page: Page, downloadsPath: string, uuid: string, url: string, suggestedFilename?: string, downloadFilename?: string) {
3030
const unaccessibleErrorMessage = page.browserContext._options.acceptDownloads === 'deny' ? 'Pass { acceptDownloads: true } when you are creating your browser context.' : undefined;
31-
this.artifact = new Artifact(page, path.join(downloadsPath, uuid), unaccessibleErrorMessage, () => {
31+
const downloadPath = path.join(downloadsPath, downloadFilename ?? uuid);
32+
this.artifact = new Artifact(page, downloadPath, unaccessibleErrorMessage, () => {
3233
return this._page.browserContext.cancelDownload(uuid);
3334
});
3435
this._page = page;

tests/bidi/expectations/moz-firefox-nightly-library.txt

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,11 @@ library/browsercontext-viewport.spec.ts › should support touch with null viewp
5858
library/browsertype-connect.spec.ts › launchServer › should be able to connect 20 times to a single server without warnings [timeout]
5959
library/browsertype-connect.spec.ts › launchServer › should save download [fail]
6060
library/browsertype-connect.spec.ts › launchServer › should saveAs videos from remote browser [timeout]
61+
library/browsertype-connect.spec.ts › launchServer › should upload a folder [fail]
6162
library/browsertype-connect.spec.ts › run-server › should be able to connect 20 times to a single server without warnings [timeout]
6263
library/browsertype-connect.spec.ts › run-server › should save download [fail]
6364
library/browsertype-connect.spec.ts › run-server › should saveAs videos from remote browser [timeout]
65+
library/browsertype-connect.spec.ts › run-server › should upload a folder [fail]
6466
library/browsertype-launch.spec.ts › should reject if launched browser fails immediately [fail]
6567
library/capabilities.spec.ts › should play video @smoke [timeout]
6668
library/channels.spec.ts › should work with the domain module [timeout]
@@ -90,7 +92,6 @@ library/chromium/launcher.spec.ts › should support request/response events whe
9092
library/debug-controller.spec.ts › should allow setting recorder mode only for specific browser [fail]
9193
library/debug-controller.spec.ts › should record [flaky]
9294
library/debug-controller.spec.ts › should work with browser._launchServer [flaky]
93-
library/defaultbrowsercontext-1.spec.ts › should support acceptDownloads option [fail]
9495
library/defaultbrowsercontext-1.spec.ts › should support bypassCSP option [fail]
9596
library/defaultbrowsercontext-1.spec.ts › should support javascriptEnabled option [fail]
9697
library/defaultbrowsercontext-2.spec.ts › should have passed URL when launching with ignoreDefaultArgs: true [timeout]
@@ -100,35 +101,9 @@ library/defaultbrowsercontext-2.spec.ts › should support forcedColors option [
100101
library/defaultbrowsercontext-2.spec.ts › should support hasTouch option [fail]
101102
library/defaultbrowsercontext-2.spec.ts › should support reducedMotion option [fail]
102103
library/download.spec.ts › download event › should be able to cancel pending downloads [timeout]
103-
library/download.spec.ts › download event › should create subdirectories when saving to non-existent user-specified path [fail]
104-
library/download.spec.ts › download event › should delete downloads on browser gone [fail]
105-
library/download.spec.ts › download event › should delete downloads on context destruction [fail]
106-
library/download.spec.ts › download event › should delete file [fail]
107-
library/download.spec.ts › download event › should download large binary.zip [fail]
108-
library/download.spec.ts › download event › should emit download event from nested iframes [fail]
109-
library/download.spec.ts › download event › should expose stream [fail]
110-
library/download.spec.ts › download event › should not fail explicitly to cancel a download even if that is already finished [fail]
111104
library/download.spec.ts › download event › should report download path within page.on('download', …) handler for Blobs [timeout]
112-
library/download.spec.ts › download event › should report download path within page.on('download', …) handler for Files [fail]
113-
library/download.spec.ts › download event › should report download when navigation turns into download @smoke [fail]
114-
library/download.spec.ts › download event › should report downloads for download attribute [fail]
115-
library/download.spec.ts › download event › should report downloads with acceptDownloads: true [fail]
116-
library/download.spec.ts › download event › should report downloads with interception [fail]
117105
library/download.spec.ts › download event › should report new window downloads [timeout]
118-
library/download.spec.ts › download event › should report non-navigation downloads [fail]
119-
library/download.spec.ts › download event › should save to overwritten filepath [fail]
120-
library/download.spec.ts › download event › should save to two different paths with multiple saveAs calls [fail]
121-
library/download.spec.ts › download event › should save to user-specified path without updating original path [fail]
122-
library/download.spec.ts › download event › should work with Cross-Origin-Opener-Policy [fail]
123-
library/download.spec.ts › should be able to download a inline PDF file via navigation [timeout]
124-
library/download.spec.ts › should be able to download a inline PDF file via response interception [fail]
125-
library/download.spec.ts › should be able to download a PDF file [fail]
126-
library/download.spec.ts › should download successfully when routing [fail]
127-
library/download.spec.ts › should save to user-specified path [fail]
128-
library/downloads-path.spec.ts › downloads path › should accept downloads in persistent context [fail]
129-
library/downloads-path.spec.ts › downloads path › should delete downloads when context closes [fail]
130-
library/downloads-path.spec.ts › downloads path › should delete downloads when persistent context closes [fail]
131-
library/downloads-path.spec.ts › downloads path › should keep downloadsPath folder [fail]
106+
library/download.spec.ts › download event › should throw if browser dies [fail]
132107
library/emulation-focus.spec.ts › should focus popups by default [fail]
133108
library/emulation-focus.spec.ts › should focus with more than one page/context [fail]
134109
library/emulation-focus.spec.ts › should think that all pages are focused @smoke [fail]

0 commit comments

Comments
 (0)