如何从编码的UI测试中访问数据绑定的WPF TreeView中的项目

时间:2015-02-13 10:29:06

标签: wpf coded-ui-tests

我在访问TreeView的每个节点中的控件时遇到了困难。我可以访问树视图节点,但不能访问它们中的控件(扩展器按钮及其消息)。TreeView绑定到ValidationResult数组(代码如下)。

我在这里上传了一张控件的图片:http://s30.postimg.org/j2vszwc01/Results_form.jpg 边框突出显示来自Coded UI Test Builder。它没有看到任何低于TreeViewItem节点级别的内容。

结果查看器控件的定义如下:



<UserControl x:Class="ResultsForm.ValidationResultsViewerUI"
             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:ResultsForm"
             mc:Ignorable="d" 
             d:DesignHeight="400" d:DesignWidth="400">
    <UserControl.Resources>
        <local:TreeViewDimensionConvertor x:Key="treeViewWidthConvertor"/>

        <DataTemplate x:Key="errorTemplate">
            <Grid Name="resultsGrid" Width="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type TreeView}}, Path=ActualWidth, Converter={StaticResource treeViewWidthConvertor}}">
                <Grid.Resources>
                    <Style TargetType="Border" x:Key="errorBorderStyle" >
                        <Style.Resources>
                            <LinearGradientBrush x:Key="errorBackBrush" StartPoint="0.5,0" EndPoint="0.5,1">
                                <GradientStop Color="#EF3132" Offset="0.1" />
                                <GradientStop Color="#D62B2B" Offset="0.9" />
                            </LinearGradientBrush>
                        </Style.Resources>
                        <Setter Property="Background" Value="{StaticResource errorBackBrush}"/>
                    </Style>
                </Grid.Resources>
                <Expander Name="expanderTest">
                    <Expander.Header>
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="300*"/>
                                <ColumnDefinition Width="5"/>
                                <ColumnDefinition Width="50*"/>
                            </Grid.ColumnDefinitions>
                            <Border Style="{StaticResource errorBorderStyle}" Grid.Column="0">
                                <TextBlock Text="{Binding Message}" 
                                           FontSize="12" 
                                           TextWrapping="Wrap" 
                                           Foreground="White">
                                </TextBlock>
                            </Border>
                            <TextBlock Grid.Column="2" HorizontalAlignment="Left">
                                 <Hyperlink Tag="{Binding UrlSuffix}">Details</Hyperlink>
                            </TextBlock>
                        </Grid>
                    </Expander.Header>
                    <TextBox Text="{Binding Description}"
                             AcceptsReturn="True" 
                             IsReadOnly="True"  
                             TextWrapping="Wrap" 
                             Width="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type TreeView}}, Path=ActualWidth, Converter={StaticResource treeViewWidthConvertor}}">
                    </TextBox>
                </Expander>
            </Grid>
        </DataTemplate>

        <DataTemplate x:Key="warningTemplate">
            <Grid Width="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type TreeView}}, Path=ActualWidth, Converter={StaticResource treeViewWidthConvertor}}">
                <Grid.Resources>
                    <Style TargetType="Border" x:Key="warningBorderStyle" >
                        <Style.Resources>
                            <LinearGradientBrush x:Key="warningBackBrush" StartPoint="0.5,0" EndPoint="0.5,1">
                                <GradientStop Color="#FFFDE203"  Offset="0.1" />
                                <GradientStop Color="#FFFDDC00" Offset="0.9" />
                            </LinearGradientBrush>
                        </Style.Resources>
                        <Setter Property="Background" Value="{StaticResource warningBackBrush}"/>
                    </Style>
                </Grid.Resources>
                <Expander Grid.Column="0" Grid.Row="1" Grid.ColumnSpan="2">
                    <Expander.Header>
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="300*"/>
                                <ColumnDefinition Width="5"/>
                                <ColumnDefinition Width="50*"/>
                            </Grid.ColumnDefinitions>
                            <Border Grid.Column="0" Style="{StaticResource warningBorderStyle}">
                                <TextBlock Text="{Binding Message}" FontSize="12" TextWrapping="Wrap" Foreground="Black" />
                            </Border>
                            <TextBlock Grid.Column="2" HorizontalAlignment="Left">
                                <Hyperlink Tag="{Binding UrlSuffix}">Details</Hyperlink>
                            </TextBlock>
                        </Grid>
                    </Expander.Header>
                    <TextBox Text="{Binding Description}"
                             AcceptsReturn="True" 
                             IsReadOnly="True"  
                             TextWrapping="Wrap" 
                             Width="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type TreeView}}, Path=ActualWidth, Converter={StaticResource treeViewWidthConvertor}}" />
                </Expander>
            </Grid>
        </DataTemplate>

        <local:TemplateSelector x:Key="warningOrErrorTemplateSelector"
                                ErrorTemplate="{StaticResource errorTemplate}"
                                WarningTemplate="{StaticResource warningTemplate}"/>

        <DataTemplate x:Key="childTemplate">
            <TreeViewItem Header="child"
                          ItemTemplateSelector="{StaticResource warningOrErrorTemplateSelector}"
                          IsExpanded="True"
                          Focusable="False"
                          IsSelected="False">
                <TreeViewItem.Resources>
                    <CollectionViewSource x:Key="childErrors" Source="{Binding Errors}"/>
                    <CollectionViewSource x:Key="childWarnings" Source="{Binding Warnings}"/>
                </TreeViewItem.Resources>
                <TreeViewItem.ItemsSource>
                    <CompositeCollection>
                        <CollectionContainer Collection="{Binding Source={StaticResource childErrors}}"/>
                        <CollectionContainer Collection="{Binding Source={StaticResource childWarnings}}"/>
                    </CompositeCollection>
                </TreeViewItem.ItemsSource>
            </TreeViewItem>
        </DataTemplate>

        <local:TemplateSelector x:Key="templateSelector"
                                ErrorTemplate="{StaticResource errorTemplate}"
                                WarningTemplate="{StaticResource warningTemplate}" 
                                VerificationResultTemplate="{StaticResource childTemplate}"/>

        <DataTemplate x:Key="parentTemplate">
            <TreeViewItem Header="{Binding Description}"
                          ItemTemplateSelector="{StaticResource templateSelector}"
                          IsExpanded="True"
                          Focusable="False"
                          IsSelected="False">
                <TreeViewItem.Resources>
                    <CollectionViewSource x:Key="errors" Source="{Binding Errors}"/>
                    <CollectionViewSource x:Key="warnings" Source="{Binding Warnings}"/>
                    <CollectionViewSource x:Key="children" Source="{Binding ChildrenResults}"/>
                </TreeViewItem.Resources>
                <TreeViewItem.ItemsSource>
                    <CompositeCollection>
                        <CollectionContainer Collection="{Binding Source={StaticResource errors}}"/>
                        <CollectionContainer Collection="{Binding Source={StaticResource warnings}}"/>
                        <CollectionContainer Collection="{Binding Source={StaticResource children}}"/>
                    </CompositeCollection>
                </TreeViewItem.ItemsSource>
            </TreeViewItem>
        </DataTemplate>
    </UserControl.Resources>

    <Grid>
        <Border>
            <TreeView Name="resultTree" ItemTemplate="{StaticResource parentTemplate}" Focusable="False" ItemsSource="{Binding}">
                <TreeView.Resources>
                    <SolidColorBrush Color="White" x:Key="{x:Static SystemColors.HighlightBrushKey}"/>
                </TreeView.Resources> 
            </TreeView>
        </Border>
    </Grid>
</UserControl>
&#13;
&#13;
&#13;

验证结果类的定义如下(VerificationError和VerificationWarning每个都包含Description和Message字符串字段):

&#13;
&#13;
[DataContract]
public class ValidationResult
{
    [DataMember(Order = 1)]
    public VerificationError[] Errors { get; set; }

    [DataMember(Order = 2)]
    public VerificationWarning[] Warnings { get; set; }

    [DataMember(Order = 3)]
    public virtual string Description { get; set; }
}
&#13;
&#13;
&#13;

为了访问TreeView中的消息,我需要做什么?

编辑:我尝试以编程方式访问项目并设置每个TreeViewItem的AutomationId,但这两种方法都不起作用。我还创建了一个类似的层次结构,没有数据绑定树视图,我没有访问控件有任何问题。我猜测问题在于数据绑定(http://blogs.msdn.com/b/tapas_sahoos_blog/archive/2011/12/13/verifying-wpf-data-bound-item-controls-in-coded-ui-test-recording-context.aspx),但我不知道如何纠正它。

1 个答案:

答案 0 :(得分:1)

您是否尝试过以编程方式访问树?例如:

UITestControl myControl = treeObjectPath;

foreach(myControl.GetChildren()中的UITestControl x) {     做一点事... }

有时使用Coded UI Test Spy无法捕获对象。