QTP中FSO的使用

FSO即文件系统对象(File System Object),在测试工作中有广泛的应有,它可以帮助我们自动生成测试目录,写日志,测试报告等。FSO有对象有很多属性和方法,今天只介绍几个常用的。

创建文件夹及文件

测试过程中需要创建文件的地方很多,比如,可以用FSO对象来构建自动化的整体目录,像下面这个小例子。

 1 '创建测试目录
 2 Function create_test_dir(filepath)
 3     Set fso=createobject("scripting.filesystemobject")
 4     '若已存在则先在d盘下做个备份,然后删除之
 5     If fso.FolderExists(filepath) Then
 6         '拷贝文件夹并重命名和Linux中的cp命令很像
 7         fso.CopyFolder filepath,"d:" & "\backup" & cstr(year(now))&cstr(month(now))&cstr(day(now))
 8 
 9         fso.DeleteFolder filepath
10         '新建文件夹
11         Set test_folder=fso.CreateFolder(filepath)
12     else
13         Set test_folder=fso.CreateFolder(filepath)
14     End If
15         fso.CreateFolder filepath&"\FrameAction"
16         fso.CreateFolder filepath&"\FrameUntilityFunction"
17         fso.CreateFolder filepath&"\FrameObjectRepository"
18         fso.CreateFolder filepath&"\TestData"
19         fso.CreateTextFile  filepath&"\TestData\Utility.xls"
20         fso.CreateFolder filepath&"\TestScript"
21         fso.CreateFolder filepath&"\TestRecovery"
22         fso.CreateFolder filepath&"\TestResult"
23         fso.CreateFolder filepath&"\TestLog"
24         fso.CreateFolder filepath&"\TestLog\Log1"
25 End Function
26 
27 create_test_dir("d:\QTPFrame")

 

获取指定路径下的文件所有文件

刚才通过FSO创建了一个目录,现在把目录内包含的文件全部打印出来

 1 '输出所有文件
 2 Function get_file_name(root_file_name)
 3 
 4    Set fso=createobject("scripting.filesystemobject")
 5    Set folder_obj=fso.GetFolder(root_file_name)
 6    Set folders_obj=folder_obj.SubFolders
 7     Set files_obj=folder_obj.Files
 8     For each file_item in files_obj
 9             print "文件"&file_item
10     Next
11    For each folder_item in folders_obj
12          Set child_folder=folder_item.SubFolders
13          print "文件夹"&folder_item
14         If child_folder.Count>0 Then
15             '递归调用,获取子文件夹
16             get_file_name folder_item
17         else
18             Set files_obj=folder_item.Files
19             For each file_item in files_obj
20                 print "文件"&file_item
21             Next
22         End If
23    Next
24 
25 End Function
26 
27 get_file_name "D:\QTPFrame"

 

写文本文件

FSO对文本文件的操作主要有三种模式

  • Forappending:追加模式,在文本末尾继续写入

  • Forreading:读取模式

  • Forwriting:写入模式,会覆盖原来文件内的内容

 1 Function write_log(file_path)
 2    '追加模式
 3     Const Forappending =8
 4     '只读
 5     Const Forreading =1
 6     '写入,覆盖原有内容
 7     Const Forwriting =2
 8 
 9     set fso=CreateObject("scripting.filesystemobject")
10     '如果文件不存在则新建
11     If fso. FileExists(file_path)Then
12         set txt=fso.OpenTextFile(file_path,Forappending,true)
13     else
14         set txt=fso.CreateTextFile(file_path,true)
15     End If
16     '写入字符串,不会自动换行
17     txt.Write "Hello,qtp"
18     txt.Write "Hi"
19     '写入一行空行
20     txt.WriteBlankLines 1
21     txt.WriteLine "How are you!"
22     txt.WriteLine "Fine thank you"
23     txt.Close
24 End Function
25 
26 write_log "D:\QTPFrame\TestLog\log.txt"

 

读取文本文件

读取文件时需要使用Forreading模式,读取时要注意每读取一次EOF指针会相应的移动。

 1 Function read_log(file_path)
 2     '只读
 3     Const Forreading =1
 4     set fso=CreateObject("scripting.filesystemobject")
 5     '如果文件不存在则新建
 6     If fso. FileExists(file_path)Then
 7         set txt=fso.OpenTextFile(file_path,Forreading,true)
 8     else
 9         msgbox "File doesn't exist!"
10     End If
11     print txt.Read(2)
12     print txt.ReadLine
13     print txt.ReadAll
14     txt.Close
15 End Function
16 read_log "D:\QTPFrame\TestLog\log.txt"

 

测试日志的生成方式

上文简单介绍了文本文件的写入方式,我们在工作中当然不会一个一个的手动去写日志,而是通过改写原有对象方法的方式来进行,我想,这也是大部分自动化同仁采用的方式吧,在这之前我们先做个获取当前时间的函数,一般日志文件的都是以系统时间来命名的。

 1 function get_date_time(date_flag,time_flag)
 2    Dim current_date_time
 3    Dim yy,mm,dd,hh,min,ss
 4 
 5    current_date_time=now()
 6    yy=Year(current_date_time)
 7    mm=Month(current_date_time)
 8    If mm<10 Then
 9      mm="0"&mm
10    End If
11    dd=Day(current_date_time)
12    If dd<10 Then
13     dd="0"&dd
14    End If
15    hh=Hour(current_date_time)
16    If hh<10 Then
17     hh="0"& hh
18   End If
19   min=Minute(current_date_time)
20    If min<10 Then
21      min="0"&min
22    End If
23   ss=Second(current_date_time)
24   If ss<10 Then
25    ss="0"&ss
26    End If
27   get_date_time=yy &date_flag&mm&date_flag& dd &" "  & hh&time_flag & min&time_flag& ss
28 end function
29 msgbox get_date_time("/",":")

 

好了,现在先来重写WebEdit对象的set方法,使其可以自动的生成日志。

 1 Function object_set_write_log(obj,val)
 2     obj.Set val
 3    '追加写,w+
 4    Const forappending=8
 5    '生成文件系统对象
 6     Set fso = CreateObject("Scripting.FileSystemObject")
 7     '左取8个字符获得日期
 8     log_file_name = left(cstr(get_date_time("",":")),8)+ ".txt"
 9 
10     log_file_path = "D:\QTPFrame\TestLog"+"\log"+log_file_name
11      If not fso.FileExists(log_file_path) Then
12         Set log_file = fso.CreateTextFile(log_file_path, True)
13     Else
14         Set log_file = fso.OpenTextFile(log_file_path,ForAppending,True)
15     End If
16     '右取8个字符获得时间,写入日志
17     log_file.WriteLine right(cstr(get_date_time("",":")),8)&" "&"INFO  [" & obj.getToProperty("micClass")&_
18     "-" & obj.getToProperty("TestObjName") & "]" & "input: " + val
19 
20     Set log_file=nothing
21     Set fso=nothing
22 End Function

 

这是我们的测试网页,随便写的很简陋(轻喷),新建脚本写入测试代码

1 '日志函数演示
2 '注册自定义函数
3 RegisterUserFunc "WebEdit","set","object_set_write_log"
4 
5 Browser("web对象演示").Page("web对象演示").WebEdit("用户名").Set "test"
6 Browser("web对象演示").Page("web对象演示").WebEdit("邮箱").Set "11111@qq.com"

 

将脚本保存在"D:\QTPFrame\TestScript",获取日期和日志函数放在"D:\QTPFrame\FrameUntilityFunction"目录下,分别命名get_date_time.vbs,object_write_log.vbs,随后设置相对路径和Resources,然后,运行脚本。

转载于:https://www.cnblogs.com/michaelle/p/4023317.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值