|
Problem ID: 22666
--------------------------------------------------------------------------------
Product:
QTP Version N/A
Ext(s):
Add-in Not Applicable
Topics:
File Operations
OS:
Any Windows Platform
Creation Date:
30 Jan 2003 Last Modified Date:
15 Dec 2004
--------------------------------------------------------------------------------
Problem Description: How to compare two files
--------------------------------------------------------------------------------
Solution: Use the external FileCompare function
Note:
Solutions 1, 2, and 4 compare the files but do not save the differences to a file. If you need to save the differences to a file, the Solutions 3, 5, and 6 are probably the best ones to use.
Solution 1:
1. Unzip the attached DLL file to your desired location.
2. Declare the external FileCompare function using the following format:
Extern.Declare micInteger, "FileCompare", "<path to DLL>", "FileCompare", micString, micString
where <path to DLL> is the full path to the DLL.
3. Call the function.
Note:
This function is not part of QuickTest Professional. It is not guaranteed to work and is not supported by Mercury Customer Support.
Extern.FileCompare (file1, file2)
file1 The full path to the first file.
file2 The full path to the second file.
Return values:
0 The files are not the same.
1 The files are the same.
2 The first file could not be opened.
3 The second file could not be opened.
Example:
Extern.Declare micInteger, "FileCompare", "c:\temp\FileCompare.dll", "FileCompare", micString, micString
rc = Extern.FileCompare ("c:\file2.txt","c:\file1.txt")
MsgBox rc
You can check the return code of this function and use report event to report to the test results.
Note:
Since the return value from the function is not a string, you may need to use CStr to cast the value to a string before you compare it or write it to the test results.
rCompare = Extern.FileCompare(sFile1,sFile2)
rc=CStr(rCompare)
Solution 2:
You can also code this functionality using VB Script methods. For example, you can use the ReadAll method to retrieve all the characters from each file and compare them.
Example:
Public Function CompareFiles(Path1, Path2)
Dim fso,f1,f2,ts1,ts2
Set fso = CreateObject("Scripting.FileSystemObject")
Set f1 = fso.GetFile(Path1)
Set f2 = fso.GetFile(Path2)
Set ts1 = f1.OpenAsTextStream()
Set ts2 = f2.OpenAsTextStream()
If ts1.ReadAll = ts2.ReadAll Then
CompareFiles = "Passed"
Else
CompareFiles = "Failed"
End If
End Function
msgbox CompareFiles ("D:\temp\hello.txt", "D:\temp\hola.txt")
Please refer to the QuickTest Professional Help files (Help -> QuickTest Professional Help) for detailed information on the ReadAll method and other VB Script methods.
Solution 3:
Execute the fc (file compare) DOS command.
For information on calling a DOS command from a script, please refer to
Problem ID 17091 - How to execute DOS commands from within an AQT/QTP script
Example:
Dim oShell
Set oShell = CreateObject ("WScript.Shell")
file1 = "c:\temp\file_a.txt"
file2 = "c:\temp\file_b.txt"
diff_file = "c:\temp\diff.txt"
' Compare the files
oShell.run "cmd /C fc " + file1 + " " + file2 + " > " + diff_file
Set oShell = Nothing
Solution 4:
The wdiff (windiff) application can be found in the <QuickTest Professional>\bin directory. You can launch the utility from within the script. Use a DOS command or the SystemUtil.Run method.
For more information on using SystemUtil.Run, please refer to
Problem ID 24728 - How to launch an application from within the script
Note: Both example below will launch wdiff, but the differences file is not saved and the utility is not closed.
EXAMPLE using DOS command:
Dim oShell
Set oShell = CreateObject ("WScript.Shell")
file1 = "c:\temp\file_a.txt"
file2 = "c:\temp\file_b.txt"
appdir = chr(34) + Environment("ProductDir") + "\bin\wdiff" + chr(34)
' Compare the files
oShell.run "cmd /C " + appdir + " " + file1 + " " + file2
Set oShell = Nothing
EXAMPLE using SystemUtil.Run:
file1 = "c:\temp\file_a.txt"
file2 = "c:\temp\file_b.txt"
appdir = chr(34) + Environment("ProductDir") + "\bin\wdiff" + chr(34)
SystemUtil.Run appdir, file1 + " " + file2, Environment("ProductDir"), "open"
Solution 5:
Use the custom file_compare function.
Note:
This function is not part of QuickTest Professional. It is not guaranteed to work and is not supported by Mercury Customer Support.
file_compare(file1, file2, diff)
file1 The first file to be compared.
file2 The second file to be compared.
diff The file containing the differences, if any, between the compared files.
function file_compare(file1, file2, diff)
Dim rc, tmpfile, contents1, contents2, com, oShell, cmpare
SystemUtil.run "cmd", "", "C:\", ""
com = "fc "& chr(34) & file1 & chr(34) & " " & chr(34) & file2 & chr(34) & " > " & chr(34) & diff & chr(34)
Set oShell = CreateObject ("WSCript.shell")
oShell.run "cmd /C " & com
Set oShell = Nothing
wait(2) ' this is needed for the above line to be completed.
set filesys = CreateObject("Scripting.FileSystemObject")
set readfile = filesys.OpenTextFile(diff, 1, false)
contents1 = readfile.ReadLine
contents2 = readfile.ReadLine
set filesys = Nothing
set readfile = Nothing
cmpare = StrComp("FC: no differences encountered", contents2, 1)
if cmpare = 0 then
Reporter.ReportEvent 0 , "File Compare","File: " & file1 & " matches File: " & file2
rc=0
else
Reporter.ReportEvent 1 , "File Compare", "Mismatch detected between File: " & file1 & " and File: " & file2 & ", See output file: "&diff
rc=-1
End If
file_compare=rc
End Function
Example:
' Calling the function
msgbox file_compare("C:\file1.txt", "C:\file2.txt", "C:\diff.txt")
Solution 6:
Use the custom FileCompare function.
Note:
This function is not part of QuickTest Professional. It is not guaranteed to work and is not supported by Mercury Customer Support. You are responsible for any and all modifications that may be required.
This function uses the FileSystemObject methods to compare the files line by line. If the lines do not match, the line number and differences will be captured and reported to the test results. The function will also specify whether one file is longer than the other.
FileCompare(file1, file2)
file1 The first file to be compared.
file2 The second file to be compared.
Function FileCompare (pfile1, pfile2)
' Create a reference to the FileSystemObject
Set fso = CreateObject("Scripting.FileSystemObject")
' Open both files
Set file1 = fso.OpenTextFile(pfile1)
Set file2 = fso.OpenTextFile(pfile2)
' Set a variable to keep track of what line number you are on.
lineNO=0
' Variable used to tell you when QTP finds a line mistmatch.
mismatchFound=false
mismatch = ""
' Keep looping until the end of either file is reached
' Note: If you want to stop at the first mismatch, uncomment the relevant portion of the While condition.
While (Not file2.AtEndofStream) AND (Not file1.AtEndOfStream ) 'AND (Not mismatchFound)
' Increment the line number variable.
lineNO=lineNO+1
' Read a line from each file
line1= file1.ReadLine
line2= file2.ReadLine
' Compare the 2 lines. If they don't match, set the mismatchFound variable to true. Setting it to true will exit the loop
If line1 <> line2 Then
mismatchFound=true
' Write the mismatch information to the test results
mismatch = mismatch & "Line Number: " & lineNO & vbNewLine & "File1: " & line1 & vbNewLine & "File2: " & line2 & vbNewLine & vbNewLine
End If
Wend
errorreported = false
' If a mismatch is found or if either file is has not reached its end, then we know a mistmach occured
If mismatchFound Then
Reporter.ReportEvent micFail, "File Mismatch detected", "The files lines did not match: " & vbNewLine & mismatch
errorreported = true
End if
If (Not file1.AtEndofStream) then
Reporter.ReportEvent micFail, "File Mismatch detected", "The first file, " & pfile1 & ", is longer than the second file, " & pfile2
errorreported = true
end if
if (Not file2.AtEndofStream) then
Reporter.ReportEvent micFail, "File Mismatch detected", "The second file, " & pfile2 & ", is longer than the first file, " & pfile1
errorreported = true
end if
If (Not errorreported) Then
Reporter.ReportEvent micPass, "File Comparison", "The files match"
End If
file1.Close
file2.Close
Set fso = Nothing
End Function
Example:
FileCompare "C:\temp\file1.txt", "C:\temp\file2.txt" |
|