Having test results as proof of testing is an essential thing. In this article, we will learn how to capture screenshots in UFT during automation script execution and insert them sequentially in a word document.UFT does have a method CaptureBitmap that can be used to capture the screenshot of the entire test application or any specific object of the test application and save it to the desired place. We will write two custom methods that will be used to capture screenshots at runtime and insert them into a word document in the same order they were captured.
Capture Screenshots In UFT
- CaptureAUTScreenShot
- CopyImagesToWord
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
Function CaptureAUTScreenShot ImageDir = "E:\UFT_WorkSPace\ImageDir\" Set BR = Browser("name:=.*mercury.*") If BR.Exist Then strTime = Split(Replace(Time,":","-")," ") ImageName = strTime(0) & " " & strTime(1) Set fso = CreateObject("Scripting.FileSystemObject") On Error Resume Next BR.CaptureBitmap ImageDir & ImageName & ".png" strCapImages = strCapImages & "," & ImageDir & ImageName & ".png" Set fso = Nothing If Err.Number > 0 Then Reporter.ReportEvent micFail,"Some error occured while capturing Screen shot","" End If On Error Goto 0 End If End Function |
The following function will insert captured images into the word document.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
Function CopyImagesToWord(WordFileName) Const MOVE_SELECTION = 0 Const END_OF_STORY = 6 strCapImages = MID(strCapImages,2) If strCapImages <> Empty Then arrStrCapImages = Split(strCapImages,",") End If Set fso = CreateObject("Scripting.FileSystemObject") If fso.FileExists(WordFileName) Then blnExistingFile = True Else blnExistingFile = False End If Set fso = Nothing Set objWord = CreateObject("Word.Application") If blnExistingFile = False Then Set objDoc = objWord.Documents.Add Else Set objDoc = objWord.Documents.Open(WordFileName) End If Set objSelection = objWord.Selection objSelection.EndKey END_OF_STORY,MOVE_SELECTION objSelection.TypeParagraph objSelection.Font.Name = "Verdana" objSelection.Font.Size = 12 objSelection.Font.Bold = True objSelection.ParagraphFormat.Alignment = wdAlignParagraphCenter objSelection.TypeText "Captured Screen Shots copied to word document on " & Now objSelection.TypeParagraph For intCnt = 0 to Ubound(arrStrCapImages) objSelection.EndKey END_OF_STORY,MOVE_SELECTION objSelection.TypeParagraph objSelection.Font.Name = "Verdana" objSelection.Font.Size = 12 objSelection.InlineShapes.AddPicture arrStrCapImages(intCnt),true objSelection.EndKey END_OF_STORY,MOVE_SELECTION objSelection.TypeParagraph If Err.number > 0 Then Reporter.ReportEvent micWarning,"Invalid Image file path: " & arrStrCapImages(intCnt) End If On error Goto 0 Next 'Saving the word document objSelection.WholeStory ObjDoc.SaveAs(WordFileName) objWord.Quit(wdSaveChanges) OutputToWord = True If Err.number > 0 Then Reporter.ReportEvent micFail,"Unable to Save word document","" End If On error Goto 0 arrStrCapImages = Null End Function |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
Set objBrowser = Createobject("InternetExplorer.Application") ObjBrowser.Visible = True objBrowser.Navigate "http://newtours.demoaut.com/" Browser("Welcome: Mercury Tours").Page("Welcome: Mercury Tours").WebEdit("userName").Set "TestUser1" Browser("Welcome: Mercury Tours").Page("Welcome: Mercury Tours").WebEdit("password").Set "Abcd1234" Call CaptureAUTScreenShot 'Capturing Screenhot of the login page Browser("Welcome: Mercury Tours").Page("Welcome: Mercury Tours").Image("Sign-In").Click If Browser("Welcome: Mercury Tours").Page("Find a Flight: Mercury").Exist Then Browser("Welcome: Mercury Tours").Page("Find a Flight: Mercury").WebList("passCount").Select "2" Browser("Welcome: Mercury Tours").Page("Find a Flight: Mercury").WebList("fromPort").Select "Frankfurt" Browser("Welcome: Mercury Tours").Page("Find a Flight: Mercury").WebList("fromDay").Select "5" Browser("Welcome: Mercury Tours").Page("Find a Flight: Mercury").WebList("toPort").Select "Paris" Browser("Welcome: Mercury Tours").Page("Find a Flight: Mercury").WebList("toDay").Select "10" Browser("Welcome: Mercury Tours").Page("Find a Flight: Mercury").WebList("airline").Select "Blue Skies Airlines" Call CaptureAUTScreenShot 'Capturing screenshot Browser("Welcome: Mercury Tours").Page("Find a Flight: Mercury").Image("findFlights").Click Call CaptureAUTScreenShot 'Capturing screenshot Browser("Welcome: Mercury Tours").Page("Select a Flight: Mercury").Image("reserveFlights").Click Call CaptureAUTScreenShot 'Capturing screenshot End If Call CopyImagesToWord("E:\UFT_WorkSpace\TestResults\MercuryTestResults.docx") 'Inserting images to word document Set objBrowser = Nothing |
Now you can run your test script and when the execution gets completed you can open the word document that was passed as a parameter in the CopyImagesToWord method. You can see all the captured screenshots have been inserted into the word document.
Recommended Posts
- Import an Excel File into Datatable in UFT
- How to Download and Install UFT One
- Automation Object Model in UFT One (AOM)
- Component Object Model in UFT | DOM | TOM & BOM
- How to Use LoadAndRunAction in UFT
- Difference between ChildObjects and ChildItem in UFT
- How to Record a Script in Chrome Browser in UFT
- How To Use Dictionary Object in UFT With Examples
- Find a Matching Pattern in String Using Regular Expression in UFT