An open API service indexing awesome lists of open source software.

https://github.com/priyankapoojari/java-imagecomparator-selenium-appium

Java based utility to compare Screenshots from different release. Can be integrated with Selenium, appium or any other Java based frameworks
https://github.com/priyankapoojari/java-imagecomparator-selenium-appium

appium automation-framework image-comparison java selenium test-automation

Last synced: about 1 month ago
JSON representation

Java based utility to compare Screenshots from different release. Can be integrated with Selenium, appium or any other Java based frameworks

Awesome Lists containing this project

README

          

# java-imageComparator-selenium-appium

# ๐Ÿ“ธ Image Comparator Utility for Java Projects

## ๐Ÿ” Use Case

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

## โœ… Key Features

* 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

## ๐Ÿš€ How It Works

* 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.

```java
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](https://github.com/pazone/ashot)

```
//Both not compatible with Selenium 4 dated 8th April 2025


```

* **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](https://github.com/romankh3/image-comparison)

```java
//compatible with Selenium 4 dated 8th April 2025

com.github.romankh3
image-comparison
4.4.0

```

```java
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"`

## โš™๏ธ Pre-requisites

Make sure you have the following installed before using the utility:

* Java (latest version. Works fine with v23)
* Maven (to download dependency)

## ๐Ÿ› ๏ธ Usage

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.

## ๐Ÿงช Example Output of Image-Comparison library

Comparing 2 different Pages: assertion fails and highlights difference
![](https://github.com/PriyankaPoojari/java-imageComparator-selenium-appium/blob/master/DifferentPage.png)

Comparing 2 same Page with Different data: assertion fails and highlights difference
![](https://github.com/PriyankaPoojari/java-imageComparator-selenium-appium/blob/master/samePageDiffData.png)

Comparing 2 different dimensions: assertion fails and saves target image
![](https://github.com/PriyankaPoojari/java-imageComparator-selenium-appium/blob/master/samsungDiff.png)

Comparing 2 same Pages: assertion passes as match and saves target image
![](https://github.com/PriyankaPoojari/java-imageComparator-selenium-appium/blob/master/sameImg.png)