如何判断某个元素是否与Microsoft UI Automation中的PropertyCondition匹配?
问题描述:
我想在一个GridView的特定行中找到一个AutomationElement(所以有很多相同的元素)。我遍历行中的元素,并且我想使用匹配器来查看特定元素是否与我传递给它的条件匹配。我从简单的PropertyConditions开始。如何判断某个元素是否与Microsoft UI Automation中的PropertyCondition匹配?
这里是我的测试:
[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));
}
}
而这里的代码:
public class ConditionMatcher : IMatchConditions
{
public bool Matches(AutomationElement element, Condition condition)
{
var propertyCondition = (PropertyCondition) condition;
return propertyCondition.Value.Equals(element.GetCurrentPropertyValue(propertyCondition.Property));
}
}
不幸的是,测试失败。根元素(桌面)的ControlType确实是ControlType.Pane,但奇怪的是PropertyCondition.Value是“50033”。
有关如何在FindFirst/FindAll之外测试PropertyCondition的任何想法?
(我的解决方法是创建自己的条件类型,并测试替代,但我想检查我没有误解的东西/做一些愚蠢的事。)
答
发现了它。
public class ConditionMatcher : IMatchConditions
{
public bool Matches(AutomationElement element, Condition condition)
{
return new TreeWalker(condition).Normalize(element) != null;
}
}
不完全明显,但它适用于匹配和不匹配的条件。感谢所有看过并仔细想过的人。希望这会帮助别人!