— New online course!  Getting Started with Azure DevOps — Special launch price 50% off —

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

by

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<string>(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.


9 Responses to "Coded UI Test Utilities for ComboBox, ListBox, and CheckBox Controls"
  1. Hi,
    Just now i started to use coded UI test. Iam facing problem to selected the list box item. can you please help me on this.

  2. Hi

    I’ve started working with the Coded UI test tool, but when I click over an item in a list box I get Select ” in ‘listBox’ list box

    is there any property I have to set?

    Thanks

  3. Hi,
    Do you have anything similar for selecting everything from an Html Div drop list?
    Is it the same just with Html Div?
    Thanks!

  4. Hi Ben,

    I have a scenario where i need to select item from combo box and click on it so it will take me to another page.

    Here is the code i have which is selecting the item but not clicking on it.

    public static List SelectXFromList
    {
    get
    {
    List listItem = new List(pageContent);
    listItem.ContainsClass(“class name”);
    listItem.SearchProperties[“TagName”] = “tagname”;
    return listItem;
    }
    }

    #region Actions

    public static void SearchX(String XName)
    {
    SearchXInput.EnterText(XName);
    SearchXButton.Click();
    SelectXFromList.SelectItem(XName);
    Playback.Wait(3000);
    }
    #endregion

    public void SelectItem (string text)
    {
    //Find the user and Select it
    HtmlListItem userItem = new HtmlListItem(this);
    userItem.SearchProperties.Add(HtmlListItem.PropertyNames.InnerText, text, PropertyExpressionOperator.Contains);
    userItem.Select();
    }

Leave a Reply to Rosie Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: