2

I am using Coded UI for creating some test cases for a web application, while doing the same I have encountered an issue. I am not able to select a Radio Button using their Displayed Text, however if I use the ValueAttribute then its working fine. But, since value attribute is not containing a number which may not be of any logical use for a person creating test data, so I need to do same work using the Displayed Text of the Radio button. Below is the HTML:

<div class="col-sm-6">                   
    <div class="formItem formItem-2365   updateDependent" id="Some ID" data-form-item-id="2365" data-template="InlineRadioList" data-defaultvalue="Junior">
        <div class="form-group">
            <label title="" class="col-lg-2 control-label" for="TypeListID" data-original-title="SUFFIX" data-toggle="tooltip">SUFFIX</label>
            <div class="col-lg-10">
                <div class="radiolist-control" data-sets='["SUFFIXShown"]'>
                    <label class="radio-inline">
                        <input name="TypeListIDName" class="formviewer-control" id="TypeListID" type="radio" value="1966" data-sets="[0]" autocomplete="off">
                        Junior
                    </label>
                    <label class="radio-inline">
                        <input name="TypeListIDName" class="formviewer-control" id="TypeListID" type="radio" value="1985" autocomplete="off">
                        Senior
                    </label>
                    <label class="radio-inline">
                        <input name="TypeListIDName" class="formviewer-control" id="TypeListID" type="radio" value="1935" autocomplete="off">
                        Normal
                    </label>
                </div>
            </div>
            <span class="field-validation-valid help-block col-lg-offset-2" data-valmsg-replace="true" data-valmsg-for="TypeListIDName"></span></div>
        </div>
    </div>

One alternate solution is to use HtmlLabel instead of HtmlRadioButton and directly click the label, but can we do it using the HtmlRadioButton class and DisplayText?

  • So is the issue that you need to select a distinct radio button? If so, can you look up the radio button by its value? – Brian Feb 2 '16 at 20:57
  • Issue is, I am not able to select Radio Button by its text "Junior", as I don't want to select Radio button using "1966" value, as this value doesn't make any sense. – Dhiman Feb 2 '16 at 21:06
  • My point is, selecting the radio button by its value in the context you are asking, would give you the radio button you want, which is "Junior". – Brian Feb 2 '16 at 21:09
  • Yes, I will provide the value "Junior" and ID add/or Class of the Radio Button – Dhiman Feb 2 '16 at 21:36
1

A possible solution for this issue could be to find the label control containing the value you are looking to select, then find the child control (which should be the radio button) then selecting the radio button.

  String labelToFind = "Senior";
        BrowserWindow browser = BrowserWindow.Launch(@"pathToHTMLFile");
        HtmlLabel theLabelToFind = new HtmlLabel(browser);
        theLabelToFind.SearchProperties.Add(HtmlLabel.PropertyNames.InnerText, labelToFind);

        if (theLabelToFind.Exists)
        {
            UITestControlCollection childrenOfLabel = theLabelToFind.GetChildren();
            if (childrenOfLabel.Count >= 1)
            {
                if (childrenOfLabel[0].ControlType.Name == "RadioButton")
                {

                    //can use either one of these two to select the radio button
                    // Mouse.Click(childrenOfLabel[0]);
                    childrenOfLabel[0].SetProperty("Selected", true);
                }
            }

        }

Like you said, another way (and a bit simpler way) could be to just click on the label containing the RadioButton.

        String labelToFind = "Senior";
        BrowserWindow browser = BrowserWindow.Launch(@"pathToHTMLFile");
        HtmlLabel theLabelToFind = new HtmlLabel(browser);
        theLabelToFind.SearchProperties.Add(HtmlLabel.PropertyNames.InnerText, labelToFind);


        if(theLabelToFind.Exists)
        {
            Mouse.Click(theLabelToFind);
        }

Disclaimer: Both of these code examples can be built out to handle more errors and are only proof of concepts.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.