使用UI Automation Framework选择ListBox项

时间:2016-11-30 08:22:02

标签: c# wpf microsoft-ui-automation

我有一个包含列表框的窗口,我的设置允许单元测试直接访问列表框。

<Window x:Class="Demo.DemoWindow"
                xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
                xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
                xmlns:local="clr-namespace:Demo">
    <ListBox 
        x:Name="MyListBox" 
        x:FieldModifier="public" 
        ItemsSource="{Binding ViewModelItems}" 
        DisplayMemberPath="Name"
        />
</Window>

我想使用UI Automation为我的单元测试在视图上设置一个值。以下是我认为与此相关的辅助方法的开始。

    public static void Select(System.Windows.Controls.ListBox listbox, string item)
    {
        ListBoxAutomationPeer peer = new ListBoxAutomationPeer(listbox);
        IItemContainerProvider provider = peer.GetPattern(PatternInterface.ItemContainer) as IItemContainerProvider;
        IRawElementProviderSimple rawItem = provider.FindItemByProperty(null, 0, item);
        //rawitem is null here -> could be because I have no concept of how to use FindItemByProperty
        //The rest seems incorrect - why would my highlevel UI interaction break down to IRawElementProviderSimple
        //Is this Item already realized or am I working on a virtualized item that I need to realize?
        ISelectionItemProvider selectable = rawItem.GetPatternProvider((int)PatternInterface.SelectionItem) as ISelectionItemProvider;
        selectable.Select();
    }

我似乎错过了文档中的内容。有msdn article解释了使用虚拟化项目的概念。由于我使用的是WPF包装类,因此这应该等同于使用IItemContainerProvider

我的实现不起作用,我找不到能够为我解决这个问题的文档。

如何使用显示的文本在项目列表中选择项目。

0 个答案:

没有答案
相关问题