Assigning Unique AutomationId for Silverlight XAML Controls
If you want to run coded UI tests or create action recordings for your Silverlight 4 application, you must have a unique automation property that identifies each control. For more information about how to set up your Silverlight application so that the controls are recognized, see How to: Set Up Your Silverlight Application for Testing.
Static XAML Definition
To specify a unique automation property for a control that is defined in your XAML file, you can set the AutomationProperties.AutomationId or AutomationProperties.Name implicitly or explicitly, as shown in the following examples. Setting either of these values gives the control a unique automation property that can be used to identify the control when you create a coded UI test or action recording.
Set the Property Implicitly
Set the AutomationProperties.AutomationId to ButtonX using the Name property in the XAML for the control.
<Button Name="ButtonX" Height="31" HorizontalAlignment="Left" Margin="23,26,0,0" VerticalAlignment="Top" Width="140" Click="ButtonX_Click" />
Set the Property Explicitly
Set the AutomationProperties.AutomationId to ButtonX explicitly in the XAML for the control.
<Button AutomationProperties.AutomationId=“ButtonX” Height="31" HorizontalAlignment="Left" Margin="23,26,0,0" VerticalAlignment="Top" Width="140" Click="ButtonX_Click" />
Assigning AutomationId using UpdateAID tool
To simplify the process of assigning unique AutomationId, we have built a small tool that will help, when there are many static XAML controls in your silverlight application project. The steps to add the AutomationId are given below
Step 1. Build the Silverlight Application Project by specifying the "UpdateUid" switch
msbuild /t:UpdateUid <SilverlightApplication1.csproj>
Please note that the switch applies only to the CSPROJ and not to the SLN file of the project. This creates an XAML directive "x"Uid" with an unique Uid for every XAML control.
Say your SilverlightApplication1 project contains only the following XAML content
<Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="186,90,0,0" Name="button1" VerticalAlignment="Top" Width="75" />
After building with UdpateUid switch, the XAML content will be as below with the highlighted part added
<Button x:Uid="button1" Content="Button" Height="23" HorizontalAlignment="Left" Margin="186,90,0,0" Name="button1" VerticalAlignment="Top" Width="75" />
Step 2. The attached file UpdateAID.zip contains both the source(UpdateAIDToolSource.zip) of the tool as well as the compiled EXE(UpdateAIDTool.zip). Unzip the UpdateAIDTool.zip file to a folder.
Now the folder should contain two files - 1. UpdateAIDTool.exe 2. ControlList.txt
Step 3. Check if you have the desired list of controls in ControlList.txt file. You can remove or add more controls as you need. We suggest specifying the full control type name as it is in the XAML.
Step 4. From command prompt, run the following command
>> UpdateAIDTool.exe <Silverlight Project top directory>
The tool adds the "AutomationProperties.AutomationId" property for all the XAML controls that had the Uid directive set earlier. After this, the XAML content will be as below with the highlighted part added
<Button x:Uid="button1" Content="Button" Height="23" HorizontalAlignment="Left" Margin="186,90,0,0" Name="button1" VerticalAlignment="Top" Width="75"
AutomationProperties.AutomationId="button1" />
For Silverlight controls other than the static XAML controls, please refer this article to know how to assign unique Automation ID - https://msdn.microsoft.com/en-us/library/gg413373.aspx
Comments
Anonymous
January 18, 2011
It seems like this tool just copied the value of the Name attribute to use for the AutomationId. However, the Coded UT test builder will use a control's Name attribute as the Automation Id if the Automation Id parameter is not present. Specifying the Name is a way to "implicitly set the property" (see msdn.microsoft.com/.../gg413373.aspx). What is the advantage of using this tool?Anonymous
February 21, 2011
The xaml exception occurs if i have Automation id to any controls inside DataGridTemplateColumn such as DataGridTextColumn and DataGridCheckBoxColumn. Is there some issues using automation id for those controls?Anonymous
March 28, 2011
Sorry for the late replies. @Brent, If the Name is also not present, then the tool will set a unique AutomationId. If Name is present, then you are good to go. @Radhika, Yes, AutomationId doesn't apply to those controls.Anonymous
July 12, 2011
Hi Vishnu, In order to test SL using Coded UI Tests, do I need to name ALL the controls or only the ones that are targeted by the tests?Anonymous
January 19, 2012
Why it can implicitly use "Name" property but not "x:Name" property? And why it is needed at the first place? It is little bit annoying that I need add a property only for testing.Anonymous
February 08, 2012
Is there any way to go about this without adding the x:Uid property? The company I work for doesn't need that or want that in there.Anonymous
October 17, 2012
[Sorry, Was offline here for a long time] Ben/Ryan Liu - This property is needed for uniquely identifying the SL Controls. if your app has already such controls, then it is not required