Skip to content

Commit

Permalink
feat(rpc): handle screenshot path on the client (#3352)
Browse files Browse the repository at this point in the history
  • Loading branch information
dgozman authored Aug 7, 2020
1 parent 7e2cc77 commit 69c88d8
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 11 deletions.
2 changes: 0 additions & 2 deletions src/rpc/channels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -911,7 +911,6 @@ export type PageReloadResult = {
export type PageScreenshotParams = {
timeout?: number,
type?: 'png' | 'jpeg',
path?: string,
quality?: number,
omitBackground?: boolean,
fullPage?: boolean,
Expand All @@ -925,7 +924,6 @@ export type PageScreenshotParams = {
export type PageScreenshotOptions = {
timeout?: number,
type?: 'png' | 'jpeg',
path?: string,
quality?: number,
omitBackground?: boolean,
fullPage?: boolean,
Expand Down
14 changes: 7 additions & 7 deletions src/rpc/client/page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -422,9 +422,12 @@ export class Page extends ChannelOwner<PageChannel, PageInitializer> {
});
}

async screenshot(options: PageScreenshotOptions = {}): Promise<Buffer> {
async screenshot(options: PageScreenshotOptions & { path?: string } = {}): Promise<Buffer> {
return this._wrapApiCall('page.screenshot', async () => {
return Buffer.from((await this._channel.screenshot(options)).binary, 'base64');
const buffer = Buffer.from((await this._channel.screenshot(options)).binary, 'base64');
if (options.path)
await fsWriteFileAsync(options.path, buffer);
return buffer;
});
}

Expand Down Expand Up @@ -541,10 +544,7 @@ export class Page extends ChannelOwner<PageChannel, PageInitializer> {
}

async _pdf(options: PDFOptions = {}): Promise<Buffer> {
const path = options.path;
const transportOptions: PagePdfParams = { ...options } as PagePdfParams;
if (path)
delete (transportOptions as any).path;
if (transportOptions.margin)
transportOptions.margin = { ...transportOptions.margin };
if (typeof options.width === 'number')
Expand All @@ -558,8 +558,8 @@ export class Page extends ChannelOwner<PageChannel, PageInitializer> {
}
const result = await this._channel.pdf(transportOptions);
const buffer = Buffer.from(result.pdf, 'base64');
if (path)
await fsWriteFileAsync(path, buffer);
if (options.path)
await fsWriteFileAsync(options.path, buffer);
return buffer;
}
}
Expand Down
1 change: 0 additions & 1 deletion src/rpc/protocol.yml
Original file line number Diff line number Diff line change
Expand Up @@ -701,7 +701,6 @@ Page:
literals:
- png
- jpeg
path: string?
quality: number?
omitBackground: boolean?
fullPage: boolean?
Expand Down
1 change: 0 additions & 1 deletion src/rpc/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,6 @@ export function createScheme(tChannel: (name: string) => Validator): Scheme {
scheme.PageScreenshotParams = tObject({
timeout: tOptional(tNumber),
type: tOptional(tEnum(['png', 'jpeg'])),
path: tOptional(tString),
quality: tOptional(tNumber),
omitBackground: tOptional(tBoolean),
fullPage: tOptional(tBoolean),
Expand Down

0 comments on commit 69c88d8

Please sign in to comment.