My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
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
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Automation;
using White.Core.AutomationElementSearch;
using White.Core.UIItems.Custom;
using White.Core.UIItems.WindowStripControls;

namespace White.Core.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>
public class SearchCriteria
{
private readonly SearchConditions conditions = new SearchConditions();
private IndexCondition indexCondition = IndexCondition.NotSpecified;
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));
}

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 ByControlType(Type testControlType)
{
var searchCriteria = new SearchCriteria(SearchConditionFactory.CreateForControlType(testControlType));
searchCriteria.InferCustomItemType(testControlType);
return searchCriteria;
}

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 AndOfFramework(string frameworkId)
{
conditions.Insert(0, SearchConditionFactory.CreateForFrameworkId(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)
{
InferCustomItemType(testControlType);
conditions.Insert(0, SearchConditionFactory.CreateForControlType(testControlType));
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 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.ToString());
return stringBuilder.ToString();
}

public override bool Equals(object obj)
{
if (obj == null) return false;
var other = obj as SearchCriteria;
if (other == null) return false;

foreach (SearchCondition searchCondition in conditions)
if (!other.conditions.Contains(searchCondition)) return false;

return indexCondition.Equals(other.indexCondition);
}

public override int GetHashCode()
{
int hashCode = 0;
foreach (SearchCondition condition in conditions)
hashCode += condition.GetHashCode();
return indexCondition.GetHashCode() + hashCode;
}

public virtual bool AppliesTo(AutomationElement automationElement)
{
foreach (SearchCondition condition in conditions)
{
if (!condition.AppliesTo(automationElement))
return false;
}
return true;
}

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.ToString()));
return searchCriteria.NotIdentifiedByText("System Menu Bar");
}

public virtual SearchCriteria Merge(SearchCriteria other)
{
foreach (var searchCondition in other.conditions)
{
if (!conditions.Contains(condition => condition.OfSameType(searchCondition)))
{
conditions.Add(searchCondition);
}
}
return this;
}
}
}

Change log

r66 by petmongrels on Apr 2, 2010   Diff
Fixed position based search bugs
Go to: 
Project members, sign in to write a code review

Older revisions

r64 by petmongrels on Mar 30, 2010   Diff
Fixed bugs
r63 by petmongrels on Mar 30, 2010   Diff
bug fixes
r40 by petmongrels on Feb 4, 2010   Diff
CustomUIItem supports all controls.

Making combobox action ready is
configurable.
Combobox value is set only when
...
All revisions of this file

File info

Size: 9111 bytes, 247 lines
Powered by Google Project Hosting