UI Automation-ExpandCollapsePattern

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;

        }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值