From 127f3966d72cbab1b3bf36db065363becd37655f Mon Sep 17 00:00:00 2001 From: Pavel Feldman Date: Thu, 17 Aug 2017 21:11:39 -0700 Subject: [PATCH] Allow capturing transparent screenshots (#354) This patch adds `omitBackground` option to the `page.screenshot` method. Fixes #344 --- docs/api.md | 1 + lib/Page.js | 4 ++++ test/golden/transparent.png | Bin 0 -> 119 bytes test/test.js | 6 ++++++ 4 files changed, 11 insertions(+) create mode 100644 test/golden/transparent.png diff --git a/docs/api.md b/docs/api.md index ccb2e82d3c8..917767bd504 100644 --- a/docs/api.md +++ b/docs/api.md @@ -604,6 +604,7 @@ Shortcut for [`keyboard.down`](#keyboarddownkey-options) and [`keyboard.up`](#ke - `y` <[number]> y-coordinate of top-left corner of clip area - `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 #### page.setContent(html) diff --git a/lib/Page.js b/lib/Page.js index d9e6230b22e..ae33ac8324e 100644 --- a/lib/Page.js +++ b/lib/Page.js @@ -467,7 +467,11 @@ class Page extends EventEmitter { await this._client.send('Emulation.setDeviceMetricsOverride', { mobile, width, height, deviceScaleFactor, screenOrientation }); } + if (options.omitBackground) + await this._client.send('Emulation.setDefaultBackgroundColorOverride', { color: { r: 0, g: 0, b: 0, a: 0 } }); let result = await this._client.send('Page.captureScreenshot', { format, quality: options.quality, clip }); + if (options.omitBackground) + await this._client.send('Emulation.setDefaultBackgroundColorOverride'); if (options.fullPage) await this.setViewport(this._viewport); diff --git a/test/golden/transparent.png b/test/golden/transparent.png new file mode 100644 index 0000000000000000000000000000000000000000..1cf45d8688fc68c85c4d250d14eaf6fdf153ec82 GIT binary patch literal 119 zcmeAS@N?(olHy`uVBq!ia0vp^DImYvN4 fzW~T20yHdVoU??1aarlqK9FipS3j3^P6 page.close())); })); + it('should allow transparency', SX(async function() { + await page.setViewport({ width: 100, height: 100 }); + await page.goto(EMPTY_PAGE); + let screenshot = await page.screenshot({omitBackground:true}); + expect(screenshot).toBeGolden('transparent.png'); + })); }); describe('Tracing', function() {