Currently, I'm trying to set up UI tests in javascript. I use several tools such as mocha and puppeeter. My test is an images comparison between two versions of the product. So I have a screenshot.js script that makes me a screenshot on each page and another compare.js script that compares me the images of the two versions and if there is a difference then an image is created showing the difference of the two images that were compared.
To be able to perform a test report with the differences encountered between the images. I found a tool that is mocha-awesome that makes test reports, but I think I have configured it wrong.
compare.js
let fs = require("fs"),
PNG = require("pngjs").PNG,
pixelmatch = require("pixelmatch");
let assert = require("chai").assert;
let expect = require("chai").expect;
const rimraf = require("rimraf");
const path = require("path");
const mkdirp = require("mkdirp");
const config = require("./config.json").compare;
const versions = require("./config.json").version;
let imgFolder = path.join(__dirname, config.image);
const folderCurrent = path.join(imgFolder, versions.current);
const folderOld = path.join(imgFolder, versions.old);
const diffFolder = path.join(imgFolder, config.diff);
const dirFolder = path.join(diffFolder, "*");
mkdirp(diffFolder);
rimraf.sync(dirFolder);
describe("Compare images", function() {
let files = fs.readdirSync(folderCurrent);
files.forEach(value => {
it(`Image: ${value}`, async () => {
return compareScreenshots(value);
});
});
});
function compareScreenshots(file) {
return new Promise((resolve, reject) => {
const img1 = fs
.createReadStream(path.join(folderCurrent, file))
.pipe(new PNG())
.on("parsed", doneReading);
const img2 = fs
.createReadStream(path.join(folderOld, file))
.pipe(new PNG())
.on("parsed", doneReading);
let filesRead = 0;
function doneReading() {
if (++filesRead < 2) return;
// The files should be the same size.
expect(img1.width, "image widths are the same").equal(img2.width);
expect(img1.height, "image heights are the same").equal(img2.height);
const diff = new PNG({ width: img1.width, height: img1.height });
const mismatch = pixelmatch(
img1.data,
img2.data,
diff.data,
img1.width,
img1.height,
{
threshold: 0.05
}
);
if (mismatch !== 0) {
diff
.pack()
.pipe(
fs.createWriteStream(
path.join(diffFolder, "diff-" + path.basename(file))
)
);
}
// The files should look the same.
expect(mismatch, "number of different pixels").equal(0);
resolve();
}
});
}
server.js
const express = require('express');
const app = express();
const path = require('path');
const port = 9988;
app.use(express.static(__dirname +'/mochawesome-report'));
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname+"/mochawesome-report"+"/mochawesome.html"));
});
app.listen(port, () => console.log(`Test Report shows on port ${port}`));
I am asking you for advice on how to adapt my code or configuration to create a test report first. Do you think it's possible?
Aucun commentaire:
Enregistrer un commentaire