无法为WPF控件生成自动化ID,以供编码UI用于自动化测试 [英] Unable to generate Automation ID for WPF Controls, to be used by coded UI for automation testing

查看:164
本文介绍了无法为WPF控件生成自动化ID,以供编码UI用于自动化测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我们的应用程序中,我们使用各种WPF控件在网格,列表或树视图中显示数据。我们最近开始探索用于UI测试自动化的codedUI,并在识别控件时遇到很多问题。



问题是Coded UI(cross hair& UISpy)不识别控件返回的对象类型,无法获取列表或网格视图元素上的文本。所以我们考虑使用Automation ID而不是text属性。

我们已经尝试生成(静态和动态)自动化ID,但它不适用于grid和listview的元素。对于标题和单元格元素,我们看不到自动化ID的任何值。



这是一个简单的示例代码,它有两个用户控件,一个使用datagrid和另一个网格定义。如XAML代码所示,AutomationProperties.AutomationID已用于生成静态ID。但是这不起作用。



请您建议,我们应该在代码中更改什么以及如何获得自动化ID?







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?



<!-- -------- 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>

推荐答案

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
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


这篇关于无法为WPF控件生成自动化ID,以供编码UI用于自动化测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆