我使用 Microsoft UI Automation(即 AutomationElement )对我的应用程序运行自动验收测试.这已经很顺利,但我遇到的情况似乎没有暴露给自动化框架. 我有一个 ItemsControl (虽然我可以使用它的一个派
AutomationElement)对我的应用程序运行自动验收测试.这已经很顺利,但我遇到的情况似乎没有暴露给自动化框架.
我有一个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 XPath=@Name}" />
<Button Content="Play" />
</StackPanel>
</Border>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Window>
这会生成一个窗口,其中包含按类别分组的项目,每个项目都有一个我想用UI Automation点击的按钮:
Screenshot of window with a list http://pics.brizzly.com/thumb_lg_2C45.jpg
但是,如果我查看UISpy.exe(或使用AutomationElement导航),我只能看到组(即使在Raw视图中):
UISpy http://pics.brizzly.com/thumb_lg_2C47.jpg
正如您所看到的,这些组在那里,但它们不包含任何项目,因此无处可寻找按钮.我在WPF 3.5 SP1和WPF 4.0中都尝试了这个并获得了相同的结果.
是否可以对分组的项目使用UI自动化,如果是,如何使用?
我遇到了这个问题并设法通过从 http://www.colinsalmcorner.com/post/genericautomationpeer–helping-the-coded-ui-framework-find-your-custom-controls实现’GenericAutomationPeer’并为GroupItems添加一个特殊案例来解决它.using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Automation;
using System.Windows.Automation.Peers;
using System.Windows.Media;
using System.Xml;
namespace ClassLibrary1
{
public class MyItemsControl : ItemsControl
{
protected override AutomationPeer OnCreateAutomationPeer()
{
return new GenericAutomationPeer(this);
}
}
public class GenericAutomationPeer : UIElementAutomationPeer
{
public GenericAutomationPeer(UIElement owner) : base(owner)
{
}
protected override List<AutomationPeer> GetChildrenCore()
{
var list = base.GetChildrenCore();
list.AddRange(GetChildPeers(Owner));
return list;
}
private List<AutomationPeer> GetChildPeers(UIElement element)
{
var list = new List<AutomationPeer>();
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(element); i++)
{
var child = VisualTreeHelper.GetChild(element, i) as UIElement;
if (child != null)
{
AutomationPeer childPeer;
if (child is GroupItem)
{
childPeer = new GenericAutomationPeer(child);
}
else
{
childPeer = UIElementAutomationPeer.CreatePeerForElement(child);
}
if (childPeer != null)
{
list.Add(childPeer);
}
else
{
list.AddRange(GetChildPeers(child));
}
}
}
return list;
}
}
}
我希望这有助于任何人仍在寻找答案!
