Skip to content
This repository has been archived by the owner on Sep 21, 2022. It is now read-only.

Commit

Permalink
Merge c757121 into 3daaf2b
Browse files Browse the repository at this point in the history
  • Loading branch information
DudaGod committed Nov 29, 2018
2 parents 3daaf2b + c757121 commit 36f73d6
Show file tree
Hide file tree
Showing 14 changed files with 206 additions and 173 deletions.
14 changes: 7 additions & 7 deletions doc/events.md
Expand Up @@ -136,7 +136,7 @@ Emitted before launching browser for test. Event is emitted with 1 argument <cod
Emitted always during update. Event is emitted with 1 argument <code>result</code>:
<pre>
{
imagePath, // absolute path to the reference image
refImg, // reference image info which includes absolute path and size (width, height)
updated, // shows if reference image has been changed
suite,
state,
Expand All @@ -154,8 +154,8 @@ Emitted if test has failed but <b>there is still number of retries left</b>.
Event is emitted with 1 argument <code>result</code>:
<pre>
{
referencePath, // absolute path to the reference image
currentPath, // absolute path to the current image on your disk
refImg, // reference image info which includes absolute path and size (width, height)
currImg, // current image info which includes absolute path and size (width, height)
equal, // always <i>false</i> for retries
tolerance, // specified for current test or globally in <i>.gemini.js</i>
saveDiffTo, /* function responsible for building <i>diff</i> and <i>present</i>
Expand All @@ -178,8 +178,8 @@ Event is emitted with 1 argument <code>result</code>:
Emitted always after the test is completed. Event is emitted with 1 argument <code>result</code>:
<pre>
{
referencePath, // absolute path to the reference image
currentPath, // absolute path to the current image on your disk
refImg, // reference image info which includes absolute path and size (width, height)
currImg, // current image info which includes absolute path and size (width, height)
equal, // shows if images are equal
tolerance, // specified for current test or globally in <i>.gemini.js</i>
saveDiffTo, /* function responsible for building <i>diff</i> and <i>present</i>
Expand Down Expand Up @@ -240,8 +240,8 @@ For example, if <i>Reference image is missing</i>, <code>err</code> will have ad

<pre>
{
currentPath,
refImagePath,
refImg,
currImg,
suite,
state,
browserId,
Expand Down
6 changes: 4 additions & 2 deletions lib/capture-session.js
Expand Up @@ -51,8 +51,10 @@ module.exports = class CaptureSession {
const path = temp.path({suffix: '.png'});

return this.browser.captureViewportImage()
.then((screenImage) => screenImage.save(path))
.then(() => (obj.imagePath = path))
.then((screenImage) => [screenImage.getSize(), screenImage.save(path)])
.spread((size) => {
obj.img = {path, size};
})
.catch(_.noop)
.then(() => obj);
}
Expand Down
8 changes: 4 additions & 4 deletions lib/errors/no-ref-image-error.js
Expand Up @@ -3,12 +3,12 @@
const StateError = require('./state-error');

module.exports = class NoRefImageError extends StateError {
constructor(refImagePath, currentPath) {
super(`Can not find reference image at ${refImagePath}.\n` +
constructor(refImg = {}, currImg = {}) {
super(`Can not find reference image at ${refImg.path}.\n` +
'Run `gemini update` command to capture all reference images.');

this.name = 'NoRefImageError';
this.currentPath = currentPath;
this.refImagePath = refImagePath;
this.refImg = refImg;
this.currImg = currImg;
}
};
2 changes: 1 addition & 1 deletion lib/errors/utils.js
Expand Up @@ -7,7 +7,7 @@ const StateError = require('./state-error');

exports.fromPlainObject = e => {
if (e.name === 'NoRefImageError') {
return new NoRefImageError(e.refImagePath, e.currentPath);
return new NoRefImageError(e.refImg, e.currImg);
}
if (e.name === 'StateError') {
return new StateError(e.message);
Expand Down
26 changes: 16 additions & 10 deletions lib/state-processor/capture-processor/capture-processor.js
@@ -1,6 +1,7 @@
'use strict';

const _ = require('lodash');
const fs = require('fs-extra');
const utils = require('./utils');
const {temp, Image} = require('gemini-core');

Expand Down Expand Up @@ -30,32 +31,37 @@ module.exports = class CaptureProcessor {
}

exec(capture, opts) {
const referencePath = opts.referencePath;
const {refImg} = opts;

return utils.existsRef(referencePath)
return utils.existsRef(refImg.path)
.then((isRefExists) => {
if (isRefExists) {
const refImgBase64 = fs.readFileSync(refImg.path);
refImg.size = Image.fromBase64(refImgBase64).getSize();
}

return isRefExists
? this._onRefHandler(referencePath) || this._compareImages(capture, opts)
: this._onNoRefHandler(referencePath, capture);
? this._onRefHandler(refImg) || this._compareImages(capture, opts)
: this._onNoRefHandler(refImg, capture);
});
}

_compareImages(capture, opts) {
const referencePath = opts.referencePath;
const currentPath = temp.path({suffix: '.png'});
const {refImg} = opts;
const currImg = {path: temp.path({suffix: '.png'}), size: capture.image.getSize()};
const compareOpts = {
canHaveCaret: capture.canHaveCaret,
pixelRatio: opts.pixelRatio,
tolerance: opts.tolerance,
antialiasingTolerance: opts.antialiasingTolerance
};

return capture.image.save(currentPath)
.then(() => Image.compare(currentPath, referencePath, compareOpts))
return capture.image.save(currImg.path)
.then(() => Image.compare(currImg.path, refImg.path, compareOpts))
.then((isEqual) => {
return isEqual
? this._onEqualHandler(referencePath, currentPath)
: this._onDiffHandler(referencePath, currentPath);
? this._onEqualHandler(refImg, currImg)
: this._onDiffHandler(refImg, currImg);
});
}
};
35 changes: 22 additions & 13 deletions lib/state-processor/capture-processor/index.js
Expand Up @@ -6,30 +6,39 @@ const utils = require('./utils');
const NoRefImageError = require('../../errors/no-ref-image-error');
const {temp} = require('gemini-core');

const throwNoRefError = (referencePath, capture) => {
const currentPath = temp.path({suffix: '.png'});
return capture.image.save(currentPath)
.then(() => Promise.reject(new NoRefImageError(referencePath, currentPath)));
const throwNoRefError = (refImg, capture) => {
const currImg = {path: temp.path({suffix: '.png'}), size: capture.image.getSize()};

return capture.image.save(currImg.path)
.then(() => Promise.reject(new NoRefImageError(refImg, currImg)));
};
const notUpdated = (referencePath) => ({imagePath: referencePath, updated: false});
const notUpdated = (refImg) => ({refImg, updated: false});

const saveRef = (refImg, capture) => {
refImg.size = capture.image.getSize();

const saveRef = (referencePath, capture) => {
return utils.saveRef(referencePath, capture)
.then((updated) => ({imagePath: referencePath, updated}));
return utils.saveRef(refImg.path, capture)
.then((updated) => ({refImg, updated}));
};

const updateRef = (referencePath, currentPath) => {
return utils.copyImg(currentPath, referencePath)
.then((updated) => ({imagePath: referencePath, updated}));
const updateRef = (refImg, currImg) => {
return utils.copyImg(currImg.path, refImg.path)
.then((updated) => {
if (updated) {
refImg.size = currImg.size;
}

return {refImg, updated};
});
};

exports.create = (type) => {
if (type === 'tester') {
return CaptureProcessor.create()
.onReference()
.onNoReference(throwNoRefError)
.onEqual((referencePath, currentPath) => ({referencePath, currentPath, equal: true}))
.onDiff((referencePath, currentPath) => ({referencePath, currentPath, equal: false}));
.onEqual((refImg, currImg) => ({refImg, currImg, equal: true}))
.onDiff((refImg, currImg) => ({refImg, currImg, equal: false}));
}

if (type === 'new-updater') {
Expand Down
7 changes: 4 additions & 3 deletions lib/state-processor/state-processor.js
Expand Up @@ -19,19 +19,20 @@ module.exports = class StateProcessor {
}

exec(state, browserSession, page) {
const browserConfig = browserSession.browser.config;
const coverage = page.coverage;
const tolerance = _.isNumber(state.tolerance)
? state.tolerance
: browserSession.browser.config.tolerance;
: browserConfig.tolerance;

const jobArgs = {
captureProcessorType: this._captureProcessorType,
browserSession: browserSession.serialize(),
page: _.omit(page, 'coverage'),
execOpts: {
refImg: {path: browserConfig.getScreenshotPath(state.suite, state.name), size: null},
pixelRatio: page.pixelRatio,
referencePath: browserSession.browser.config.getScreenshotPath(state.suite, state.name),
antialiasingTolerance: browserSession.browser.config.antialiasingTolerance,
antialiasingTolerance: browserConfig.antialiasingTolerance,
tolerance
},
temp: temp.serialize()
Expand Down
4 changes: 2 additions & 2 deletions lib/state-processor/test-state-processor.js
Expand Up @@ -27,8 +27,8 @@ module.exports = class TestStateProcessor extends StateProcessor {
_attachDiffBuilder(result) {
return _.extend(result, {
saveDiffTo: (diffPath) => Image.buildDiff({
reference: result.referencePath,
current: result.currentPath,
reference: result.refImg.path,
current: result.currImg.path,
diff: diffPath,
diffColor: this._diffColor,
tolerance: result.tolerance
Expand Down
37 changes: 0 additions & 37 deletions lib/typedefs.js

This file was deleted.

18 changes: 12 additions & 6 deletions test/unit/capture-session.js
Expand Up @@ -145,13 +145,16 @@ describe('capture session', () => {
let browser;
let session;
let error;
let image;

beforeEach(() => {
image = {
save: sinon.stub().resolves(),
getSize: sinon.stub()
};
browser = {
config: {},
captureViewportImage: sinon.stub().returns(Promise.resolve({
save: sinon.stub()
}))
captureViewportImage: sinon.stub().returns(Promise.resolve(image))
};
session = CaptureSession.create(browser);
error = {};
Expand All @@ -162,14 +165,17 @@ describe('capture session', () => {
.then(() => assert.called(browser.captureViewportImage));
});

it('should add an image path to error', () => {
it('should add image to an error', () => {
temp.path.returns('/path/to/img');
image.getSize.returns({width: 100, height: 200});

return session.extendWithPageScreenshot(error)
.then(() => assert.deepEqual(error.imagePath, '/path/to/img'));
.then(() => {
assert.deepEqual(error.img, {path: '/path/to/img', size: {width: 100, height: 200}});
});
});

it('should not add an image to error if can not captureViewportImage', () => {
it('should not add image to an error if can not captureViewportImage', () => {
browser.captureViewportImage = sinon.stub().callsFake(() => Promise.reject({}));

error = new StateError('some error');
Expand Down
6 changes: 3 additions & 3 deletions test/unit/errors/no-ref-image-error.js
Expand Up @@ -2,9 +2,9 @@

const NoRefImageError = require('lib/errors/no-ref-image-error');

describe('NoRefImageError', function() {
it('should include the error message in the stacktrace', function() {
const error = new NoRefImageError('anyPath');
describe('NoRefImageError', () => {
it('should include the error message in the stacktrace', () => {
const error = new NoRefImageError({path: 'anyPath'});

assert.match(error.stack, /^NoRefImageError: Can not find reference image at anyPath.\n/);
});
Expand Down
2 changes: 1 addition & 1 deletion test/unit/errors/utils.js
Expand Up @@ -15,7 +15,7 @@ describe('errors utils', () => {
});

it('should handle NoRefImageError', () => {
let obj = {name: 'NoRefImageError'};
let obj = {name: 'NoRefImageError', refImg: {}, currImg: {}};

let error = errorUtils.fromPlainObject(obj);

Expand Down

0 comments on commit 36f73d6

Please sign in to comment.