[英]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). 我正在尝试在GridView的特定行中找到一个AutomationElement(因此有许多相同的元素)。 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. 我从简单的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". 根元素(桌面)的ControlType确实是ControlType.Pane,但奇怪的是PropertyCondition.Value是“50033”。
Any ideas as to how I can test a PropertyCondition outside of FindFirst / FindAll? 关于如何在FindFirst / FindAll之外测试PropertyCondition的任何想法?
(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! 希望这会帮助别人!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.