feat(download): introduce Download.cancel (#7462)
This commit is contained in:
parent
2231992d74
commit
b846ddda04
|
|
@ -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]>
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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 });
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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 });
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
6
types/types.d.ts
vendored
|
|
@ -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.
|
||||
*/
|
||||
|
|
|
|||
Loading…
Reference in a new issue