Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DOCS] add wiki entry on how to compare different sized images #7

Open
jpmschuler opened this issue Jul 6, 2023 · 0 comments
Open

Comments

@jpmschuler
Copy link

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);
	}
},
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant