使用UI Automation实现自动化测试--4.6.2 (SelectItemPattern Demo)
如下代码演示了使用SelectionItemPattern来实现listview item 的多选操作:

Code
1
using System;
2
using System.Text;
3
using System.Diagnostics;
4
using System.Threading;
5
using System.Windows.Automation;
6
7
namespace 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:

Code
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
1
using System;2
using System.Text;3
using System.Diagnostics;4
using System.Threading;5
using System.Windows.Automation;6

7
namespace UIATest8


{9
class Program10

{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
try30

{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
else45

{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 item68
/// </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
else93

{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 SelectItemPattern109

110

/**//// <summary>111
/// Get SelectItemPattern112
/// </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
#endregion127
}128
}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



浙公网安备 33010602011771号