How to take a screenshot of application using QTP?

qtp
20,182

Solution 1

Dim ScreenName

  On Error Resume Next

  ScreenName = " "
  
  CurrentTime = "_Test_Case"&"_"& Day(Now)&"_"& Month(Now)&"_"& Year(Now)&"_"&

   Hour(Now)&"_"& Minute(Now)&"_"& Second(Now)

  ScreenShotName = "Name_of_the_Screen" &  CurrentTime & ".png"

  ScreenName ="Path where the Screenshot needs to be stored"&"\"&ScreenShotName

  Desktop.CaptureBitmap ScreenName,True

This should work.

Solution 2

Your code is doing exactly what you are asking it to do - taking a screenshot of the desktop. If you want to take a screenshot of a specific application then just make sure the application is visible/top-most on the desktop.

If you need to do that in code you can try the Activate method on various top-level objects. For example:

Window("Notepad").Activate

If you have a Browser you need to activate, you could try something like:

hwnd = Browser("...").GetROProperty("hwnd")
Window("hwnd:=" & hwnd).Activate

...and then take a screenshot. Really though the application under test should already be in the foreground and active if QTP opened it and has been performing actions on it.

Share:
20,182
onkar
Author by

onkar

Updated on July 09, 2022

Comments

  • onkar
    onkar almost 2 years

    I want to take a screen shot of application when error occurs & save it in a specific folder. Below code works but it doesnt take a screen shot of application, it takes a screen shot of QTP Code screen.

    Function CaptureImage()
        Dim Date_Time
        Dim Myfile
        Date_Time=Now() 
        Myfile= Date_Time&".png"
        Myfile = Replace(Myfile,"/","-") 
        Myfile = Replace(Myfile,":","-")
        Myfile= "C:\"&Myfile 
        Desktop.CaptureBitmap Myfile, True
    End Function