在QTP中,经常会遇到需要写入外部文件的地方,比如写Log什么的,这时,可以使用下面代码进行写Txt操作。

来源:互联网 发布:淘宝评价修改链接 编辑:程序博客网 时间:2024/05/22 16:52

'新建文件

Dim FSO
Const ForReading=1,ForWriting=2,ForAppending=8          '参数赋值(1:只读,2:只写,3:追加)
Set FSO = CreateObject("Scripting.FileSystemObject")        '创建一个文本对象

Dim txtPath
txtPath = "D:\log.txt"
FSO.OpenTextFile txtPath,8,true                                          'true表示如果当前目录下不存在1.txt文件则创建一个。

Set FSO = Nothing


 '写文件
Call QTP_Writetxt(txtPath,"我是追加")
Call QTP_Writetxt2(txtPath,"我是改写")

然后是两个函数:

'===========================================
'写文件函数(追加)
'===========================================
Public Function QTP_Writetxt(oPath,words)
    Dim FSO
    Set FSO = CreateObject("Scripting.FileSystemObject")
    Set logFile = FSO.OpenTextFile(oPath, 8true)
 
    logFile.WriteLine (CStr(words))
    logFile.Close

    Set logFile = Nothing
    Set FSO = Nothing
End Function

 
'===========================================
'写文件函数(改写)
'===========================================
Public Function QTP_Writetxt2(oPath,words)
    Dim FSO
    Set FSO = CreateObject("Scripting.FileSystemObject")
    Set logFile = FSO.OpenTextFile(oPath, 2true)

    logFile.WriteLine (CStr(words))
    logFile.Close

    Set logFile = Nothing
    Set FSO = Nothing
End Function

       除了直接打印之外,我还会加上写html语句,然后新建文件的后缀名也改成.html,这样,保存的文件就是一个网页啦,可以写入超链接,颜色,插入图片等等一系列动作。

这样,一个打印就可以做到图文并茂啦~

下面的图是我的用法,用了下QTP报告自带的CSS,大概如下

0 0