From 7810b70ef28b00097e519da288bd5dee4f6e8c5d Mon Sep 17 00:00:00 2001 From: Roman Kuznetsov Date: Fri, 18 Nov 2022 00:16:55 +0300 Subject: [PATCH] perf(assert-view): optimize fs operations --- lib/browser/commands/assert-view/index.js | 8 ++- .../lib/browser/commands/assert-view/index.js | 57 ++++++++++++++++++- 2 files changed, 61 insertions(+), 4 deletions(-) diff --git a/lib/browser/commands/assert-view/index.js b/lib/browser/commands/assert-view/index.js index 73de24ef8..1159772ba 100644 --- a/lib/browser/commands/assert-view/index.js +++ b/lib/browser/commands/assert-view/index.js @@ -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)); } @@ -77,7 +78,8 @@ 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) { @@ -85,6 +87,8 @@ module.exports = (browser) => { 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)); } diff --git a/test/lib/browser/commands/assert-view/index.js b/test/lib/browser/commands/assert-view/index.js index f143c3662..ea072fe38 100644 --- a/test/lib/browser/commands/assert-view/index.js +++ b/test/lib/browser/commands/assert-view/index.js @@ -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) }; @@ -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'); @@ -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'); }); [ @@ -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(); @@ -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 () => {