QuickTest Pro (9.0 or 8.2) method: WebRadioGroup.SelectByText
Tuesday, March 21st, 2006We've had to solve this problem quite a bit, so Will finally turned it into an object method.
Here's the problem:
Milk Butter CheeseYou want to be able to say "Select the 'Butter' radio button", but the enterprising and oh-so-efficient programmer has made it so that each option has a value that corresponds to some GUID (e.g. "AFED-0839-02E8A5-0923CDB"), and "Option A" is nowhere to be found anywhere in the object's properties.
The method below allows you to send the text you wish to select, and the radio group will know exactly which button to select.
Select a Radio Button based on the adjacent descriptive text
- The WebRadioGroup to use
- The text to look for
- optional: The sWhere statement for getAdjacentText. Default is afterEnd
- optional: The Index number of the matched button to select. 0 selects first match, 1 selects second. Default is 0
-
Public Function WebRadioGroupSelectByText ( ByRef oRadGroup, sSelectText, _
-
sWhereOpt, sIndexOpt )
-
If isNull(sWhereOpt) Then
-
sWhereOpt = "afterEnd"
-
End If
-
-
If isNull(sIndexOpt) Then
-
sIndexOpt = 0
-
End If
-
-
sMatches = Array()
-
-
Set oParent = oRadGroup.GetTOProperty("parent")
-
radName = oRadGroup.GetROProperty("name")
-
Set oRadioButtons = oParent.Object.GetElementsByName( radName )
-
-
For each oElement in oRadioButtons
-
sName = oElement.getAdjacentText("afterEnd")
-
If sSelectText = sName Then
-
ArrayPush sMatches, oElement.Value
-
End If
-
Next
-
-
If Ubound(sMatches) <0 Then
-
' TODO: report error...item doesn't exist in list.
-
Exit Function
-
End If
-
-
If Ubound(sMatches) <sIndexOpt Then
-
sIndexOpt = Ubound(sMatches)
-
' TODO: report warning...index doesn't exist. selecting last match
-
End If
-
-
sSelectValue = sMatches(sIndexOpt)
-
oRadGroup.Select sSelectValue
-
-
End Function
-
RegisterUserFunc "WebRadioGroup", "SelectByText", "WebRadioGroupSelectByText"
Now, to select the radio button I want, I just to this:
-
Browser("B").Page("P").WebRadioGroup("All Options").SelectByText "Option B"
Note that this performs an exact string match. The next step is to create our WebRadioGroup.SmartSelect function, which would be much more flexible, and would allow for partial matches of either adjascent text or the traditional button values. It would also allow for pattern matching via regular expressions. We'll post that one later.

