I’ve been helping one of my customers to develop and roll out Visual Studio 2010 Coded UI Tests for their applications. After we went through the initial round of training and we got down to recording some tests, they started cranking out a series of really good questions about how to extend the recorded tests and add additional custom functionality.
One of their first big issues was how to script out changing the value for a ComboBox (aka. “drop down list”), ListBox, or CheckBox control to another value. Put another way, they wanted to go into a form and, without having to know what the current selected values are, change them to another value.
The answer was to create some utility classes that knew how to generically address controls in the test’s UIMap.cs and perform common actions. Using the sample form shown in Figure 1, if you want to change the values in the ComboBox or ListBox you need to:
1) get the current selected value from the control
2) get all the available values in the list
3) find a value in the list of available values that isn’t currently selected
4) set the selected value in the control to a new value
To change the value for a CheckBox, the process is a little shorter because the possible values are only True or False. The process there is:
1) get the current selected value from the CheckBox control
2) set the selected value to the opposite of the value from step 1
In the sample project, I’ve created a static utility class for each control type: UIMapCheckBoxUtility.cs, UIMapComboBoxUtility.cs, and UIMapListBoxUtility.cs (see Figure 2).
Each of these utility classes contains a SelectAnotherValue() and GetSelectedValue() method. The classes for the ComboBox and ListBox controls also have a method called GetListOfItems() that returns the list of items as strings. (See Figure 3)
Once you’ve included these utility classes in your Coded UI test project, you can then modify your test methods and UIMap.cs files to take advantage of the methods. The code block below runs the sample application, modifies the selected value for a combobox and then verifies that it’s not the same value any longer.
public void ChangeComboBoxValue()
string startingComboBoxValue = this.UIMap.GetComboBoxSelectedText();
string endingComboBoxValue = this.UIMap.GetComboBoxSelectedText();
“ComboBox values should not match.”);
In the sample application, I modify my UIMap.cs and create some additional helper methods that know how to take UI control references and pass them into the appropriate utility function. The method below grabs the reference to the ComboBox on the screen and passes it into UIMapComboBoxUtility.SelectAnotherValue() to change the value to any other valid but unselected value.
public void SelectAnotherValueFromComboBox()
WinComboBox control = this.UIMainFormWindow.UIM_ComboBoxWindow.UIComboBoxComboBox;
Here’s the link to download the sample application and sample tests. Be sure to build the Benday.CodedUiSamples.WinUI project before attempting to run the tests.
I hope this helps you out.
— Not sure where to start with Microsoft Test Manager? Need help figuring out Visual Studio 2010 Coded UI Tests? Want to incorporate Coded UI Tests into an automated Lab Management build? Drop us a line at firstname.lastname@example.org.