50.5 The Image API

The Image API provides classes and interfaces to take screenshots, to save or load images or for own image comparisons. The image API is designed so that the different methods in general do not throw any exception. Instead, the different methods are logging warnings.

50.5.1 The ImageWrapper class

For taking screenshots you can use the Jython class ImageWrapper, located in the module imagewrapper.py, which comes with the QF-Test installation.

Here is a short sample Jython script demonstrating the usage of the Image API:

from imagewrapper import ImageWrapper
            #create ImageWrapper instance
            iw = ImageWrapper(rc)

            #take screenshot of the whole screen
            currentScreenshot = iw.grabScreenshot()

            #save screenshot to a file
            iw.savePng("/tmp/screenshot.png", currentScreenshot)
Example 50.7:  Image API in Jython

And the same in Groovy:

import de.qfs.ImageWrapper

            def iw = new ImageWrapper(rc)
            def currentScreenshot = iw.grabScreenshot()
            iw.savePng("/tmp/screenshot.png", currentScreenshot)
Example 50.8:  Image API in Groovy

Following is a list of the methods of the ImageWrapper class in alphabetical order. The syntax used is a bit of a mixture of Java and Python. Python doesn't support static typing, but the parameters are passed on to Java, so they must be of the correct type to avoid triggering exceptions. If a parameter is followed by an '=' character and a value, that value is the default and the parameter is optional.

 
 
ImageWrapper ImageWrapper(RunContext rc)

Constructor method of the ImageWrapper class.

Parameters
rc The current run context of QF-Test.
 
int getMonitorCount()

Return the number of monitors.

ReturnsThe total number of monitors.
 
ImageRep grabImage(Object com, int x=None, int y=None, int width=None, int height=None)

Take screenshot of a given component. If you use the parameters x, y, width and height, you can take a screenshot of a specific region of the component.

Parameters
com The QF-Test ID of the component to take a screenshot from.
x The X coordinate of the left upper corner of the region to take the screenshot.
y The Y coordinate of the left upper corner of the region to take the screenshot.
width The width of the region to take the screenshot.
height The height of the region to take the screenshot.
ReturnsAn ImageRep object containing the actual screenshot.
 
ImageRep grabScreenshot(int x=None, int y=None, int width=None, int height=None)

Take screenshot of the whole screen. If you use the parameters x, y, width and height, you can take a screenshot of a specific region of the screen.

Parameters
x The X coordinate of the left upper corner of the region to take the screenshot.
y The Y coordinate of the left upper corner of the region to take the screenshot.
width The width of the region to take the screenshot.
height The height of the region to take the screenshot.
ReturnsAn ImageRep object containing the actual screenshot.
 
ImageRep grabScreenshotOfAllScreens()

Creates a combined screenshot of all available screens.

ReturnsAn ImageRep object
 
ImageRep[] grabScreenshots(int monitor=None)

Take screenshots of all available screens. This method might be useful, if you work with more than one screen.

If you want to take a screenshot of one specific screen, you can also use this method.

Parameters
monitor Index of the monitor to take the screenshot from. The first monitor has 0, the second 1 and so on.
ReturnsAn array of ImageRep objects of all screenshots or the specific ImageRep object, if the monitor parameter has been used.
 
ImageRep loadPng(String filename)

Load an image from a given file return an ImageRep object containing this image. The file has to contain the image in PNG format.

Parameters
filename The path to the file, where the image is stored.
ReturnsAn ImageRep object containing loaded image.
 
void savePng(String filename, ImageRep image)

Save the given ImageRep object to a file. The file will be in PNG format.

Parameters
filename The path to the file, where the image should be stored to.
image The ImageRep object to store.