Allow capturing transparent screenshots (#354)

This patch adds `omitBackground` option to the `page.screenshot` method.

Fixes #344
This commit is contained in:
Pavel Feldman 2017-08-17 21:11:39 -07:00 committed by Andrey Lushnikov
parent 1ca7849017
commit 127f3966d7
4 changed files with 11 additions and 0 deletions

View File

@ -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)

View File

@ -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);

BIN
test/golden/transparent.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 119 B

View File

@ -1810,6 +1810,12 @@ describe('Page', function() {
expect(screenshots[i]).toBeGolden(`grid-cell-${i}.png`);
await Promise.all(pages.map(page => 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() {