feat: use captureBeyondViewport in Page.captureScreenshot (#6805)

BREAKING CHANGE:
- `page.screenshot` makes a screenshot with the clip dimensions, not cutting it by the ViewPort size.
This commit is contained in:
Maksim Sadym 2021-02-03 14:30:46 +01:00 committed by GitHub
parent 890d5c2e57
commit 401d84e4a3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 4 additions and 35 deletions

View File

@ -1680,20 +1680,8 @@ export class Page extends EventEmitter {
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.
clip = { x: 0, y: 0, width, height, scale: 1 }; clip = { x: 0, y: 0, width, height, scale: 1 };
const { isMobile = false, deviceScaleFactor = 1, isLandscape = false } =
this._viewport || {};
const screenOrientation: Protocol.Emulation.ScreenOrientation = isLandscape
? { angle: 90, type: 'landscapePrimary' }
: { angle: 0, type: 'portraitPrimary' };
await this._client.send('Emulation.setDeviceMetricsOverride', {
mobile: isMobile,
width,
height,
deviceScaleFactor,
screenOrientation,
});
} }
const shouldSetDefaultBackground = const shouldSetDefaultBackground =
options.omitBackground && format === 'png'; options.omitBackground && format === 'png';
@ -1705,6 +1693,7 @@ export class Page extends EventEmitter {
format, format,
quality: options.quality, quality: options.quality,
clip, clip,
captureBeyondViewport: true,
}); });
if (shouldSetDefaultBackground) if (shouldSetDefaultBackground)
await this._client.send('Emulation.setDefaultBackgroundColorOverride'); await this._client.send('Emulation.setDefaultBackgroundColorOverride');

Binary file not shown.

Before

Width:  |  Height:  |  Size: 194 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 161 B

After

Width:  |  Height:  |  Size: 266 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 279 B

View File

@ -50,26 +50,8 @@ describe('Screenshots', function () {
}); });
expect(screenshot).toBeGolden('screenshot-clip-rect.png'); expect(screenshot).toBeGolden('screenshot-clip-rect.png');
}); });
// TODO: enable after the screenshot is fixed.
// https://crbug.com/1173457
it.skip('should clip elements to the viewport size without artefacts', async () => {
const { page, server } = getTestState();
await page.setViewport({ width: 50, height: 50 });
await page.goto(server.PREFIX + '/grid.html');
const screenshot = await page.screenshot({
clip: {
x: 25,
y: 25,
width: 100,
height: 100,
},
});
expect(screenshot).toBeGolden('screenshot-offscreen-clip.png');
});
// TODO: remove after the screenshot is fixed.
// https://crbug.com/1173457
itFailsFirefox( itFailsFirefox(
'should clip elements to the viewport size with artefacts', 'should get screenshot bigger than the viewport',
async () => { async () => {
const { page, server } = getTestState(); const { page, server } = getTestState();
await page.setViewport({ width: 50, height: 50 }); await page.setViewport({ width: 50, height: 50 });
@ -82,9 +64,7 @@ describe('Screenshots', function () {
height: 100, height: 100,
}, },
}); });
expect(screenshot).toBeGolden( expect(screenshot).toBeGolden('screenshot-offscreen-clip.png');
'screenshot-offscreen-clip-artefacts.png'
);
} }
); );
it('should run in parallel', async () => { it('should run in parallel', async () => {