UI Automation-ScrollPattern

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

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


ScrollPattern控件模式用于支持可充当子对象集合的可滚动容器的控件。虽然通常都会通过该控件来使用滚动功能,但这并不是必需的。目前支持的ScrollPattern的控件有ListBox,ListView,GridView,TreeView。

ScrollPattern主要方法

·         Scroll水平和垂直滚动内容区域的可见区域滚动,Scroll有个两个参数,其类型为ScrollAmout枚举类型。

·         ScrollHorizontal按指定的ScrollAmount水平滚动内容区域的当前可见区域滚动。

·         ScrollVertial按指定的ScrollAmount垂直滚动内容区域的当前可见区域滚动。

示例:

      public static void launchScorollpattern()

        {

            Process scroll =Process.Start(@"C:/Users/roxio/Desktop/ScrollPatternTest.exe");

            Thread.Sleep(2000);

            //Get Main window

            AutomationElement window1 =AutomationElement.RootElement.FindFirst(TreeScope.Children, newPropertyCondition(AutomationElement.NameProperty, "Form1"));

            Assert.IsNotNull(window1);

            Thread.Sleep(2000);

 

            //Get listbox

            AutomationElement listbox =window1.FindFirst(TreeScope.Children,newPropertyCondition(AutomationElement.AutomationIdProperty,"listBox1"));

            Assert.IsNotNull(listbox);

            Thread.Sleep(2000);

 

            AutomationElementCollection listitems = listbox.FindAll(TreeScope.Children, newPropertyCondition(AutomationElement.ControlTypeProperty, ControlType.ListItem));

 

            for (int i = 0; i < listitems.Count; i++)

            {

                Utility.UIA.ControlPattern.SelectionItempattern.select(listitems[i]);

 

                Thread.Sleep(2000);

            }

 

            ScrollPattern scrollpattern = Utility.UIA.ControlPattern.Scrollpattern.GetScrollPattern(listbox);

            if (scrollpattern.Current.VerticallyScrollable)

                while (listitems[8].Current.IsOffscreen)

                {

                    scrollpattern.ScrollVertical(ScrollAmount.LargeDecrement);

                }

 

        }

 

 

        public static ScrollPattern GetScrollPattern(

            AutomationElement targetControl)

        {

            ScrollPattern scrollPattern = null;

 

            try

            {

                scrollPattern =

                    targetControl.GetCurrentPattern(

                    ScrollPattern.Pattern)

                    as ScrollPattern;

            }

            // Object doesn't support the ScrollPattern control pattern

            catch (InvalidOperationException)

            {

                Log.Error(string.Format("Element with AutomationId '{0}' and Name '{1}' does not support the ScrollPattern.",

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

                return null;

               

            }

 

            return scrollPattern;

        }


0 0