Skip to content

Commit

Permalink
Add a threshold for the image hash comparison
Browse files Browse the repository at this point in the history
  • Loading branch information
wooferzfg committed May 27, 2020
1 parent dae3946 commit a4cac16
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
16 changes: 16 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"hex64": "^0.4.0",
"html-webpack-plugin": "^4.3.0",
"imghash": "0.0.7",
"leven": "^3.1.0",
"mocha": "^7.1.2",
"moment": "^2.25.0",
"node-fetch": "^2.6.0",
Expand Down
13 changes: 12 additions & 1 deletion test/rendering-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const chrome = require("selenium-webdriver/chrome");
const chromedriver = require("chromedriver");
const imghash = require("imghash");
const hex64 = require("hex64");
const leven = require("leven");
const pixelmatch = require("pixelmatch");
const PNG = require("pngjs").PNG;

Expand Down Expand Up @@ -48,6 +49,12 @@ describe("Layout Rendering Tests", function() {
await driver.sleep(500);
}

const hashToBinary = (hash) => {
const buffer = Buffer.from(hash, "base64");
const values = Array.from(buffer.values());
return values.map((value) => value.toString(2).padStart(8, "0")).join("");
};

before(async () => {
console.log('Starting server...');

Expand Down Expand Up @@ -111,7 +118,11 @@ describe("Layout Rendering Tests", function() {

fs.renameSync(tempFilePath, actualScreenshotPath);

if (actualHash !== expectedHash) {
const actualBinary = hashToBinary(actualHash);
const expectedBinary = hashToBinary(expectedHash);
const distance = leven(actualBinary, expectedBinary);

if (distance <= 1) {
let showWarning = false;
try {
if (fs.existsSync(expectedScreenshotPath)) {
Expand Down

0 comments on commit a4cac16

Please sign in to comment.