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?

asked Feb 2, 2016 at 19:41
4
  • 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? Commented Feb 2, 2016 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. Commented Feb 2, 2016 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". Commented Feb 2, 2016 at 21:09
  • Yes, I will provide the value "Junior" and ID add/or Class of the Radio Button Commented Feb 2, 2016 at 21:36

1 Answer 1

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.

answered Jun 7, 2016 at 3:31

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.