2013-11-15 28 views
0

我試圖創建一些編碼的UI測試,用於多次使用WPF控件的「集合」。我可以使用Coded UI測試生成器單獨創建每個測試,但我期望創建「常規」的情況,因爲每個測試都非常相似,只需稍作修改即可。試圖找到一個特定的WpfControl在編碼的UI測試中使用

爲了創建這個一般情況下,我需要能夠找到(然後單擊)給定的父母控制(在這種情況下,WpfWindow;我正在尋找的控制是幾個級別深,但我希望能夠找到控制,而不管深度如何)。我一直能夠使用AutomationElement找到我正在尋找的控件,但編碼的UI似乎並不喜歡它,並且一旦找到它就無法點擊它。

我已經嘗試在父窗口上使用GetChildren(),然後遞歸搜索以查找有問題的控件。我正在使用SearchProperties尋找我正在尋找的特定WPF控件的NameControlType,但它看起來並不像所有東西都有Name,並且它失敗了。

我也試過設置SearchProperties爲特定控件,但它會拋出一個異常說它找不到。

有什麼建議嗎?我是以正確的方式開展這項工作的,還是缺少一些根本性的東西?

除此之外:它是如何使用Coded UI測試生成器構建類似的Coded UI測試的「不好」?我的意思是,使用編碼UI測試生成器創建800個不同的測試似乎是錯誤的,它們都具有相同的前5個步驟(根據運行位置的不同而有所不同)。

回答

0

我重溫了AutomationElement解決方案,並能夠找出點擊部分。

public AutomationElement findControl(AutomationProperty propertyOfItem, string itemToSearchFor, System.Windows.Automation.ControlType controlType) 
{ 
      Condition condition = new System.Windows.Automation.AndCondition(
        new System.Windows.Automation.PropertyCondition(propertyOfItem, itemToSearchFor), 
        new System.Windows.Automation.PropertyCondition(AutomationElement.ControlTypeProperty, controlType)); 

      AutomationElement foundControl = AutomationElement.RootElement.FindFirst(TreeScope.Descendants | TreeScope.Children, condition); 
      return foundControl; 
} 

點擊部分從AutomationElement並將它獲取點擊點在System.Windows.Point,並基於該創建System.Drawing.Point。然後在這一點上致電Mouse.Click()

0

我們如何處理這一點是記錄控件,但不是步驟。然後您可以傳遞映射到完整的父 - 父 - 父的控件。 。 。 -parent映射成一種方法。

ControlB = genericControl(parentWindow,  
    "ControlType","Button","ControlName","Send"); 
TestOne(ControlB); 
TestOne(ControlC); 

public void TestOne(UITestControl control, string Value) 
{ 
    //do stuff 
    Mouse.Click(control); 
    Assert.AreEqual(Value, control.GetProperty("InnerText")); 
} 

通用控制:

public UITestControl genericControl(UITestControl parentWindow, String Property1, String Value1, String Property2, String Value2) 
{ 
    UITestControl control = new UITestControl(parentWindow); 
    control.TechnologyName = "MSAA"; 
    control.SearchProperties.Add(Property1, Value1, Property2, Value2); 

    return control; 
} 

如果每一步都是通用的它應該工作。

ETA - 您必須具有您想要使用的控件的某些特徵。除非要使用FindAllMatchingControls並遍歷所有控件或不同類型來過濾列表,否則必須爲每個控件都有唯一的標識信息。

+0

我不太確定這有助於。是的,它是通用的,但我的問題實際上是找到我想要的具體控制。 這裏的問題是找到控制傳遞給'公共無效TestOne(UITestControl控件,字符串值)' – user2996787

+0

我明白你的意思。我想我的下一個問題是,如果一個或多個父母對當前運行特定時,如何獲得完整的父 - 父 - 父 - 父映射的控制權。例如,我的控制(s)的結尾看起來像這樣:'...UIBrowseTreeCustom.UITreeViewTree.UICNameTreeItem.UIItemTreeItem;'其中'CName'指的是編碼UI測試所在的計算機的名稱。我會嘗試一些事情,然後會回來。 – user2996787

+0

現在我們處於問題的核心!在對象的屬性窗口中,您可以將搜索屬性從「Equals」更改爲「Contains」,以便您可以將name = WindowParent123d改爲name.Contains(WindowParent),並且它會找到匹配項。您也可以更改/刪除控件的窗口標題。 – yonitdm

相關問題