selecting combobox item using ui automation

后端 未结 9 1908
猫巷女王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:20

    This is what worked for me.

        /// 
        /// Extension method to select item from comboxbox
        /// 
        /// Combobox Element
        /// Item to select
        /// 
        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

提交回复
热议问题