In
Qtp
Custom test object
Winlist
Of
Select
Method, supports regular expressions and multiple choices. Similar ideas can be extended to other
List
Type Control.
The following script is taken from
Qtp
Of
Codesamplesplus
:
Function selectregexp (OBJ, patrn, button,
Offset)
Dim numofitems, I, currentvalue, RegEx, itemtoselect, oldfilter
'
Initialize the regular expression object with the pattern
Set RegEx = new Regexp
RegEx. pattern = patrn
RegEx. ignorecase = false
Oldfilter = Reporter. Filter 'Save the default setting
Reporter. Filter = 2 'send only errors
Itemtoselect =-1
'
Retrieve the number of items in the list
Numofitems = obj. getroproperty ("items count ")
For I = 0 to NumOfItems-1
Currentvalue = obj. getitem (I)
If RegEx. Test (currentvalue) then
If (itemtoselect <>-1) then
Selectregexp =-1 'item not
Unique
Reporter. Filter = oldfilter
Exit Function
End if
Itemtoselect = I
End if
Next
Reporter. Filter = oldfilter 'Restore the default setting
'
The actual selection
If (itemtoselect> = 0) then
Selectregexp = obj. Select (itemtoselect, button, offset)
Else
Selectregexp =-1
End if
End Function
Function selectitems (OBJ, items)
Dim idx, item
If (strcomp (obj. getroproperty ("type"), "select-multiple ",
1) = 0) then
For each item in items
OBJ. Select (item)
Next
Else
OBJ. Select (items (0 ))
End if
End Function
'Override the select function of
Winlist
Registeruserfunc "winlist ",
"Select", "selectregexp"
'Or add the selectregexp function to
Winlist object
Registeruserfunc "winlist ",
"Selectregexp", "selectregexp"
Registeruserfunc "winlist ",
"Selectitems", "selectitems"
'Example of usage:
Winlist ("mylist"). Select
"2002 .*"
Winlist ("mylist"). selectitems
Array ("Item1", "item3", "item6 ")