By Anish Pillai Anish Pillai Posted under QTP Concepts

How to capture application screenshot and display it in test results

1 Flares Twitter 0 Facebook 0 Google+ 1 LinkedIn 0 Email -- 1 Flares ×

A common feature that QTP testers are often asked to implement is to capture the application screenshot every time an error occurs during test run. This is definitely a good to have feature in your automation framework because –

a) When a script fails, you would have a proof that it failed due to an application issue and not due to your script, and

b) with the error screenshot, the developers usually find it easier to identify/fix the issue.

Capture Screenshot in QTP

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

1. You cannot load images from Quality Center.
2. If you include large images in the run results, it may impact performance.
3. If you specify an image as a relative path, QTP will first search the Results folder for the image and then the search paths specified in the Folders pane of the Options dialog box.

If you enjoyed this article, you can join our blog to get free email updates directly in your inbox.


1 Flares Twitter 0 Facebook 0 Google+ 1 LinkedIn 0 Email -- 1 Flares ×
  • Spoonwizard

    I have a simple script that iterates using data from an excel file and every time a specific error appears it takes a screen shot. Does anyone know the code I would use to make the name of the screen shot change based on the iteration the script is in?

    Thanks in advance,

1 Flares Twitter 0 Facebook 0 Google+ 1 LinkedIn 0 Email -- 1 Flares ×