Coded UI tests : AutomationId or how to find the chose one (control) !


In my previous post on Coded UI tests, we have seen how to make a control discoverable by a Microsoft UI Automation client including the Coded UI tests builder.

This is a first step and the controls now need to be uniquely identified. This is done via the AutomationId property. In this post we’ll digg a little more in coded UI tests and discover the AutomationId property.

AutomationId : what is it ?

As stated in the previous post, an application exposes AutomationPeer. They are then found by UI Automation Client as AutomationElement objects. These items are identified uniquely by the AutomationId property.

This identifier is not mandatory but it’s a best practice to set it when you plan to perform UI tests.

To retrieve a control, the automation peer tree is used : first the top element is found, then the automation client iterate on its children, then the children of the children until it finds the aimed element. Each of the children and the final target is find using conditions and usually, one is set on the automation id.

The automation id is a property which helps you to find a specific UI element among others. This is, for example, very useful when you want to find a specific item in a collection (listbox, menus, etc.). Another benefit is that, as the control is uniquely identified, you can move it elsewhere or change its name and it will still be found by a previous code needing it.

To leverage the power of it, there is a rule to respect: it’s expected to be the same in any instance of the same application and ‘unique by instance in its scope‘.

  1. If you exit the application and launch it again, the value will not change.
  2. If you change the culture of the PC/Application, it will not change.
  3. If you launch several instance of the application, it remains the same on each of them.
  4. If there is several usage of the control in the application, each of them has a different automation id on each scope. A scope is defined by the path in the tree to go to the element. For example each menu item of a menu can be named in a generic way : menuItem1, menuItem2, etc.

This is a rule and you are not forced by the runtime or anything to follow it. As a proof, AutomationPeer returns a default value of string.empty which is really not unique!

By the way, if you want to set a name to a control(for example to improve accesibility) uses the Name property instead of the AutomationId one.

How to set it ?

The AutomationPeer exposes a method GetAutomationId of type string. When you create a custom AutomationPeer, you can override the GetAutomationIdCore abstract method to return a custom value. The default value is string.empty.

You can also set the returned value via XAML using the AutomationProperties’s AutomationId attached property.
[xml]<MyControl AutomationProperties.AutomationId="AnUniqueValue"/>[/xml]

The default implementation (which you can override) of the the AutomationPeer’s GetAutomationIdCore method read the value from the attached property too:
//In the AutomationPeer class.
protected override string GetAutomationIdCore()
return (AutomationProperties.GetAutomationId(_owner));

How to use it in automation clients ?

This is pretty straightforward, you just have to define a condition on it, providing the searched value:
[csharp]string seekedAutomationId = "AnUniqueValue";

//Create the condition
var condition = new PropertyCondition(
AutomationElement.AutomationIdProperty, seekedAutomationId);

//Retrieve the element via FindFirst(best practice)
var elt = AutomationElement.RootElement.FindFirst(TreeScope.Descendants, condition);

Also, you can retrieve the AutomationId value of an element using this code:
[csharp]AutomationElement autoElement = //Retrieves the auto element here.
string autoId =
as string;[/csharp]

How is it used by the Coded UI tests builder ?

This tool is the one shipped with Visual Studio with which you can record coded UI tests.

If no AutomationId is set, the tools generates code which search the button based on its properties’ values. As you can see, if you move the button elsewhere, the test won’t be able to find it again.
[csharp]var mUIYoupiButton1 = new WpfButton(this);
#region Search Criteria
mUIYoupiButton1.SearchProperties[WpfButton.PropertyNames.Name] = "Youpi !";
mUIYoupiButton1.SearchProperties[WpfButton.PropertyNames.Instance] = "2";

return mUIYoupiButton1;

When you set the automation id, the generated code is smart enough to use it to identify the button:
[csharp]var mUIYoupiButton = new WpfButton(this);
#region Search Criteria
.SearchProperties[WpfButton.PropertyNames.AutomationId] = "AnUniqueValue";

return mUIYoupiButton;[/csharp]

How to find it on existing application ?

Snoop for a WPF application, UI Spy for a Silverlight one !

As this is a standard property you can read it as any other with these tools! In using it, don’t forget that it’s not an inherited property and that you have to select precisely the control (It can be difficult using the Ctrl+Shift shortcut).
AutomationId retrieval with snoop

When to set the AutomationId ?

As you’ve read, this is not mandatory to set it but it is a best practice.

My opinion is to do it right when you code in any application which may be tested trough coded UI tests.

To set it on all control is useless because not all control will be tested. Instead, I recommend to identify the key controls of the application. Usually, it’s button, menu items, any controls which can receive input from the users (keyboard and/or mouse).
Also, don’t forget the controls which evolve on user interaction and display results / information.

It can be a good thing, before to start to write any code, to set up a meeting with the quality team, deciding what are those controls. The member of the quality team usually know what they test 🙂

AutomationId and ItemsControl or dynamically generated controls.

In any user interface, there is dynamic parts. One of the most common are ItemsControls. A recurrent question is “how can I define an AutomationId on something which is dynamic ?“.

My answer is : “as you would do for any other problem – via a binding“. The AutomationId is an attached dependency property and you can set a binding on it which will solve your problem.

Let’s say, that I want to display a list of person, then in the DataTemplate representing a person, I can use the Id property of the person as an Automation id. Here is an example:
[xml]<DataTemplate x:Key="PersonDataTemplate" DataType="model:Person">
<TextBlock Text="{Binding Name}">
<MultiBinding StringFormat="AID_{0}-{1}">
<Binding Path="Name" />
<Binding Path="Id" />
</MultiBinding >

<ItemsControl x:Name="_myItemsControl"
ItemTemplate="{StaticResource PersonDataTemplate}" />


The real issue is to respect the previously mentioned rule: “how to keep the id unique and the same in all the instance of the application”. The solution will depends of the application and the goal of the control.

If the aimed control is targeted because of the value it represents (a specific person) then you can generate the id using the model property (the id of the person).

If the aimed control is targeted because of its position in the interface (the 5th item in the list) then you can bind to UI-related properties like the alternation count for example. You can also find a technique which uses the index of the item in the list as an AutomationId.

I am sad to tell it but there is no solution which will work in all case 🙁

More to read on the subject

If you want to discover more on this subject, here is some interesting links:

  1. Visual Studio Team Test blog posts on the subject : part 1 and part 2.
  2. UI Automation Properties Overview on MSDN.
  3. Use the AutomationID Property on MSDN.
  4. WPF Application Quality Guidelines on UI testing.
  5. My previous post on the subject explaining Automation peers.



13 Responses

  1. Biff Turckle

    09/11/2011 16 h 23 min

    Thanks for this post- it's really useful.

    I had a quick question- I am using an a ListView and a DataTemplate in WPF. I try and set the automation id of the textblock using a binding to the Id property (which is unique) with the following XAML.

    <ColumnDefinition Width="auto" />
    <ColumnDefinition Width="*" />

    <Image Source="/Resources/Images/MyImage.png"
    Stretch="None" />

    <TextBlock AutomationProperties.AutomationId="{Binding Id}"
    Text="{Binding Caption}"


    This compiles and runs ok- but when I start recording my coded UI test, I still cannot locate the specific textblock in the UI using the crosshair and the coded UI test builder; it just highlights the entire list item which doesn't have an automation ID.

    The can successfully bind the automation Id of the image in the template- and I can locate that with the crosshair- I was just wondering why it doesn't work the same for the textblock. Any ideas?

    Thanks in advance.

    • jmix90

      09/11/2011 16 h 28 min

      Hello Biff,

      This is a common issue (the texblock not found) I struggled with too. I am going to write a post on it later but to be quick, the TextBlock Automation peer hides it when the TextBlock is inside a Template.

      I didn't know it was the case with ItemTemplate but it seems 🙁


  2. memory

    17/01/2012 2 h 58 min

    I loved up to you’ll receive carried out proper here. The cartoon is attractive, your authored subject matter stylish. nonetheless, you command get bought an shakiness over that you wish be turning in the following. in poor health no doubt come further formerly once more as precisely the similar nearly very steadily within case you defend this increase.

  3. issamo

    25/01/2012 14 h 56 min

    Do you know any way to set the automation property for text and p html tags ? (Id=value works perfectly for the img tag)

  4. aralele

    15/05/2012 10 h 53 min

    will i be right if i say that control without Automationid can be missed at the coded ui builder tree ?

    • jmix90

      16/05/2012 8 h 02 min

      AutomationId are not mandatory, you can use other properties to find a control. But the best practice is to use them 🙂

      • SILENT

        03/05/2015 20 h 38 min

        Why is AutomationId a best practice? Are there any performance enhancements for using unique AutomationId vs unique name properties?

Comments are closed.