selecting combobox item using ui automation

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

提交回复
热议问题