【发布时间】: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