UI Automation-SelectItemPattern

来源:互联网 发布:mac spss许可证 编辑:程序博客网 时间:2024/05/18 03:46

 感谢原作者的分享  http://blog.csdn.net/lynn_yan/article/category/611860


SelectItemPattern用于选择容器控件如列表框和组合框中的各个项。

支持SlectItemPattern的控件有ListView,ListBox,RadioButton,GridView等。

SelectItemPattern的三个重要方法:

·         AddToSelection:将当前元素添加到所选项的集合;

·         RemoveFromSelction:从选定项的集合中移除当前元素。

·         Select:取消所有以选中的项,然后选择当前元素。

可以通过Current属性的IsSelected属性来判断AutomationElement是否被selected.

public static SelectionItemPattern GetSelectionItemPattern(AutomationElement element)

        {

            object currentPattern = null;

            if (!element.TryGetCurrentPattern(SelectionItemPattern.Pattern, out currentPattern))

            {

                throw new Exception(string.Format("Element with AutomationId '{0}' and Name '{1}' does not support the SelectionItemPattern.",

               element.Current.AutomationId, element.Current.Name));

            }

            return currentPattern as SelectionItemPattern;

        }

 

        public static void select(AutomationElement element)

        {

            SelectionItemPattern selectitempattern = GetSelectionItemPattern(element);

            selectitempattern.Select();

        }

 

示例:

public static void launchCal()

        {

            //Launch Calculator

            Process proc = Process.Start("calc");

            Thread.Sleep(2000);

 

            //Recognize Calculator Window  "Calculator"

            AutomationElement window = AutomationElement.RootElement.FindFirst(TreeScope.Children, newPropertyCondition(AutomationElement.NameProperty, "Calculator"));

 

            //Assert.IsNotNull(window);

            Thread.Sleep(2000);

 

            //Expand view button

            //Recognize view menu

 

            AutomationElement view = window.FindFirst(TreeScope.Descendants, newPropertyCondition(AutomationElement.NameProperty, "View"));

            Assert.IsNotNull(view);

            Thread.Sleep(2000);

 

            Utility.UIA.ControlPattern.ExpandCollapsepattern.Expand(view);

            Thread.Sleep(2000);

 

            //Recognize menu item "Programmer"

            AutomationElement Programmer = window.FindFirst(TreeScope.Descendants, newPropertyCondition(AutomationElement.NameProperty, "Programmer"));

            Thread.Sleep(2000);

            InvokePattern programmerpattern=Utility.UIA.ControlPattern.Invokepattern.GetInvokePattern(Programmer);

            programmerpattern.Invoke();

            Thread.Sleep(2000);

 

            //Recoginze Oct Radio Button "Oct"

            AutomationElement oct=window.FindFirst(TreeScope.Descendants,newPropertyCondition(AutomationElement.NameProperty,"Oct"));

            Thread.Sleep(2000);

           

            SelectionItemPatternoctpattern=Utility.UIA.ControlPattern.SelectionItempattern.GetSelectionItemPattern(oct);

            if (octpattern.Current.IsSelected.ToString() == "False")

            {

                octpattern.Select();

            }       


0 0
原创粉丝点击