mirror of
https://github.com/elastic/kibana.git
synced 2025-04-24 17:59:23 -04:00
Tsfy screenshot service and comparepng internal lib (#37177)
This commit is contained in:
parent
100daf6efe
commit
2c2cc5565b
3 changed files with 47 additions and 19 deletions
|
@ -47,7 +47,6 @@ import { QueryBarProvider } from './query_bar';
|
||||||
import { RemoteProvider } from './remote';
|
import { RemoteProvider } from './remote';
|
||||||
// @ts-ignore not TS yet
|
// @ts-ignore not TS yet
|
||||||
import { RenderableProvider } from './renderable';
|
import { RenderableProvider } from './renderable';
|
||||||
// @ts-ignore not TS yet
|
|
||||||
import { ScreenshotsProvider } from './screenshots';
|
import { ScreenshotsProvider } from './screenshots';
|
||||||
// @ts-ignore not TS yet
|
// @ts-ignore not TS yet
|
||||||
import { SnapshotsProvider } from './snapshots';
|
import { SnapshotsProvider } from './snapshots';
|
||||||
|
|
|
@ -17,22 +17,46 @@
|
||||||
* under the License.
|
* under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import path from 'path';
|
import { parse, join } from 'path';
|
||||||
import Jimp from 'jimp';
|
import Jimp from 'jimp';
|
||||||
|
import { ToolingLog } from '@kbn/dev-utils';
|
||||||
|
|
||||||
export async function comparePngs(sessionPath, baselinePath, diffPath, sessionDirectory, log) {
|
/**
|
||||||
|
* Comparing pngs and writing result to provided directory
|
||||||
|
*
|
||||||
|
* @param sessionPath
|
||||||
|
* @param baselinePath
|
||||||
|
* @param diffPath
|
||||||
|
* @param sessionDirectory
|
||||||
|
* @param log
|
||||||
|
* @returns Percent
|
||||||
|
*/
|
||||||
|
export async function comparePngs(
|
||||||
|
sessionPath: string,
|
||||||
|
baselinePath: string,
|
||||||
|
diffPath: string,
|
||||||
|
sessionDirectory: string,
|
||||||
|
log: ToolingLog
|
||||||
|
) {
|
||||||
log.debug(`comparePngs: ${sessionPath} vs ${baselinePath}`);
|
log.debug(`comparePngs: ${sessionPath} vs ${baselinePath}`);
|
||||||
const session = (await Jimp.read(sessionPath)).clone();
|
const session = (await Jimp.read(sessionPath)).clone();
|
||||||
const baseline = (await Jimp.read(baselinePath)).clone();
|
const baseline = (await Jimp.read(baselinePath)).clone();
|
||||||
|
|
||||||
if (session.bitmap.width !== baseline.bitmap.width || session.bitmap.height !== baseline.bitmap.height) {
|
if (
|
||||||
console.log('expected height ' + baseline.bitmap.height + ' and width ' + baseline.bitmap.width);
|
session.bitmap.width !== baseline.bitmap.width ||
|
||||||
|
session.bitmap.height !== baseline.bitmap.height
|
||||||
|
) {
|
||||||
|
// eslint-disable-next-line no-console
|
||||||
|
console.log(
|
||||||
|
'expected height ' + baseline.bitmap.height + ' and width ' + baseline.bitmap.width
|
||||||
|
);
|
||||||
|
// eslint-disable-next-line no-console
|
||||||
console.log('actual height ' + session.bitmap.height + ' and width ' + session.bitmap.width);
|
console.log('actual height ' + session.bitmap.height + ' and width ' + session.bitmap.width);
|
||||||
|
|
||||||
const width = Math.min(session.bitmap.width, baseline.bitmap.width);
|
const width = Math.min(session.bitmap.width, baseline.bitmap.width);
|
||||||
const height = Math.min(session.bitmap.height, baseline.bitmap.height);
|
const height = Math.min(session.bitmap.height, baseline.bitmap.height);
|
||||||
session.resize(width, height);//, Jimp.HORIZONTAL_ALIGN_LEFT | Jimp.VERTICAL_ALIGN_TOP);
|
session.resize(width, height); // , Jimp.HORIZONTAL_ALIGN_LEFT | Jimp.VERTICAL_ALIGN_TOP);
|
||||||
baseline.resize(width, height);//, Jimp.HORIZONTAL_ALIGN_LEFT | Jimp.VERTICAL_ALIGN_TOP);
|
baseline.resize(width, height); // , Jimp.HORIZONTAL_ALIGN_LEFT | Jimp.VERTICAL_ALIGN_TOP);
|
||||||
}
|
}
|
||||||
|
|
||||||
session.quality(60);
|
session.quality(60);
|
||||||
|
@ -43,15 +67,15 @@ export async function comparePngs(sessionPath, baselinePath, diffPath, sessionDi
|
||||||
// any affect when comparing neighboring pixels - so slight shifts, font variations, or "blurry-ness"
|
// any affect when comparing neighboring pixels - so slight shifts, font variations, or "blurry-ness"
|
||||||
// will still show up as diffs, but upping this will not help that. Instead we keep the threshold low, and expect
|
// 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.
|
// some the diffCount to be lower than our own threshold value.
|
||||||
const THRESHOLD = .1;
|
const THRESHOLD = 0.1;
|
||||||
const { image, percent } = Jimp.diff(session, baseline, THRESHOLD);
|
const { image, percent } = Jimp.diff(session, baseline, THRESHOLD);
|
||||||
log.debug(`percent different: ${percent}`);
|
log.debug(`percent different: ${percent}`);
|
||||||
if (percent > 0) {
|
if (percent > 0) {
|
||||||
image.write(diffPath);
|
image.write(diffPath);
|
||||||
|
|
||||||
// For debugging purposes it'll help to see the resized images and how they compare.
|
// For debugging purposes it'll help to see the resized images and how they compare.
|
||||||
session.write(path.join(sessionDirectory, `${path.parse(sessionPath).name}-session-resized.png`));
|
session.write(join(sessionDirectory, `${parse(sessionPath).name}-session-resized.png`));
|
||||||
baseline.write(path.join(sessionDirectory, `${path.parse(baselinePath).name}-baseline-resized.png`));
|
baseline.write(join(sessionDirectory, `${parse(baselinePath).name}-baseline-resized.png`));
|
||||||
}
|
}
|
||||||
return percent;
|
return percent;
|
||||||
}
|
}
|
|
@ -19,15 +19,21 @@
|
||||||
|
|
||||||
import { resolve, dirname } from 'path';
|
import { resolve, dirname } from 'path';
|
||||||
import { writeFile, readFileSync } from 'fs';
|
import { writeFile, readFileSync } from 'fs';
|
||||||
import { fromNode as fcb, promisify } from 'bluebird';
|
import Bluebird, { fromNode as fcb, promisify } from 'bluebird';
|
||||||
|
// @ts-ignore
|
||||||
import mkdirp from 'mkdirp';
|
import mkdirp from 'mkdirp';
|
||||||
import del from 'del';
|
import del from 'del';
|
||||||
import { comparePngs } from './lib/compare_pngs';
|
import { comparePngs } from './lib/compare_pngs';
|
||||||
|
import { FtrProviderContext } from '../ftr_provider_context';
|
||||||
|
import { WebElementWrapper } from './lib/web_element_wrapper';
|
||||||
|
|
||||||
const mkdirAsync = promisify(mkdirp);
|
type WriteFileAsync = (path: string | number | Buffer | URL, data: any) => Bluebird<void>;
|
||||||
const writeFileAsync = promisify(writeFile);
|
type MkDirAsync = (dirName: string) => Bluebird<void>;
|
||||||
|
|
||||||
export async function ScreenshotsProvider({ getService }) {
|
const mkdirAsync = promisify(mkdirp) as MkDirAsync;
|
||||||
|
const writeFileAsync = promisify(writeFile) as WriteFileAsync;
|
||||||
|
|
||||||
|
export async function ScreenshotsProvider({ getService }: FtrProviderContext) {
|
||||||
const log = getService('log');
|
const log = getService('log');
|
||||||
const config = getService('config');
|
const config = getService('config');
|
||||||
const browser = getService('browser');
|
const browser = getService('browser');
|
||||||
|
@ -38,14 +44,13 @@ export async function ScreenshotsProvider({ getService }) {
|
||||||
await del([SESSION_DIRECTORY, FAILURE_DIRECTORY]);
|
await del([SESSION_DIRECTORY, FAILURE_DIRECTORY]);
|
||||||
|
|
||||||
class Screenshots {
|
class Screenshots {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @param name {string} name of the file to use for comparison
|
* @param name {string} name of the file to use for comparison
|
||||||
* @param updateBaselines {boolean} optional, pass true to update the baseline snapshot.
|
* @param updateBaselines {boolean} optional, pass true to update the baseline snapshot.
|
||||||
* @return {Promise.<number>} Percentage difference between the baseline and the current snapshot.
|
* @return {Promise.<number>} Percentage difference between the baseline and the current snapshot.
|
||||||
*/
|
*/
|
||||||
async compareAgainstBaseline(name, updateBaselines, el) {
|
async compareAgainstBaseline(name: string, updateBaselines: boolean, el: WebElementWrapper) {
|
||||||
log.debug('compareAgainstBaseline');
|
log.debug('compareAgainstBaseline');
|
||||||
const sessionPath = resolve(SESSION_DIRECTORY, `${name}.png`);
|
const sessionPath = resolve(SESSION_DIRECTORY, `${name}.png`);
|
||||||
await this._take(sessionPath, el);
|
await this._take(sessionPath, el);
|
||||||
|
@ -63,15 +68,15 @@ export async function ScreenshotsProvider({ getService }) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async take(name, el) {
|
async take(name: string, el: WebElementWrapper) {
|
||||||
return await this._take(resolve(SESSION_DIRECTORY, `${name}.png`), el);
|
return await this._take(resolve(SESSION_DIRECTORY, `${name}.png`), el);
|
||||||
}
|
}
|
||||||
|
|
||||||
async takeForFailure(name, el) {
|
async takeForFailure(name: string, el: WebElementWrapper) {
|
||||||
await this._take(resolve(FAILURE_DIRECTORY, `${name}.png`), el);
|
await this._take(resolve(FAILURE_DIRECTORY, `${name}.png`), el);
|
||||||
}
|
}
|
||||||
|
|
||||||
async _take(path, el) {
|
async _take(path: string, el: WebElementWrapper) {
|
||||||
try {
|
try {
|
||||||
log.info(`Taking screenshot "${path}"`);
|
log.info(`Taking screenshot "${path}"`);
|
||||||
const screenshot = await (el ? el.takeScreenshot() : browser.takeScreenshot());
|
const screenshot = await (el ? el.takeScreenshot() : browser.takeScreenshot());
|
Loading…
Add table
Add a link
Reference in a new issue