It has always been difficult to solve UI automation problems. There are many UI automation frameworks not only on the Internet but also internal at Microsoft. Choosing the right one that best fits your need could be difficult. Finally, there is an ultimate solution that’s shipped with .NET 3.0 or later: System.Windows.Automation. It supports automating WPF (Windows Presentation Foundation) applications and will probably support SilverLight applications in the future. Dr. James McCaffrey talked about how to use it at I took his approach and built an automation project. The difference is that I know all the values in the Windows Form Control Name property which identify the objects in the Windows Form application I want to automate. In the code, you will see new PropertyCondition(AutomationElement.AutomationIdProperty, "EndpointTextBox") versus using AutomationElement.NameProperty. EndpointTextBox is the Control Name property in the Windows Form project I want to automate. 2 references are required to use the automation library: UIAutomationClient, UIAutomationTypes.

using System;

using System.Text;

using System.Windows.Automation;

using System.Diagnostics;

using System.Threading;

using System.Text.RegularExpressions;

using Microsoft.VisualStudio.TestTools.UnitTesting;

// Get the main form element from the desktop

Process ProductServiceCheckProc = Process.Start(FormUIAuto.ProductServiceCheckExePath);

AutomationElement desktop = AutomationElement.RootElement;

AutomationElement mainForm = null;



    mainForm = desktop.FindFirst(TreeScope.Children,

        new PropertyCondition(AutomationElement.NameProperty, "MDSClientForm"));



while (mainForm == null);
// In the Windows Form project being automated, the TextBox.Name property is set to InputFileTextBox
AutomationElement inputFileTextBox = mainForm.FindFirst(TreeScope.Children,

new PropertyCondition(AutomationElement.AutomationIdProperty, "InputFileTextBox"));

Dr. James McCaffrey did not write about how to select a single item from a dropdown list Control. I thought the library would have this format: DropDownList.Select(desiredItem) but it turned out to be desiredItem.Select():

// Find the dropdown list of Name property createService
AutomationElement endpointComboBox = mainForm.FindFirst(TreeScope.Descendants,

    new PropertyCondition(AutomationElement.AutomationIdProperty, "createService"));

SelectionPattern selectEndpointComboBox = (SelectionPattern)endpointComboBox.GetCurrentPattern(SelectionPattern.Pattern);
// The dropdown list can only select one item

Assert.IsFalse(selectEndpointComboBox.Current.CanSelectMultiple, "ComboBox can select multiple endpoints");
// Find the item to select in the dropdown list
AutomationElement ItemInDropdown = endpointComboBox.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.NameProperty, "desiredItem"));
// returns null if the item is not found

Assert.IsNotNull(ItemInDropdown, "Can't find list item {0} in createService ComboBox", EndpointName);

SelectionItemPattern ItemInDropdownSelectItem = (SelectionItemPattern) ItemInDropdown.GetCurrentPattern(SelectionItemPattern.Pattern);
// Simulate choosing the item in the dropdown list


It could be difficult to figure out how to automate some controls such as ErrorProvider Class, OpenFileDialog Class, and ToolTip Class. They might have something to do with writing a provider for those controls. If you know how to automate them, please leave a comment with the link to the article.