stracker-phil / codeceptjs-pixelmatchhelper Goto Github PK
View Code? Open in Web Editor NEWA CodeceptJS helper that integrates pixelmatch image comparison for automated, visual regression tests.
License: MIT License
A CodeceptJS helper that integrates pixelmatch image comparison for automated, visual regression tests.
License: MIT License
Suppose I have no base screenshot created yet. I'm trying this:
Feature('My');
Scenario('test something', ({ I }) => {
I.amOnPage("https://codecept.io");
I.wait(5);
I.saveElementScreenshot(".sub-bar", "Bar.png");
I.checkVisualDifferences("Bar.png");
});
But when I run the test, a base screenshot of whole page is created instead of the element .sub-bar
only.
In codeceptjs-resemblehelper I do it this way:
Feature('My');
Scenario('test something', ({ I }) => {
I.amOnPage("https://codecept.io");
I.wait(5);
I.saveElementScreenshot(".sub-bar", "Bar.png");
I.seeVisualDiff("Bar.png", { prepareBaseImage: true, tolerance: 0 });
});
and you get a base screenshot of the element only as expected.
Currently getVisualDifference
just throws generic errors. Thus, a client cannot implement their own error handling. My suggestion would be to implement specific exceptions in order for them to be handled individually.
This would especially allow to still diff images which have different dimensions (e.g. by all expected images to match the actual image).
Of course an option would need to be introduced to fallback to current behaviour, too.
Motivation:
If due to some change an element gets added to a website, or some elements have different sizes, the dimensions won't be the same, however a diff can help identify the problem easily.
Will come back to this with a PR as soon as time comes by.
For my visual regression tests I like to have diff images and diff calculation even if the dimensions of the images mismatch. This could e.g. occur between two website screenshots if an additional line of text was added.
While this could be incorporated directly in pixelmatch
as well as codeceptjs-pixelmatchhelper
I more or less agree with mapbox/pixelmatch#25 (comment) in it being easy to do outside and adding another dependency like sharp
just for edge cases should in general be avoided.
Nevertheless I think documenting the solution would help. So perhaps this could be added to the wiki/FAQ:
I created a custom function for my steps-file.js
which is just called before a this.getVisualDifferences()
and increases the smaller screenshot with black pixels. Yes this overrides both files, but is the most versatile solution.
const fs = require('fs');
const sharp = require('sharp');
...
async adaptImageCanvasForCompareIfNecessary(fileExpected, fileActual) {
const expectedImage = sharp(fs.readFileSync(fileExpected));
const actualImage = sharp(fs.readFileSync(fileActual));
const expectedImageMetadata = await expectedImage.metadata();
const actualImageMetadata = await actualImage.metadata();
const maximumWidth = Math.max(expectedImageMetadata.width, actualImageMetadata.width);
const maximumHeight = Math.max(expectedImageMetadata.height, actualImageMetadata.height);
if (expectedImageMetadata.width !== actualImageMetadata.width
|| expectedImageMetadata.height !== actualImageMetadata.height) {
this.say(`Screenshots have different dimensions; increasing canvas to ${maximumWidth}x${maximumHeight}`);
const newExpectedImage = await expectedImage
.resize({
width: maximumWidth,
height: maximumHeight,
fit: 'contain',
position: 'left top'
})
.png()
.toFile(fileExpected);
const newActualImage = await actualImage
.resize({
width: maximumWidth,
height: maximumHeight,
fit: 'contain',
position: 'left top'
})
.png()
.toFile(fileActual);
}
},
...
Hi!
There is issue when run tests in docker:
fs.renameSync(); (480 line) throws an exception:
Error: EXDEV: cross-device link not permitted, rename '/codecept/output/~60mi4.temp' -> '/codecept/tests/output/debug.png' at Object.renameSync (node:fs:993:3) at PixelmatchHelper._takeScreenshot (node_modules/codeceptjs-pixelmatchhelper/index.js:479:6) at async PixelmatchHelper._maybeCaptureImage (node_modules/codeceptjs-pixelmatchhelper/index.js:639:3) at async PixelmatchHelper.getVisualDifferences (node_modules/codeceptjs-pixelmatchhelper/index.js:280:3) at async /codecept/node_modules/codeceptjs-pixelmatchhelper/index.js:241:5
It could be fixed by changing method to fs.copyFileSync();
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.