【问题标题】:How do you get the Microsoft Coded UI Test Builder to recognize an ItemsControl?如何让 Microsoft Coded UI Test Builder 识别 ItemsControl?
【发布时间】:2017-08-10 01:02:27
【问题描述】:

我创建了一个小型 WPF 应用程序(只是为了探索 Coded UI 测试的工作原理)。我的应用程序包含一个ItemsControl,但编码的 UI 测试生成器 UIMap 找不到合适的控件。

XAML:

<ItemsControl ItemsSource="{Binding Path=Customers, Mode=OneTime}"
              AutomationProperties.AutomationId="CustomersItemsControl">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <TextBlock AutomationProperties.AutomationId="{Binding Path=Id, StringFormat='Customer_{0}', Mode=OneWay}"
                       Margin="5">
                <TextBlock.Text>
                    <MultiBinding StringFormat="{}{1}, {0}">
                        <Binding Path="FirstName" />
                        <Binding Path="LastName" />
                    </MultiBinding>
                </TextBlock.Text>
            </TextBlock>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

我遇到的问题是,即使 WPF 有一个ItemsControlAutomationPeer,编码的 UI 测试生成器也无法找到要添加到 UIMap 的控件。我解决它的唯一方法是获取根 AutomationElement,然后使用 TreeWalker.ControlViewWalker(我使用了在 this answer 中找到的代码),但是该解决方案并没有让我访问控件本身,只是 @987654327 @。理想情况下,由于控件有一个 AutomationPeer,我想知道是否有更好的方法来获取此控件(即使 UIMap 无法获取)。


另外,附注。我已经了解 Visual Studio 在进行编码 UI 测试时会忽略 TextBlocks,因为可能会产生很多。现在我更关心的是获取ItemsControl 本身,而不是从其中生成的单个TextBlocks。

【问题讨论】:

    标签: c# wpf unit-testing ui-automation coded-ui-tests


    【解决方案1】:

    在深入研究了这个问题之后,我找到了一个半解决方案。我查看了UIMap 设计器文件如何为ListView 生成代码(我为自己的变量稍微调整了代码):

    var itemscontrol = new WpfList(window);
    itemscontrol.SearchProperties[WpfList.PropertyNames.AutomationId] = "CustomersItemsControl";
    itemscontrol.WindowTitles.Add("MainWindow");
    
    var count = itemscontrol.Items.Count(); // Returns the correct value!
    

    我复制了这段代码,它似乎也适用于ItemsControl,至少某些属性可以,例如WpfList.Items。所以,我猜这是部分解决方案。

    【讨论】:

      【解决方案2】:

      ItemsControl 和 TextBlock 内部没有 AutomationPeer。 ItemsControlAutomationPeer 是一个抽象类。 这是查找项目的解决方案之一,而不是 ItemsControl 本身:WPF ItemsControl with TestStack White。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-05-04
        • 1970-01-01
        • 1970-01-01
        • 2016-08-10
        • 2012-07-11
        • 1970-01-01
        • 2021-12-31
        • 1970-01-01
        相关资源
        最近更新 更多