preload

How to select random list item (QTP, VBScript)

Posted by Albert Gareev on Nov 24, 2011 | Categories: How toSource codeWeb

Description 

Get object reference. Get total number of items in the list. Generate an index within the given range. Select item by index.

Implementation


Dim objPage, objList
Dim boolRC
Dim intItemsCount, intRndItem

'Assumptions:
' "Google Advanced Search" page is up
' "Results per page" WebList exists and enabled

'Get page object
Set objPage = Browser("CreationTime:=0").Page("title:=Google Advanced Search")
boolRC = objPage.Exist(0)
If Not boolRC Then
 ExitTest
End If

'Get list object
Set objList = objPage.WebList("name:=num")
boolRC = objList.Exist(0)
If Not boolRC Then
 ExitTest
End If

'Get number of list items
intItemsCount = objList.GetROProperty("items count")
'Create random index
Randomize
intRndItem = CInt(Rnd*intItemsCount)
'Select the item
'Note: selection by index is zero-based
objList.Select "#" & intRndItem


Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported
This work by Albert Gareev is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported.