From 85be0337e5a4c50bfb05aa87eae7db916d6befab Mon Sep 17 00:00:00 2001 From: Roy Sutton Date: Mon, 14 Oct 2019 12:51:56 -0400 Subject: [PATCH 1/3] Add isNewScreenshot to results Fixes #83 --- src/methods/BaseCompare.js | 5 +++-- src/methods/LocalCompare.js | 6 +++--- test/unit/methods/LocalCompare.test.js | 24 +++++++++++++++++++++--- 3 files changed, 27 insertions(+), 8 deletions(-) diff --git a/src/methods/BaseCompare.js b/src/methods/BaseCompare.js index 4a3e8aa..be560e9 100644 --- a/src/methods/BaseCompare.js +++ b/src/methods/BaseCompare.js @@ -126,12 +126,13 @@ export default class BaseCompare { return path.join(runtimeConfigPath, `${name}-${suffix}.json`); } - createResultReport(misMatchPercentage, isWithinMisMatchTolerance, isSameDimensions) { + createResultReport(misMatchPercentage, isWithinMisMatchTolerance, isSameDimensions, isNewScreenshot) { return { misMatchPercentage, isWithinMisMatchTolerance, isSameDimensions, - isExactSameImage: misMatchPercentage === 0 + isExactSameImage: misMatchPercentage === 0, + isNewScreenshot }; } diff --git a/src/methods/LocalCompare.js b/src/methods/LocalCompare.js index 42d9dc2..6e4c3b9 100644 --- a/src/methods/LocalCompare.js +++ b/src/methods/LocalCompare.js @@ -43,18 +43,18 @@ export default class LocalCompare extends BaseCompare { const png = compareData.getDiffImage().pack(); await this.writeDiff(png, diffPath); - return this.createResultReport(misMatchPercentage, false, isSameDimensions); + return this.createResultReport(misMatchPercentage, false, isSameDimensions, false); } else { log(`Image is within tolerance or the same`); await fs.remove(diffPath); - return this.createResultReport(misMatchPercentage, true, isSameDimensions); + return this.createResultReport(misMatchPercentage, true, isSameDimensions, false); } } else { log('first run - create reference file'); await fs.outputFile(referencePath, base64Screenshot, 'base64'); - return this.createResultReport(0, true, true); + return this.createResultReport(0, true, true, true); } } diff --git a/test/unit/methods/LocalCompare.test.js b/test/unit/methods/LocalCompare.test.js index 2b847e9..2b66526 100644 --- a/test/unit/methods/LocalCompare.test.js +++ b/test/unit/methods/LocalCompare.test.js @@ -55,11 +55,20 @@ describe('LocalCompare', function () { misMatchPercentage: 0, isWithinMisMatchTolerance: true, isSameDimensions: true, - isExactSameImage: true + isExactSameImage: true, + isNewScreenshot: false }; }); - it('creates the captured screenshot', async function () { + this.resultNewFile = { + misMatchPercentage: 0, + isWithinMisMatchTolerance: true, + isSameDimensions: true, + isExactSameImage: true + }; + }); + + it('creates the captured screenshot', async function () { const context = {}; const base64Screenshot = await readAsBase64(path.join(dirFixture, 'image/100x100.png')); @@ -85,7 +94,7 @@ describe('LocalCompare', function () { assert.isTrue(this.getReferenceFile.calledWithExactly(context), 'Reference getter should receive context as arg'); // check image results - assert.deepEqual(results, this.resultIdentical, 'Result should be reported'); + assert.deepEqual(results, this.resultNewFile, 'Result should be reported'); // check if reference image was created const existsReference = await fs.exists(this.referencFile); @@ -165,6 +174,7 @@ describe('LocalCompare', function () { assert.isFalse(resultSecond.isExactSameImage, 'Images should diff'); assert.isFalse(resultSecond.isWithinMisMatchTolerance, 'Images should be marked as diff'); assert.isTrue(resultSecond.isSameDimensions, 'Image dimensioms should be the same'); + assert.isFalse(resultSecond.isNewScreenshot, 'Image should not be new screenshot') // check if reference is still the same const statsSecond = await fs.stat(this.referencFile); @@ -272,6 +282,7 @@ describe('LocalCompare', function () { assert.isAbove(result.misMatchPercentage, this.misMatchTolerance, 'Images should diff'); assert.isFalse(result.isExactSameImage, 'Images should diff'); assert.isFalse(result.isWithinMisMatchTolerance, 'Images should be marked as diff'); + assert.isFalse(result.isNewScreenshot, 'Image should not be new screenshot'); // check if diff image was created const existsDiff = await fs.exists(this.diffFile); @@ -307,6 +318,7 @@ describe('LocalCompare', function () { assert.isAtMost(result.misMatchPercentage, this.misMatchTolerance, 'Images should diff'); assert.isFalse(result.isExactSameImage, 'Images should diff'); assert.isTrue(result.isWithinMisMatchTolerance, 'Diff should be in tolerance'); + assert.isFalse(result.isNewScreenshot, 'Image should not be new screenshot'); // check if diff image was not created const existsDiff = await fs.exists(this.diffFile); @@ -321,6 +333,7 @@ describe('LocalCompare', function () { assert.isAbove(result.misMatchPercentage, this.misMatchTolerance, 'Images should diff'); assert.isFalse(result.isExactSameImage, 'Images should diff'); assert.isFalse(result.isWithinMisMatchTolerance, 'Images should be marked as diff'); + assert.isFalse(result.isNewScreenshot, 'Image should not be new screenshot'); // check if diff image was created const existsDiff = await fs.exists(this.diffFile); @@ -359,6 +372,7 @@ describe('LocalCompare', function () { assert.isAtMost(result.misMatchPercentage, this.misMatchTolerance, 'Images should diff'); assert.isFalse(result.isExactSameImage, 'Images should diff'); assert.isTrue(result.isWithinMisMatchTolerance, 'Diff should be in tolerance'); + assert.isFalse(result.isNewScreenshot, 'Image should not be new screenshot'); // check if diff image was not created const existsDiff = await fs.exists(this.diffFile); @@ -373,6 +387,7 @@ describe('LocalCompare', function () { assert.isAbove(result.misMatchPercentage, this.misMatchTolerance, 'Images should diff'); assert.isFalse(result.isExactSameImage, 'Images should diff'); assert.isFalse(result.isWithinMisMatchTolerance, 'Images should be marked as diff'); + assert.isFalse(result.isNewScreenshot, 'Image should not be new screenshot'); // check if diff image was created const existsDiff = await fs.exists(this.diffFile); @@ -424,6 +439,7 @@ describe('LocalCompare', function () { assert.isAbove(result.misMatchPercentage, 0, 'Images should diff'); assert.isFalse(result.isExactSameImage, 'Images should diff'); assert.isFalse(result.isWithinMisMatchTolerance, 'Diff should not be in tolerance'); + assert.isFalse(result.isNewScreenshot, 'Image should not be new screenshot'); // check if diff image was created const existsDiff = await fs.exists(this.diffFile); @@ -457,6 +473,7 @@ describe('LocalCompare', function () { // check diff results assert.isTrue(result.isExactSameImage, 'Images should not diff'); assert.isTrue(result.isWithinMisMatchTolerance, 'Diff should be in tolerance'); + assert.isFalse(result.isNewScreenshot, 'Image should not be new screenshot'); // check if diff image was not created const existsDiff = await fs.exists(this.diffFile); @@ -494,6 +511,7 @@ describe('LocalCompare', function () { // check diff results assert.isTrue(result.isExactSameImage, 'Images should not diff'); assert.isTrue(result.isWithinMisMatchTolerance, 'Diff should be in tolerance'); + assert.isFalse(result.isNewScreenshot, 'Image should not be new screenshot'); // check if diff image was not created const existsDiff = await fs.exists(this.diffFile); From 697390742ea17b604acf937e04fe9cc1d962707b Mon Sep 17 00:00:00 2001 From: Roy Sutton Date: Mon, 14 Oct 2019 13:37:23 -0400 Subject: [PATCH 2/3] Fixes typo and some tests --- test/unit/methods/LocalCompare.test.js | 16 ++++++++-------- test/unit/methods/SaveScreenshot.test.js | 13 +++++++++++-- test/unit/methods/Spectre.test.js | 4 +++- 3 files changed, 22 insertions(+), 11 deletions(-) diff --git a/test/unit/methods/LocalCompare.test.js b/test/unit/methods/LocalCompare.test.js index 2b66526..b256e05 100644 --- a/test/unit/methods/LocalCompare.test.js +++ b/test/unit/methods/LocalCompare.test.js @@ -58,15 +58,15 @@ describe('LocalCompare', function () { isExactSameImage: true, isNewScreenshot: false }; - }); - this.resultNewFile = { - misMatchPercentage: 0, - isWithinMisMatchTolerance: true, - isSameDimensions: true, - isExactSameImage: true - }; - }); + this.resultNewFile = { + misMatchPercentage: 0, + isWithinMisMatchTolerance: true, + isSameDimensions: true, + isExactSameImage: true, + isNewScreenshot: true + }; + }); it('creates the captured screenshot', async function () { const context = {}; diff --git a/test/unit/methods/SaveScreenshot.test.js b/test/unit/methods/SaveScreenshot.test.js index d6a4182..a616a76 100644 --- a/test/unit/methods/SaveScreenshot.test.js +++ b/test/unit/methods/SaveScreenshot.test.js @@ -51,7 +51,16 @@ describe('SaveScreenshot', function () { misMatchPercentage: 0, isWithinMisMatchTolerance: true, isSameDimensions: true, - isExactSameImage: true + isExactSameImage: true, + isNewScreenshot: false + }; + + this.resultNewFile = { + misMatchPercentage: 0, + isWithinMisMatchTolerance: true, + isSameDimensions: true, + isExactSameImage: true, + isNewScreenshot: true }; }); @@ -66,7 +75,7 @@ describe('SaveScreenshot', function () { assert.isTrue(this.getReferenceFile.calledWithExactly(context), 'Reference getter should receive context as arg'); // check image results - assert.deepEqual(results, this.resultIdentical, 'Result should be reported'); + assert.deepEqual(results, this.resultNewFile, 'Result should be reported'); // check if reference image was created const existsReference = await fs.exists(this.referencFile); diff --git a/test/unit/methods/Spectre.test.js b/test/unit/methods/Spectre.test.js index b5b914c..294ad10 100644 --- a/test/unit/methods/Spectre.test.js +++ b/test/unit/methods/Spectre.test.js @@ -126,7 +126,8 @@ describe('Spectre', function () { misMatchPercentage: 0, isWithinMisMatchTolerance: true, isSameDimensions: true, - isExactSameImage: true + isExactSameImage: true, + isNewScreenshot: undefined, }, 'Result should be reported'); nock(this.url) @@ -167,6 +168,7 @@ describe('Spectre', function () { isWithinMisMatchTolerance: false, isSameDimensions: true, isExactSameImage: false, + isNewScreenshot: undefined, }, 'Result should be reported'); await instance.onComplete(); From 88ae2ecd5dd574863a1ffa14b08780fa5bf169ac Mon Sep 17 00:00:00 2001 From: Roy Sutton Date: Mon, 14 Oct 2019 14:53:48 -0400 Subject: [PATCH 3/3] Fixes rest of the tests --- src/methods/SaveScreenshot.js | 3 ++- test/unit/methods/LocalCompare.test.js | 4 ++-- test/unit/methods/SaveScreenshot.test.js | 4 ++-- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/methods/SaveScreenshot.js b/src/methods/SaveScreenshot.js index 10a657f..982832e 100644 --- a/src/methods/SaveScreenshot.js +++ b/src/methods/SaveScreenshot.js @@ -13,10 +13,11 @@ export default class SaveScreenshot extends BaseCompare { async processScreenshot(context, base64Screenshot) { const screenshotPath = this.getScreenshotFile(context); + const screenshotExists = await fs.exists(screenshotPath); log(`create screenshot file at ${screenshotPath}`); await fs.outputFile(screenshotPath, base64Screenshot, 'base64'); - return this.createResultReport(0, true, true); + return this.createResultReport(0, true, true, !screenshotExists); } } diff --git a/test/unit/methods/LocalCompare.test.js b/test/unit/methods/LocalCompare.test.js index b256e05..1ca6ee5 100644 --- a/test/unit/methods/LocalCompare.test.js +++ b/test/unit/methods/LocalCompare.test.js @@ -114,7 +114,7 @@ describe('LocalCompare', function () { assert.isTrue(this.getReferenceFile.calledWithExactly(context), 'Reference getter should receive context as arg'); // check image results - assert.deepEqual(resultFirst, this.resultIdentical, 'Result should be reported'); + assert.deepEqual(resultFirst, this.resultNewFile, 'Result should be reported'); // check if reference was created const existsReference = await fs.exists(this.screenshotFile); @@ -152,7 +152,7 @@ describe('LocalCompare', function () { assert.isTrue(this.getReferenceFile.calledWithExactly(context), 'Reference getter should receive context as arg'); // check image results - assert.deepEqual(resultFirst, this.resultIdentical, 'Result should be reported'); + assert.deepEqual(resultFirst, this.resultNewFile, 'Result should be reported'); // check if reference was created const existsReference = await fs.exists(this.screenshotFile); diff --git a/test/unit/methods/SaveScreenshot.test.js b/test/unit/methods/SaveScreenshot.test.js index a616a76..35b3e63 100644 --- a/test/unit/methods/SaveScreenshot.test.js +++ b/test/unit/methods/SaveScreenshot.test.js @@ -95,7 +95,7 @@ describe('SaveScreenshot', function () { assert.isTrue(this.getReferenceFile.calledWithExactly(context), 'Reference getter should receive context as arg'); // check image results - assert.deepEqual(resultFirst, this.resultIdentical, 'Result should be reported'); + assert.deepEqual(resultFirst, this.resultNewFile, 'Result should be reported'); // check if reference was created const existsReference = await fs.exists(this.referencFile); @@ -136,7 +136,7 @@ describe('SaveScreenshot', function () { assert.isTrue(this.getReferenceFile.calledWithExactly(context), 'Reference getter should receive context as arg'); // check image results - assert.deepEqual(resultFirst, this.resultIdentical, 'Result should be reported'); + assert.deepEqual(resultFirst, this.resultNewFile, 'Result should be reported'); // check if reference was created const existsReference = await fs.exists(this.referencFile);