2

In coded UI is there a way to set the value of a ComboBox using the value of Value Attribute. Consider the following Html of a ComboBox:

<select name="ParametersID" tabindex="-1" title="ABC" class="form-control select-control select2-offscreen" id="Parameters_ID" autocomplete="off" data-val="true" data-val-number="This is a sample HTML" data-sets='["A","B","C","D","E","F"]' data-placeholder="Select only one">
  <option value=""></option>
      <option value="7311">A</option>
      <option value="7312">B</option>
      <option value="7313">C</option>
      <option value="7314">D</option>
      <option value="7315">E</option>
</select>

I want to set value of Html ComboBox using the values 7311, 7312 etc. not by the text "A", "B" etc.

I found that we properties like SelectedItem (to select using the text value) and SelectedIndex (to select by Index).

SelectedItem: Currently I am using this option, it is good but it is not that reliable. Since, text values inside dropdown can be updated from Admin pages. Also, these text values depends upon language selected by user.

SelectedIndex: This doesn't fit to any of my requirement, because I don't know which value will be at what index position.

There is a property "ValueAttribute" but it is only for getting the value not for set.

3

(This was meant to be a comment, but I do not have enough rep to do so)

If you can find the ComboBox within your page, you should be able to iterate over all of the different options. If you can do that you should be able to check the ValueAttribute within the option. Once you find the option that you would like to select, you should be able to set the selected item to that option.

Disclaimer, I havnt had the pleasure of working with Coded UI to work with Web Controls, just c# form controls (but the idea should be the same)


EDIT with an actual answer

Here is one way to do it, I am sure you could make it better.... Disclaimer... This isnt built out the best and should only be used as a basis for understanding.

        String valueToSelect = "7312";
        BrowserWindow browser = BrowserWindow.Launch(@"websiteAddress");
        HtmlComboBox box = new HtmlComboBox(browser);

        UITestControlCollection col = box.FindMatchingControls();


        if(col.Count > 0)
        {
            foreach(HtmlComboBox comboBox in col)
            {
                if(comboBox.Name == "ParametersID")
                {
                   UITestControlCollection col2 = comboBox.Items;
                    foreach(HtmlListItem item in col2)
                    {
                        if(item.ValueAttribute == valueToSelect)
                        {
                            comboBox.SelectedItem = item.InnerText;
                            break;
                        }
                    }

                }
            }
        }

First find the combobox, then get all of the List items within that Combo Box. After that, iterate through each item looking for the ValueAttribute you would like to select. Please let me know if you would like it explained better.

| improve this answer | |
  • Will try this solution. – Dhiman Apr 15 '16 at 20:00
  • @Dhiman let me know if you run into any issues or want me to build it out a bit better – Tango199 Apr 15 '16 at 21:57
  • Actually from your solution I had got the right direction on how to implement this and will solve my issue. – Dhiman Apr 15 '16 at 23:43
  • Glad to hear it, if you'd like you can mark your question as answered. – Tango199 Apr 15 '16 at 23:50
  • Is there a way where I don't need to iterate the whole List of ComboBox Items, because many of my lists have more than 100-150 items. Current solution is working fine but still want to know if there is a more fast/direct way of doing this. – Dhiman Apr 27 '16 at 14:41
2

After getting idea from Tango199 answer, I modified his solution and have now implemented below code (once ComboBox is found):

HtmlListItem html_listItem = new HtmlListItem(html_comboBox);
html_listItem.SearchProperties.Add(HtmlListItem.PropertyNames.ValueAttribute, str_Value);
html_listItem.Select();

where str_Value = Value Attribute (like 7311, 7312) as mentioned in my question.

Thanks to Tango199 for his answer which gave me right idea of how to implement this.

| improve this answer | |

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.