我使用Microsoft UI Automation(即AutomationElement
)對我的應用程序進行自動驗收測試。這個過程很順利,但是我遇到了一個似乎並未暴露於自動化框架的情況。如何在對項目進行分組的WPF ItemsControl上使用UI自動化?
我有一個ItemsControl
(雖然我可以使用其派生控件之一,例如ListBox
),我使用CollectionViewSource
來分組項目。下面是一個完整的窗口證明:
<Window x:Class="GroupAutomation.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Orchestra">
<Window.Resources>
<!-- Take some simple data -->
<XmlDataProvider x:Key="SampleData" XPath="Orchestra/Instrument">
<x:XData>
<Orchestra xmlns="">
<Instrument Name="Flute" Category="Woodwind" />
<Instrument Name="Trombone" Category="Brass" />
<Instrument Name="French horn" Category="Brass" />
</Orchestra>
</x:XData>
</XmlDataProvider>
<!-- Add grouping -->
<CollectionViewSource Source="{Binding Source={StaticResource SampleData}}" x:Key="GroupedView">
<CollectionViewSource.GroupDescriptions>
<PropertyGroupDescription PropertyName="@Category" />
</CollectionViewSource.GroupDescriptions>
</CollectionViewSource>
</Window.Resources>
<!-- Show it in an ItemsControl -->
<ItemsControl ItemsSource="{Binding Source={StaticResource GroupedView}}" HorizontalAlignment="Left" Margin="4">
<ItemsControl.GroupStyle>
<GroupStyle>
<GroupStyle.HeaderTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Name}" FontWeight="Bold" />
</DataTemplate>
</GroupStyle.HeaderTemplate>
</GroupStyle>
</ItemsControl.GroupStyle>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Border Padding="4" Margin="4" Background="#FFDEDEDE">
<StackPanel>
<Label Content="{Binding [email protected]}" />
<Button Content="Play" />
</StackPanel>
</Border>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Window>
這會產生含有分爲其類別的商品的窗口,並且每個項目都有一個按鈕,我想點擊與UI自動化:
Screenshot of window with a list http://pics.brizzly.com/thumb_lg_2C45.jpg
但是,如果我看着UISpy.exe(或AutomationElement
導航),我只看到了組(甚至在原始視圖):
UISpy http://pics.brizzly.com/thumb_lg_2C47.jpg
正如你所看到的,這些組在那裏,但它們不包含任何項目,所以無處尋找按鈕。我已經在WPF 3.5 SP1和WPF 4.0中嘗試了這一點,並得到相同的結果。
是否可以在分組的項目上使用UI自動化,如果是這樣,如何?
直接使用項目控制項目,如下所示。 [link](http://abusanad.net/2016/12/19/wpf-itemscontrol-with-teststack-white/) – user1912383 2016-12-19 21:15:16