我创建了一个小型 WPF 应用程序(只是为了探索编码 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 中找到的代码),但是该解决方案无法让我访问控制自身,只是 AutomationElement
。理想情况下,由于控件有一个 AutomationPeer,我想知道是否有更好的方法来访问此控件(即使无法访问 UIMap)。
另外,附注。我已经知道 Visual Studio 在进行编码 UI 测试时会忽略 TextBlock
,因为可能会产生很多。现在我更关心获取 ItemsControl
本身,而不是从其中生成的单个 TextBlock
。
最佳答案
在深入研究这个问题之后,我找到了一个半解决方案。我查看了 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
.所以,我猜这是一个部分解决方案。
关于c# - 如何让 Microsoft Coded UI Test Builder 识别 ItemsControl?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21384706/