我创建了一个小的WPF应用程序(只是为了探索Coded UI测试的工作原理)。我的应用程序包含一个
我遇到的问题是,即使WPF有一个
另外,顺便一提。我已经了解到 Visual Studio 在进行 Coded UI 测试时会忽略
ItemsControl
,但是Coded 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
,Coded UI测试生成器也无法定位要添加到UIMap中的控件。我唯一解决的方法是获取根AutomationElement,然后利用TreeWalker.ControlViewWalker
(我使用了this answer中的代码),但这种解决方案并没有让我访问控件本身,只有AutomationElement
。理想情况下,由于该控件有一个AutomationPeer,我想知道是否有更好的方法来访问此控件(即使对于UIMap来说是不可能的)。
另外,顺便一提。我已经了解到 Visual Studio 在进行 Coded UI 测试时会忽略
TextBlock
,因为可能会产生大量的 TextBlock
。现在我更关心的是如何获取 ItemsControl
本身,而不是从中生成的单个 TextBlock
。