Azure DevOps, Scrum, & .NET Software Leadership and Consulting Services

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


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. Rajasekaran Avatar

    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. Deeps Avatar

    Thanks ben, for this great idea.
    But how to implement the same for Web Application. Can you throw more light

    1. Ben Day Avatar

      Hi Deepan,

      Thanks for the suggestion about doing this for a web app. I just posted a new sample for web apps a couple days ago. Here’s that post.

      -Ben

  3. […] in a web application By Ben Day On September 29, 2012 · A little over a year ago, I wrote a blog post about using Coded UI Tests to programmatically manipulate ListBoxes and ComboBoxes in a Windows […]

  4. Marcello Avatar

    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

    1. Ben Day Avatar

      Marcello,

      Can you email me a screenshot?

      -Ben

  5. Rosie Avatar

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

    1. Ben Day Avatar

      Hi Rosie–

      I’m not sure that I know what you mean. Can you point me to a sample of what you mean by an HTML div drop list?

      -Ben

  6. Ram Avatar

    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

Your email address will not be published.

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