/
SampleUIAAutoamtion.cs
344 lines (313 loc) · 14.7 KB
/
SampleUIAAutoamtion.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Automation;
using System.Threading;
using System.Security.Cryptography;
using System.Security.Permissions;
using System.Security;
using System.Runtime.InteropServices;
namespace UIAUtomation
{
public class UIAutomation
{
public AutomationElement GetRootElement()
{
return AutomationElement.RootElement;
}
public AutomationElement GetElement(AutomationElement rootElement, AutomationProperty property, object value)
{
return GetElement(rootElement, property, value, TreeScope.Children);
}
public AutomationElement GetElement(AutomationElement rootElement, AutomationProperty property, object value, TreeScope searchScope)
{
AutomationElement aeMainWindow = null;
int numWaits = 0;
do
{
aeMainWindow = rootElement.FindFirst(searchScope, new PropertyCondition(property, value));
++numWaits;
Thread.Sleep(200);
} while (aeMainWindow == null && numWaits < 50);
return aeMainWindow;
}
public AutomationElement GetElementWithoutWait(AutomationElement rootElement, AutomationProperty property, object value)
{
return GetElementWithoutWait(rootElement, property, value, TreeScope.Children);
}
public AutomationElement GetElementWithoutWait(AutomationElement rootElement, AutomationProperty property, object value, TreeScope searchScope)
{
AutomationElement aeMainWindow = rootElement.FindFirst(searchScope, new PropertyCondition(property, value));
return aeMainWindow;
}
public bool PressButtonOnWindow(AutomationElement element, AutomationProperty property, object value)
{
return PressButtonOnWindow(element, property, value, TreeScope.Children);
}
public bool PressButtonOnWindow(AutomationElement element, AutomationProperty property, object value, TreeScope treeScope)
{
try
{
//var window = GetElementWithoutWait(element, property, value,treeScope);
//if (window == null)
// window = GetElement(element,property,value,treeScope);
//if(window==null)
// return false;
//else
//{
var buttonInvoke = GetInvokePattern(element, property, value, TreeScope.Descendants);
buttonInvoke.Invoke();
return true;
// }
}
catch
{
return false;
}
}
public InvokePattern GetInvokePattern(AutomationElement window, AutomationProperty property, object value, TreeScope treeScope)
{
AutomationElement aeButtonElement;
int numWaits = 0;
do
{
aeButtonElement = window.FindFirst(treeScope, this.GetPropertyCondition(property, value));
++numWaits;
Thread.Sleep(500);
} while (aeButtonElement == null && numWaits < 75);
object objPattern;
InvokePattern invokePatternObj;
if (aeButtonElement.TryGetCurrentPattern(InvokePattern.Pattern, out objPattern))
{
invokePatternObj = objPattern as InvokePattern;
return invokePatternObj;
}
return null;
}
public ValuePattern GetValuePatternWithoutWait(AutomationElement window, AutomationProperty property, object value, TreeScope searchScope)
{
AutomationElement aeTextBoxElement = window.FindFirst(searchScope, GetPropertyCondition(property, value));
if (aeTextBoxElement == null)
throw new ElementNotAvailableException("TextBoxElement Element not Available");
object objPattern;
ValuePattern valuePatternObj;
if (aeTextBoxElement.TryGetCurrentPattern(ValuePattern.Pattern, out objPattern))
{
valuePatternObj = objPattern as ValuePattern;
return valuePatternObj;
}
else
throw new ElementNotEnabledException("The Value Pattern was not retrieved from the element");
}
public InvokePattern GetInvokePatternWithoutWait(AutomationElement window, AutomationProperty property, object value, TreeScope searchScope)
{
AutomationElement aeButtonElement = window.FindFirst(searchScope, GetPropertyCondition(property, value));
if (aeButtonElement == null)
throw new ElementNotAvailableException("Button Element not Available. Try the GetInvokePattern method which has a wait");
object objPattern;
InvokePattern invokePatternObj;
if (aeButtonElement.TryGetCurrentPattern(InvokePattern.Pattern, out objPattern))
{
invokePatternObj = objPattern as InvokePattern;
return invokePatternObj;
}
else
throw new ElementNotEnabledException("The Invoke Pattern was not retrieved from the element");
}
public ExpandCollapsePattern GetExpandCollapsePattern(AutomationElement element, AutomationProperty property, object value, TreeScope searchScope)
{
AutomationElement aeExpanderElement;
int numWaits = 0;
do
{
aeExpanderElement = GetFirstChildNode(element, property, value, searchScope);
++numWaits;
Thread.Sleep(300);
} while (aeExpanderElement == null && numWaits < 75);
object objPattern;
ExpandCollapsePattern togPattern;
if (true == aeExpanderElement.TryGetCurrentPattern(ExpandCollapsePattern.Pattern, out objPattern))
{
togPattern = objPattern as ExpandCollapsePattern;
return togPattern;
}
else
return null;
}
public void SetValueOnTextBox(AutomationElement element, AutomationProperty property, object value, TreeScope searchScope, string valueToBeSet)
{
var valuePattern = GetValuePatternWithoutWait(element, property, value, searchScope);
if (valuePattern != null)
valuePattern.SetValue(valueToBeSet);
}
public ExpandCollapsePattern GetExpandCollapsePatternWithoutWait(AutomationElement element, AutomationProperty property, object value, TreeScope searchScope)
{
try
{
AutomationElement aeExpanderElement = GetFirstChildNode(element, property, value, searchScope);
if (aeExpanderElement == null)
throw new ElementNotAvailableException("Expander Element not available. Try the GetExpandCollapsePattern which has a wait");
object objPattern;
ExpandCollapsePattern togPattern;
if (true == element.TryGetCurrentPattern(ExpandCollapsePattern.Pattern, out objPattern))
{
togPattern = objPattern as ExpandCollapsePattern;
return togPattern;
}
else
return null;
}
catch
{
return null;
}
}
public SelectionItemPattern GetSelectionItemPattern(AutomationElement element, AutomationProperty property, object value, TreeScope searchScope)
{
AutomationElement aeSelectionPattern;
int numWaits = 0;
do
{
aeSelectionPattern = GetFirstChildNode(element, property, value, searchScope);
++numWaits;
Thread.Sleep(300);
} while (aeSelectionPattern == null && numWaits < 75);
object objPattern;
SelectionItemPattern selectionItemPattern;
if (true == element.TryGetCurrentPattern(SelectionItemPattern.Pattern, out objPattern))
{
selectionItemPattern = objPattern as SelectionItemPattern;
return selectionItemPattern;
}
else
return null;
}
public SelectionItemPattern GetSelectionItemWithoutWait(AutomationElement element, AutomationProperty property, object value, TreeScope searchScope)
{
try
{
AutomationElement aeExpanderElement = GetFirstChildNode(element, property, value, searchScope);
if (aeExpanderElement == null)
throw new ElementNotAvailableException("Expander Element not available. Try the GetSelectionItemPattern which has a wait");
object objPattern;
SelectionItemPattern togPattern;
if (true == element.TryGetCurrentPattern(SelectionItemPattern.Pattern, out objPattern))
{
togPattern = objPattern as SelectionItemPattern;
return togPattern;
}
else
return null;
}
catch
{
return null;
}
}
public TextPattern GetTextPatternWithoutWait(AutomationElement element, AutomationProperty property, object value, TreeScope searchScope)
{
try
{
AutomationElement aeTextElement = GetFirstChildNode(element, property, value, searchScope);
if (aeTextElement == null)
throw new ElementNotAvailableException("Text Element not available");
object objPattern;
TextPattern txtPattern;
if (true == aeTextElement.TryGetCurrentPattern(TextPattern.Pattern, out objPattern))
{
txtPattern = objPattern as TextPattern;
return txtPattern;
}
else
return null;
}
catch
{
return null;
}
}
public string GetTextFromTextElement(AutomationElement element, AutomationProperty property, object value, TreeScope searchScope)
{
var textPattern = GetTextPatternWithoutWait(element, property, value, searchScope);
if (textPattern != null)
return textPattern.DocumentRange.GetText(-1);
return null;
}
public PropertyCondition GetPropertyCondition(AutomationProperty property, object value)
{
return new PropertyCondition(property, value);
}
public AutomationElementCollection GetAllChildNodes(AutomationElement element, AutomationProperty automationProperty, object value, TreeScope treeScope)
{
var allChildNodes = element.FindAll(treeScope, GetPropertyCondition(automationProperty, value));
if (allChildNodes == null)
throw new ElementNotAvailableException("Not able to find the child nodes of the element");
return allChildNodes;
}
public AutomationElement GetFirstChildNode(AutomationElement element, AutomationProperty property, object value, TreeScope searchScope)
{
var firstchildNode = element.FindFirst(searchScope, GetPropertyCondition(property, value));
if (firstchildNode == null)
throw new ElementNotAvailableException("Not able to find the first child node of the element");
return firstchildNode;
}
public bool FirstChildTextNodeContains(AutomationElement element, string toCheck)
{
var firstTextNode = this.GetFirstChildNode(element, AutomationElement.ControlTypeProperty, ControlType.Text, TreeScope.Children);
if (firstTextNode.Current.Name == toCheck)
return true;
else
return false;
}
public void SelectComboBoxItem(AutomationElement element, int indexToBeSelected, AutomationProperty property, object value, TreeScope searchScope)
{
var allChildren = this.ExpandComboBoxViewAndReturnChildren(element, property, value, searchScope);
if (allChildren.Count < indexToBeSelected)
throw new Exception("The combobox has fewer items than those that need to be selected");
var itemToBeSelected = allChildren[indexToBeSelected];
var selectPattern = this.GetSelectionItemPattern(itemToBeSelected, AutomationElement.ControlTypeProperty, ControlType.ListItem, TreeScope.Element);
if (selectPattern != null)
selectPattern.Select();
var togglePattern = this.GetExpandCollapsePattern(element, property, value, TreeScope.Descendants);
if (togglePattern != null)
togglePattern.Collapse();
}
public AutomationElementCollection ExpandComboBoxViewAndReturnChildren(AutomationElement element, AutomationProperty property, object value, TreeScope searchScope)
{
try
{
var comboBox = GetFirstChildNode(element, AutomationElement.AutomationIdProperty, value, searchScope);
var expandPattern = GetExpandCollapsePattern(comboBox, AutomationElement.ControlTypeProperty, ControlType.ComboBox, TreeScope.Element);
if (expandPattern == null)
throw new ElementNotAvailableException("Couldnt Find Expand Pattern in combobox");
expandPattern.Expand();
return this.GetAllChildNodes(comboBox, AutomationElement.ControlTypeProperty, ControlType.ListItem, TreeScope.Children);
}
catch (Exception e1)
{
throw e1;
}
}
public void WorkaroundPopulateControlTree(int x1, int y1)
{
uint x = (uint)x1;
uint y = (uint)y1;
NativeMethods.SetCursorPos(x1, y1);
Thread.Sleep(200);
NativeMethods.mouse_event(NativeMethods.MOUSEEVENTF_LEFTDOWN | NativeMethods.MOUSEEVENTF_LEFTUP, x, y, 0, 0);
}
public bool WaitTillElementSelected(AutomationElement aeMainWindow, AutomationProperty automationProperty, object value, TreeScope treeScope)
{
var btnStartAnalysis = GetElement(aeMainWindow, automationProperty, value, treeScope);
int numWait = 0;
do
{
if (btnStartAnalysis.Current.IsEnabled == true)
return true;
Thread.Sleep(2000);
} while (btnStartAnalysis.Current.IsEnabled == false && numWait < 90);
return false;
}
}
}