我創建了一個小型WPF應用程序(只是爲了探討編碼UI測試的工作原理)。我的應用程序包含ItemsControl
,但編碼的UI測試生成器UIMap找不到合適的控件。如何讓Microsoft Coded UI測試生成器識別ItemsControl?
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控制的問題。我解決這個問題的唯一方法是獲取Root AutomationElement,然後利用TreeWalker.ControlViewWalker
(我使用this answer中的代碼),但是該解決方案不允許我訪問控制本身,只有AutomationElement
。理想情況下,因爲控制器有一個AutomationPeer,所以我想知道是否有更好的方法來實現這種控制(即使不可能達到UIMap)。
另外,附註。我已經瞭解Visual Studio在執行Coded UI測試時會忽略TextBlock
,因爲可能會產生如此多的內容。現在我更關注的是ItemsControl
本身,而不是從其中生成的個人TextBlock
。