Implement fullPage screenshots

This patch adds a 'fullPage' option to the Page.screenshot
method.

Fixes #6.
This commit is contained in:
Andrey Lushnikov 2017-06-16 22:34:29 -07:00
parent af52f13e22
commit 868814ac7f
3 changed files with 26 additions and 7 deletions

View File

@ -401,14 +401,11 @@ class Page extends EventEmitter {
screenshotType = 'png'; screenshotType = 'png';
else if (mimeType === 'image/jpeg') else if (mimeType === 'image/jpeg')
screenshotType = 'jpeg'; screenshotType = 'jpeg';
else console.assert(screenshotType, 'Unsupported screenshot mime type: ' + mimeType);
throw new Error('Unsupported screenshot mime type: ' + mimeType);
} }
if (options.type) { if (options.type) {
if (screenshotType && options.type !== screenshotType) console.assert(!screenshotType || options.type === screenshotType, `Passed screenshot type '${options.type}' does not match the type inferred from the file path: '${screenshotType}'`);
throw new Error(`Passed screenshot type '${options.type}' does not match to the type inferred from the file path: '${screenshotType}'`); console.assert(options.type === 'png' || options.type === 'jpeg', 'Unknown options.type value: ' + options.type);
if (options.type !== 'png' && options.type !== 'jpeg')
throw new Error('Unknown screenshot type: ' + options.type);
screenshotType = options.type; screenshotType = options.type;
} }
if (!screenshotType) 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(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.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) { 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.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)); 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, 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', { var result = await this._client.send('Page.captureScreenshot', {
fromSurface: true, fromSurface: true,
format: screenshotType, format: screenshotType,
quality: options.quality quality: options.quality
}); });
if (options.clip) { if (options.clip || options.fullPage) {
await Promise.all([ await Promise.all([
this.setViewportSize(this.viewportSize()), this.setViewportSize(this.viewportSize()),
this._client.send('Emulation.resetViewport') this._client.send('Emulation.resetViewport')

Binary file not shown.

After

Width:  |  Height:  |  Size: 73 KiB

View File

@ -258,6 +258,14 @@ describe('Puppeteer', function() {
var screenshot = await promises[1]; var screenshot = await promises[1];
expect(screenshot).toBeGolden('screenshot-parallel-calls.png'); 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');
}));
}); });
}); });