QTP中如何快速获取Page中的对象个数

 

relevantcodes.com的《QTP: Retrieve The Count of Objects in a Browser Efficiently》这篇文章介绍了如何封装一个类来快速获取到某个页面中指定类型的对象的个数:

http://relevantcodes.com/qtp-quickly-retrieve-object-count/

 

封装的类如下所示:

Class clsClassCount

 

    Public CountType

 

    '—————————————————————————————————————————————

    ' Name: Function GetClassCount (Public)

    '

    ' Purpose:

    '

    ' Input:

    '     BaseObject- Object containing the ClassName objects

    '     ClassName- MicClass for which the count is being retrieved for

    '

    ' Output:

    '     Integer

    '

    ' Author: Anshoo Arora

    '

    '—————————————————————————————————————————————

    Public Function GetClassCount( BaseObject, ClassName ) ' As Integer

    '—————————————————————————————————————————————

        BaseObject.Init

        If Not BaseObject.Exist( 0 ) Then

            Reporter.ReportEvent micWarning, "GetClassCount", "BaseObject was not found."

            GetClassCount = -1

            Exit Function

        End If

 

        Dim oDesc, intCount

 

        intCount = 0

 

        Set oDesc = Description.Create

        oDesc( "micclass" ).Value = ClassName

 

        intCount = BaseObject.ChildObjects( oDesc ).Count

 

        If Not CountType = "ALL" Then

            oDesc( "x" ).Value = 0

            intCount = intCount - BaseObject.ChildObjects( oDesc ).Count

        End If

 

        GetClassCount = intCount

    End Function

 

End Class

 

Public refClassCount: Set refClassCount = New clsClassCount

 

'—————————————————————————————————————————————

' Name: Function GetObjectCount (Public)

'

' Purpose: Retrieve object count (visible + hidden)

'

' Input:

'     BaseObject- Object containing the ClassName objects

'     ClassName- MicClass for which the count is being retrieved for

'

' Output:

'     Integer

'—————————————————————————————————————————————

Public Function GetObjectCount( BaseObject, ClassName ) ' As Integer

'—————————————————————————————————————————————

    refClassCount.CountType = "ALL"

    GetObjectCount = refClassCount.GetClassCount( BaseObject, ClassName )

End Function

 

'—————————————————————————————————————————————

' Name: Function GetVisibleObjectCount (Public)

'

' Purpose: Retrieve visible objects count

'

' Input:

'     BaseObject- Object containing the ClassName objects

'     ClassName- MicClass for which the count is being retrieved for

'

' Output:

'     Integer

'—————————————————————————————————————————————

Public Function GetVisibleObjectCount( BaseObject, ClassName ) ' As Integer

'—————————————————————————————————————————————

    refClassCount.CountType = ""

    GetVisibleObjectCount = refClassCount.GetClassCount( BaseObject, ClassName )

End Function

 

 

支持获取所有对象的个数以及可见对象的个数:

GetObjectCount: Retrieves the total count of objects on the webpage. This includes hidden + non-hidden objects.

GetVisibleObjectCount: This retrieves the number of non-hidden objects on the webpage.

 

 

使用的例子如下所示:

'This is where the objects are being searched for:

Set BaseObject = Browser("title:=.*实用性测试.*").Page("micclass:=Page")

 

Print BaseObject.Exist

 

' Retrieve the total number of Link Objects

MsgBox GetObjectCount( BaseObject, "Link" )

 

' Retrieve the total non-hidden number of Link Objects

MsgBox GetVisibleObjectCount( BaseObject, "Link" )

 

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值