When applications are opened across various mobile devices (e.g., iPhone, Samsung models), screen dimensions and rendering can differ significantly. This makes it challenging to visually compare how the UI looked in a previous release versus the current one.
Image Comparator Utility helps solve this problem by programmatically comparing screenshots and generating a new image that highlights the differences between two versions. This is especially useful for regression testing and ensuring consistent UI across devices.
The utility code is easy to integrate with any Java based projects like selenium, appium
- Compare two UI screenshots and highlight visual differences
- Useful for responsive UI testing across different screen resolutions
- Ideal for regression testing in mobile web or hybrid applications
- Supports integration into CI/CD pipelines
- Run the utility to compare two images
- A new image is generated with highlighted visual differences
I have explored 3 different libraries which can be used to compare images:
- Basic pixel to pixel comparison using Java inbuilt library
Pro: No additional jar downloads or utility required
Con: Can only return true value if match or false if mismatch. Cannot highlight the difference.
public static boolean compareImages(File fileA, File fileB) throws IOException {
BufferedImage imgA = ImageIO.read(fileA);
BufferedImage imgB = ImageIO.read(fileB);
// Check dimensions first
if (imgA.getWidth() != imgB.getWidth() || imgA.getHeight() != imgB.getHeight()) {
return false;
}
for (int y = 0; y < imgA.getHeight(); y++) {
for (int x = 0; x < imgA.getWidth(); x++) {
if (imgA.getRGB(x, y) != imgB.getRGB(x, y)) {
return false;
}
}
}
return true;
}For more usage details, refer to "src/comparison/BasicPixelToPixel.java"
- Ashot library
Pro: Can generate new image highlighting difference
Con: Not compatible with Selenium 4
For more usage details, refer to https://github.com/pazone/ashot
//Both not compatible with Selenium 4 dated 8th April 2025
<!--<dependency>
<groupId>io.github.bernardomg</groupId>
<artifactId>ashot</artifactId>
<version>1.7.0</version>
</dependency>-->
<!--<dependency>
<groupId>ru.yandex.qatools.ashot</groupId>
<artifactId>ashot</artifactId>
<version>1.5.4</version>
</dependency>-->
- Image-Comparison library
Pro: Can generate new image highlighting difference
Con: Nothing as of. Compatible with Selenium 4
For more usage details, refer to https://github.com/romankh3/image-comparison
//compatible with Selenium 4 dated 8th April 2025
<dependency>
<groupId>com.github.romankh3</groupId>
<artifactId>image-comparison</artifactId>
<version>4.4.0</version>
</dependency> public static void imageComp(String sourceFile, String targetFile, String fileName) {
// load images to be compared:
BufferedImage expectedImage = ImageComparisonUtil
.readImageFromResources(sourceFile);
BufferedImage actualImage = ImageComparisonUtil
.readImageFromResources(targetFile);
// Create ImageComparison object with result destination and compare the images.
ImageComparisonResult imageComparisonResult = new ImageComparison(expectedImage, actualImage).compareImages();
File resultDestination = new File(fileName+".png");
// Image can be saved after comparison, using ImageComparisonUtil.
ImageComparisonUtil.saveImage(resultDestination, imageComparisonResult.getResult());
// Check the result
assertEquals(ImageComparisonState.MATCH, imageComparisonResult.getImageComparisonState(), "Mismatch");
}For more usage details, refer to "src/comparison/ImageComparisonTest.java"
Make sure you have the following installed before using the utility:
- Java (latest version. Works fine with v23)
- Maven (to download dependency)
Right click on ImageComparisonTest > run as TestNG Test
I have included multiple test scenarios to check behavior of this utility and to validate its output.
Comparing 2 different Pages: assertion fails and highlights difference

Comparing 2 same Page with Different data: assertion fails and highlights difference

Comparing 2 different dimensions: assertion fails and saves target image

Comparing 2 same Pages: assertion passes as match and saves target image
