Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.
TypeScript automationchromechromiume2e-testingelectronend-to-end-testingfirefoxjavascriptplaywrighttesttest-automationtestingtesting-toolswebwebkit
feat(download): introduce Download.cancel (#7462)
Max Schmittb846ddda0467
diff --git a/docs/src/api/class-download.md b/docs/src/api/class-download.mdindex 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 anot 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.tsindex 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<void> {+ async cancel(): Promise<void> {return this._artifact.cancel();}diff --git a/src/server/firefox/ffBrowser.ts b/src/server/firefox/ffBrowser.tsindex 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.tsindex 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.jsindex 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.jsdiff --git a/tests/download.spec.ts b/tests/download.spec.tsindex 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(`<a href="${server.PREFIX}/downloadWithDelay">download</a>`);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(`<a href="${server.PREFIX}/download">download</a>`);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.tsindex 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<void>;+/*** Returns readable stream for current download or `null` if download failed.*/