-
Notifications
You must be signed in to change notification settings - Fork 476
Expand file tree
/
Copy pathSearchCriteria.cs
More file actions
287 lines (245 loc) · 10.4 KB
/
SearchCriteria.cs
File metadata and controls
287 lines (245 loc) · 10.4 KB
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Windows.Automation;
using TestStack.White.AutomationElementSearch;
using TestStack.White.UIItems.Custom;
using TestStack.White.UIItems.WindowItems;
using TestStack.White.UIItems.WindowStripControls;
namespace TestStack.White.UIItems.Finders
{
/// <summary>
/// SearchCritera can be used when UIItem identification is not satisfied by standard Get methods on UIItemContainer (Window is subclass of
/// UIItemContainer). Multiple criterias can be supplied together based on which the UIItem would be searched. All the conditions would put together as
/// AND condition
/// e.g. SearchCriteria.ByAutomationId("foo").ByControlType(typeof(TextBox)).Indexed(1)
/// </summary>
[DataContract]
public class SearchCriteria
{
[DataMember]
private readonly SearchConditions conditions = new SearchConditions();
[DataMember]
private IndexCondition indexCondition = IndexCondition.NotSpecified;
[DataMember]
private Type customItemType;
private SearchCriteria() { }
private SearchCriteria(SearchCondition searchCondition)
{
conditions.Add(searchCondition);
}
private SearchCriteria(IndexCondition searchCondition)
{
conditions.Add(searchCondition);
indexCondition = searchCondition;
}
public static SearchCriteria All
{
get { return new SearchCriteria(); }
}
/// <summary>
/// Create a SearchCriteria with text
/// </summary>
/// <param name="text">For managed applications this is name given to controls in the application code.
/// For unmanaged applications this is text of the control or label next to it if it doesn't have well defined text.</param>
/// <returns></returns>
public static SearchCriteria ByText(string text)
{
return new SearchCriteria(SearchConditionFactory.CreateForName(text));
}
/// <summary>
/// Create criteria with specified index
/// </summary>
/// <param name="zeroBasedIndex"></param>
/// <returns></returns>
public static SearchCriteria Indexed(int zeroBasedIndex)
{
var indexCondition = new IndexCondition(zeroBasedIndex);
var criteria = new SearchCriteria(indexCondition);
return criteria;
}
public static SearchCriteria ByAutomationId(string identification)
{
return new SearchCriteria(SearchConditionFactory.CreateForAutomationId(identification));
}
/// <summary>
/// Create criteria for specified window with specified xpath
/// </summary>
/// <param name="xpath"></param>
/// <param name="window"></param>
/// <returns></returns>
public static SearchCriteria ByXPath(string xpath, Window window)
{
return new SearchCriteria(SearchConditionFactory.CreateForXPath(xpath, window));
}
public static SearchCriteria ByFramework(string framework)
{
return new SearchCriteria(SearchConditionFactory.CreateForFrameworkId(framework));
}
public static SearchCriteria ByControlType(ControlType controlType)
{
return new SearchCriteria(SearchConditionFactory.CreateForControlType(controlType));
}
public static SearchCriteria ByNativeProperty(AutomationProperty automationProperty, string value)
{
return new SearchCriteria(SearchConditionFactory.CreateForNativeProperty(automationProperty, value));
}
public static SearchCriteria ByNativeProperty(AutomationProperty automationProperty, bool value)
{
return new SearchCriteria(SearchConditionFactory.CreateForNativeProperty(automationProperty, value));
}
public static SearchCriteria ByNativeProperty(AutomationProperty automationProperty, object value)
{
return new SearchCriteria(SearchConditionFactory.CreateForNativeProperty(automationProperty, value));
}
public static SearchCriteria ByControlType(Type testControlType, WindowsFramework framework)
{
var searchCriteria = new SearchCriteria(SearchConditionFactory.CreateForControlType(testControlType, framework));
searchCriteria.InferCustomItemType(testControlType);
return searchCriteria;
}
public static SearchCriteria ByClassName(string className)
{
return new SearchCriteria(SearchConditionFactory.CreateForClassName(className));
}
internal virtual Condition AutomationCondition
{
get { return conditions.AutomationCondition; }
}
internal virtual Condition AutomationConditionWith(PropertyCondition propertyCondition)
{
Condition condition = conditions.AutomationCondition;
return new AndCondition(condition, propertyCondition);
}
public virtual bool IsIndexed
{
get { return indexCondition != IndexCondition.NotSpecified; }
}
public virtual Type CustomItemType
{
get { return customItemType; }
}
internal virtual AutomationSearchCondition AutomationSearchCondition
{
get
{
var automationSearchCondition = new AutomationSearchCondition();
foreach (SearchCondition searchCondition in conditions)
{
Condition condition = searchCondition.AutomationCondition;
if (condition != null) automationSearchCondition.Add(condition);
}
return automationSearchCondition;
}
}
public virtual SearchCriteria AndIndex(int zeroBasedIndex)
{
indexCondition = new IndexCondition(zeroBasedIndex);
conditions.Add(indexCondition);
return this;
}
public virtual SearchCriteria AndByText(string text)
{
conditions.Insert(0, SearchConditionFactory.CreateForName(text));
return this;
}
public virtual SearchCriteria AndByClassName(string className)
{
conditions.Insert(0, SearchConditionFactory.CreateForClassName(className));
return this;
}
public virtual SearchCriteria AndOfFramework(WindowsFramework framework)
{
conditions.Insert(0, SearchConditionFactory.CreateForFrameworkId(framework.FrameworkId()));
return this;
}
public virtual SearchCriteria NotIdentifiedByText(string name)
{
conditions.Insert(0, new NotCondition(SearchConditionFactory.CreateForName(name)));
return this;
}
public virtual SearchCriteria AndControlType(Type testControlType, WindowsFramework framework)
{
InferCustomItemType(testControlType);
conditions.Insert(0, SearchConditionFactory.CreateForControlType(testControlType, framework));
return this;
}
public virtual SearchCriteria AndControlType(ControlType controlType)
{
conditions.Insert(0, SearchConditionFactory.CreateForControlType(controlType));
return this;
}
public virtual SearchCriteria AndAutomationId(string id)
{
conditions.Insert(0, SearchConditionFactory.CreateForAutomationId(id));
return this;
}
public virtual SearchCriteria AndNativeProperty(AutomationProperty automationProperty, string value)
{
conditions.Insert(0, SearchConditionFactory.CreateForNativeProperty(automationProperty, value));
return this;
}
public virtual SearchCriteria AndNativeProperty(AutomationProperty automationProperty, bool value)
{
conditions.Insert(0, SearchConditionFactory.CreateForNativeProperty(automationProperty, value));
return this;
}
public virtual SearchCriteria AndNativeProperty(AutomationProperty automationProperty, object value)
{
conditions.Insert(0, SearchConditionFactory.CreateForNativeProperty(automationProperty, value));
return this;
}
public virtual List<AutomationElement> Filter(List<AutomationElement> automationElements)
{
return conditions.Filter(automationElements);
}
public virtual IUIItem IndexedItem(UIItemCollection collection)
{
return indexCondition.Filter(collection);
}
public override string ToString()
{
var stringBuilder = new StringBuilder();
stringBuilder.Append(conditions);
return stringBuilder.ToString();
}
public override bool Equals(object obj)
{
if (obj == null) return false;
var other = obj as SearchCriteria;
if (other == null) return false;
return conditions.All(searchCondition => other.conditions.Contains(searchCondition)) && indexCondition.Equals(other.indexCondition);
}
public override int GetHashCode()
{
int hashCode = conditions.Sum(condition => condition.GetHashCode());
return indexCondition.GetHashCode() + hashCode;
}
public virtual bool AppliesTo(AutomationElement automationElement)
{
return conditions.All(condition => condition.AppliesTo(automationElement));
}
private void InferCustomItemType(Type testControlType)
{
customItemType = typeof(CustomUIItem).IsAssignableFrom(testControlType) ? testControlType : null;
}
internal static SearchCriteria ForMenuBar(WindowsFramework framework)
{
var searchCriteria = new SearchCriteria(SearchConditionFactory.CreateForControlType(typeof(MenuBar), framework));
return searchCriteria.NotIdentifiedByText("System Menu Bar");
}
public virtual SearchCriteria Merge(SearchCriteria other)
{
foreach (var searchCondition in other.conditions)
{
if (!conditions.Any(condition => condition.OfSameType(searchCondition)))
{
conditions.Add(searchCondition);
}
}
return this;
}
}
}