Wednesday, April 14, 2010

WPF UI Automation

Some things to be aware of when engaging Microsoft Automation.

1. Run UI SPY to browse around your app structure.

2. Expand Controls that do not support UI Automation

3. Provide “good” AutomationProperties.AutomationId for controls.

Why would you expose UserControl for Automation? Some third party user controls do not support automation. Or you might want to expose custom UI structure that tailored towards desired Testing.

How to expose UserControl and see it in UI Spy ?

Something like that:

image

 

filling AutomationId
  1. <Style x:Key="AutomationEnabledRibbonGroup" TargetType="ribbon:Group">
  2.     <Style.Setters>
  3.       <Setter Property="AutomationProperties.AutomationId" Value="{Binding RelativeSource={RelativeSource Self}, Path=Label}" />
  4.     </Style.Setters>
  5.   </Style>
  6.  
  7.     <Style x:Key="AutomationEnabledButton" TargetType="{x:Type Button}"BasedOn="{StaticResource {x:Type Button}}">
  8.     <Style.Setters>
  9.       <!-- Buttons without Name/x:Name have no AutomationId, this setter initializes id with the button's text  -->
  10.       <Setter Property="AutomationProperties.AutomationId" Value="{Binding RelativeSource={RelativeSource Self}, Path=Content}" />
  11.     </Style.Setters>
  12.   </Style>
  13.  
  14.   <Style x:Key="AutomationEnabledListBoxItem" TargetType="{x:Type ListBoxItem}" BasedOn="{StaticResource {x:Type ListBoxItem}}">
  15.     <Setter Property="AutomationProperties.AutomationId" Value="{Binding RelativeSource={RelativeSource Self}, Path=Content}" />
  16.   </Style>

0 comments:

Post a Comment