如何在对项目进行分组的WPF ItemsControl上使用UI自动化?

Gra*_*meF 5 wpf xaml itemscontrol ui-automation

我正在使用Microsoft UI Automation(即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>
Run Code Online (Sandbox Code Playgroud)

这将产生一个窗口,其中包含按类别分组的项目,每个项目都有一个我想使用UI Automation单击的按钮:

带有列表的窗口的屏幕截图
(来源:brizzly.com

但是,如果我查看UISpy.exe(或使用导航AutomationElement),我只会看到组(即使在Raw视图中):

UISpy
(来源:brizzly.com

如您所见,这些组在那里,但是它们不包含任何项目,因此没有地方可以找到这些按钮。我已经在WPF 3.5 SP1和WPF 4.0中进行了尝试,并获得了相同的结果。

是否可以对分组的项目使用UI自动化,如果可以,如何使用?

Tra*_*svi 6

我遇到了这个问题,并设法从执行“GenericAutomationPeer”来解决它http://www.colinsalmcorner.com/post/genericautomationpeer--helping-the-coded-ui-framework-find-your-custom-controls和为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;
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

希望这对仍在寻找答案的人有所帮助!


Ken*_*art 1

您使用什么工具来编写自动化脚本?我本以为可以选择深入研究 WPF 的逻辑/可视树,而不是依赖 Win32 树(如 UISpy 所示)。

如果您使用Snoop查看同一应用程序,您将看到完整的视觉和逻辑树。