From b846ddda046781c83f3c85a0156a160bba66e9e6 Mon Sep 17 00:00:00 2001 From: Max Schmitt Date: Tue, 6 Jul 2021 09:38:50 +0200 Subject: [PATCH] feat(download): introduce Download.cancel (#7462) --- docs/src/api/class-download.md | 5 +++++ src/client/download.ts | 2 +- src/server/firefox/ffBrowser.ts | 3 +-- src/server/webkit/wkBrowser.ts | 3 +-- tests/config/checkCoverage.js | 1 - tests/download.spec.ts | 6 ++---- types/types.d.ts | 6 ++++++ 7 files changed, 16 insertions(+), 10 deletions(-) diff --git a/docs/src/api/class-download.md b/docs/src/api/class-download.md index 7464ebbb88..05f8f654c8 100644 --- a/docs/src/api/class-download.md +++ b/docs/src/api/class-download.md @@ -62,6 +62,11 @@ downloaded content. If [`option: acceptDownloads`] is not set, download events a not performed and user has no access to the downloaded files. ::: +## async method: Download.cancel + +Cancels a download. Will not fail if the download is already finished or canceled. +Upon successful cancellations, `download.failure()` would resolve to `'canceled'`. + ## async method: Download.createReadStream * langs: java, js, csharp - returns: <[null]|[Readable]> diff --git a/src/client/download.ts b/src/client/download.ts index 0f530d5184..a010dd1f6d 100644 --- a/src/client/download.ts +++ b/src/client/download.ts @@ -60,7 +60,7 @@ export class Download implements api.Download { return this._artifact.createReadStream(); } - async _cancel(): Promise { + async cancel(): Promise { return this._artifact.cancel(); } diff --git a/src/server/firefox/ffBrowser.ts b/src/server/firefox/ffBrowser.ts index 6b0fc9d080..5b844f857d 100644 --- a/src/server/firefox/ffBrowser.ts +++ b/src/server/firefox/ffBrowser.ts @@ -332,8 +332,7 @@ export class FFBrowserContext extends BrowserContext { } async _doCancelDownload(uuid: string) { - // TODO: Have this implemented - throw new Error('Download cancellation not yet implemented in Firefox'); + await this._browser._connection.send('Browser.cancelDownload', { uuid }); } } diff --git a/src/server/webkit/wkBrowser.ts b/src/server/webkit/wkBrowser.ts index 4802ce0ea7..ca73aba131 100644 --- a/src/server/webkit/wkBrowser.ts +++ b/src/server/webkit/wkBrowser.ts @@ -327,7 +327,6 @@ export class WKBrowserContext extends BrowserContext { } async _doCancelDownload(uuid: string) { - // TODO: Have this implemented - throw new Error('Download cancellation not yet implemented in WebKit'); + await this._browser._browserSession.send('Playwright.cancelDownload', { uuid }); } } diff --git a/tests/config/checkCoverage.js b/tests/config/checkCoverage.js index fad3c0edb9..d288d4a2b6 100644 --- a/tests/config/checkCoverage.js +++ b/tests/config/checkCoverage.js @@ -45,7 +45,6 @@ if (browserName !== 'chromium') { api.delete('coverage.startCSSCoverage'); api.delete('coverage.stopCSSCoverage'); api.delete('page.pdf'); - api.delete('download._cancel'); } // Some permissions tests are disabled in webkit. See permissions.jest.js diff --git a/tests/download.spec.ts b/tests/download.spec.ts index c803be2a8c..65a0d7cdac 100644 --- a/tests/download.spec.ts +++ b/tests/download.spec.ts @@ -474,14 +474,13 @@ it.describe('download event', () => { it('should be able to cancel pending downloads', async ({browser, server, browserName, browserVersion}) => { // The exact upstream change is in b449b5c, which still does not appear in the first few 91.* tags until 91.0.4437.0. it.fixme(browserName === 'chromium' && Number(browserVersion.split('.')[0]) < 91, 'The upstream Browser.cancelDownload command is not available before Chrome 91'); - it.fixme(browserName !== 'chromium', 'Download cancellation currently implemented for only Chromium'); const page = await browser.newPage({ acceptDownloads: true }); await page.setContent(`download`); const [ download ] = await Promise.all([ page.waitForEvent('download'), page.click('a') ]); - await (download as any)._cancel(); + await download.cancel(); const failure = await download.failure(); expect(failure).toBe('canceled'); await page.close(); @@ -490,7 +489,6 @@ it.describe('download event', () => { it('should not fail explicitly to cancel a download even if that is already finished', async ({browser, server, browserName, browserVersion}) => { // The exact upstream change is in b449b5c, which still does not appear in the first few 91.* tags until 91.0.4437.0. it.fixme(browserName === 'chromium' && Number(browserVersion.split('.')[0]) < 91, 'The upstream Browser.cancelDownload command is not available before Chrome 91'); - it.fixme(browserName !== 'chromium', 'Download cancellation currently implemented for only Chromium'); const page = await browser.newPage({ acceptDownloads: true }); await page.setContent(`download`); const [ download ] = await Promise.all([ @@ -500,7 +498,7 @@ it.describe('download event', () => { const path = await download.path(); expect(fs.existsSync(path)).toBeTruthy(); expect(fs.readFileSync(path).toString()).toBe('Hello world'); - await (download as any)._cancel(); + await download.cancel(); const failure = await download.failure(); expect(failure).toBe(null); await page.close(); diff --git a/types/types.d.ts b/types/types.d.ts index 23939cf045..99505cabec 100644 --- a/types/types.d.ts +++ b/types/types.d.ts @@ -9552,6 +9552,12 @@ export interface Dialog { * performed and user has no access to the downloaded files. */ export interface Download { + /** + * Cancels a download. Will not fail if the download is already finished or canceled. Upon successful cancellations, + * `download.failure()` would resolve to `'canceled'`. + */ + cancel(): Promise; + /** * Returns readable stream for current download or `null` if download failed. */