QTP/UFT tutorial for beginners – capturing and saving screenshots dynamically using FSO(File System Object)

Good morning soon to be Gurus of QA automation. Today, you will quickly learn how to capture screenshots and save them dynamically. Let's jump right in!

1. To capture a screenshot in qtp/uft, you have to call Desktop.CaptureBitmap "StaticFilePathName", True

The problem with the code above is, we are using a static path. This means that this line of code can run only on the computer that has the same folder structure. Since we are automation gurus, we want to make this dynamic and never worry about where we run our scripts.

2. Let's create a function that we can use to capture an image.

Function funCaptureScreenshot()
    
End Function

3. Within this function, we will use QTP/UFT's folder structure environmental variable. This variable automatically gets the folder structure that your script is in.

Function funCaptureScreenshot()
    strScreenshotsFolder = Environment("TestDir")
    msgBox strScreenshotsFolder
End Function

I set the value of the environmental variable to  strScreenshotsFolder and used MsgBox to print it's value.

4. Now that we are dynamically pulling the script path, we need to create a directory that we can use to store our screenshots. For this, we will need to use the FSO(File System Object). This object accesses your directories and lets you modify them within your script.

       Set fso = CreateObject("Scripting.FileSystemObject")
        If Not fso.FolderExists(strScreenshotsFolder & "\Screenshots") Then 
            Set f = fso.GetFolder(strScreenshotsFolder)
            Set fc = f.SubFolders
            fc.Add("Screenshots")
        End If

  • First, we access the File System Object.
  • Then we set a condition. If a specific folder doesn't exist, then create it. That way we don't have an issue if that folder already exists, and we are trying to overwrite it by accident using our script.
  • We set f = to the dynamic path we created strScreenshotsFolder
  • then we set fc to access the folders within this path
  • lastly, we create our folder using fc.Add and we name the folder Screenshots

5. Now let's create another variable containing our new path along with the screenshot name.

strScreenshotName = strScreenshotsFolder & "\Screenshots\" & pScreenshotName & ".png"

I grabbed our new path folder path for the screenshots and created a parameter that we will be using to name our screenshot. I set this path to a new variable strScreenshotName.

6. Lastly, let's use the code we called in the beginning, Desktop.CaptureBitmap "StaticFilePathName", True and replace it with our new path Desktop.CaptureBitmap strScreenshotName, True    

Now if we call our function, you can see that QTP/UFT takes a screen shot and places it in the directory we created. See our full function below.

funCaptureScreenshot "so cool"

Function funCaptureScreenshot(pScreenshotName)
strScreenshotsFolder = Environment("TestDir")

        Set fso = CreateObject("Scripting.FileSystemObject")
        If Not fso.FolderExists(strScreenshotsFolder & "\Screenshots") Then 
            Set f = fso.GetFolder(strScreenshotsFolder)
            Set fc = f.SubFolders
            fc.Add("Screenshots")
        End If

strScreenshotName = strScreenshotsFolder & "\Screenshots\" & pScreenshotName & ".png"
Desktop.CaptureBitmap strScreenshotName, True    
End Function

Look in your directory and you will see the "so cool" screenshot. This tutorial is meant to help you understand how QTP/UFT works with certain objects and methods to make your life as an automation tester much easier. Please leave all of your comments and questions below.

Pin It on Pinterest

Clef two-factor authentication