TestComplete Tips: Finding an Image on a Web Page
Web page tests often require image checks. You might want to verify that the page load was good, or you may need to search for an image and simulate a click or key press on it. TestComplete includes at least two ways to find and verify images.
One way to check if a web page contains an image is to search the web page structure for the IMG element whose SRC attribute contains the image file name. This method is fast and efficient as it just has to find the name in the correct place. To do the search, you use the
The following code snippet demonstrates the
Page.NativeWebObject.Find method. The sample script works with Internet Explorer 6. If you're testing another browser, you'll need to modify the lines responsible for obtaining the
The string that's passed to the
Page.NativeWebPage.Find method contains the file name of the image we want to find. The * wildcard character at the beginning of the string makes the script match any file path. You'll want to include the image file path if the image name is not unique. If the image is found, it is clicked. If the image is not found, a warning is logged. This method works well unless you want to verify that the image is not just the correct file name, but also the correct appearance.
Another way to find the the image is to use the image-search capabilities in TestComplete. This method does an pixel by pixel search of the browser window to verify that the exact image is present on-screen. Every window control in TestComplete has a
Picture object property which gives you access to window image bitmap as it appears on-screen. To locate the image you're looking for, you get the
Picture object of the web browser window and then use the
Find method of this object to search for the desired image within it. TestComplete will verify that the exact image is found, or not found. The following code demonstrates this:
Picture.Find method returns the box coordinates of the found image within the bitmap image of the web browser window. We then use the
Sys.ObjectFromPoint method to get an object corresponding to the found image. This method returns the TestComplete object with
Click and other actions we use to simulate mouse clicks on the image.
Is one method better than another? Not really, both methods are equally valid depending on the test you're building. Just keep in mind that finding the image file name is always faster than finding the image pixels, weigh the cost and use the appropriate method.