WebTable 是使用 QTP 进行 WEB 页面测试时经常碰到的测试对象,由于 WebTable 可以嵌套,因此通常需要 用 ChildItem 来获取嵌套的子元素
可以封装一些方法来扩展 WebTable 测试对象,简化测试脚本的实现,例如返回 WebTable 中包含的指定类型( MicClass )的测试对象。
以下脚本摘自 QTP 的 CodeSamplesPlus :
' Function: ObjectsByMicClass
' Description: Returns a collection of objects all the objects in a
' WebTable that have the specified MicClass
' Return Value: A Collection of Objects
' Arguments:
' Obj - Test Object (WebTable)
' micClass - The micClass of the objects to retrieve
'-----------------------------------------------------------------------------------------------------------
Function ObjectsByMicClass(Obj, micClass)
Set Table = Obj
' Create a collection object to hold the items
Set objCollection = CreateObject("Scripting.Dictionary")
' Go over all the cells in the table, and look for objects with the specified micClass
For row=1 to Table.RowCount
ColumnCount=Table.ColumnCount(row)
For col=1 to ColumnCount
For ItemIndex=0 to Table.ChildItemCount(row, col, micClass)-1
Set childItem=Nothing
Set childItem = Table.ChildItem(row, col, micClass, ItemIndex)
If Not childItem is Nothing Then
' If the cell contains a micClass object, add it to the collection
ItemKey = objCollection.Count + 1
objCollection.Add ItemKey, childItem
End if
Next
Next
Next
Set ObjectsbyMicClass = objCollection
End Function
RegisterUserFunc "WebTable","ObjectsByMicClass","ObjectsByMicClass"
使用 ObjectsByMicClass 的例子:
Set collection = Browser("Browser").Page("Web Tours").Frame("info").WebTable("Joseph Marshall 's Flight").ObjectsByMicClass("WebCheckBox")
For i=1 to collection.count
If collection(i).GetROProperty("checked") Then
collection(i).Set "OFF"
Else
collection(i).Set "ON"
End If
Next