我在用微软用户界面自动化 (i.e. 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](https://i.stack.imgur.com/4UJ7W.png)
(source: brizzly.com)
但是,如果我查看UISpy.exe(或使用导航AutomationElement
)我只看到组(即使在原始视图中):
![UISpy](https://i.stack.imgur.com/lKb2k.png)
(source: brizzly.com)
正如您所看到的,组在那里,但它们不包含任何项目,因此无处可查找按钮。我在 WPF 3.5 SP1 和 WPF 4.0 中都尝试过此操作,并得到相同的结果。
是否可以对分组的项目使用 UI 自动化?如果可以,如何实现?
我遇到了这个问题并设法通过实现“GenericAutomationPeer”来解决它
并添加一个特殊情况GroupItem
s.
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;
}
}
}
我希望这对仍在寻找答案的人有所帮助!
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)