使用UI Automation实现自动化测试(4.6.2)——SelectItemPattern Demo

发表于:2009-11-12 14:46

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:开着拖拉机    来源:51Testing软件测试网采编

  如下代码演示了使用SelectionItemPattern来实现listview item 的多选操作:

  1using System;
  2using System.Text;
  3using System.Diagnostics;
  4using System.Threading;
  5using System.Windows.Automation;
  6
  7namespace UIATest
  8{
  9    class Program
 10    {
 11        static void Main(string[] args)
 12        {
 13            Process process = Process.Start(@"F:\CSharpDotNet\AutomationTest\ATP\WpfApp\bin\Debug\WpfApp.exe");
 14            int processId = process.Id;
 15
 16            Thread.Sleep(1000);
 17            MutlSelect(new int[] { 0, 1 }, processId, false);
 18        }
 19
 20        /**//// <summary>
 21        /// Get the automation elemention of current form.
 22        /// </summary>
 23        /// <param name="processId">Process Id</param>
 24        /// <returns>Target element</returns>
 25        public static AutomationElement FindWindowByProcessId(int processId)
 26        {
 27            AutomationElement targetWindow = null;
 28            int count = 0;
 29            try
 30            {
 31                Process p = Process.GetProcessById(processId);
 32                targetWindow = AutomationElement.FromHandle(p.MainWindowHandle);
 33                return targetWindow;
 34            }
 35            catch (Exception ex)
 36            {
 37                count++;
 38                StringBuilder sb = new StringBuilder();
 39                string message = sb.AppendLine(string.Format("Target window is not existing.try #{0}", count)).ToString();
 40                if (count > 5)
 41                {
 42                    throw new InvalidProgramException(message, ex);
 43                }
 44                else
 45                {
 46                    return FindWindowByProcessId(processId);
 47                }
 48            }
 49        }
 50
 51
 52        /**//// <summary>
 53        /// Get the automation element by automation Id.
 54        /// </summary>
 55        /// <param name="windowName">Window name</param>
 56        /// <param name="automationId">Control automation Id</param>
 57        /// <returns>Automatin element searched by automation Id</returns>
 58        public static AutomationElement FindElementById(int processId, string automationId)
 59        {
 60            AutomationElement aeForm = FindWindowByProcessId(processId);
 61            AutomationElement tarFindElement = aeForm.FindFirst(TreeScope.Descendants,
 62            new PropertyCondition(AutomationElement.AutomationIdProperty, automationId));
 63            return tarFindElement;
 64        }
 65       
 66        /**//// <summary>
 67        /// Bulk select the list item
 68        /// </summary>
 69        /// <param name="indexes">List item index collection</param>
 70        /// <param name="processId">Application process Id</param>
 71        /// <param name="isSelectAll">Is select all or not</param>
 72        public static void MutlSelect(int[] indexes, int processId, bool isSelectAll)
 73        {
 74            AutomationElement targetElement = FindElementById(processId, "listView1");
 75
 76            AutomationElementCollection rows =
 77                targetElement.FindAll(TreeScope.Descendants,
 78                new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.ListItem));
 79
 80            object multiSelect;
 81
 82            if (isSelectAll)
 83            {
 84                for (int i = 1; i < rows.Count - 1; i++)
 85                {
 86                    if (rows[i].TryGetCurrentPattern(SelectionItemPattern.Pattern, out multiSelect))
 87                    {
 88                        (multiSelect as SelectionItemPattern).AddToSelection();
 89                    }
 90                }
 91            }
 92            else
 93            {
 94                if (indexes.Length > 0)
 95                {
 96                    for (int j = 0; j < indexes.Length; j++)
 97                    {
 98                        int tempIndex = indexes[j];
 99                        if (rows[tempIndex].TryGetCurrentPattern(SelectionItemPattern.Pattern, out multiSelect))
100                        {
101                            (multiSelect as SelectionItemPattern).AddToSelection();
102                        }
103                    }
104                }
105            }
106        }
107
108        SelectItemPattern#region SelectItemPattern
109
110        /**//// <summary>
111        /// Get SelectItemPattern
112        /// </summary>
113        /// <param name="element">AutomationElement instance</param>
114        /// <returns>SelectItemPattern instance</returns>
115        public static SelectionItemPattern GetSelectionItemPattern(AutomationElement element)
116        {
117            object currentPattern;
118            if (!element.TryGetCurrentPattern(SelectionItemPattern.Pattern, out currentPattern))
119            {
120                throw new Exception(string.Format("Element with AutomationId '{0}' and Name '{1}' does not support the SelectionItemPattern.",
121                    element.Current.AutomationId, element.Current.Name));
122            }
123            return currentPattern as SelectionItemPattern;
124        }
125
126        #endregion
127    }
128}

  如下代码为对应的XAML:

 1<Window x:Class="WpfApp.Window2"
 2    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 3    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 4    Title="Window2" Height="412" Width="585">
 5    <Grid>
 6        <ListView Margin="2,97,0,163" Name="listView1">
 7            <ListViewItem>Kaden</ListViewItem>
 8            <ListViewItem>KangYi</ListViewItem>
 9            <ListViewItem>John</ListViewItem>
10        </ListView>
11    </Grid>
12</Window>
13

相关阅读:

使用UI Automation实现自动化测试(4.6.1)——SelectionItemPattern

使用UI Automation实现自动化测试(4.5)——WindowPattern

使用UI Automation实现自动化测试(4.4)—— ValuePattern

使用UI Automation实现自动化测试(4.3)——InvokePattern

使用UI Automation实现自动化测试(4.2)——ExpandCollapsePattern

使用UI Automation实现自动化测试(4.1)——DockPattern

《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号