feat(page): allow screenshot to return a base64 string (#2586)

Fixes #2566
This commit is contained in:
Yotam Laufer 2018-05-30 22:27:08 +01:00 committed by Andrey Lushnikov
parent e1c40bd776
commit 469b910a2d
3 changed files with 13 additions and 4 deletions

View File

@ -1327,7 +1327,8 @@ Shortcut for [page.mainFrame().executionContext().queryObjects(prototypeHandle)]
- `width` <[number]> width of clipping area
- `height` <[number]> height of clipping area
- `omitBackground` <[boolean]> Hides default white background and allows capturing screenshots with transparency. Defaults to `false`.
- returns: <[Promise]<[Buffer]>> Promise which resolves to buffer with captured screenshot
- `encoding` <[string]> The encoding of the image, can be either `base64` or `binary`. Defaults to `binary`.
- returns: <[Promise]<[Buffer|String]>> Promise which resolves to buffer or a base64 string (depending on the value of `encoding`) with captured screenshot.
> **NOTE** Screenshots take at least 1/6 second on OS X. See https://crbug.com/741689 for discussion.

View File

@ -729,7 +729,7 @@ class Page extends EventEmitter {
/**
* @param {!Object=} options
* @return {!Promise<!Buffer>}
* @return {!Promise<!Buffer|!String>}
*/
async screenshot(options = {}) {
let screenshotType = null;
@ -769,7 +769,7 @@ class Page extends EventEmitter {
/**
* @param {"png"|"jpeg"} format
* @param {!Object=} options
* @return {!Promise<!Buffer>}
* @return {!Promise<!Buffer|!String>}
*/
async _screenshotTask(format, options) {
await this._client.send('Target.activateTarget', {targetId: this._target._targetId});
@ -801,7 +801,7 @@ class Page extends EventEmitter {
if (options.fullPage)
await this.setViewport(this._viewport);
const buffer = Buffer.from(result.data, 'base64');
const buffer = options.encoding === 'base64' ? result.data : Buffer.from(result.data, 'base64');
if (options.path)
await writeFileAsync(options.path, buffer);
return buffer;

View File

@ -1510,6 +1510,14 @@ module.exports.addTests = function({testRunner, expect, puppeteer, DeviceDescrip
});
expect(screenshot).toBeGolden('screenshot-clip-odd-size.png');
});
it('should return base64', async({page, server}) => {
await page.setViewport({width: 500, height: 500});
await page.goto(server.PREFIX + '/grid.html');
const screenshot = await page.screenshot({
encoding: 'base64'
});
expect(Buffer.from(screenshot, 'base64')).toBeGolden('screenshot-sanity.png');
});
});
describe('Page.select', function() {