0

I have a window that contains a listbox, My setup allows a unit test to access the listbox directly.

<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>

I want to set a value on the view using UI Automation for my unit tests. Here is the start of a helper method that I think would be relevant for that.

    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();
    }

I seem to miss something in the documentation. There is an msdn article that explains the concepts of working with virtualized items. Since I am using the WPF wrapper classes this should be equivalent to using the IItemContainerProvider.

My implementation does not work and I can not find the piece of documentation that would clear this up for me.

How do I select an Item In a list of items using the displayed text.

Johannes
  • 6,490
  • 10
  • 59
  • 108
  • 3
    Microsoft UI Automation is for automating an App from another App (another process). So you don't work with peers, pattern providers, IRawXXX interfaces, etc, you work with AutomationElement, AutomationProperty, etc.. What are you trying to do? – Simon Mourier Dec 01 '16 at 07:46
  • @SimonMourier I am experimenting with peers so I can learn a bit about some backgrounds. The premise for this learning session is UI testing in an nunit test suite. In a real life situation I would go for Teststack.White and use a wrapping framework that removes complexity for me. The motivation behind this question is to get a better understanding of the topic based on an explicit example. – Johannes Dec 05 '16 at 10:06

0 Answers0