17 Jan 2013

How to display the application screenshot in the test run results window

How to capture application screenshot

QTP provides a method called CaptureBitmap that can capture the image of either an object within the application or the entire screen/desktop and save it in any desired location. The image is saved as .png or .bmp file depending upon the file extension provided during the function call. The object is captured as it appears in the application when the method is performed. If only a part of the object is visible, then only the visible part is captured. Let’s see some examples on how this functionality can be used -

Example 1: Capture the image of a WebTable object and save it in the test’s run result folder (relative path).

Function fnCaptureBitmap1
Browser("brBrowser").Page("pgPage").WebTable("tblResults").CaptureBitmap "webtable.bmp"
End Function
In the above function since the file name path is relative, the screenshot is saved in the test result folder. To save the image in some specific folder, the user needs to provide the complete/absolute path of the folder.

Example 2: Capture the Browser image and save it in a specified folder (absolute path).

Function fnCaptureBitmap2
Browser("brBrowser").CaptureBitmap "D:/CaptureScreenShotDemo/generic_image.png"
End Function
With this method the image gets stored in a specific location. Let’s suppose that there is already an image with the same name in the folder. With the above code, QTP will not overwrite the already existing image. To overwrite an already existing file, we need to set the OverrideExisting argument to True. Let’s see how this can be done.

Example 3: Capture Desktop Screenshot and save it in a specific folder (overwriting an image with same name, if any)

Function fnCaptureBitmap3
Browser("brBrowser").CaptureBitmap "D:/CaptureScreenShotDemo/generic_image.png", True
End Function
In this case, if an image with name generic_image.png already exists in the CaptureScreenShotDemo folder, running the above code will overwrite the previous image.

How to display the application screenshot in the test run results

Reporter.ReportEvent method of QTP provides an optional argument ImageFilePath, that can be used to display the screenshot in the QTP Test Results. Let’s see how this can be done.

Example 4: Capture Desktop Screenshot and display the image in the test run results

Function fnDisplayBitmapInTestResults
Browser("brBrowser").CaptureBitmap "D:/CaptureScreenShotDemo/image.png", True
Reporter.ReportEvent micDone, "Image", "Image", "D:/CaptureScreenShotDemo/image.png"
End Function

No comments:

Post a Comment