Skip to content

Commit

Permalink
Merge pull request #686 from gemini-testing/HERMIONE-770.optimize_loo…
Browse files Browse the repository at this point in the history
…ksSame_integration

perf(assert-view): optimize fs operations
  • Loading branch information
KuznetsovRoman authored Nov 21, 2022
2 parents 2a22531 + 7810b70 commit b345818
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 4 deletions.
8 changes: 6 additions & 2 deletions lib/browser/commands/assert-view/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,14 @@ module.exports = (browser) => {
const currImgInst = await screenShooter.capture(page, screenshoterOpts);
const currSize = await currImgInst.getSize();
const currImg = {path: temp.path(Object.assign(tempOpts, {suffix: '.png'})), size: currSize};
await currImgInst.save(currImg.path);

const test = getTestContext(session.executionContext);
const refImg = {path: config.getScreenshotPath(test, state), size: null};
const {emitter} = browser;

if (!fs.existsSync(refImg.path)) {
await currImgInst.save(currImg.path);

return handleNoRefImage(currImg, refImg, state, {emitter}).catch((e) => handleCaptureProcessorError(e));
}

Expand All @@ -77,14 +78,17 @@ module.exports = (browser) => {
pixelRatio,
compareOpts
};
const {equal, diffBounds, diffClusters, metaInfo = {}} = await Image.compare(refImg.path, currImg.path, imageCompareOpts);
const currBuffer = await currImgInst.toPngBuffer({resolveWithObject: false});
const {equal, diffBounds, diffClusters, metaInfo = {}} = await Image.compare(refImg.path, currBuffer, imageCompareOpts);
Object.assign(refImg, metaInfo.refImg);

if (!equal) {
const diffAreas = {diffBounds, diffClusters};
const {tolerance, antialiasingTolerance} = opts;
const imageDiffOpts = {tolerance, antialiasingTolerance, canHaveCaret, diffAreas, config, emitter};

await fs.outputFile(currImg.path, currBuffer);

return handleImageDiff(currImg, refImg, state, imageDiffOpts).catch((e) => handleCaptureProcessorError(e));
}

Expand Down
57 changes: 55 additions & 2 deletions test/lib/browser/commands/assert-view/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ describe('assertView command', () => {
});

return {
toPngBuffer: sandbox.stub().resolves('defaultPngBuffer'),
save: sandbox.stub().named('save'),
getSize: sandbox.stub().named('getSize').returns(opts.size)
};
Expand All @@ -69,6 +70,7 @@ describe('assertView command', () => {

sandbox.stub(fs, 'readFileSync');
sandbox.stub(fs, 'existsSync').returns(true);
sandbox.stub(fs, 'outputFile').resolves();

sandbox.stub(temp, 'path');
sandbox.stub(temp, 'attach');
Expand Down Expand Up @@ -277,10 +279,11 @@ describe('assertView command', () => {
const image = stubImage_();

ScreenShooter.prototype.capture.resolves(image);
image.toPngBuffer.resolves('currPngBuffer');

await fn(browser);

assert.calledOnceWith(image.save, '/curr/path');
assert.calledOnceWith(fs.outputFile, '/curr/path', 'currPngBuffer');
});

[
Expand Down Expand Up @@ -357,6 +360,28 @@ describe('assertView command', () => {
});
});

it('should not save current image if reference is already exists', async () => {
const browser = await stubBrowser_({getScreenshotPath: () => '/ref/path'}).init();
const image = stubImage_();
fs.existsSync.withArgs('/ref/path').returns(true);
ScreenShooter.prototype.capture.resolves(image);

await fn(browser);

assert.notCalled(image.save);
});

it('should save current image if reference does not exist', async () => {
const browser = await stubBrowser_({getScreenshotPath: () => '/ref/path'}).init();
const image = stubImage_();
fs.existsSync.withArgs('/ref/path').returns(false);
ScreenShooter.prototype.capture.resolves(image);

await fn(browser);

assert.calledOnce(image.save);
});

it('should not fail if there is no reference image', async () => {
fs.existsSync.returns(false);
const browser = await stubBrowser_().init();
Expand Down Expand Up @@ -442,13 +467,41 @@ describe('assertView command', () => {

it('should compare a current image with a reference', async () => {
const config = mkConfig_({getScreenshotPath: () => '/ref/path'});
const image = stubImage_();
Image.compare.resolves({equal: true});
temp.path.returns('/curr/path');
ScreenShooter.prototype.capture.resolves(image);
image.toPngBuffer.resolves('currPngBuffer');
const browser = await stubBrowser_(config).init();

await fn(browser);

assert.calledOnceWith(Image.compare, '/ref/path', '/curr/path');
assert.calledOnceWith(Image.compare, '/ref/path', 'currPngBuffer');
});

it('should not save current image if images are equal', async () => {
const image = stubImage_();
const browser = await stubBrowser_({getScreenshotPath: () => '/ref/path'}).init();
image.toPngBuffer.resolves('currPngBuffer');
Image.compare.withArgs('/ref/path', 'currPngBuffer').resolves({equal: true});
ScreenShooter.prototype.capture.resolves(image);

await fn(browser);

assert.notCalled(fs.outputFile);
});

it('should save current image if images are not equal', async () => {
const image = stubImage_();
const browser = await stubBrowser_({getScreenshotPath: () => '/ref/path'}).init();
image.toPngBuffer.resolves('currPngBuffer');
Image.compare.withArgs('/ref/path', 'currPngBuffer').resolves({equal: false});
temp.path.returns('/curr/path');
ScreenShooter.prototype.capture.resolves(image);

await fn(browser);

assert.calledOnceWith(fs.outputFile, '/curr/path', 'currPngBuffer');
});

it('should compare images with given set of parameters', async () => {
Expand Down

0 comments on commit b345818

Please sign in to comment.