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 con开发者_如何学Cdition)
{
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.)
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!
精彩评论