在GridViewRowPresenter(ListView)中将AutomationId分配给ContentPresenter

时间:2014-03-20 22:35:10

标签: wpf ui-automation coded-ui-tests

我正在尝试使用包含复选框项列表的ListView进行编码UI测试。

由于编码的UI代码选择复选框单元格的问题,我一直在尝试将AutomationId添加到控件中,以便编码的UI测试工作。

我差不多了,在窥探中我可以看到UIItemCell没有设置AutomationId,但我无法弄清楚如何在我的应用程序中设置它。

coded UI test builder window

UIItemCell是我需要设置AutomationId

的地方

我在Snoop发现它是ContentPresenter

Snoop control tree

ListView代码很复杂,所以我会稍微提炼一下

<ListView HorizontalAlignment="Left"
          Height="194"
          Margin="53,123,0,0"
          VerticalAlignment="Top"
          Width="424"
          AutomationProperties.AutomationId="listviewoption">
    <ListView.Resources>
        <Style x:Key="ListViewItemContainerStyle1" TargetType="{x:Type ListViewItem}">
            <Setter Property="ToolTip"
                    Value="{Binding RelativeSource={RelativeSource Self}, Path=Content.Description }" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ListViewItem}">

                        <Border SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
                                BorderThickness="{TemplateBinding BorderThickness}"
                                BorderBrush="{TemplateBinding BorderBrush}"
                                Background="{TemplateBinding Background}"
                                AutomationProperties.AutomationId="Bxaid1" >

                            <Grid AutomationProperties.AutomationId="Gxaid1">

                                <!-- This is used when GridView is put inside the ListView -->
                                <GridViewRowPresenter AutomationProperties.AutomationId="gvrp"
                                                      Content="{TemplateBinding Content}"
                                                      HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                                      VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                                      SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
                                <!-- ... -->
                            </Grid>

                        </Border>

                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </ListView.Resources>

    <ListView.ItemContainerStyle>
        <StaticResource ResourceKey="ListViewItemContainerStyle1"/>
    </ListView.ItemContainerStyle>

    <ListView.View>

        <GridView AutomationProperties.AutomationId="aid1">

            <GridViewColumn AutomationProperties.AutomationId="xc0"
                            DisplayMemberBinding="{Binding OptionName, Converter={StaticResource CamelCaseConverter}, Mode=OneWay}"
                            Width="180"/>

            <GridViewColumn AutomationProperties.AutomationId="xc1"
                            Width="60">

                <GridViewRowPresenter AutomationProperties.AutomationId="pp" />

                <GridViewColumn.CellTemplate>
                    <DataTemplate  >
                        <CheckBox Name="x1"
                                  AutomationProperties.AutomationId="xaid1"
                                  IsHitTestVisible="False"
                                  HorizontalAlignment="Right"
                                  Tag="{Binding OptionName}"
                                  IsChecked=""
                                  Padding="0"
                                  Margin="0"/>
                    </DataTemplate>
                </GridViewColumn.CellTemplate>

            </GridViewColumn>
        </GridView>

    </ListView.View>
</ListView>

其中有一些AutomationIds没有帮助但是很好的参考点; 'gvrp'是GridViewRowPresenter [016],它保存我要将id放入的Content Presenter [017],'xaid1'是Content Presenter [017]内的CheckBox。

在我的头爆炸之前请帮助。

1 个答案:

答案 0 :(得分:1)

我最终能够用

做到这一点
<GridViewRowPresenter Content="{TemplateBinding Content}"
                      HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                      VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                      SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}">

    <GridViewRowPresenter.Resources>
        <Style TargetType="{x:Type ContentPresenter}">
            <Setter Property="AutomationProperties.AutomationId"
                    Value="{Binding RelativeSource={RelativeSource Self}, Path=Content.Name }"/>
        </Style>
    </GridViewRowPresenter.Resources>

</GridViewRowPresenter>

然而,自动测试生成的代码(编码的UI)仍引用了表列(即使它是多余的),这是我试图首先避免的问题......

无论如何,可以在AutomationId中设置ContentPresenter,如果它对未来的任何人有帮助,就在这里!