QTP - 描述性编程

只有对象存储在对象存储库中时,才能执行QTP脚本.对象的描述是使用描述性编程和减号创建的;

  • 当测试人员想要对一个对象执行操作时对象存储库中不存在

  • 当应用程序中的对象本质上非常动态时.

  • 当对象存储库变大时,随着对象存储库大小的增加,性能会下降.

  • 构建框架时,如此已经决定根本不使用Object Repository.

  • 当测试人员希望在运行时对应用程序执行操作而不知道对象的唯一属性.

语法

使用描述性编程技术有两种编写脚本的方法.它们是 :

  • 描述对象

  • 描述字符串

描述对象

脚本是使用描述对象开发的,这取决于所使用的属性及其对应的值.然后,这些描述用于构建脚本.

'Creating a description object
Set btncalc = Description.Create()

'Add descriptions and properties
btncalc("type").value = "Button"
btncalc("name").value = "calculate"
btncalc("html tag").value = "INPUT"

' Use the same to script it
Browser("Math Calc").Page("Num Calculator").WebButton(btncalc).Click

描述字符串

使用属性和值作为字符串开发对象的描述,如下所示.

Browser("Math Calc").Page("Num Calculator").WebButton("html 
tag:=INPUT","type:=Button","name:=calculate").Click

子对象

QTP提供了ChildObjects方法,该方法使我们能够创建对象集合.父对象在ChildObjects之前.

Dim oDesc
Set oDesc = Description.Create
oDesc("micclass").value = "Link"

'Find all the Links
Set obj = Browser("Math Calc").Page("Math Calc").ChildObjects(oDesc)

Dim i
'obj.Count value has the number of links in the page

For i = 0 to obj.Count - 1	 
   'get the name of all the links in the page			
   x = obj(i).GetROProperty("innerhtml") 
   print x 
Next

序数标识符

描述性编程用于根据序号标识符编写脚本,这将使QTP在两个或多个对象具有相同属性时对这些对象起作用.

' Using Location
Dim Obj
Set Obj = Browser("title:=.*google.*").Page("micclass:=Page")
Obj.WebEdit("name:=Test","location:=0").Set "ABC"
Obj.WebEdit("name:=Test","location:=1").Set "123"
 
' Index
Obj.WebEdit("name:=Test","index:=0").Set "1123"
Obj.WebEdit("name:=Test","index:=1").Set "2222"
 
' Creation Time
Browser("creationtime:=0").Sync
Browser("creationtime:=1").Sync
Browser("creationtime:=2").Sync