Appium Visual Testing -5(Part-1) : Complete Details On Built-In method — .getImagesSimilarity
“.getImagesSimilarity(byte[] base64image1, byte[] base64image2)”

Watch the video below for your complete step-by-step guide!
In this article, the first in a four-part series, will explore one of Appium’s built-in methods
“.getImagesSimilarity(byte[] base64image1, byte[] base64image2)”

Introduction to built-in method “getImagesSimilarity” in Appium:
getImagesSimilarity is an Appium mobile command used to compare two Base64-encoded images
& return a similarity score.
This is useful for visual testing, validating that two images (e.g., current screenshot vs. expected image)
are visually similar within a tolerance level.
Method Signature
driver.getImagesSimilarity(byte[] base64image1, byte[] base64image2)Parameters
- base64image1: The first image to compare, encoded in Base64 and represented as a byte array.
- base64image2: The second image to compare, also Base64-encoded and a byte array.
Return Type
- Typically returns a float or double value representing the similarity score between 0.0 and 1.0.
- 1.0: Images are identical.
- 0.0: Images are completely different.
- Any value in between reflects partial similarity.
Note: Exact return types and behavior may vary depending on the framework or library implementing this method.
Step-by-step demonstration of how to use the method
getImagesSimilarity(byte[] base64image1, byte[] base64image2).
Step 1: Capture the Screenshot
//Step 1: Capture the Screenshot
System.out.println("Step 1: Capture the Screenshot");
FileUtils.copyFile(driver.getScreenshotAs(OutputType.FILE), new File("actual_image"+".png"));Step 2: Covert the file to base64image
//Step 2: Covert the file to base64image
System.out.println("Step 2: Covert the file to base64image");
byte[] img1Bytes=Files.readAllBytes(new File(System.getProperty("user.dir")+"/actual_image.png").toPath());
byte[] img2Bytes=Files.readAllBytes(new File(System.getProperty("user.dir")+"/Expected_image.png").toPath());
byte[] base64Images1=Base64.getEncoder().encode(img1Bytes);
byte[] base64Images2=Base64.getEncoder().encode(img2Bytes);Step 3: Perform the Comparison
//Step 3: Perform the Comparison
System.out.println("Step 3: Perform the Comparison");
//System.out.println(driver.getImagesSimilarity(base64Images1, base64Images2).getScore());
SimilarityMatchingResult result=driver.getImagesSimilarity(base64Images1, base64Images2);Step 4: Output the Result
//Step 4: Print the Result
System.out.println("Step 4: Print the Result");
System.out.println("Similarity Scrore of the Images - " +result.getScore());Step5: Run Appium Server with below flags:
The plugin must be explicitly activated when launching the Appium server:
appium - use-plugins=images“Complete Code: Optimized and Ready to Use”:
package com.appiumguide.appiumimages;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.options.UiAutomator2Options;
import io.appium.java_client.imagecomparison.SimilarityMatchingResult;
import java.io.File;
import java.io.IOException;
import java.net.*;
import java.nio.file.Files;
import java.time.Duration;
import java.util.Base64;
/**
* Script Details - Appium-Visual Testing : Complete Details On .getImagesSimilarity driver.getImagesSimilarity(byte[], byte[])
*
* appium-java-client version: Latest
*
* @author 'Ramesh Kodumuru' for AppiumGuide [appiumguide@gmail.com]
*/
public class GetimagesSimilarity_Demo1 {
private AndroidDriver driver;
File screenshot;
@BeforeTest
public void setup() throws MalformedURLException {
UiAutomator2Options cap=new UiAutomator2Options();
cap.setPlatformName("android");
cap.setAutomationName("uiautomator2");
cap.setDeviceName("Pixel9");
driver = new AndroidDriver(new URL("http://127.0.0.1:4723"), cap);
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(100));
}
@Test
public void test() throws InterruptedException, IOException {
driver.activateApp("com.wdiodemoapp");
Thread.sleep(5000);
//Step 1: Capture the Screenshot
System.out.println("Step 1: Capture the Screenshot");
FileUtils.copyFile(driver.getScreenshotAs(OutputType.FILE), new File("actual_image"+".png"));
Thread.sleep(5000);
//Step 2: Covert the file to base64image
System.out.println("Step 2: Covert the file to base64image");
byte[] img1Bytes=Files.readAllBytes(new File(System.getProperty("user.dir")+"/actual_image.png").toPath());
byte[] img2Bytes=Files.readAllBytes(new File(System.getProperty("user.dir")+"/Expected_image.png").toPath());
byte[] base64Images1=Base64.getEncoder().encode(img1Bytes);
byte[] base64Images2=Base64.getEncoder().encode(img2Bytes);
//Step 3: Perform the Comparison
System.out.println("Step 3: Perform the Comparison");
//System.out.println(driver.getImagesSimilarity(base64Images1, base64Images2).getScore());
SimilarityMatchingResult result=driver.getImagesSimilarity(base64Images1, base64Images2);
//Step 4: Print the Result
System.out.println("Step 4: Print the Result");
System.out.println("Similarity Scrore of the Images - " +result.getScore());
}
@AfterTest
public void teardown() {
if(driver!=null) {
driver.quit();
System.out.println("Test Completed");
}
}
}Limitations
- Resolution Dependency: Differences in resolution or aspect ratio can affect similarity scores.
- Dynamic Elements: Screens with animations or time-dependent elements may not match exactly.
- False Positives/Negatives: Small rendering differences might result in low similarity scores even when the UI is acceptable.
Best Practices
- Always compare images taken under consistent conditions (device, resolution, theme).
- Use image masks to ignore irrelevant regions (e.g., timestamps or ads).
- Set a similarity threshold based on project tolerance (e.g., 0.95 or higher).
Conclusion
The driver.getImagesSimilarity(byte[] base64image1, byte[] base64image2) method is a powerful tool for visual validation in test automation. It simplifies image comparison by abstracting the complexity of visual similarity algorithms, letting you focus on building reliable tests.
GitHub Link:
🎬 Explore More! Watch My Latest Videos on YouTube!
No comments:
Post a Comment