TL;DR: What am I doing wrong that is causing the workspace pane to show up in Inspect Objects but not show up in my custom code?
I am trying t
Very nice question. Based upon the problem you've documented, it's clear that your conditional:
PropertyCondition workspaceCond = new PropertyCondition(
AutomationElement.NameProperty, "Workspace", PropertyConditionFlags.IgnoreCase);
... fails evaluation. Why?
The answer is how your conditional is evaluated. Looking at your element tree, we notice this property for Workspace
:
IsWindowPatternAvailable: false
And for the main window and Untitled3
:
IsWindowPatternAvailable: true
From MSDN:
UIA_IsWindowPatternAvailablePropertyId 30044
Identifies the
IsWindowPatternAvailable
property, which indicates whether the Window control pattern is available for the automation element. IfTRUE
, a client can retrieve an IUIAutomationWindowPattern interface from the element.
We find a repro in this thread, which implies the same failure pattern as the one you are currently experiencing. We also note the lack of Window
properties present for this element because IUIAutomationWindowPattern
is inaccessible.
A workaround is available from the aforelinked thread. Instead of PropertyCondition
, one might use:
public class ConditionMatcher : IMatchConditions
{
public bool Matches(AutomationElement element, Condition condition)
{
return new TreeWalker(condition).Normalize(element) != null;
}
}
Or, alternately, one might use the workaround you've given, provided your tree structure is guaranteed to be shallow (and thus, appropriate to the name of this site, will not trigger a stack overflow).
Admittedly, this wasn't the most obvious issue. In the perfect world, MSDN should have better documentation on this topic.