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.
:::
## 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]>

View file

@ -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();
}

View file

@ -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 });
}
}

View file

@ -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 });
}
}

View file

@ -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

View file

@ -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();

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.
*/
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.
*/