/
HelperExtensions.cs
377 lines (326 loc) · 12.4 KB
/
HelperExtensions.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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
using System.Collections.Immutable;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Drawing;
using System.Xml.Linq;
using OpenQA.Selenium;
using OpenQA.Selenium.Appium;
using OpenQA.Selenium.Appium.MultiTouch;
using UITest.Core;
namespace UITest.Appium
{
public static class HelperExtensions
{
static TimeSpan DefaultTimeout = TimeSpan.FromSeconds(15);
public static void Click(this IApp app, string element)
{
app.FindElement(element).Click();
}
public static string? GetText(this IUIElement element)
{
var response = element.Command.Execute("getText", new Dictionary<string, object>()
{
{ "element", element },
});
return (string?)response.Value;
}
public static T? GetAttribute<T>(this IUIElement element, string attributeName)
{
var response = element.Command.Execute("getAttribute", new Dictionary<string, object>()
{
{ "element", element },
{ "attributeName", attributeName },
});
return (T?)response.Value;
}
public static Rectangle GetRect(this IUIElement element)
{
var response = element.Command.Execute("getRect", new Dictionary<string, object>()
{
{ "element", element },
});
if (response?.Value != null)
{
return (Rectangle)response.Value;
}
throw new InvalidOperationException($"Could not get Rect of element");
}
public static void EnterText(this IApp app, string element, string text)
{
var appElement = app.FindElement(element);
appElement.SendKeys(text);
app.DismissKeyboard();
}
public static void DismissKeyboard(this IApp app)
{
app.CommandExecutor.Execute("dismissKeyboard", ImmutableDictionary<string, object>.Empty);
}
public static bool IsKeyboardShown(this IApp app)
{
var response = app.CommandExecutor.Execute("isKeyboardShown", ImmutableDictionary<string, object>.Empty);
var responseValue = response?.Value ?? false;
return (bool)responseValue;
}
public static void ClearText(this IApp app, string element)
{
app.FindElement(element).Clear();
}
public static void Click(this IUIElement element)
{
element.Command.Execute("click", new Dictionary<string, object>()
{
{ "element", element }
});
}
public static void SendKeys(this IUIElement element, string text)
{
element.Command.Execute("sendKeys", new Dictionary<string, object>()
{
{ "element", element },
{ "text", text }
});
}
public static void Clear(this IUIElement element)
{
element.Command.Execute("clear", new Dictionary<string, object>()
{
{ "element", element },
});
}
public static void DoubleClick(this IApp app, string element)
{
var elementToClick = app.FindElement(element);
app.CommandExecutor.Execute("doubleClick", new Dictionary<string, object>
{
{ "element", elementToClick },
});
}
public static void DragAndDrop(this IApp app, string dragSource, string dragTarget)
{
var dragSourceElement = app.FindElement(dragSource);
var targetSourceElement = app.FindElement(dragTarget);
app.CommandExecutor.Execute("dragAndDrop", new Dictionary<string, object>
{
{ "sourceElement", dragSourceElement },
{ "destinationElement", targetSourceElement }
});
}
public static void ScrollTo(this IApp app, string toElementId, bool down = true)
{
app.CommandExecutor.Execute("scrollTo", new Dictionary<string, object>
{
{ "elementId", toElementId},
{ "down", down }
});
}
public static IUIElement WaitForElement(this IApp app, string marked, string timeoutMessage = "Timed out waiting for element...", TimeSpan? timeout = null, TimeSpan? retryFrequency = null, TimeSpan? postTimeout = null)
{
IUIElement result() => app.FindElement(marked);
var results = WaitForAtLeastOne(result, timeoutMessage, timeout, retryFrequency);
return results;
}
public static void WaitForNoElement(this IApp app, string marked, string timeoutMessage = "Timed out waiting for no element...", TimeSpan? timeout = null, TimeSpan? retryFrequency = null, TimeSpan? postTimeout = null)
{
IUIElement result() => app.FindElement(marked);
WaitForNone(result, timeoutMessage, timeout, retryFrequency);
}
public static bool WaitForTextToBePresentInElement(this IApp app, string automationId, string text)
{
TimeSpan timeout = DefaultTimeout;
TimeSpan retryFrequency = TimeSpan.FromMilliseconds(500);
string timeoutMessage = $"Timed out on {nameof(WaitForTextToBePresentInElement)}.";
DateTime start = DateTime.Now;
while (true)
{
var element = app.FindElements(automationId).FirstOrDefault();
if (element != null && (element.GetText()?.Contains(text, StringComparison.OrdinalIgnoreCase) ?? false))
{
return true;
}
long elapsed = DateTime.Now.Subtract(start).Ticks;
if (elapsed >= timeout.Ticks)
{
Debug.WriteLine($">>>>> {elapsed} ticks elapsed, timeout value is {timeout.Ticks}");
return false;
}
Task.Delay(retryFrequency.Milliseconds).Wait();
}
}
/// <summary>
/// Performs a left to right swipe gesture on the screen.
/// </summary>
/// <param name="app">Represents the main gateway to interact with an app.</param>
/// <param name="swipePercentage">How far across the element to swipe (from 0.0 to 1.0).</param>
/// <param name="swipeSpeed">The speed of the gesture.</param>
/// <param name="withInertia">Whether swipes should cause inertia.</param>
public static void SwipeLeftToRight(this IApp app, double swipePercentage = 0.67, int swipeSpeed = 500, bool withInertia = true)
{
app.CommandExecutor.Execute("swipeLeftToRight", new Dictionary<string, object>
{
{ "swipePercentage", swipePercentage },
{ "swipeSpeed", swipeSpeed },
{ "withInertia", withInertia }
});
}
/// <summary>
/// Performs a left to right swipe gesture on the matching element.
/// If multiple elements are matched, the first one will be used.
/// </summary>
/// <param name="app">Represents the main gateway to interact with an app.</param>
/// <param name="marked">Marked selector to match.</param>
/// <param name="swipePercentage">How far across the element to swipe (from 0.0 to 1.0).</param>
/// <param name="swipeSpeed">The speed of the gesture.</param>
/// <param name="withInertia">Whether swipes should cause inertia.</param>
public static void SwipeLeftToRight(this IApp app, string marked, double swipePercentage = 0.67, int swipeSpeed = 500, bool withInertia = true)
{
var elementToSwipe = app.FindElement(marked);
app.CommandExecutor.Execute("swipeLeftToRight", new Dictionary<string, object>
{
{ "element", elementToSwipe},
{ "swipePercentage", swipePercentage },
{ "swipeSpeed", swipeSpeed },
{ "withInertia", withInertia }
});
}
/// <summary>
/// Performs a right to left swipe gesture on the screen.
/// </summary>
/// <param name="app">Represents the main gateway to interact with an app.</param>
/// <param name="swipePercentage">How far across the element to swipe (from 0.0 to 1.0).</param>
/// <param name="swipeSpeed">The speed of the gesture.</param>
/// <param name="withInertia">Whether swipes should cause inertia.</param>
public static void SwipeRightToLeft(this IApp app, double swipePercentage = 0.67, int swipeSpeed = 500, bool withInertia = true)
{
app.CommandExecutor.Execute("swipeRightToLeft", new Dictionary<string, object>
{
{ "swipePercentage", swipePercentage },
{ "swipeSpeed", swipeSpeed },
{ "withInertia", withInertia }
});
}
/// <summary>
/// Performs a right to left swipe gesture on the matching element.
/// If multiple elements are matched, the first one will be used.
/// </summary>
/// <param name="app">Represents the main gateway to interact with an app.</param>
/// <param name="marked">Marked selector to match.</param>
/// <param name="swipePercentage">How far across the element to swipe (from 0.0 to 1.0).</param>
/// <param name="swipeSpeed">The speed of the gesture.</param>
/// <param name="withInertia">Whether swipes should cause inertia.</param>
public static void SwipeRightToLeft(this IApp app, string marked, double swipePercentage = 0.67, int swipeSpeed = 500, bool withInertia = true)
{
var elementToSwipe = app.FindElement(marked);
app.CommandExecutor.Execute("swipeRightToLeft", new Dictionary<string, object>
{
{ "element", elementToSwipe},
{ "swipePercentage", swipePercentage },
{ "swipeSpeed", swipeSpeed },
{ "withInertia", withInertia }
});
}
/// Changes the device orientation to landscape mode.
/// </summary>
/// <param name="app">Represents the main gateway to interact with an app.</param>
public static void SetOrientationLandscape(this IApp app)
{
app.CommandExecutor.Execute("setOrientationLandscape", ImmutableDictionary<string, object>.Empty);
}
/// <summary>
/// Changes the device orientation to portrait mode.
/// </summary>
/// <param name="app">Represents the main gateway to interact with an app.</param>
public static void SetOrientationPortrait(this IApp app)
{
app.CommandExecutor.Execute("setOrientationPortrait", ImmutableDictionary<string, object>.Empty);
}
/// <summary>
/// Performs a tap / touch gesture on the given coordinates.
/// </summary>
/// <param name="app">Represents the main gateway to interact with an app.</param>
/// <param name="x">The x coordinate to tap.</param>
/// <param name="y">The y coordinate to tap.</param>
public static void TapCoordinates(this IApp app, float x, float y)
{
app.CommandExecutor.Execute("tapCoordinates", new Dictionary<string, object>
{
{ "x", x },
{ "y", y }
});
}
/// <summary>
/// Sets the value of a Slider element that matches marked.
/// </summary>
/// <param name="app">Represents the main gateway to interact with an app.</param>
/// <param name="marked">Marked selector of the Slider element to update.</param>
/// <param name="value">The value to set the Slider to.</param>
public static void SetSliderValue(this IApp app, string marked, double value)
{
var element = app.FindElement(marked);
double defaultMinimum = 0d;
double defaultMaximum = 1d;
app.CommandExecutor.Execute("setSliderValue", new Dictionary<string, object>
{
{ "element", element },
{ "value", value },
{ "minimum", defaultMinimum },
{ "maximum", defaultMaximum },
});
}
/// <summary>
/// Sets the value of a Slider element that matches marked.
/// </summary>
/// <param name="app">Represents the main gateway to interact with an app.</param>
/// <param name="marked">Marked selector of the Slider element to update.</param>
/// <param name="value">The value to set the Slider to.</param>
/// <param name="minimum">Te minimum selectable value for the Slider.</param>
/// <param name="maximum">Te maximum selectable value for the Slider.</param>
public static void SetSliderValue(this IApp app, string marked, double value, double minimum = 0d,double maximum = 1d)
{
var element = app.FindElement(marked);
app.CommandExecutor.Execute("setSliderValue", new Dictionary<string, object>
{
{ "element", element },
{ "value", value },
{ "minimum", minimum },
{ "maximum", maximum },
});
}
static IUIElement Wait(Func<IUIElement> query,
Func<IUIElement, bool> satisfactory,
string? timeoutMessage = null,
TimeSpan? timeout = null, TimeSpan? retryFrequency = null)
{
timeout ??= DefaultTimeout;
retryFrequency ??= TimeSpan.FromMilliseconds(500);
timeoutMessage ??= "Timed out on query.";
DateTime start = DateTime.Now;
IUIElement result = query();
while (!satisfactory(result))
{
long elapsed = DateTime.Now.Subtract(start).Ticks;
if (elapsed >= timeout.Value.Ticks)
{
Debug.WriteLine($">>>>> {elapsed} ticks elapsed, timeout value is {timeout.Value.Ticks}");
throw new TimeoutException(timeoutMessage);
}
Task.Delay(retryFrequency.Value.Milliseconds).Wait();
result = query();
}
return result;
}
static IUIElement WaitForAtLeastOne(Func<IUIElement> query,
string? timeoutMessage = null,
TimeSpan? timeout = null,
TimeSpan? retryFrequency = null)
{
var results = Wait(query, i => i != null, timeoutMessage, timeout, retryFrequency);
return results;
}
static void WaitForNone(Func<IUIElement> query,
string? timeoutMessage = null,
TimeSpan? timeout = null, TimeSpan? retryFrequency = null)
{
Wait(query, i => i == null, timeoutMessage, timeout, retryFrequency);
}
}
}