fix(page): full page screenshot when defaultViewport is null (#3306)

Fixes #3104
This commit is contained in:
Łukasz Usarz 2018-09-27 19:50:21 +02:00 committed by Andrey Lushnikov
parent e75e36b9c2
commit 842fee860b
2 changed files with 21 additions and 7 deletions

View File

@ -796,19 +796,20 @@ class Page extends EventEmitter {
clip.scale = 1; clip.scale = 1;
if (options.fullPage) { if (options.fullPage) {
assert(this._viewport, 'fullPage screenshots do not work without first setting viewport.');
const metrics = await this._client.send('Page.getLayoutMetrics'); const metrics = await this._client.send('Page.getLayoutMetrics');
const width = Math.ceil(metrics.contentSize.width); const width = Math.ceil(metrics.contentSize.width);
const height = Math.ceil(metrics.contentSize.height); const height = Math.ceil(metrics.contentSize.height);
// Overwrite clip for full page at all times. // Overwrite clip for full page at all times.
clip = { x: 0, y: 0, width, height, scale: 1 }; clip = { x: 0, y: 0, width, height, scale: 1 };
const mobile = this._viewport.isMobile || false; const {
const deviceScaleFactor = this._viewport.deviceScaleFactor || 1; isMobile = false,
const landscape = this._viewport.isLandscape || false; deviceScaleFactor = 1,
isLandscape = false
} = this._viewport || {};
/** @type {!Protocol.Emulation.ScreenOrientation} */ /** @type {!Protocol.Emulation.ScreenOrientation} */
const screenOrientation = landscape ? { angle: 90, type: 'landscapePrimary' } : { angle: 0, type: 'portraitPrimary' }; const screenOrientation = isLandscape ? { angle: 90, type: 'landscapePrimary' } : { angle: 0, type: 'portraitPrimary' };
await this._client.send('Emulation.setDeviceMetricsOverride', { mobile, width, height, deviceScaleFactor, screenOrientation }); await this._client.send('Emulation.setDeviceMetricsOverride', { mobile: isMobile, width, height, deviceScaleFactor, screenOrientation });
} }
const shouldSetDefaultBackground = options.omitBackground && format === 'png'; const shouldSetDefaultBackground = options.omitBackground && format === 'png';
if (shouldSetDefaultBackground) if (shouldSetDefaultBackground)
@ -817,7 +818,7 @@ class Page extends EventEmitter {
if (shouldSetDefaultBackground) if (shouldSetDefaultBackground)
await this._client.send('Emulation.setDefaultBackgroundColorOverride'); await this._client.send('Emulation.setDefaultBackgroundColorOverride');
if (options.fullPage) if (options.fullPage && this._viewport)
await this.setViewport(this._viewport); await this.setViewport(this._viewport);
const buffer = options.encoding === 'base64' ? result.data : Buffer.from(result.data, 'base64'); const buffer = options.encoding === 'base64' ? result.data : Buffer.from(result.data, 'base64');

View File

@ -279,6 +279,19 @@ module.exports.addTests = function({testRunner, expect, defaultBrowserOptions})
expect(page.viewport()).toBe(null); expect(page.viewport()).toBe(null);
await browser.close(); await browser.close();
}); });
it('should take fullPage screenshots when defaultViewport is null', async({server}) => {
const options = Object.assign({}, defaultBrowserOptions, {
defaultViewport: null
});
const browser = await puppeteer.launch(options);
const page = await browser.newPage();
await page.goto(server.PREFIX + '/grid.html');
const screenshot = await page.screenshot({
fullPage: true
});
expect(screenshot).toBeInstanceOf(Buffer);
await browser.close();
});
}); });
describe('Puppeteer.connect', function() { describe('Puppeteer.connect', function() {
it('should be able to connect multiple times to the same browser', async({server}) => { it('should be able to connect multiple times to the same browser', async({server}) => {