feat(download): introduce Download.cancel (#7462)

This commit is contained in:
Max Schmitt 2021-07-06 09:38:50 +02:00 committed by GitHub
parent 2231992d74
commit b846ddda04
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 16 additions and 10 deletions

View file

@ -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. 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 ## async method: Download.createReadStream
* langs: java, js, csharp * langs: java, js, csharp
- returns: <[null]|[Readable]> - returns: <[null]|[Readable]>

View file

@ -60,7 +60,7 @@ export class Download implements api.Download {
return this._artifact.createReadStream(); return this._artifact.createReadStream();
} }
async _cancel(): Promise<void> { async cancel(): Promise<void> {
return this._artifact.cancel(); return this._artifact.cancel();
} }

View file

@ -332,8 +332,7 @@ export class FFBrowserContext extends BrowserContext {
} }
async _doCancelDownload(uuid: string) { async _doCancelDownload(uuid: string) {
// TODO: Have this implemented await this._browser._connection.send('Browser.cancelDownload', { uuid });
throw new Error('Download cancellation not yet implemented in Firefox');
} }
} }

View file

@ -327,7 +327,6 @@ export class WKBrowserContext extends BrowserContext {
} }
async _doCancelDownload(uuid: string) { async _doCancelDownload(uuid: string) {
// TODO: Have this implemented await this._browser._browserSession.send('Playwright.cancelDownload', { uuid });
throw new Error('Download cancellation not yet implemented in WebKit');
} }
} }

View file

@ -45,7 +45,6 @@ if (browserName !== 'chromium') {
api.delete('coverage.startCSSCoverage'); api.delete('coverage.startCSSCoverage');
api.delete('coverage.stopCSSCoverage'); api.delete('coverage.stopCSSCoverage');
api.delete('page.pdf'); api.delete('page.pdf');
api.delete('download._cancel');
} }
// Some permissions tests are disabled in webkit. See permissions.jest.js // Some permissions tests are disabled in webkit. See permissions.jest.js

View file

@ -474,14 +474,13 @@ it.describe('download event', () => {
it('should be able to cancel pending downloads', async ({browser, server, browserName, browserVersion}) => { 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. // 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' && 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 }); const page = await browser.newPage({ acceptDownloads: true });
await page.setContent(`<a href="${server.PREFIX}/downloadWithDelay">download</a>`); await page.setContent(`<a href="${server.PREFIX}/downloadWithDelay">download</a>`);
const [ download ] = await Promise.all([ const [ download ] = await Promise.all([
page.waitForEvent('download'), page.waitForEvent('download'),
page.click('a') page.click('a')
]); ]);
await (download as any)._cancel(); await download.cancel();
const failure = await download.failure(); const failure = await download.failure();
expect(failure).toBe('canceled'); expect(failure).toBe('canceled');
await page.close(); 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}) => { 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. // 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' && 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 }); const page = await browser.newPage({ acceptDownloads: true });
await page.setContent(`<a href="${server.PREFIX}/download">download</a>`); await page.setContent(`<a href="${server.PREFIX}/download">download</a>`);
const [ download ] = await Promise.all([ const [ download ] = await Promise.all([
@ -500,7 +498,7 @@ it.describe('download event', () => {
const path = await download.path(); const path = await download.path();
expect(fs.existsSync(path)).toBeTruthy(); expect(fs.existsSync(path)).toBeTruthy();
expect(fs.readFileSync(path).toString()).toBe('Hello world'); expect(fs.readFileSync(path).toString()).toBe('Hello world');
await (download as any)._cancel(); await download.cancel();
const failure = await download.failure(); const failure = await download.failure();
expect(failure).toBe(null); expect(failure).toBe(null);
await page.close(); await page.close();

6
types/types.d.ts vendored
View file

@ -9552,6 +9552,12 @@ export interface Dialog {
* performed and user has no access to the downloaded files. * performed and user has no access to the downloaded files.
*/ */
export interface Download { 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. * Returns readable stream for current download or `null` if download failed.
*/ */