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 



本文转自hcy's workbench博客园博客,原文链接:http://www.cnblogs.com/alterhu/archive/2011/12/31/2309231.html,如需转载请自行联系原作者。
目录
相关文章
|
7月前
|
算法 Java 程序员
重温经典《Thinking in java》第四版之第五章 初始化与清理(三十一)
重温经典《Thinking in java》第四版之第五章 初始化与清理(三十一)
28 0
|
3月前
|
存储 Java 索引
从零开始学习 Java:简单易懂的入门指南之Collection集合及list集合(二十一)
从零开始学习 Java:简单易懂的入门指南之Collection集合及list集合(二十一)
|
8月前
|
设计模式 Java 编译器
重温经典《Thinking in java》第四版之第五章 初始化与清理(二十九)
重温经典《Thinking in java》第四版之第五章 初始化与清理(二十九)
48 0
|
4月前
|
存储 编译器 C++
C++入门第八篇---STL模板---list的模拟实现
C++入门第八篇---STL模板---list的模拟实现
48 1
|
8月前
|
Java 编译器
重温经典《Thinking in java》第四版之第五章 初始化与清理(二十八)
重温经典《Thinking in java》第四版之第五章 初始化与清理(二十八)
43 0
|
索引
第三章--第五节:集合
第三章--第五节:集合
113 0
第三章--第五节:集合
|
vr&ar 图形学
【Unity3D 灵巧小知识点】 ☀️ | 快速查找场景中勾选Raycast Target的游戏对象
【Unity3D 灵巧小知识点】 ☀️ | 字符串截取,截取某个路径字符串中 末尾文件 的名字 47/100 发布文章 zhangay1998 未选择任何文件 Unity 小科普 老规矩,先介绍一下 Unity 的科普小知识: Unity是 实时3D互动内容创作和运营平台 。 包括游戏开发、美术、建筑、汽车设计、影视在内的所有创作者,借助 Unity 将创意变成现实。 Unity 平台提供一整套完善的软件解决方案,可用于创作、运营和变现任何实时互动的2D和3D内容,支持平台包括手机、平板电脑、PC、游戏主机、增强现实和虚拟现实设备。
【Unity3D 灵巧小知识点】 ☀️ | 快速查找场景中勾选Raycast Target的游戏对象
|
JavaScript Android开发 iOS开发
第二十章:异步和文件I/O.(十二)
虽然每个方法都被定义为返回Task或Task 对象,但是方法的主体没有任何对Task或Task 的引用。相反,返回Task对象的方法只是执行一些工作,然后使用隐式return语句结束该方法。 ExistsAsync方法定义为返回Task 但返回true或false。
785 0
第二十章:异步和文件I/O.(十一)
特定于平台的库 每个程序员都知道应该将可重用的代码放在库中,对于依赖服务使用的代码也是如此。这里开发的异步文件I / O函数将在第24章的NoteTaker程序中重用,您可能希望在自己的应用程序中使用这些函数,或者开发自己的函数。
834 0