selecting combobox item using ui automation

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

    I thought I would share this as a simple way to select any item from a ComboBox or other item container:

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

    element is the ComboBox or item container, item is the string name or literal value of the item in the container. Once you have the item you can do the following to select it:

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

    Hope this helps others. I derived this pattern from the MSDN documentation on Automation, found here:

    MSDN - Automation and Cached Children

提交回复
热议问题