selecting combobox item using ui automation

后端 未结 9 1879
猫巷女王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 02:57

    I think this might be the easiest way for a simple generic ComobBox value setter this one is fine provided the list items in the ComboBox doesn't have duplicates.

    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();
    }
    
    0 讨论(0)
  • 2021-02-06 02:58
    <pre>
      public static void SetComboBox(AutomationElement ComboxBox, string SelectedValue)
            {
                AutomationElement ListBox = ComboxBox.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.AutomationIdProperty, "ListBox"));
                AutomationElement SelectedItem = ListBox.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.NameProperty, SelectedValue));
                ((SelectionItemPattern)SelectedItem.GetCurrentPattern(SelectionItemPattern.Pattern)).Select();
            }
    

    Instruction on how to use: 1) Copy and Paste into Utility Class 2) Find your ComboBox AutomationElement 3) Utility.SetCombox(ComboxAutomationElement, "SelectedText")

    To understand:

    Tree structure of ComboBox as follow:

    ComboBox->ListBox (children)->ListItems(children) [each combobox has a ListBox as a child, and the ListBox has all ListItems as children].

    Each ListItem has SelectedItemPattern, invoke which you want to select.

    I later found out that "Shaz " has better coding, and i vote him as the best code.

    ** Comment: To do UIAAutomation, you must map the AutomationElements of your application to a TreeView, which makes everything simple and understood.

    0 讨论(0)
  • 2021-02-06 03:04

    This is about 100 times more complicated than it needs to be, but I finally got it working. The big issue with the WPF ComboBox is that as far as Automation goes, it doesn't appear to have any ListItems until the ComboBox has been expanded.

    The following code uses the ExpandCollapse pattern to momentarily drop down the list and then collapse it, then it can use FindFirst on the ComboBox to get the ListItem to be selected, and then use the SelectionItem pattern to select it.

    In the case of the original question, a selection of -1 means no items selected. There is no method for this, but you could simply use FindAll to get a collection of ListItems, get the SelectionItem pattern for each one in turn and call its RemoveFromSelection method.

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

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

    That is the answer to your question as I understood it.

    But.. is it really your question?

    Anyhow, you can add or remove SelectableItems from a selection which - I suppose - belong to its parent, same goes for some other things like checking whether they are selected.

    0 讨论(0)
  • 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

    0 讨论(0)
  • 2021-02-06 03:12

    I used this code in WindowsForms comboBox

    Usage

    comboBox.SetSelectedComboBoxItem("ValueYouWantToSelect");
    

    Add this Class to your project:

    public static class AutomationElementExtensions
        { 
            public static void InvokeControl(this AutomationElement element)
            {
                InvokePattern invokePattern = null;
    
                try
                {
                    invokePattern =
                        element.GetCurrentPattern(InvokePattern.Pattern)
                        as InvokePattern;
                }
                catch (ElementNotEnabledException)
                {
                    // Object is not enabled
                    return;
                }
                catch (InvalidOperationException)
                {
                    // object doesn't support the InvokePattern control pattern
                    return;
                }
    
                invokePattern.Invoke();
                Thread.Sleep(500);
            } 
    
    
            public static void SetSelectedComboBoxItem(this AutomationElement comboBox, string item)
            {
                AutomationPattern automationPatternFromElement = GetSpecifiedPattern(comboBox, "ExpandCollapsePatternIdentifiers.Pattern");
    
                ExpandCollapsePattern expandCollapsePattern = comboBox.GetCurrentPattern(automationPatternFromElement) as ExpandCollapsePattern;
    
                expandCollapsePattern.Expand();
    
    
    
                AutomationElement listItem = comboBox.FindFirst(TreeScope.Subtree, new PropertyCondition(AutomationElement.NameProperty, item));
    
                InvokeControl(listItem); 
            }
    
            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;
            }
    
    
        }
    
    0 讨论(0)
提交回复
热议问题