Skip to content

Commit

Permalink
add docs & tests for #58 (#73)
Browse files Browse the repository at this point in the history
resolves #45
  • Loading branch information
zinserjan authored Apr 3, 2018
1 parent 9c1dd8d commit 7706549
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 1 deletion.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ pass in a function that returns the filename for the diff screenshot. Function r
* **misMatchTolerance** `Number` ( default: 0.01 ) <br>
number between 0 and 100 that defines the degree of mismatch to consider two images as identical, increasing this value will decrease test coverage.

* **ignoreComparison** `String` ( default: nothing ) <br>
pass in a string with value of `nothing` , `colors` or `antialiasing` to adjust the comparison method.

For an example of generating screenshot filesnames dependent on the current test name, have a look at the sample code of [Configuration](#configuration).

#### VisualRegressionCompare.SaveScreenshot
Expand Down Expand Up @@ -201,6 +204,9 @@ available:
* **fuzzLevel** `Number` <br>
Overrides the global *fuzzLevel* value for this command. Pass in a number between 0 and 100 that defines the fuzz factor of Spectre's image comparison method.

* **ignoreComparison** `String` <br>
Overrides the global *ignoreComparison* value for this command. Pass in a string with value of `nothing` , `colors` or `antialiasing` to adjust the comparison method.

* **viewportChangePause** `Number` <br>
Overrides the global *viewportChangePause* value for this command. Wait x milliseconds after viewport change.

Expand Down
2 changes: 1 addition & 1 deletion src/methods/LocalCompare.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export default class LocalCompare extends BaseCompare {
this.getReferencefile = options.referenceName;
this.getDiffFile = options.diffName;
this.misMatchTolerance = _.get(options, 'misMatchTolerance', 0.01);
this.ignoreComparison = _.get(options, 'ignoreComparison', '');
this.ignoreComparison = _.get(options, 'ignoreComparison', 'nothing');
}

async afterScreenshot(context, base64Screenshot) {
Expand Down
Binary file added test/fixture/ignoreComparison/100x100-red.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/fixture/ignoreComparison/100x100-red2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
121 changes: 121 additions & 0 deletions test/unit/methods/LocalCompare.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -380,4 +380,125 @@ describe('LocalCompare', function () {
});
});
});

context('ignoreComparison', function () {
before(async function () {
this.screenshotRed = await readAsBase64(path.join(dirFixture, 'ignoreComparison/100x100-red.png'));
this.screenshotRed2 = await readAsBase64(path.join(dirFixture, 'ignoreComparison/100x100-red2.png'));
});

beforeEach(async function () {
this.screenshotFile = path.join(dirTmp, 'screenshot.png');
this.referencFile = path.join(dirTmp, 'reference.png');
this.diffFile = path.join(dirTmp, 'diff.png');

this.getScreenshotFile = stub().returns(this.screenshotFile);
this.getReferenceFile = stub().returns(this.referencFile);
this.getDiffFile = stub().returns(this.diffFile);
});


context('uses default ignoreComparison', function () {
beforeEach(async function () {
this.context = {};

this.localCompare = new LocalCompare({
screenshotName: this.getScreenshotFile,
referenceName: this.getReferenceFile,
diffName: this.getDiffFile,
});

// 1st run -> create reference
const resultFirst = await this.localCompare.afterScreenshot({}, this.screenshotRed);

// check if reference was created
const existsReference = await fs.exists(this.screenshotFile);
assert.isTrue(existsReference, 'Captured screenshot should exist');
});

it('reports diff when colors differs', async function () {
// compare screenshots
const result = await this.localCompare.afterScreenshot(this.context, this.screenshotRed2);

// check diff results
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');

// check if diff image was created
const existsDiff = await fs.exists(this.diffFile);
assert.isTrue(existsDiff, 'Diff screenshot should exist');
});
});

context('uses custom ignoreComparison passed in constructor option', function () {
beforeEach(async function () {
this.ignoreComparison = 'colors';
this.context = {};

this.localCompare = new LocalCompare({
screenshotName: this.getScreenshotFile,
referenceName: this.getReferenceFile,
diffName: this.getDiffFile,
ignoreComparison: this.ignoreComparison,
});

// 1st run -> create reference
const resultFirst = await this.localCompare.afterScreenshot({}, this.screenshotRed);

// check if reference was created
const existsReference = await fs.exists(this.screenshotFile);
assert.isTrue(existsReference, 'Captured screenshot should exist');
});

it('reports equal with ignoreComparison=colors when colors differs', async function () {
// compare screenshots
const result = await this.localCompare.afterScreenshot(this.context, this.screenshotRed2);
// check diff results
assert.isTrue(result.isExactSameImage, 'Images should not diff');
assert.isTrue(result.isWithinMisMatchTolerance, 'Diff should be in tolerance');

// check if diff image was not created
const existsDiff = await fs.exists(this.diffFile);
assert.isFalse(existsDiff, 'Diff screenshot should not exist');
});
});

context('uses custom ignoreComparison passed in command options', function () {
beforeEach(async function () {
this.ignoreComparison = 'colors';
this.context = {
options: {
ignoreComparison: this.ignoreComparison,
}
};

this.localCompare = new LocalCompare({
screenshotName: this.getScreenshotFile,
referenceName: this.getReferenceFile,
diffName: this.getDiffFile,
});

// 1st run -> create reference
const resultFirst = await this.localCompare.afterScreenshot({}, this.screenshotRed);

// check if reference was created
const existsReference = await fs.exists(this.screenshotFile);
assert.isTrue(existsReference, 'Captured screenshot should exist');
});

it('reports equal with ignoreComparison=colors when colors differs', async function () {
// compare screenshots
const result = await this.localCompare.afterScreenshot(this.context, this.screenshotRed2);

// check diff results
assert.isTrue(result.isExactSameImage, 'Images should not diff');
assert.isTrue(result.isWithinMisMatchTolerance, 'Diff should be in tolerance');

// check if diff image was not created
const existsDiff = await fs.exists(this.diffFile);
assert.isFalse(existsDiff, 'Diff screenshot should not exist');
});
});
});
});

0 comments on commit 7706549

Please sign in to comment.