51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

手机号码,快捷登录

查看: 2803|回复: 4
打印 上一主题 下一主题

[原创] 请问QTP里有没有文本文件比较的模块?

[复制链接]

该用户从未签到

跳转到指定楼层
1#
发表于 2006-7-11 10:06:37 | 只看该作者 回帖奖励 |正序浏览 |阅读模式
类似于WinMerge界样滴工具,可以比较出一个文本文件和另外一个那一行不一样,或者那一行缺失等
谢谢
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏
回复

使用道具 举报

该用户从未签到

5#
发表于 2006-7-12 17:52:36 | 只看该作者
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"

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?(注-册)加入51Testing

x
回复 支持 反对

使用道具 举报

该用户从未签到

4#
 楼主| 发表于 2006-7-11 10:59:26 | 只看该作者

谢谢版主:)

我试下,希望不会有什么特殊情况发生
回复 支持 反对

使用道具 举报

该用户从未签到

3#
发表于 2006-7-11 10:50:59 | 只看该作者
你可以这样,把两个文本分别一行行读出来,放在两个变量中,然后用strcomp函数来比较是否相等.
这个函数的用法:
StrComp Function
Requirements
Version 1
Returns a value indicating the result of a string comparison.

StrComp(string1, string2[, compare])
Arguments
string1
Required. Any valid string expression.
string2
Required. Any valid string expression.
compare
Optional. Numeric value indicating the kind of comparison to use when evaluating strings. If omitted, a binary comparison is performed. See Settings section for values.
Settings
The compare argument can have the following values:

Constant Value Description
vbBinaryCompare      0                     Perform a binary comparison.
vbTextCompare         1                        Perform a textual comparison.

Return Values
The StrComp function has the following return values:

If StrComp returns
string1 is less than string2                -1
string1 is equal to string2                  0
string1 is greater than string2           1
string1 or string2 is Null                     Null

Remarks
The following example uses the StrComp function to return the results of a string comparison. If the third argument is 1, a textual comparison is performed; if the third argument is 0 or omitted, a binary comparison is performed.

Dim MyStr1, MyStr2, MyComp
MyStr1 = "ABCD": MyStr2 = "abcd"   ' Define variables.
MyComp = StrComp(MyStr1, MyStr2, 1)   ' Returns 0.
MyComp = StrComp(MyStr1, MyStr2, 0)   ' Returns -1.
MyComp = StrComp(MyStr2, MyStr1)   ' Returns 1.
回复 支持 反对

使用道具 举报

该用户从未签到

2#
发表于 2006-7-11 10:46:08 | 只看该作者

同问

我也想知道。
回复 支持 反对

使用道具 举报

本版积分规则

关闭

站长推荐上一条 /1 下一条

小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

GMT+8, 2024-11-26 07:51 , Processed in 0.434378 second(s), 29 queries .

Powered by Discuz! X3.2

© 2001-2024 Comsenz Inc.

快速回复 返回顶部 返回列表