Can QTP do file compare?

Question ID: 105018

I’m trying to work with some files, and I don’t see any built-in way to do a file compare.

Marked as spam
Posted by (Questions: 175, Answers: 22)
Asked on May 21, 2013 3:09 pm
Answers (1)
Private answer

You can try using the Mercury.FileCompare undocumented object in QTP. Please look at the following code examples, and you should be able to see how you can use it. I've provided two examples. One deals with text files, and the other uses bitmaps.

'This code demonstrates how to use the Mercury.FileCompare tool

' Example of comparing two images captured during run-time against each other. In this case, we are using Notepad

'Capture our first bitmap image
Window(''Notepad'').WinEditor(''Edit'').CaptureBitmap ''c:notepad_before.bmp'',True

'Add some text to notepad, and capture another bitmap
Window(''Notepad'').WinEditor(''Edit'').SetCaretPos 0,0
Window(''Notepad'').WinEditor(''Edit'').Type ''This is some text''
Window(''Notepad'').WinEditor(''Edit'').CaptureBitmap ''c:notepad_after.bmp'',True

'Assign our two images to variables for comparison
a = ''C:notepad_before.bmp''
b = ''C:notepad_after.bmp''

Set objCompareUtil = CreateObject(''Mercury.FileCompare'') 'Instantiate the Mercury.FileCompare object
If objCompareUtil.IsEqualBin(a,b,0,1) Then 'Compare the two images, and report result. We should expect a failure since the images SHOULD be different

Reporter.ReportEvent micPass, ''Bitmap Check'', ''The bitmap file: ''&a & '' matched bitmap file:''& b
Reporter.ReportEvent micFail, ''Bitmap Check'', ''The bitmap file: ''&a & '' did not match bitmap file:''& b
End If


' Example of comparing two text files

a = ''C:File1.txt'' 'Define text file 1
b = ''C:File2.txt'' 'Define text file 2

Set objCompareUtil = CreateObject(''Mercury.FileCompare'') ' Instantiate the Mercuy.FileCompare object
If objCompareUtil.IsEqualBin(a,b,0,1) Then 'Compare the two files and report pass or fail based on the conditional result

Reporter.ReportEvent micPass, ''Text File Check'', ''The file: ''&a & '' matched file:''& b


Reporter.ReportEvent micFail, ''Text File Check'', ''The file: ''&a & '' did not match file:''& b

End If

Marked as spam
Posted by (Questions: 2, Answers: 425)
Answered on May 21, 2013 3:10 pm