WPF UI Automation - AutomationElement.FindFirst fails when there are lots of elements

Posted by Orion Edwards on Stack Overflow See other posts from Stack Overflow or by Orion Edwards
Published on 2010-06-08T22:28:00Z Indexed on 2010/06/08 22:32 UTC
Read the original article Hit count: 518

Filed under:
|

We've got some automated UI tests for our WPF app (.NET 4); these test use the UI Automation API's.

We call AutomationElement.FindFirst to find a target element, and then interact with it.

Example (pseudocode):

var nameEquals = new PropertyCondition(AutomationElement.NameProperty, "OurAppWindow");
var appWindow = DesktopWindow.FindFirst(TreeScope.Children, nameEquals);
// this succeeds

var idEquals = new PropertyCondition(AutomationElement.AutomationIdProperty, "ControlId");
var someItem = appWindow.FindFirst(TreeScope.Descendants, idEquals);
// this suceeds sometimes, and fails sometimes!

The problem is, the appWindow.FindFirst will sometimes fail and return null, even when the element is present. I've written a helper function which walks the UI automation tree manually and prints it out, and the element with the correct ID is present in all cases.

It seems to be related to how many other items are also being displayed in the window. If there are no other items then it always succeeds, but when there are many other complex UI elements being displayed alongside it, then the find fails.

I can't find any documented element limit mentioned for any of the automation API's - is there some way around this? I'm thinking I might have to write my own implemententation of FindFirst which does the tree walk manually itself... As far as I can tell this should work, because my tree-printer utility function does exactly that, and it's ok, but it seems like this would be unnecessary and slow :-(

Any help would be greatly appreciated

© Stack Overflow or respective owner

Related posts about wpf

Related posts about ui-automation