Skip to content
Permalink
Browse files

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

Fixes #2566
  • Loading branch information...
yotamlaufer authored and aslushnikov committed May 30, 2018
1 parent e1c40bd commit 469b910a2de82e165a6a065741f5c99a84c891db
Showing with 13 additions and 4 deletions.
  1. +2 −1 docs/api.md
  2. +3 −3 lib/Page.js
  3. +8 −0 test/page.spec.js
@@ -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.
@@ -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;
@@ -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() {

0 comments on commit 469b910

Please sign in to comment.
You can’t perform that action at this time.