问题
I'm trying to do coded UI testing with a ListView that is has a list of checkbox items.
Due to trouble with the coded UI code selecting the checkbox cell, I have been trying to add AutomationId to the controls, so that the coded UI test works.
I'm almost there, in snoop I can see that the UIItemCell does not have AutomationId set, but I can't figure out how to set it in my app.

The UIItemCell is where I need to set AutomationId
I found with Snoop that it's the ContentPresenter

The ListView code is this complex, so I'll distill it a bit
<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>
There are some AutomationIds in there that haven't helped but are good points of reference; 'gvrp' is the GridViewRowPresenter [016] that holds the Content Presenter [017] that I want to put the id on, and 'xaid1' is the CheckBox inside the Content Presenter [017].
Please help before my head explodes.
回答1:
I was able to do it eventually with
<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>
However, the automatically testing generated code (coded UI) still referenced the table column (even though it was redundant) which was the problem I was trying to avoid in the first place...
Anyway, it is possible to set the AutomationId
in the ContentPresenter
and in case it's helpful to anyone living in the future, here it is!
来源:https://stackoverflow.com/questions/22546602/assign-an-automationid-to-contentpresenter-in-a-gridviewrowpresenter-listview