QTP/UFT tutorial for beginners – adding screenshots to our functions

Hi. So today, I will show you how to take screenshots with your functions for both passing and failing conditions. Screenshots for automated tests are helpful in determining that your script is actually running as designed. These screenshots are also very useful for documenting failures. Let's get started.

Take a Picture

I created an object above. We will create a simple function to click on this object. If the function clicks, we will get a passing condition as well as a screenshot.

1. Create a simple function to click on the link above.

 Function funClickLink(pstrLinkName)
      Browser("Index:=1").Page("title:=.*").link("name:="& pstrLinkName).Click
 End Function

For this example, I am using one unique property value to click on the link.

2. Add a pass/fail conditional statement for synchronization, and for reporting purposes.

 Function funClickLink(pstrLinkName)
     If Browser("Index:=0").Page("title:=.*").link("name:="& pstrLinkName).Exist(120) Then
      Browser("Index:=1").Page("title:=.*").link("name:="& pstrLinkName).Click
      Reporter.ReportEvent micPass, pstrLinkName, "Link is clicked on the web page"
      Else
      Reporter.ReportEvent micFail, pstrLinkName, "Link is not found on the web page"
      End If
 End Function

Our condition states, look for the specified link for a maximum of 120 seconds, if you find the link, then click on it and print a pass on the report. If the link is not found within 120 seconds, then print a fail on the report.

3. Lastly, let's add the screenshots. I am pulling my screenshot function which I created in a previous tutorial. Click below to go through the tutorial.

Capture Screenshots

  Function funCaptureScreenshot(pScreenshotName)

     Browser("Index:=.*").Page("title:=.*").Sync
     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

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

We will be calling this capture screenshot function within the click link function we just created.

 Function funClickLink(pstrLinkName)
If Browser("Index:=0").Page("title:=.*").link("name:="& pstrLinkName).Exist(120) Then
Browser("Index:=0").Page("title:=.*").link("name:="& pstrLinkName).Click
funCaptureScreenshot pstrLinkName
Reporter.ReportEvent micPass, pstrLinkName, "Link is clicked on the web page" & " <" & funCaptureScreenshot(pstrLinkName) & ">"
Else
funCaptureScreenshot pstrLinkName
Reporter.ReportEvent micFail, pstrLinkName, "Link is not found on the web page" & " <" & funCaptureScreenshot(pstrLinkName) & ">"
End If
End Function

I am calling the capture screenshot function funCaptureScreenshot pstrLinkName with the link name that I am passing for my click link function. I am also passing the image directory path within our reporting details so that we can see where the image is stored.

Now let's run the full code and see what happens!

run the full code below--------------------------------------------------------------

funClickLink "Take a Picture"

Function funClickLink(pstrLinkName)
If Browser("Index:=1").Page("title:=.*").link("name:="& pstrLinkName).Exist(120) Then
Browser("Index:=1").Page("title:=.*").link("name:="& pstrLinkName).Click
funCaptureScreenshot pstrLinkName
Reporter.ReportEvent micPass, pstrLinkName, "Link is clicked on the web page" & " <" & funCaptureScreenshot(pstrLinkName) & ">"
Else
funCaptureScreenshot pstrLinkName
Reporter.ReportEvent micFail, pstrLinkName, "Link is not found on the web page" & " <" & funCaptureScreenshot(pstrLinkName) & ">"
End If
End Function

Function funCaptureScreenshot(pScreenshotName)
Browser("Index:=.*").Page("title:=.*").Sync
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

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

-------------------------------------------------------------------------------------------------------

Click on the results and you should see the run passed with the screenshot name and directory printed within the details section.

Go to that directory and you should see the screenshot.

 

Open the screenshot and you should see an image of the destination page that the link was supposed to take you to.

 

Pretty awesome right? Please leave all comments and questions below. I appreciate all of your feedback.

 

Pin It on Pinterest

Clef two-factor authentication