Visual Testing Using Pre-Build Commands
  • 29 Mar 2023
  • 1 Minute to read
  • Dark
    Light

Visual Testing Using Pre-Build Commands

  • Dark
    Light

Article summary

Performing Visual Testing using Pre-build Command

Steps to get imageID:

  1. Import javaconnector, you can refer to this link to import

Note: Uploaded image should be in .png format

  1. Use the below mentioned function to get the ImageID of the uploaded image
Connector con = new Connector("CLOUD_URL");
//To get the Access key - Login to pCloudy platform->Go to Profile and click on Settings->Copy the access key
String authToken = con.authenticateUser("EMAIL_ID", "API_KEY");
File fileToBeUploaded = new File("LOCAL_IMAGE_PATH");
String baseImageId = con.getImageId(authToken, fileToBeUploaded);
String secondImageId = con.getImageId(authToken, fileToBeUploaded);

Using the above function user will get the ImageID of the Base image and second image (that is used for visual analysis)

Let's see how to use Pre-build commands for visual analysis

  1. To compare and find the difference between two images
mobile:visual:imageDiff :

Eg.

Map< String, Object> params = new HashMap<>();
//Declare Image ID of Base image
params.put("baseImageId", imageId);
//declare Image ID of second image
params.put("secondImageId", secondImageId);
//Find the difference between two image
String base64=(String) driver.executeScript("mobile:visual:imageDiff",params);
//Enter path
File imgFile = new File("C:\\Users\\Admin\\OneDrive\\Desktop\\diff.png");
BufferedImage img = ImageIO.read(new ByteArrayInputStream(org.apache.commons.codec.binary.Base64.decodeBase64(base64)));
ImageIO.write(img, "png", imgFile);
  1. To get all the text present in an image
:mobile:ocr: text

Eg.

Map< String, Object> params = new HashMap<>();
System.out.println(driver.executeScript("mobile:ocr:text",params));
  1. Command to verify if any word exists or not in an image
:mobile:ocr:textExists
Map< String, Object> params = new HashMap<>();
params.put("imageId", baseImageId);
params.put("word", "cleartrip");
System.out.println(driver.executeScript("mobile:ocr:textExists",params));
  1. Command to find the “coordinates” of a word
:mobile:ocr:coordinate

Eg.

Map< String, Object> params = new HashMap<>();
params.put("imageId", baseImageId);
params.put("word", "cleartrip");
System.out.println(driver.executeScript("mobile:ocr:coordinate",params));

Was this article helpful?