UI Automation-ExpandCollapsePattern

来源:互联网 发布:notepad 排版json 编辑:程序博客网 时间:2024/05/23 16:55

ExpandCollapsePattern是可以进行展开(以显示内容)和折叠(以隐藏内容)的控件。例如ComboBox控件支持ExpandCollapsePattern

ExpandCollapsePattern有两个主要方法:

Expand()方法和Collapse()方法。可以把ExpandCollpase再封装。示例如下:

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, new PropertyCondition(AutomationElement.NameProperty, "Calculator"));

 

            //Assert.IsNotNull(window);

            Thread.Sleep(2000);

 

            //Expand view button

            //Recognize view menu

 

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

            Assert.IsNotNull(view);

            Thread.Sleep(2000);

 

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

            Thread.Sleep(2000);

 

            //Recognize menu item Worksheets

            AutomationElement worksheets = window.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.NameProperty, "Worksheets"));

            Assert.IsNotNull(worksheets);

            Utility.UIA.ControlPattern.ExpandCollapsepattern.ExpandCollapseMenuItem(worksheets);

            Thread.Sleep(2000);

 

             }

       

public static void Expand(AutomationElement targetControl)

        {

            ExpandCollapsePattern expandCollapsePattern = null;

 

            try

            {

                expandCollapsePattern =

                    targetControl.GetCurrentPattern(

                    ExpandCollapsePattern.Pattern)

                    as ExpandCollapsePattern;

            }

            // Object doesn't support the ExpandCollapsePattern control pattern.

            catch (InvalidOperationException)

            {

                throw new InvalidOperationException(string.Format("Element with AutomationId '{0}' and Name '{1}' does not support the ExpandCollapsePattern control pattern ",

      targetControl.Current.AutomationId, targetControl.Current.Name));

            }

 

            expandCollapsePattern.Expand();

        }

 

 

 public static void Collapse(AutomationElement targetControl)

        {

            ExpandCollapsePattern expandCollapsePattern = null;

 

            try

            {

                expandCollapsePattern =

                    targetControl.GetCurrentPattern(

                    ExpandCollapsePattern.Pattern)

                    as ExpandCollapsePattern;

            }

            // Object doesn't support the ExpandCollapsePattern control pattern.

            catch (InvalidOperationException)

            {

                throw new InvalidOperationException(string.Format("Element with AutomationId '{0}' and Name '{1}' does not support the ExpandCollapsePattern control pattern ",

                    targetControl.Current.AutomationId, targetControl.Current.Name));

            }

 

            expandCollapsePattern.Collapse();

        }

 

public static ExpandCollapsePattern GetExpandCollapsePattern(

            AutomationElement targetControl)

        {

            ExpandCollapsePattern expandCollapsePattern = null;

 

            try

            {

                expandCollapsePattern =

                    targetControl.GetCurrentPattern(

                    ExpandCollapsePattern.Pattern)

                    as ExpandCollapsePattern;

            }

            // Object doesn't support the ExpandCollapsePattern control pattern.

            catch (InvalidOperationException)

            {

                return null;

            }

 

            return expandCollapsePattern;

        }

原创粉丝点击