feat(download): add Page in Download (#6501)

This commit is contained in:
Sébastien Règne 2021-05-13 23:18:21 +02:00 committed by GitHub
parent 3bded35834
commit e87fbfcc1d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 20 additions and 2 deletions

View file

@ -75,6 +75,11 @@ Deletes the downloaded file. Will wait for the download to finish if necessary.
Returns download error if any. Will wait for the download to finish if necessary.
## method: Download.page
- returns: <[Page]>
Get the page that the download belongs to.
## async method: Download.path
- returns: <[null]|[path]>

View file

@ -17,18 +17,25 @@
import { Readable } from 'stream';
import * as api from '../../types/types';
import { Artifact } from './artifact';
import { Page } from './page';
export class Download implements api.Download {
private _page: Page;
private _url: string;
private _suggestedFilename: string;
private _artifact: Artifact;
constructor(url: string, suggestedFilename: string, artifact: Artifact) {
constructor(page: Page, url: string, suggestedFilename: string, artifact: Artifact) {
this._page = page;
this._url = url;
this._suggestedFilename = suggestedFilename;
this._artifact = artifact;
}
page(): Page {
return this._page;
}
url(): string {
return this._url;
}

View file

@ -125,7 +125,7 @@ export class Page extends ChannelOwner<channels.PageChannel, channels.PageInitia
this._channel.on('download', ({ url, suggestedFilename, artifact }) => {
const artifactObject = Artifact.from(artifact);
artifactObject._isRemote = !!this._browserContext._browser && !!this._browserContext._browser._remoteType;
this.emit(Events.Page.Download, new Download(url, suggestedFilename, artifactObject));
this.emit(Events.Page.Download, new Download(this, url, suggestedFilename, artifactObject));
});
this._channel.on('fileChooser', ({ element, isMultiple }) => this.emit(Events.Page.FileChooser, new FileChooser(this, ElementHandle.from(element), isMultiple)));
this._channel.on('frameAttached', ({ frame }) => this._onFrameAttached(Frame.from(frame)));

View file

@ -43,6 +43,7 @@ it.describe('download event', () => {
page.click('a')
]);
let error;
expect(download.page()).toBe(page);
expect(download.url()).toBe(`${server.PREFIX}/downloadWithFilename`);
expect(download.suggestedFilename()).toBe(`file.txt`);
await download.path().catch(e => error = e);

5
types/types.d.ts vendored
View file

@ -9532,6 +9532,11 @@ export interface Download {
*/
failure(): Promise<null|string>;
/**
* Get the page that the download belongs to.
*/
page(): Page;
/**
* Returns path to the downloaded file in case of successful download. The method will wait for the download to finish if
* necessary. The method throws when connected remotely.