selecting combobox item using ui automation

后端 未结 9 1893
猫巷女王i
猫巷女王i 2021-02-06 02:45

how do I select ComboBox\'s SelectedIndex = -1?

I wrote a code to automate testing:

AutomationElement aeBuildMachine = null;
int count =         


        
相关标签:
9条回答
  • 2021-02-06 03:15

    For me, the answer by gotmug required the CacheRequest to be activated. I implemented this as an extension method

     public static bool SelectDropDownItem(this AutomationElement comboBoxElement, string item)
            {
                bool itemFound = false;
                AutomationElement elementList;
                CacheRequest cacheRequest = new CacheRequest();
                cacheRequest.Add(AutomationElement.NameProperty);
                cacheRequest.TreeScope = TreeScope.Element | TreeScope.Children;
    
                using (cacheRequest.Activate())
                {
                    // Load the list element and cache the specified properties for its descendants.
                    Condition cond = new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.List);
                    elementList = comboBoxElement.FindFirst(TreeScope.Children, cond);
                }
    
                //Loop thru and find the actual ListItem
                foreach (AutomationElement child in elementList.CachedChildren)
                    if (child.Cached.Name == item)
                    {
                        SelectionItemPattern select = (SelectionItemPattern)child.GetCurrentPattern(SelectionItemPattern.Pattern);
                        select.Select();
                        itemFound = true;
                        break;
                    }
                return itemFound;
            }
    
    0 讨论(0)
  • 2021-02-06 03:17

    There isn't a big change but only a few to notice,

    1. Don't need to use collapse pattern, calling expand will do the trick for you.
    2. Using treescope.subtree worked for me instead of children.

    The code sample would be like this,

    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();
    }
    
    0 讨论(0)
  • 2021-02-06 03:20

    This is what worked for me.

        /// <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;
        }
    

    Usage

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

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

    0 讨论(0)
提交回复
热议问题