QTP中如何快速獲取Page中的物件個數

TIB發表於2010-03-22

 

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" )

 

 

 

相關文章