mirror of
https://github.com/puppeteer/puppeteer
synced 2024-06-14 14:02:48 +00:00
fix: set JPG background to white when omitBackground option is used (#3240)
Fixes #3234
This commit is contained in:
parent
6ec3ce6920
commit
d929f7e213
@ -872,11 +872,11 @@ class Page extends EventEmitter {
|
|||||||
const screenOrientation = landscape ? { angle: 90, type: 'landscapePrimary' } : { angle: 0, type: 'portraitPrimary' };
|
const screenOrientation = landscape ? { 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, width, height, deviceScaleFactor, screenOrientation });
|
||||||
}
|
}
|
||||||
|
const shouldSetDefaultBackground = options.omitBackground && format === 'png';
|
||||||
if (options.omitBackground)
|
if (shouldSetDefaultBackground)
|
||||||
await this._client.send('Emulation.setDefaultBackgroundColorOverride', { color: { r: 0, g: 0, b: 0, a: 0 } });
|
await this._client.send('Emulation.setDefaultBackgroundColorOverride', { color: { r: 0, g: 0, b: 0, a: 0 } });
|
||||||
const result = await this._client.send('Page.captureScreenshot', { format, quality: options.quality, clip });
|
const result = await this._client.send('Page.captureScreenshot', { format, quality: options.quality, clip });
|
||||||
if (options.omitBackground)
|
if (shouldSetDefaultBackground)
|
||||||
await this._client.send('Emulation.setDefaultBackgroundColorOverride');
|
await this._client.send('Emulation.setDefaultBackgroundColorOverride');
|
||||||
|
|
||||||
if (options.fullPage)
|
if (options.fullPage)
|
||||||
|
@ -51,6 +51,7 @@
|
|||||||
"cross-env": "^5.0.5",
|
"cross-env": "^5.0.5",
|
||||||
"eslint": "^4.19.1",
|
"eslint": "^4.19.1",
|
||||||
"esprima": "^4.0.0",
|
"esprima": "^4.0.0",
|
||||||
|
"jpeg-js": "^0.3.4",
|
||||||
"minimist": "^1.2.0",
|
"minimist": "^1.2.0",
|
||||||
"ncp": "^2.0.0",
|
"ncp": "^2.0.0",
|
||||||
"pixelmatch": "^4.0.2",
|
"pixelmatch": "^4.0.2",
|
||||||
|
@ -18,26 +18,30 @@ const fs = require('fs');
|
|||||||
const Diff = require('text-diff');
|
const Diff = require('text-diff');
|
||||||
const mime = require('mime');
|
const mime = require('mime');
|
||||||
const PNG = require('pngjs').PNG;
|
const PNG = require('pngjs').PNG;
|
||||||
|
const jpeg = require('jpeg-js');
|
||||||
const pixelmatch = require('pixelmatch');
|
const pixelmatch = require('pixelmatch');
|
||||||
|
|
||||||
module.exports = {compare};
|
module.exports = {compare};
|
||||||
|
|
||||||
const GoldenComparators = {
|
const GoldenComparators = {
|
||||||
'image/png': compareImages,
|
'image/png': compareImages,
|
||||||
|
'image/jpeg': compareImages,
|
||||||
'text/plain': compareText
|
'text/plain': compareText
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {?Object} actualBuffer
|
* @param {?Object} actualBuffer
|
||||||
* @param {!Buffer} expectedBuffer
|
* @param {!Buffer} expectedBuffer
|
||||||
|
* @param {!string} mimeType
|
||||||
* @return {?{diff: (!Object:undefined), errorMessage: (string|undefined)}}
|
* @return {?{diff: (!Object:undefined), errorMessage: (string|undefined)}}
|
||||||
*/
|
*/
|
||||||
function compareImages(actualBuffer, expectedBuffer) {
|
function compareImages(actualBuffer, expectedBuffer, mimeType) {
|
||||||
if (!actualBuffer || !(actualBuffer instanceof Buffer))
|
if (!actualBuffer || !(actualBuffer instanceof Buffer))
|
||||||
return { errorMessage: 'Actual result should be Buffer.' };
|
return { errorMessage: 'Actual result should be Buffer.' };
|
||||||
|
|
||||||
const actual = PNG.sync.read(actualBuffer);
|
const actual = mimeType === 'image/png' ? PNG.sync.read(actualBuffer) : jpeg.decode(actualBuffer);
|
||||||
const expected = PNG.sync.read(expectedBuffer);
|
const expected = mimeType === 'image/png' ? PNG.sync.read(expectedBuffer) : jpeg.decode(expectedBuffer);
|
||||||
if (expected.width !== actual.width || expected.height !== actual.height) {
|
if (expected.width !== actual.width || expected.height !== actual.height) {
|
||||||
return {
|
return {
|
||||||
errorMessage: `Sizes differ: expected image ${expected.width}px X ${expected.height}px, but got ${actual.width}px X ${actual.height}px. `
|
errorMessage: `Sizes differ: expected image ${expected.width}px X ${expected.height}px, but got ${actual.width}px X ${actual.height}px. `
|
||||||
@ -93,14 +97,15 @@ function compare(goldenPath, outputPath, actual, goldenName) {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
const expected = fs.readFileSync(expectedPath);
|
const expected = fs.readFileSync(expectedPath);
|
||||||
const comparator = GoldenComparators[mime.getType(goldenName)];
|
const mimeType = mime.getType(goldenName);
|
||||||
|
const comparator = GoldenComparators[mimeType];
|
||||||
if (!comparator) {
|
if (!comparator) {
|
||||||
return {
|
return {
|
||||||
pass: false,
|
pass: false,
|
||||||
message: 'Failed to find comparator with type ' + mime.getType(goldenName) + ': ' + goldenName
|
message: 'Failed to find comparator with type ' + mimeType + ': ' + goldenName
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
const result = comparator(actual, expected);
|
const result = comparator(actual, expected, mimeType);
|
||||||
if (!result)
|
if (!result)
|
||||||
return { pass: true };
|
return { pass: true };
|
||||||
ensureOutputDir();
|
ensureOutputDir();
|
||||||
|
BIN
test/golden/white.jpg
Normal file
BIN
test/golden/white.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 357 B |
@ -1656,6 +1656,12 @@ module.exports.addTests = function({testRunner, expect, headless}) {
|
|||||||
const screenshot = await page.screenshot({omitBackground: true});
|
const screenshot = await page.screenshot({omitBackground: true});
|
||||||
expect(screenshot).toBeGolden('transparent.png');
|
expect(screenshot).toBeGolden('transparent.png');
|
||||||
});
|
});
|
||||||
|
it('should render white background on jpeg file', async({page, server}) => {
|
||||||
|
await page.setViewport({ width: 100, height: 100 });
|
||||||
|
await page.goto(server.EMPTY_PAGE);
|
||||||
|
const screenshot = await page.screenshot({omitBackground: true, type: 'jpeg'});
|
||||||
|
expect(screenshot).toBeGolden('white.jpg');
|
||||||
|
});
|
||||||
it('should work with odd clip size on Retina displays', async({page, server}) => {
|
it('should work with odd clip size on Retina displays', async({page, server}) => {
|
||||||
const screenshot = await page.screenshot({
|
const screenshot = await page.screenshot({
|
||||||
clip: {
|
clip: {
|
||||||
|
Loading…
Reference in New Issue
Block a user