QuickTest Pro (9.0 or 8.2) method: WebRadioGroup.SelectByText

We've had to solve this problem quite a bit, so Will finally turned it into an object method.

Here's the problem:

Milk

Butter

Cheese

You 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

Visual Basic:
  1. Public Function WebRadioGroupSelectByText ( ByRef oRadGroup, sSelectText, _
  2.                                                               sWhereOpt, sIndexOpt )
  3.     If isNull(sWhereOpt) Then
  4.         sWhereOpt = "afterEnd"
  5.     End If
  6.  
  7.     If isNull(sIndexOpt) Then
  8.         sIndexOpt = 0
  9.     End If
  10.  
  11.     sMatches = Array()
  12.  
  13.     Set oParent = oRadGroup.GetTOProperty("parent")
  14.     radName = oRadGroup.GetROProperty("name")
  15.     Set oRadioButtons = oParent.Object.GetElementsByName( radName )
  16.  
  17.     For each oElement in oRadioButtons
  18.         sName = oElement.getAdjacentText("afterEnd")
  19.         If sSelectText = sName Then
  20.             ArrayPush sMatches, oElement.Value
  21.         End If
  22.     Next
  23.  
  24.     If Ubound(sMatches) <0 Then
  25.         ' TODO: report error...item doesn't exist in list.
  26.         Exit Function
  27.     End If
  28.  
  29.     If Ubound(sMatches) <sIndexOpt Then
  30.         sIndexOpt = Ubound(sMatches)
  31.         ' TODO: report warning...index doesn't exist.  selecting last match
  32.     End If
  33.  
  34.     sSelectValue = sMatches(sIndexOpt)
  35.     oRadGroup.Select sSelectValue
  36.    
  37. End Function
  38. RegisterUserFunc "WebRadioGroup", "SelectByText", "WebRadioGroupSelectByText"

Now, to select the radio button I want, I just to this:

Visual Basic:
  1. 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.

Tags: , , , , ,

4 Comments for “QuickTest Pro (9.0 or 8.2) method: WebRadioGroup.SelectByText”

  1. Bob Says:

    First of all, your GUID is not really a GUID at all, no guid’s contain a ‘g’. It looks like your development staff is exposing ID’s in the form of SQL Server UUID’s to the presentation layer. This is perfectly acceptable especially when you stop to consider the I18N and L10N ramifications of doing what you requested to expose “Option A” back to the code behind.

    Reply to Bob

  2. shahid Says:

    Hi

    Iam Shahid,newbie to QTP.
    My query is as follows

    QTP is unable to recognize the radio button
    i.e., every time a new “ID” is generated.

    First of all iam “adding destination” and every time a time a new “Destination id” is generated.

    While performing any operation(modify/delete) on the added destination,QTP unable to recognize the SYSTEM GENERATED ID.

    Please send me reply ASAP,so that i can capture

    “SYSTEM GENERATED ID”

    THE code is as follows

    Browser(”XXXXXX”).Page(”XXX”).WebRadioGroup(”XXXX”).Select “177″

    The properties for the radio button as follows

    name=myradio
    html tag=INPUT

    Reply to shahid

  3. swappy Says:

    u can go to standard properties and enable regular expression check box or use descriptive method
    micclass(”property1:value1″,”property2:value2″)
    first one is for manditory properties second for assessive properties

    Reply to swappy

  4. Kishore Says:

    what main advantage&Disadvantage in QTP compare to other tools

    let me know

    Reply to Kishore

Leave a Reply