Functional tests comparePngs write resized images to the session directory (#15976)

This commit is contained in:
Brandon Kobel 2018-01-11 07:59:41 -05:00 committed by GitHub
parent 07a67916c4
commit 738c5e7f85
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 17 deletions

View file

@ -1,22 +1,23 @@
import path from 'path';
import Jimp from 'jimp';
export async function comparePngs(actualPath, expectedPath, diffPath, log) {
log.debug(`comparePngs: ${actualPath} vs ${expectedPath}`);
const actual = (await Jimp.read(actualPath)).clone();
const expected = (await Jimp.read(expectedPath)).clone();
export async function comparePngs(sessionPath, baselinePath, diffPath, sessionDirectory, log) {
log.debug(`comparePngs: ${sessionPath} vs ${baselinePath}`);
const session = (await Jimp.read(sessionPath)).clone();
const baseline = (await Jimp.read(baselinePath)).clone();
if (actual.bitmap.width !== expected.bitmap.width || actual.bitmap.height !== expected.bitmap.height) {
console.log('expected height ' + expected.bitmap.height + ' and width ' + expected.bitmap.width);
console.log('actual height ' + actual.bitmap.height + ' and width ' + actual.bitmap.width);
if (session.bitmap.width !== baseline.bitmap.width || session.bitmap.height !== baseline.bitmap.height) {
console.log('expected height ' + baseline.bitmap.height + ' and width ' + baseline.bitmap.width);
console.log('actual height ' + session.bitmap.height + ' and width ' + session.bitmap.width);
const width = Math.min(actual.bitmap.width, expected.bitmap.width);
const height = Math.min(actual.bitmap.height, expected.bitmap.height);
actual.resize(width, height);//, Jimp.HORIZONTAL_ALIGN_LEFT | Jimp.VERTICAL_ALIGN_TOP);
expected.resize(width, height);//, Jimp.HORIZONTAL_ALIGN_LEFT | Jimp.VERTICAL_ALIGN_TOP);
const width = Math.min(session.bitmap.width, baseline.bitmap.width);
const height = Math.min(session.bitmap.height, baseline.bitmap.height);
session.resize(width, height);//, Jimp.HORIZONTAL_ALIGN_LEFT | Jimp.VERTICAL_ALIGN_TOP);
baseline.resize(width, height);//, Jimp.HORIZONTAL_ALIGN_LEFT | Jimp.VERTICAL_ALIGN_TOP);
}
actual.quality(60);
expected.quality(60);
session.quality(60);
baseline.quality(60);
log.debug(`calculating diff pixels...`);
// Note that this threshold value only affects color comparison from pixel to pixel. It won't have
@ -24,14 +25,14 @@ export async function comparePngs(actualPath, expectedPath, diffPath, log) {
// will still show up as diffs, but upping this will not help that. Instead we keep the threshold low, and expect
// some the diffCount to be lower than our own threshold value.
const THRESHOLD = .1;
const { image, percent } = Jimp.diff(actual, expected, THRESHOLD);
const { image, percent } = Jimp.diff(session, baseline, THRESHOLD);
log.debug(`percentSimilar: ${percent}`);
if (percent > 0) {
image.write(diffPath);
// For debugging purposes it'll help to see the resized images and how they compare.
actual.write(actualPath.substring(0, actualPath.length - 4) + '-resized.png');
expected.write(expectedPath.substring(0, expectedPath.length - 4) + '-resized.png');
session.write(path.join(sessionDirectory, `${path.parse(sessionPath).name}-session-resized.png`));
baseline.write(path.join(sessionDirectory, `${path.parse(baselinePath).name}-baseline-resized.png`));
}
return percent;
}

View file

@ -41,7 +41,7 @@ export async function ScreenshotsProvider({ getService }) {
return 0;
} else {
await mkdirAsync(FAILURE_DIRECTORY);
return await comparePngs(sessionPath, baselinePath, failurePath, log);
return await comparePngs(sessionPath, baselinePath, failurePath, SESSION_DIRECTORY, log);
}
}