![](/img/trans.png)
[英]How do I get the entire contents of a Wpf DataGrid with Coded UI Test
[英]How do you get the Microsoft Coded UI Test Builder to recognize an ItemsControl?
我創建了一個小型WPF應用程序(僅用於探索Coded UI測試的工作原理)。 我的應用程序包含ItemsControl
,但Coded UI Test Builder 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 Test Builder也無法找到要添加到UIMap的控件。 我解決它的唯一方法是獲取根AutomationElement,然后使用TreeWalker.ControlViewWalker
(我使用了本答案中的代碼),但是該解決方案不允許我訪問控件本身,只有AutomationElement
。 理想情況下,由於控制器有一個AutomationPeer,我想知道是否有更好的方法來獲得此控件(即使無法獲得UIMap)。
另外,附注。 我已經理解Visual Studio在進行Coded 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
。 所以,我認為這是一個部分解決方案。
ItemsControl和TextBlock內部沒有AutomationPeer。 ItemsControlAutomationPeer是一個抽象類。 這是找到項目的解決方案之一,而不是ItemsControl本身: 帶有TestStack White的WPF ItemsControl 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.