How do you get the Microsoft Coded UI Test Builder to recognize an ItemsControl?
Asked Answered
S

2

6

I created a small WPF application (just to explore how Coded UI Testing works). My application contained an ItemsControl, but the Coded UI Test Builder UIMap could not find the appropriate control.

XAML:

<ItemsControl ItemsSource="{Binding Path=Customers, Mode=OneTime}"
              AutomationProperties.AutomationId="CustomersItemsControl">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <TextBlock AutomationProperties.AutomationId="{Binding Path=Id, StringFormat='Customer_{0}', Mode=OneWay}"
                       Margin="5">
                <TextBlock.Text>
                    <MultiBinding StringFormat="{}{1}, {0}">
                        <Binding Path="FirstName" />
                        <Binding Path="LastName" />
                    </MultiBinding>
                </TextBlock.Text>
            </TextBlock>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

The problem that I have is that even though WPF has an ItemsControlAutomationPeer, the Coded UI Test Builder cannot locate the control to be added to the UIMap. The only way I got around it was by getting the root AutomationElement, then utilizing the TreeWalker.ControlViewWalker (I used the code found in this answer), however that solution doesn't give me access to the control itself, just the AutomationElement. Ideally, since there is an AutomationPeer for the control, I was wondering if there was a better way to get at this control (even if it is impossible to get to for the UIMap).


Also, a side note. I understand already that Visual Studio ignores TextBlocks when doing Coded UI Testing because there can be so many produced. Right now I'm more concerned with getting to the ItemsControl itself, not the individual TextBlocks that are generated from within it.

Stane answered 27/1, 2014 at 15:25 Comment(0)
S
4

After digging a bit into this issue, I found a semi-solution. I looked at how the UIMap designer files generate their code for a ListView (I adjusted the code a bit for my own variables):

var itemscontrol = new WpfList(window);
itemscontrol.SearchProperties[WpfList.PropertyNames.AutomationId] = "CustomersItemsControl";
itemscontrol.WindowTitles.Add("MainWindow");

var count = itemscontrol.Items.Count(); // Returns the correct value!

I copied this code and it seems to work for an ItemsControl as well, at least some properties do, such as WpfList.Items. So, it's a partial solution I guess.

Stane answered 29/1, 2014 at 22:42 Comment(0)
U
0

The ItemsControl and TextBlock don't have the AutomationPeer inside. The ItemsControlAutomationPeer is an abstract class. Here's one of the solutions to find the items, not the ItemsControl itself: WPF ItemsControl with TestStack White.

Uncanonical answered 9/8, 2017 at 17:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.