Coded UI Test Utilities for ComboBox, ListBox, and CheckBox Controls

September 06, 2011

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

SNAGHTML16e0baa
Figure 1 – A sample form with ComboBox, ListBox, and CheckBox controls

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).

SNAGHTML17b4b1a
Figure 2 – The UIMap Utility classes in the test project

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)

SNAGHTML1884d6b
Figure 3 – The methods in the utility classes

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.

[TestMethod]
public void ChangeComboBoxValue()
{
try
{
this.UIMap.RunTheWindowsFormApp(PathToExecutable);

this.UIMap.ClickSelectItemsToPopulateTheValues();

string startingComboBoxValue = this.UIMap.GetComboBoxSelectedText();

this.UIMap.SelectAnotherValueFromComboBox();

string endingComboBoxValue = this.UIMap.GetComboBoxSelectedText();

Assert.AreNotEqual(startingComboBoxValue,
endingComboBoxValue,
"ComboBox values should not match.");
}
finally
{
this.UIMap.CloseTheApp();
}
}

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;

UIMapComboBoxUtility.SelectAnotherValue(control);
}

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.

-Ben

-- 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 info@benday.com.

Tags: coded-ui