(6)描述性编程: 如不使用对象库,可以用描述性编程: Window("text:=FlightReservation").WinComboBox("selection:=Denver").GetROProperty("text") 注:如果父级对象使用使用描述性编程,则子级对象必须使用描述性编程,以下两种写法都有错 Window("text:=FlightReservation").WinComboBox(Fly From .GetROProperty("text") Window(FlightReservation).WinComboBox("selection:=Denver").GetROProperty("text") (7)函数.vbs中函数有Function与Sub Function与Sub,Function可有返回值,Sub没有 Sub: Function: 用函数重构检查FlyFrom值代码: 我们可用Set定义FlyFrom对象,如: Set flyFrom = Window("FlightReservation").WinComboBox("Fly From:") 定义后求列表框元素总数可用flyFrom.GetItemsCount 脚本如下: flyFrom= Array("Denver","Frankfurt","London","LosAngeles"," aris"," ortland","San Francisco","Seattle","Sydney","Zurich")
Set fly = Window("Flight Reservation").WinComboBox("Fly From:")
Call CompareComobox(fly) // 调用函数
Function CompareComobox(obj)
For i =0 to obj.GetItemsCount-1
If (obj.GetItem(i)=flyFrom(i)) Then
msgbox i &" right"
Else
msgbox i & "wrong"
End If
Next
End Function 注释:定义函数CompareComobox(obj),其中obj是需要传递的FlyFrom对象,函数内部直接用obj代替之前的Window("FlightReservation").WinComboBox("Fly From:"),调用函数时直接把fly作为参数传递过去便好. (8) GetROProperty、SetTOProperty和 GetTOProperty GetROProperty:获取被测对象的运行时属性值 SetROProperty:修改运行时被测对象的属性, GetTOProperty:取得仓库对象的某个属性的值 如:获取FlyFrom当前值,可用GetROProperty方法,点”+”可以看到更详细的Name,Value.其中text对应的值是”Denver” 由上,可以用以下脚本得到FlyFrom当前值 Set fly = Window("Flight Reservation").WinComboBox("Fly From:")
msgbox fly.GetROProperty("text") SetTOProperty 增加属性与属性值 fly.SetTOProperty "add","wang" msgbox fly.GetTOProperty("add")用GetTOProperty查看对象库中该属性值 (9)测试报告report QTP自带测试报告Reporter方法 Reporter.ReportEvent EventStatus, ReportStepName, Details, [ImageFilePath] EventStatus有四种状态0,1,2,3分别对应micPass,micFail,micDone,micWarnging 用micPass,micFail重构检查FlyFrom下拉框值,脚本如下: Call CompareComobox(fly)
Function CompareComobox(obj)
For i =0 to obj.GetItemsCount-1
If (obj.GetItem(i)=flyFrom(i)) Then
Reporter.ReportEvent micPass,"Fly From: ",obj.GetItem(i)&"is right"
Else
Reporter.ReportEvent micFail,"Fly From: ",obj.GetItem(i)&"is wrong"
End If
Next
End Function 注:ImageFilePath可以不要,输出时采用obj.GetItem(i)& "is right"方便查看是哪边出错,在"is right" is前应增加一个空格,便不会与前面字符连接在一起bj.GetItem(i)&" is right" QTP在运行结束后,不让它立即产生测试报告: Tools-Options-Runsessions-View results 变为uncheck 配置Configure选项可以产生不同形式测试报告HTML,Doc,Pdf (10)开启应用程序 打开flight程序: SystemUtil.Run "D:\backup\qtp\samples\flight\app\flight4a.exe" 打开浏览器 SystemUtil.Run "C:\Program Files (x86)\Internet Explorer\iexplore.exe"
|