Share this page

# ImageUtils code samples (.NET SDK)

In this topic:

## 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 TestObjectVerifyImageExistsExample()
    {
    	// Launch the Chrome browser.
    	var browser = BrowserFactory.Launch(BrowserType.Chrome);
    	// Pause to ensure that the browser has fully opened.
    	Thread.Sleep(4 * 1000);

    	// Load the image to search for in the browser window.
    	Image imageToFind = new Bitmap(@"C:\Images1\ImageToFind_In_Browser.bmp");

    	// Try to locate the image with a default similarity value of 100 percent.
    	// This means that no pixel difference should exist between the expected image and the actual image.
    	Point? point = browser.VerifyImageExists(imageToFind);
    	Assert.NotNull(point);

    	// Try to locate the image with a similarity value of 90 percent.
    	// This means that at least 90 percent of the pixels should match between the expected image and the actual image.
    	point = browser.VerifyImageExists(imageToFind, 90);
    	Assert.NotNull(point);

    	// 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()
    {
    	// Launch the Chrome browser.
    	var browser = BrowserFactory.Launch(BrowserType.Chrome);
    	// Pause to ensure that the browser has fully opened on the computer.
    	Thread.Sleep(4 * 1000);

    	// Load an image to search for in the browser window.
    	Image expectedImage = new Bitmap(@"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.
    	var 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, 10, 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.
    	var maskArea = new ImageMaskArea
    	{
    		IncludeRectangle = new Rectangle(0, 0, 200, 200),
    		ExcludeRectangle = new Rectangle(100, 100, 10, 10)
    	};
    	// 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()
    	{
    		var image1 = new Bitmap(@"C:\Images1\Image1.bmp");
    		var image2 = new Bitmap(@"C:\Images1\Image2.bmp");
    		var imagesMatch = ImageUtils.CompareImages(image1, image2, 10, 20);
    		Verify.IsTrue(imagesMatch, "ImageComparisonExample", "Verify images match using pixel tolerance and RGB tolerance values.");
    }

Back to top

https://admhelp.microfocus.com Functional Testing for Developers 8942
Tips to improve your search results... OpenText™ Functional Testing for Developers Help Center OpenText™ Functional Testing for Developers Help 25.2 All View Local Help Center View Online Help Center Version: