QTP的那些事---webtable的一些重要使用集合精解

' ******************************** Function Library ********************************* 

' Registering both functions 
RegisterUserFunc "WebTable", "ObjectsByMicClass", "ObjectsByMicClass" 
RegisterUserFunc "WebTable", "ItemByKeyColumn", "ItemByKeyColumn" 
' 函数功能: ObjectsByMicClass 
' 描述: Returns a collection of objects. All the objects in a 
' WebTable that have the specified MicClass 得到webtable下面指定的一个micclass类型的元素的所有对象的集合
' Return Value: A collection of objects    得到对象的集合
' Arguments: 参数无
' Obj - Test Object (WebTable) 测试对象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")    '字典集合,很重要的一个方式,存放的对象可以在action之间传递
    ' Go over all the cells in the table, and look for objects with the specified micClass 
    For row=1 to Table.RowCount   '遍历webtable的所有行
        ColumnCount=Table.ColumnCount(row)      '得到webtable中指定行的列数
        For col=1 to ColumnCount      '遍及指定行的所有的列
            For ItemIndex=0 to Table.ChildItemCount(row, col, micClass)-1   '遍及webtable中指定行,指定列的指定类型对象的个数
                Set childItem=Nothing 
                Set childItem = Table.ChildItem(row, col, micClass, ItemIndex)     '获得webtable中指定行,指定列中指定对象类型,加上特定的index的对象
                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 
'******************************************************
' Function: ItemByKeyColumn 
' Description: Returns an item from a column, based on the value of a 
' key column 
' Return Value: Object 
' Arguments: 
' Obj - Test Object (WebTable) 
' KeyColumnIndex - Index of the KeyColumn 
' KeyColumnValue - Value to search for in the key column 
' KeyItemIndex - Index of the value in the key column (if there is 
' more than one). If 0, the first item will be used. 
' TargetColumnIndex - Column from which to retrieve the target item 
' micClass - The micClass of the target item 
' TargetItemIndex - Index of the target item to retrieve (if there is 
' more than one). If 0, the first item will be used. 
' ---------------------------------------------------------------------------------------------------- 
Function ItemByKeyColumn(Obj, KeyColumnIndex, KeyColumnValue, KeyItemIndex, TargetColumnIndex, micClass, TargetItemIndex) 
    Set Table = Obj 
    rowCount = Table.RowCount    '获得webtable中总计的行数
    ' If TargetItemIndex 没有指定,就采用默认的1
    If TargetItemIndex < 1 Then 
        TargetItemIndex = 1 
    End If 
    ' 如果 KeyColumnIndex没有指定,采用默认的1
    If KeyItemIndex < 1 Then 
        KeyItemIndex = 1 
    End If 
    ' Look for KeyColumnValue in the key column to determine from which 
    ' row to retrieve the target item 
    Row = 0 
    foundIndex = 0 
    While Row <= RowCount And foundIndex < KeyItemIndex 
        Row = Row + 1 
        CellData = Table.GetCellData(Row, KeyColumnIndex) 
        If CellData = KeyColumnValue Then 
            foundIndex = foundIndex + 1 
        End If 
    Wend 
    If foundIndex < KeyItemIndex Then 
        Exit Function 
End If 
    ' Now that we know the row, retrieve the item (according to its micClass) 
    ' from the target column. 
    ChildItemsCount = Table.ChildItemCount(Row, TargetColumnIndex, micClass) 
    If ChildItemsCount >=1 And ChildItemsCount >= TargetItemIndex Then 
    Set GetItemByKeyColumn = Table.ChildItem(Row, TargetColumnIndex, micClass, TargetItemIndex-1) 
    End If 
End Function 

' *******************函数使用例子 ****************************** 

' 使用 ItemByKeyColumn 函数 例子
Set obj = Browser("Table with objects").Page("Itenerary: Mercury Tours").WebTable("Acapulco to Zurich").ItemByKeyColumn(1,"FLIGHT",2,3,"WebElement",1) 
msgbox obj.GetROProperty("innerhtml") 
' 使用 ObjectsByMicClass 函数例子
Set collection = Browser("Browser").Page("Page").WebTable("Table").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 

posted @ 2011-12-31 23:56  高级测试开发网  阅读(470)  评论(0编辑  收藏  举报
了解开发资源最新动态:https://seniortesting.club