QTP_描述性编程总结1

来源:互联网 发布:忘记windows管理员密码 编辑:程序博客网 时间:2024/05/21 17:11

描述性编程(descriptive programming 缩写 DP)

DP在QTP中作用:在对象库无法识别控件或者该控件是动态时会用到。

使用spy来查看所要测试控件的属性方法

1)通过对象库识别
对象类型(“对象名称”).方法


2)通过临时描述的对象识别
隐式创建:对象类型(“属性1:=属性1的值”,“属性2:=属性2的值”……).方法,写到可以唯一标识该对象为止。(注意:=前后不能有空格,大小写敏感)

注意:对象类型中属性与属性要以“"隔开;如果父对象描述了,子对象则一定描述。

例如:
Window("计算器").WinButton("text:=MC").Click

显式创建:Description.Create
Description.create()
对象变量名(“属性”).value=“属性值”
对象类型(对象变量名).操作

例如:
Set =Nothing
set aa=Description.Create()
aa("text").value="MR"
Window("计算器").winbutton(aa).click
Set aa=Nothing
推荐用显式创建,创建了2个对象而且最后释放了,这样比较节省资源,加快脚本运行速度。但隐式创建每条语句都在创建对象,而且没有释放,脚本运行开销很大,导致脚本运行很慢。

例1:获取单元格中的值
thisText = Browser(…).Page(…).Frame.(…).WebTable("sample").GetCellData(2,1)

例2:获取图片的名称
ObjectName = Browser(…).Page(…).Image("Find").GetProperty("Name")

例3:检查某个对象是否存在,如果存在弹出对话框说明对象存在。
If Browser("Browser").Page("Page").Applet("login.html").JavaEdit("username").Exist Then
MsgBox("The object exists.")
End if

例4:出现多个相同控件时。

Dim i
i = 0
while (Window("Text:=Yahoo! - Microsoft Internet Explorer", "index:="&i).exist)
Window("Text:=Yahoo! - Microsoft Internet Explorer", "index:="&i).close
i = i +1
wend
这里我们可以看到,对于具有相同属性的对象,我们可以通过index 参数来对其进行区别,第一个对象为index=0,第二个为
index=1 等等,依次类推。当然我们还可以通过CreationTime 和Location 参数来定位对象,这里就不详细叙述了。

例5:我们需要完成下面一系列操作
Window("Text:=HyperSna").WinButton("Caption:=日期").Click
Window("Text:=HyperSna").WinButton("Caption:=时间").Click
Window("Text:=HyperSna").WinButton("Caption:=确定").Click
那么,为了方便其见,我们可以将Window("Text:=HyperSna")赋值到一个变量,然后再使用,参见下面的代码:
Set WinHyper = Window("Text:=HyperSna")
WinHyper.WinButton("Caption:=日期").Click
WinHyper.WinButton("Caption:=时间").Click
WinHyper.WinButton("Caption:=确定").Click
如果使用了VBScript 里面的With 语句,还可以简化为以下代码:
With Window("Text:=HyperSna")
.WinButton("Caption:=日期").Click
.WinButton("Caption:=时间").Click
.WinButton("Caption:=确定").Click
End With




原创粉丝点击