Click here to Skip to main content
15,886,813 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
In our application we use various WPF controls for displaying data in a grid, list or tree view. We recently started exploring codedUI for UI test automation and face lots of problems while identifying the controls.

Problem is the Coded UI (cross hair & UISpy) don’t recognize the type of object returned by the controls and can’t get hold of the text on the elements of list or grid view. So we thought of using Automation ID instead of text property.
We have tried generating both (static and dynamic) automation IDs but it doesn’t work for elements of grid and listview. We do not see any value for the automation ID neither for the headers nor the cell elements.

Here is a simple sample code which has two user controls, one using a datagrid and the other a grid definition. As seen in the XAML code, AutomationProperties.AutomationID has been used to generate the Static IDs. But this doesn't work.

Please could you suggest, what should we change in the code and how do we get the automation IDs?



HTML
<!-- -------- DATAGRID ----------->
<UserControl x:Class="SampleProjectForCodedUI.ItemControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:SampleProjectForCodedUI" 
             DataContext="{Binding RelativeSource={RelativeSource Self}}"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <StackPanel Height="69" HorizontalAlignment="Left" Margin="0,1,0,0" Name="stackPanel1" VerticalAlignment="Top" Width="194">
            <DatePicker Width="180" HorizontalAlignment="Left" DatePicker.SelectedDateChanged="TextBox_SelectedDateChanged" />
        </StackPanel>
        <StackPanel Height="69" HorizontalAlignment="Right" Margin="0,1,0,0" Name="stackPanel2" VerticalAlignment="Top" Width="75">
            <Button Content="Add Row" Click="AddRow_Click" Height="26" Width="61" />
        </StackPanel>
        <Grid Grid.Row="1">
            <DataGrid Name="DG1" ItemsSource="{Binding DateCollection}" AutoGenerateColumns="False">
                <DataGrid.Columns>
                    <DataGridTextColumn Header="Date" Binding="{Binding Date}" AutomationProperties.AutomationId="Date" />
                    <DataGridTextColumn Header="DayOfWeek" Binding="{Binding DayOfWeek}" AutomationProperties.AutomationId="DayOfWeek"/>
                    <DataGridTextColumn Header="Year" Binding="{Binding Year}" AutomationProperties.AutomationId="Year"/>
                </DataGrid.Columns>
            </DataGrid>            
        </Grid>
    </Grid>
</UserControl>

<!-- -------- GRID ----------->
<UserControl x:Class="SampleProjectForCodedUI.UserControl1"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
              xmlns:local="clr-namespace:SampleProjectForCodedUI" 
             mc:Ignorable="d" 
             DataContext="{Binding RelativeSource={RelativeSource Self}}"
             d:DesignHeight="300" d:DesignWidth="300">
   
    <Grid x:Name="grid" Margin="0,0,16,16">

        <Grid.Resources>
            <GridView x:Key="DateTracker" x:Shared="False" AllowsColumnReorder="False">
                <GridViewColumn x:Name="Date" Header="Date" Width="30" >
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <Border>
                                <TextBlock 
                                    Text="{Binding Date}" 
                                    Tag="Caption" 
                                    TextAlignment="Left" 
                                    Margin="0,2,0,0"                                    
                                    AutomationProperties.AutomationId="DateCol" />                                   
                                
                            </Border>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>

                <GridViewColumn x:Name="DayOfWeek" Header="DayOfWeek" Width="140">
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <Border>
                                <TextBlock Text="{Binding DayOfWeek}" TextAlignment="Left" Margin="0,2,0,0" Tag="Caption" AutomationProperties.AutomationId="DayOfWeek" />                                    
                            </Border>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>

                <GridViewColumn x:Name="Year" Header="Year" Width="40">
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <Border>
                                <TextBlock Text="{Binding Year}" TextAlignment="Left" Margin="0,2,0,0" Tag="Caption" AutomationProperties.AutomationId="Year" />
                            </Border>                           
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
            </GridView>
        </Grid.Resources>

        <Grid.RowDefinitions>
            <RowDefinition x:Name="rowdef_Toolbar" Height="42"/>
            <RowDefinition x:Name="rowdef_NameField" Height="*"/>
        </Grid.RowDefinitions>

        <ListView ItemsSource="{Binding DateCollection}" Name="m_Spectra" Grid.Row="1" Margin="0" VerticalAlignment="Stretch" Padding="0"                      
                      View="{StaticResource DateTracker}"
                      ScrollViewer.CanContentScroll="True"
                      ScrollViewer.HorizontalScrollBarVisibility="Disabled" />
        <DatePicker HorizontalAlignment="Left" Margin="5,5,5,5" 
        DatePicker.SelectedDateChanged="TextBox_SelectedDateChanged"/>
        <Button HorizontalAlignment="Right" Margin="5,5,5,5" 
       Content="Add Row" Click="AddRow_Click" />

    </Grid>
</UserControl>
Posted
Updated 27-Jul-12 1:35am
v2

1 solution

Hi . the problem is UserControl. you must implement accessibility for your custom control.
My suggestion is using Grid instead of UserControl. native controls of WPF have already implemented accessibility
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900