Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I tell if an element matches a PropertyCondition in Microsoft UI Automation?

I'm trying to find an AutomationElement in a particular row of a GridView (so there are many identical elements). I'm iterating over the elements in the row, and I'd like to use a matcher to see if a particular element matches the Condition I'm passing to it. I'm starting with simple PropertyConditions.

Here's my test:

[TestFixture]
public class ConditionMatcherBehaviour
{
    [Test]
    public void ShouldMatchAPropertyConditionByItsValue()
    {
        var conditionMatcher = new ConditionMatcher();
        var condition = new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Pane);
        Assert.True(conditionMatcher.Matches(AutomationElement.RootElement, condition));
    }
}

And here's the code:

public class ConditionMatcher : IMatchConditions
{
    public bool Matches(AutomationElement element, Condition condition)
    {
        var propertyCondition = (PropertyCondition) condition;
        return propertyCondition.Value.Equals(element.GetCurrentPropertyValue(propertyCondition.Property));
    }
}

Unfortunately the test fails. The ControlType of the root element (the desktop) is indeed ControlType.Pane, but bizarrely the PropertyCondition.Value is "50033".

Any ideas as to how I can test a PropertyCondition outside of FindFirst / FindAll?

(My workaround is to create my own condition type and test that instead, but I'd like to check that I'm not misunderstanding something / doing something stupid.)

like image 642
Lunivore Avatar asked Jul 25 '10 09:07

Lunivore


1 Answers

Found it.

public class ConditionMatcher : IMatchConditions
{
    public bool Matches(AutomationElement element, Condition condition)
    {
        return new TreeWalker(condition).Normalize(element) != null;
    }
}

Not exactly obvious, but it works for both matching and non-matching conditions. Thanks to all who looked and thought about it for a bit. Hopefully this will help someone else!

like image 193
Lunivore Avatar answered Oct 16 '22 15:10

Lunivore