使用UI Automation实现自动化测试--4.6.2

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值