在QTP中,经常会遇到需要写入外部文件的地方,比如写Log什么的,这时,可以使用下面代码进行写Txt操作。
'新建文件
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文件则创建一个。
'写文件
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, 8, true)
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, 2, true)
logFile.WriteLine (CStr(words))
logFile.Close
Set logFile = Nothing
Set FSO = Nothing
End Function
这样,一个打印就可以做到图文并茂啦~
下面的图是我的用法,用了下QTP报告自带的CSS,大概如下