2011-04-28 51 views
6

如何選擇組合框的SelectedIndex = -1?使用ui automation選擇組合框項目

我寫了代碼進行自動化測試:

AutomationElement aeBuildMachine = null; 
int count = 0; 
do 
{ 
    Console.WriteLine("\nLooking for Build Machine Combo Box"); 
    aeBuildMachine = aeTabitemmain.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.ClassNameProperty, "ListBoxItem")); 
    if (aeBuildMachine == null) 
      throw new Exception("No Build Machine Combo Box"); 
    else 
      Console.WriteLine("Found Build Machine Combo Box"); 
    ++count; 
} 
while (aeBuildMachine == null && count < 50); 

Console.WriteLine("Selecting Build machine from combobox..."); 
SelectionItemPattern spBuildmachine = (SelectionItemPattern)aeBuildMachine.GetCurrentPattern(SelectionItemPattern.Pattern); 

如何使用這個SelectionItemPattern

回答

1

http://msdn.microsoft.com/de-de/library/system.windows.automation.selectionitempattern_members(v=VS.85).aspx

這是回答你的問題我的理解它。

但是..它真的是你的問題嗎?

無論如何,您可以從選擇中添加或移除SelectableItems,我認爲它屬於它的父項,同樣適用於檢查它們是否被選中。

+0

或者http://msdn.microsoft.com/en-us/library/system.windows.automation.selectionitempattern_members%28v=vs.90 %29.aspx如果你更喜歡英文MSDN。 – SteveWilkinson 2012-01-17 09:58:09

17

這是關於的100倍更復雜比它需要,但我終於得到它的工作。 WPF組合框最大的問題是,就自動化而言,它看起來沒有任何ListItems ,直到組合框被擴展爲

以下代碼使用ExpandCollapse模式暫時下拉列表然後摺疊它,然後它可以使用ComboBox上的FindFirst獲取要選擇的ListItem,然後使用SelectionItem模式選擇它。

在原始問題的情況下,選擇-1意味着未選擇任何項目。對此沒有辦法,但可以簡單地使用FindAll來獲取ListItems的集合,依次爲每個ListItem獲取SelectionItem模式並調用其RemoveFromSelection方法。

public static void SetSelectedComboBoxItem(AutomationElement comboBox, string item) 
    { 
     AutomationPattern automationPatternFromElement = GetSpecifiedPattern(comboBox, "ExpandCollapsePatternIdentifiers.Pattern"); 

     ExpandCollapsePattern expandCollapsePattern = comboBox.GetCurrentPattern(automationPatternFromElement) as ExpandCollapsePattern; 

     expandCollapsePattern.Expand(); 
     expandCollapsePattern.Collapse(); 

     AutomationElement listItem = comboBox.FindFirst(TreeScope.Subtree, new PropertyCondition(AutomationElement.NameProperty, item)); 

     automationPatternFromElement = GetSpecifiedPattern(listItem, "SelectionItemPatternIdentifiers.Pattern"); 

     SelectionItemPattern selectionItemPattern = listItem.GetCurrentPattern(automationPatternFromElement) as SelectionItemPattern; 

     selectionItemPattern.Select(); 
    } 

    private static AutomationPattern GetSpecifiedPattern(AutomationElement element, string patternName) 
    { 
     AutomationPattern[] supportedPattern = element.GetSupportedPatterns(); 

     foreach (AutomationPattern pattern in supportedPattern) 
     { 
      if (pattern.ProgrammaticName == patternName) 
       return pattern; 
     } 

     return null; 
    } 
1

,我想我會分享這是一個簡單的方法來選擇從ComboBox或其它物品容器中的任何項目:

protected AutomationElement GetItem(AutomationElement element, string item) 
    { 
     AutomationElement elementList; 
     CacheRequest cacheRequest = new CacheRequest(); 
     cacheRequest.Add(AutomationElement.NameProperty); 
     cacheRequest.TreeScope = TreeScope.Element | TreeScope.Children; 

     elementList = element.GetUpdatedCache(cacheRequest); 

     foreach (AutomationElement child in elementList.CachedChildren) 
      if (child.Cached.Name == item) 
       return child; 

     return null; 
    } 

元素是ComboBox或物品的容器,產品字符串名稱或容器中項目的字面值。一旦你有這個項目,你可以做以下選擇它:

protected void Select(AutomationElement element) 
    { 
     SelectionItemPattern select = (SelectionItemPattern)element.GetCurrentPattern(SelectionItemPattern.Pattern); 
     select.Select(); 
    } 

希望這可以幫助別人。我來源於自動化MSDN文檔這種模式,在這裏找到:

MSDN - Automation and Cached Children

+0

不知道爲什麼,但對於我的項目,如果select是當前選定的元素,則select.Select()將引發異常。 - Win32目標如果有所作爲 – 2014-03-07 02:06:20

+0

使用緩存的請求使其變得複雜。 – 2017-09-18 06:38:05

1

我認爲這可能是這一個細微提供在組合框中沒有按列表項的一個簡單的通用ComobBox值設置器的最簡單方法沒有重複。

private void SetCombobValueByUIA(AutomationElement ctrl, string newValue) 
{ 
    ExpandCollapsePattern exPat = ctrl.GetCurrentPattern(ExpandCollapsePattern.Pattern) 
                   as ExpandCollapsePattern; 

    if(exPat== null) 
    { 
     throw new ApplicationException("Bad Control type..."); 
    } 

    exPat.Expand(); 

    AutomationElement itemToSelect = ctrl.FindFirst(TreeScope.Descendants, new 
          PropertyCondition(AutomationElement.NameProperty,newValue)); 

    SelectionItemPattern sPat = itemToSelect.GetCurrentPattern(
               SelectionItemPattern.Pattern) as SelectionItemPattern ; 
    sPat. Select(); 
} 
2

這就是爲我工作的。

/// <summary> 
    /// Extension method to select item from comboxbox 
    /// </summary> 
    /// <param name="comboBox">Combobox Element</param> 
    /// <param name="item">Item to select</param> 
    /// <returns></returns> 
    public static bool SelectComboboxItem(this AutomationElement comboBox, string item) 
    { 
     if (comboBox == null) return false; 
     // Get the list box within the combobox 
     AutomationElement listBox = comboBox.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.List)); 
     if (listBox == null) return false; 
     // Search for item within the listbox 
     AutomationElement listItem = listBox.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.NameProperty, item)); 
     if (listItem == null) return false; 
     // Check if listbox item has SelectionItemPattern 
     object objPattern; 
     if (true == listItem.TryGetCurrentPattern(SelectionItemPatternIdentifiers.Pattern, out objPattern)) 
     { 
      SelectionItemPattern selectionItemPattern = objPattern as SelectionItemPattern; 
      selectionItemPattern.Select(); // Invoke Selection 
      return true; 
     } 
     return false; 
    } 

使用

 AutomationElement paymentCombobox = element.FindFirst(
      TreeScope.Children, 
      new PropertyCondition(AutomationElement.NameProperty, "cbPayment") 
     ); 
     paymentCombobox.SelectComboboxItem("Cash"); 

資源https://msdn.microsoft.com/en-us/library/ms752305(v=vs.110).aspx

+1

你可以在代碼中留下一些註釋來描述你的代碼的作用嗎? – Shawn 2016-07-07 20:45:01

1

沒有大的變化,但只有少數注意到,

  1. 不需要使用坍塌模式,調用擴展將爲你做的伎倆。
  2. 使用treescope.subtree爲我工作,而不是兒童。

的代碼示例會是這樣,

public static void SelectValueInComboBox(string comboBox, string value) 
{ 
    var comboBoxElement = HelperMethods.FindElementFromAutomationID(comboBox); 
    if (comboBoxElement == null) 
     throw new Exception("Combo Box not found"); 

    ExpandCollapsePattern expandPattern = (ExpandCollapsePattern)comboBoxElement.GetCurrentPattern(ExpandCollapsePattern.Pattern); 

    AutomationElement comboboxItem = comboBoxElement.FindFirst(TreeScope.Subtree, new PropertyCondition(AutomationElement.NameProperty, value)); 

    SelectionItemPattern selectPattern = (SelectionItemPattern)comboboxItem.GetCurrentPattern(SelectionItemPattern.Pattern); 
    selectPattern.Select(); 
}