diff --git a/lib/Page.js b/lib/Page.js index 77b5fa0afdb..3f5220d1bfa 100644 --- a/lib/Page.js +++ b/lib/Page.js @@ -401,14 +401,11 @@ class Page extends EventEmitter { screenshotType = 'png'; else if (mimeType === 'image/jpeg') screenshotType = 'jpeg'; - else - throw new Error('Unsupported screenshot mime type: ' + mimeType); + console.assert(screenshotType, 'Unsupported screenshot mime type: ' + mimeType); } if (options.type) { - if (screenshotType && options.type !== screenshotType) - throw new Error(`Passed screenshot type '${options.type}' does not match to the type inferred from the file path: '${screenshotType}'`); - if (options.type !== 'png' && options.type !== 'jpeg') - throw new Error('Unknown screenshot type: ' + options.type); + console.assert(!screenshotType || options.type === screenshotType, `Passed screenshot type '${options.type}' does not match the type inferred from the file path: '${screenshotType}'`); + console.assert(options.type === 'png' || options.type === 'jpeg', 'Unknown options.type value: ' + options.type); screenshotType = options.type; } if (!screenshotType) @@ -420,6 +417,7 @@ class Page extends EventEmitter { console.assert(Number.isInteger(options.quality), 'Expected options.quality to be an integer'); console.assert(options.quality >= 0 && options.quality <= 100, 'Expected options.quality to be between 0 and 100 (inclusive), got ' + options.quality); } + console.assert(!options.clip || !options.fullPage, 'options.clip and options.fullPage are exclusive'); if (options.clip) { console.assert(typeof options.clip.x === 'number', 'Expected options.clip.x to be a number but found ' + (typeof options.clip.x)); console.assert(typeof options.clip.y === 'number', 'Expected options.clip.y to be a number but found ' + (typeof options.clip.y)); @@ -448,13 +446,26 @@ class Page extends EventEmitter { scale: 1, }) ]); + } else if (options.fullPage) { + var response = await this._client.send('Page.getLayoutMetrics'); + await Promise.all([ + this._client.send('Emulation.setVisibleSize', { + width: Math.ceil(response.contentSize.width / this._screenDPI), + height: Math.ceil(response.contentSize.height / this._screenDPI), + }), + this._client.send('Emulation.forceViewport', { + x: 0, + y: 0, + scale: 1, + }) + ]); } var result = await this._client.send('Page.captureScreenshot', { fromSurface: true, format: screenshotType, quality: options.quality }); - if (options.clip) { + if (options.clip || options.fullPage) { await Promise.all([ this.setViewportSize(this.viewportSize()), this._client.send('Emulation.resetViewport') diff --git a/test/golden/screenshot-grid-fullpage.png b/test/golden/screenshot-grid-fullpage.png new file mode 100644 index 00000000000..d6d38217f7f Binary files /dev/null and b/test/golden/screenshot-grid-fullpage.png differ diff --git a/test/test.js b/test/test.js index 7dc45ea701b..c613bac3869 100644 --- a/test/test.js +++ b/test/test.js @@ -258,6 +258,14 @@ describe('Puppeteer', function() { var screenshot = await promises[1]; expect(screenshot).toBeGolden('screenshot-parallel-calls.png'); })); + it('should take fullPage screenshots', SX(async function() { + await page.setViewportSize({width: 500, height: 500}); + await page.navigate(STATIC_PREFIX + '/grid.html'); + var screenshot = await page.screenshot({ + fullPage: true + }); + expect(screenshot).toBeGolden('screenshot-grid-fullpage.png'); + })); }); });