在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文件则创建一个。

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值