selecting combobox item using ui automation

后端 未结 9 1910
猫巷女王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;
            }
    

提交回复
热议问题