2010-07-25 29 views
4

我想在一個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的任何想法?

(我的解決方法是創建自己的條件類型,並測試替代,但我想檢查我沒有誤解的東西/做一些愚蠢的事。)

回答

3

發現了它。

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

不完全明顯,但它適用於匹配和不匹配的條件。感謝所有看過並仔細想過的人。希望這會幫助別人!

相關問題