Reputation: 6717
I want to use Windows AutomationElement
s to simulate Userinput during testing.
My particulat usecase is manupilating a ListBox selection and from what I find online I will need an AutomationElement for my listbox in order to manipulate it.
Suppose I had a window like this:
<Window x:Class="CryptoAdmin_Test.Helper.FreshWindow"
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:CryptoAdmin_Test.Helper">
<StackPanel>
<UserControl x:FieldModifier="public" x:Name="FindMe" />
</StackPanel>
</Window>
Since I have a reference to the UserControl I should be able to find it without starting my search from the desktop (AutomationElement.RootElement
).
What is the fastest way to get an AutomationElement
for my window.FindMe
UserControl
?
Using AutomationElement.RootElement.FindFirst(...);
would start with the desktop and I do not see a generic way that would make this search fast without any possibility for false positives.
Upvotes: 1
Views: 1729
Reputation: 1662
This should be the fastest way to find it. This also assumes you give the window a name because otherwise it will be pretty hard to find unless you start the process from your application and have a process id for it.
AutomationElement mainWindow = AutomationElement.RootElement.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.NameProperty, "MainWindow"));
AutomationElement findMe = mainWindow.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.NameProperty, "FindMe"));
Since the TreeScope is set to children it will not scan the whole tree when looking for the elements in question. Depending on what your user control does the element you get back may be some what useless though. Without implementing some custom patterns for your control about the only thing you will be able to do is get other elements from it.
Upvotes: 2