ImageUtils code samples (Java SDK)

Verify that an image exists

This sample shows how to locate an image in a browser window using the VerifyImageExists method.

The sample assumes that there is an image named ImageToFind_In_Browser.bmp at the following path: C:\Images1

@Test
public void testObjectVerifyImageMatchExample() throws GeneralLeanFtException, IOException {
	// Launch the Chrome browser.
	Browser browser = BrowserFactory.launch(BrowserType.CHROME);

	// Load an image to search for in the browser window.
	RenderedImage expectedImage = javax.imageio.ImageIO.read(new File("C:\\Images1\\Expected_Browser_Image.bmp"));

	// Compare the image with a snapshot of the browser window.
	// Use the default pixel tolerance and RGB tolerance values.
	boolean imagesMatch = browser.verifyImageMatch(expectedImage);
	Verify.isTrue(imagesMatch, "Compare Browser Images", "Compare the browser's snapshot image to the current browser.");

	// Compare the image with a snapshot of the browser window.
	// Use a pixel tolerance and RGB tolerance of up to a 10 percent difference.
	imagesMatch = browser.verifyImageMatch(expectedImage, (byte)10, (byte)10);
	Verify.isTrue(imagesMatch, "Compare Browser Images", "Compare the browser's snapshot image to the current browser using a pixel tolerance and RGB tolerance of up to a 10 percent difference.");

	// Prepare a mask area for the comparison described below.
	Rectangle IncludeRectangle = new Rectangle(0, 0, 200, 200);
	Rectangle ExcludeRectangle = new Rectangle(100, 100, 10, 10);
	ImageMaskArea maskArea = new ImageMaskArea(IncludeRectangle, ExcludeRectangle);

	// Compare the image with a snapshot of the browser window.
	// Use the default pixel tolerance and RGB tolerance values.
	// Use a mask area that:
	// - Includes all of the area in the rectangle located at 0,0,200,200 in the browser window.
	// - Excludes the area in the rectangle located at 100,100,10,10 in the browser window.
	imagesMatch = browser.verifyImageMatch(expectedImage, maskArea);
	Verify.isTrue(imagesMatch, "Compare Browser Images", "Compare the browser's snapshot image to the current browser using a mask image.");

	// Close the browser.
	browser.close();
}

Back to top

Verify that an image matches a test object image

This sample shows how to compare an image to a test object's image - in this case, a browser window - using the VerifyImageMatch method.

The sample assumes that there is an image named Expected_Browser_Image.bmp at the following path: C:\Images1

@Test
public void testObjectVerifyImageMatchExample() throws GeneralLeanFtException, IOException {
	// Launch the Chrome browser.
	Browser browser = BrowserFactory.launch(BrowserType.CHROME);

	// Load an image to search for in the browser window.
	RenderedImage expectedImage = javax.imageio.ImageIO.read(new File("C:\\Images1\\Expected_Browser_Image.bmp"));

	// Compare the image with a snapshot of the browser window.
	// Use the default pixel tolerance and RGB tolerance values.
	boolean imagesMatch = browser.verifyImageMatch(expectedImage);
	Verify.isTrue(imagesMatch, "Compare Browser Images", "Compare the browser's snapshot image to the current browser.");

	// Compare the image with a snapshot of the browser window.
	// Use a pixel tolerance and RGB tolerance of up to a 10 percent difference.
	imagesMatch = browser.verifyImageMatch(expectedImage, (byte)10, (byte)10);
	Verify.isTrue(imagesMatch, "Compare Browser Images", "Compare the browser's snapshot image to the current browser using a pixel tolerance and RGB tolerance of up to a 10 percent difference.");

	// Prepare a mask area for the comparison described below.
	Rectangle IncludeRectangle = new Rectangle(0, 0, 200, 200);
	Rectangle ExcludeRectangle = new Rectangle(100, 100, 10, 10);
	ImageMaskArea maskArea = new ImageMaskArea(IncludeRectangle, ExcludeRectangle);

	// Compare the image with a snapshot of the browser window.
	// Use the default pixel tolerance and RGB tolerance values.
	// Use a mask area that:
	// - Includes all of the area in the rectangle located at 0,0,200,200 in the browser window.
	// - Excludes the area in the rectangle located at 100,100,10,10 in the browser window.
	imagesMatch = browser.verifyImageMatch(expectedImage, maskArea);
	Verify.isTrue(imagesMatch, "Compare Browser Images", "Compare the browser's snapshot image to the current browser using a mask image.");

	// Close the browser.
	browser.close();
}

Back to top

Search for an image inside another Iimage

This example shows how to use the ImageUtils' class LocateImage to search for one image inside another image.

The sample assumes:

  • There is an image named ImageToSearchIn.bmp at the following path: C:\Images1. This is the containing image.
  • There is an image named ImageToSearchFor.bmp at the following path: C:\Images1. This is the image we are searching for.
[Test]
public void ImageSearchExample()
{
	var imageToSearchIn = new Bitmap(@"C:\Images1\ImageToSearchIn.bmp");
	var imageToSearchFor = new Bitmap(@"C:\Images1\ImageToSearchFor.bmp");
	var imageFoundAtPoint = ImageUtils.LocateImage(imageToSearchIn, imageToSearchFor, 90);
	Assert.NotNull(imageFoundAtPoint);
}

Back to top

Compare two images

This example shows how to use the ImageUtils' class LocateCompareImages to compare two images.

The comparison is done with a pixel tolerance of 10, and an RGB tolerance of 20. This means that:

  • Up to 10 percent of the pixels might be different
  • Up to 20 percent of the RBG values of the pixels might be different.

This example assumes that the images are located at the following path: C:\Images1

@Test
	public void imageComparisonExample() throws GeneralLeanFtException, IOException {
	RenderedImage image1 = javax.imageio.ImageIO.read(new File("C:\\Images1\\Image1.bmp"));
	RenderedImage image2 = javax.imageio.ImageIO.read(new File("C:\\Images1\\Image2.bmp"));
	boolean imagesMatch = ImageUtils.compareImages(image1, image2, (byte)10, (byte)20);
	Verify.isTrue(imagesMatch, "ImageComparisonExample", "Verify images match using pixel tolerance and RGB tolerance values.");
}

Back to top