一、描述性编程
1、QTP录制脚本的语法
父类(对象逻辑名).子类(对象逻辑名).操作 值
ParentObject(Object Logic Name).Object(Object Logic Name).Operation value
原理:QTP是利用测试脚本代码和对象库来完成测试脚本的执行,它以"."作为分隔符来划分语句层次,通过对象逻辑名到对象库搜寻直到搜寻到操作,最后把操作信息放入到当前窗口的操作消息队列中,该窗口就会自动执行该操作。
2、描述性编程的语法
对象类别("属性1:=值1","属性2:=值2")
ObjectClass("Property1:=value1","Property2:=value2")
原理:它不需要从对象库中查找对象,而是直接通过对象属性来识别对象,只要提供足够的属性信息给QTP就可以操作任何一个需要操作的对象。
例如:
systemutil.Run "C:\Program Files (x86)\Internet Explorer\iexplore.exe","http://www.baidu.com/" Set Baidu = browser("micClass:=Browser").page("micClass:=Page") Print Baidu.link("name:=新 闻").Exist With Baidu Print .link("name:=贴 吧").Exist Print .link("name:=音 乐").Exist Print .link("name:=图 片").Exist Print .link("name:=知 道").Exist End With Set Baidu = nothing
二、对象操作编程
1、动态加载对象
Function AddObjectRepository(objectrepositoryname) Dim Pos '判断文件后缀名是否为.tsr If instr(objectrepositoryname,".tsr")>0 Then RepPath = objectrepositoryname else RepPath = objectrepositoryname & ".tsr" End If '清除副对象库中已经存在的对象 RepositoriesCollection.RemoveAll() '从文件中添加对象至副对象库 RepositoriesCollection.Add(RepPath) '查找对象,若存在则返回1 Pos = RepositoriesCollection.Find(RepPath) MsgBox Pos If Pos <> 1 Then MsgBox "找不到副对象库" End If End Function '调用添加副对象库中对象的函数 Call AddObjectRepository("D:\Program Files\Mercury Interactive\QuickTest Professional\Tests\objects.tsr")
代码执行后,文件对象库中的对象将会添加至副对象库。
2、实用对象Utility Objects
'1、加密 NewPw = Crypt.Encrypt("original") '2、桌面抓图 Desktop.CaptureBitmap "D:\test.bmp" '3、计时 MercuryTimers("XX页面").Start '...do something... MercuryTimers("XX页面").Stop Print MercuryTimers("XX页面").ElapsedTime '4、获取参数文件路径 Msgbox PathFinder.Locate("Default.xls") '5、随机数产生 Num = RandomNumber.Value (0, 100)
3、DataTable的使用
'在百度搜索框中输入不同内容进行搜索
systemutil.Run "C:\Program Files (x86)\Internet Explorer\iexplore.exe","http://www.baidu.com/" For i = 1 To DataTable.GetSheet("Action1").GetRowCount set baidu = browser("百度一下,你就知道").Page("百度一下,你就知道") DataTable.GetSheet("Action1").SetCurrentRow(i) With baidu .WebEdit("搜索框").Set DataTable.Value(1,"Action1") .WebButton("百度一下").Click End With Set baidu= nothing browser("百度一下,你就知道").Page("百度搜索结果页").WebElement("返回百度").Click Next
'导入excel数据 DataTable.Import("D:\test.xls")
4、输出测试报告
''reporter.ReportEvent 报告一个事件的运行结果,标识报告的四种状态为: '0 or micPass: Causes the status of this step to be passed and sends the specified message to the Run Results window. '1 or micFail: Causes the status of this step to be failed and sends the specified message to the Run Results window. When this step runs, the test or component fails. '2 or micDone: Sends a message to the Run Results window without affecting the pass/fail status of the test or component. '3 or micWarning: Sends a warning message to the Run Results window, but does not cause the test or component to stop running, and does not affect the pass/fail status of the test or component. For i = 1 To 2 If i=1 Then reporter.ReportEvent micPass,"step 1","micPass = 0" ElseIf i=2 Then reporter.ReportEvent micWarning,"step 2","micPass = 3" Else End If Next