# Visual Testing Using Pre-Build Commands

Last updated on : 21 Jan 2025

## 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

2. Use the below mentioned function to get the ImageID of the uploaded image

```javascript title="Json"
	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

```javascript title="Json"
	mobile:visual:imageDiff :
```

Eg.

```javascript title="Json"
	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);
```

2. To get all the text present in an image

```javascript title="Json"
	:mobile:ocr: text
```

Eg.

```javascript title="Json"
	Map< String, Object> params = new HashMap<>();
System.out.println(driver.executeScript("mobile:ocr:text",params));
```

3. Command to verify if any word exists or not in an image

```javascript title="Json"
	:mobile:ocr:textExists
```

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

4. Command to find the “coordinates” of a word

```javascript title="Json"
	:mobile:ocr:coordinate
```

Eg.

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