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