forked from microsoft/WinAppDriver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ActionsTouch.cs
372 lines (325 loc) · 20.3 KB
/
ActionsTouch.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
//******************************************************************************
//
// Copyright (c) 2018 Microsoft Corporation. All rights reserved.
//
// This code is licensed under the MIT License (MIT).
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
//******************************************************************************
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Threading;
using OpenQA.Selenium.Interactions;
using OpenQA.Selenium.Appium.Interactions;
using OpenQA.Selenium.Appium.Windows;
// Define an alias to OpenQA.Selenium.Appium.Interactions.PointerInputDevice to hide
// inherited OpenQA.Selenium.Interactions.PointerInputDevice that causes ambiguity.
// In the future, all functions of OpenQA.Selenium.Appium.Interactions should be moved
// up to OpenQA.Selenium.Interactions and this alias can simply be removed.
using PointerInputDevice = OpenQA.Selenium.Appium.Interactions.PointerInputDevice;
namespace WebDriverAPI
{
[TestClass]
public class ActionsTouch : AlarmClockBase
{
[ClassInitialize]
public static void ClassInitialize(TestContext context)
{
Setup(context);
}
[ClassCleanup]
public static void ClassCleanup()
{
TearDown();
}
[TestMethod]
public void Touch_Click_OriginElement()
{
var alarmPivotItem = session.FindElementByAccessibilityId(AlarmTabAutomationId);
var worldClockPivotItem = session.FindElementByAccessibilityId(WorldClockTabAutomationId);
Assert.IsNotNull(alarmPivotItem);
Assert.IsNotNull(worldClockPivotItem);
Assert.IsTrue(alarmPivotItem.Selected);
Assert.IsFalse(worldClockPivotItem.Selected);
// Perform touch click action using element coordinate origin to switch from Alarm to WorldClock tab
PointerInputDevice touchDevice = new PointerInputDevice(PointerKind.Touch);
ActionSequence sequence = new ActionSequence(touchDevice, 0);
sequence.AddAction(touchDevice.CreatePointerMove(worldClockPivotItem, 0, 0, TimeSpan.Zero));
sequence.AddAction(touchDevice.CreatePointerDown(PointerButton.TouchContact));
sequence.AddAction(touchDevice.CreatePointerUp(PointerButton.TouchContact));
session.PerformActions(new List<ActionSequence> { sequence });
Thread.Sleep(TimeSpan.FromSeconds(1));
Assert.IsFalse(alarmPivotItem.Selected);
Assert.IsTrue(worldClockPivotItem.Selected);
// Perform touch click action using element coordinate origin to switch from WorldClock to Alarm tab
sequence = new ActionSequence(touchDevice, 0);
sequence.AddAction(touchDevice.CreatePointerMove(alarmPivotItem, 0, 0, TimeSpan.Zero));
sequence.AddAction(touchDevice.CreatePointerDown(PointerButton.TouchContact));
sequence.AddAction(touchDevice.CreatePointerUp(PointerButton.TouchContact));
session.PerformActions(new List<ActionSequence> { sequence });
Thread.Sleep(TimeSpan.FromSeconds(1));
Assert.IsTrue(alarmPivotItem.Selected);
Assert.IsFalse(worldClockPivotItem.Selected);
}
[TestMethod]
public void Touch_Click_OriginPointer()
{
WindowsElement alarmPivotItem = session.FindElementByAccessibilityId(AlarmTabAutomationId);
WindowsElement worldClockPivotItem = session.FindElementByAccessibilityId(WorldClockTabAutomationId);
int relativeX = 0; // Initial x coordinate
int relativeY = 0; // Initial y coordinate
Assert.IsNotNull(alarmPivotItem);
Assert.IsNotNull(worldClockPivotItem);
Assert.IsTrue(alarmPivotItem.Selected);
Assert.IsFalse(worldClockPivotItem.Selected);
// Perform touch click action using pointer coordinate origin to switch from Alarm to WorldClock tab
PointerInputDevice touchDevice = new PointerInputDevice(PointerKind.Touch);
ActionSequence sequence = new ActionSequence(touchDevice, 0);
relativeX = worldClockPivotItem.Location.X - relativeX;
relativeY = worldClockPivotItem.Location.Y - relativeY;
sequence.AddAction(touchDevice.CreatePointerMove(CoordinateOrigin.Pointer, relativeX, relativeY, TimeSpan.Zero));
sequence.AddAction(touchDevice.CreatePointerDown(PointerButton.TouchContact));
sequence.AddAction(touchDevice.CreatePointerUp(PointerButton.TouchContact));
session.PerformActions(new List<ActionSequence> { sequence });
Thread.Sleep(TimeSpan.FromSeconds(1));
Assert.IsFalse(alarmPivotItem.Selected);
Assert.IsTrue(worldClockPivotItem.Selected);
// Perform touch click action using pointer coordinate origin to switch from WorldClock to Alarm tab
sequence = new ActionSequence(touchDevice, 0);
relativeX = alarmPivotItem.Location.X - relativeX;
relativeY = alarmPivotItem.Location.Y - relativeY;
sequence.AddAction(touchDevice.CreatePointerMove(CoordinateOrigin.Pointer, relativeX, relativeY, TimeSpan.Zero));
sequence.AddAction(touchDevice.CreatePointerDown(PointerButton.TouchContact));
sequence.AddAction(touchDevice.CreatePointerUp(PointerButton.TouchContact));
session.PerformActions(new List<ActionSequence> { sequence });
Thread.Sleep(TimeSpan.FromSeconds(1));
Assert.IsTrue(alarmPivotItem.Selected);
Assert.IsFalse(worldClockPivotItem.Selected);
}
[TestMethod]
public void Touch_Click_OriginViewport()
{
WindowsElement alarmPivotItem = session.FindElementByAccessibilityId(AlarmTabAutomationId);
WindowsElement worldClockPivotItem = session.FindElementByAccessibilityId(WorldClockTabAutomationId);
int x = worldClockPivotItem.Location.X; // x coordinate of UI element relative to application window
int y = worldClockPivotItem.Location.Y; // y coordinate of UI element relative to application window
Assert.IsNotNull(alarmPivotItem);
Assert.IsNotNull(worldClockPivotItem);
Assert.IsTrue(alarmPivotItem.Selected);
Assert.IsFalse(worldClockPivotItem.Selected);
// Perform touch click action using viewport coordinate origin to switch from Alarm to WorldClock tab
PointerInputDevice touchDevice = new PointerInputDevice(PointerKind.Touch);
ActionSequence sequence = new ActionSequence(touchDevice, 0);
sequence.AddAction(touchDevice.CreatePointerMove(CoordinateOrigin.Viewport, x, y, TimeSpan.Zero));
sequence.AddAction(touchDevice.CreatePointerDown(PointerButton.TouchContact));
sequence.AddAction(touchDevice.CreatePointerUp(PointerButton.TouchContact));
session.PerformActions(new List<ActionSequence> { sequence });
Thread.Sleep(TimeSpan.FromSeconds(1));
Assert.IsFalse(alarmPivotItem.Selected);
Assert.IsTrue(worldClockPivotItem.Selected);
// Perform touch click action using viewport coordinate origin to switch from WorldClock to Alarm tab
sequence = new ActionSequence(touchDevice, 0);
x = alarmPivotItem.Location.X;
y = alarmPivotItem.Location.Y;
sequence.AddAction(touchDevice.CreatePointerMove(CoordinateOrigin.Viewport, x, y, TimeSpan.Zero));
sequence.AddAction(touchDevice.CreatePointerDown(PointerButton.TouchContact));
sequence.AddAction(touchDevice.CreatePointerUp(PointerButton.TouchContact));
session.PerformActions(new List<ActionSequence> { sequence });
Thread.Sleep(TimeSpan.FromSeconds(1));
Assert.IsTrue(alarmPivotItem.Selected);
Assert.IsFalse(worldClockPivotItem.Selected);
}
[TestMethod]
public void Touch_DoubleClick()
{
WindowsElement appNameTitle = FindAppTitleBar();
WindowsElement maximizeButton = session.FindElementByAccessibilityId("Maximize");
// Set focus on the application by switching window to itself
session.SwitchTo().Window(session.CurrentWindowHandle);
// Restore the application window if it is currently maximized
if (!maximizeButton.Text.Contains("Maximize"))
{
maximizeButton.Click();
}
// Verify that window is currently not maximized
Assert.IsTrue(maximizeButton.Text.Contains("Maximize"));
// Perform touch double click action on the title bar to maximize the application window
PointerInputDevice touchDevice = new PointerInputDevice(PointerKind.Touch);
ActionSequence sequence = new ActionSequence(touchDevice, 0);
sequence.AddAction(touchDevice.CreatePointerMove(appNameTitle, 0, 0, TimeSpan.Zero));
sequence.AddAction(touchDevice.CreatePointerDown(PointerButton.TouchContact));
sequence.AddAction(touchDevice.CreatePointerUp(PointerButton.TouchContact));
sequence.AddAction(touchDevice.CreatePointerDown(PointerButton.TouchContact));
sequence.AddAction(touchDevice.CreatePointerUp(PointerButton.TouchContact));
session.PerformActions(new List<ActionSequence> { sequence });
Thread.Sleep(TimeSpan.FromSeconds(1));
Assert.IsFalse(maximizeButton.Text.Contains("Maximize"));
// Perform touch double click action on the title bar to restore the application window
sequence = new ActionSequence(touchDevice, 0);
sequence.AddAction(touchDevice.CreatePointerMove(appNameTitle, 0, 0, TimeSpan.Zero));
sequence.AddAction(touchDevice.CreatePointerDown(PointerButton.TouchContact));
sequence.AddAction(touchDevice.CreatePointerUp(PointerButton.TouchContact));
sequence.AddAction(touchDevice.CreatePointerDown(PointerButton.TouchContact));
sequence.AddAction(touchDevice.CreatePointerUp(PointerButton.TouchContact));
session.PerformActions(new List<ActionSequence> { sequence });
Thread.Sleep(TimeSpan.FromSeconds(1));
Assert.IsTrue(maximizeButton.Text.Contains("Maximize"));
}
[TestMethod]
public void Touch_DragAndDrop()
{
WindowsElement appNameTitle = FindAppTitleBar();
const int offset = 100;
// Save application window original position
Point originalPosition = session.Manage().Window.Position;
Assert.IsNotNull(originalPosition);
// Send touch down, move, and up actions combination to perform a drag and drop
// action on the app title bar. These actions reposition the application window.
PointerInputDevice touchDevice = new PointerInputDevice(PointerKind.Touch);
ActionSequence sequence = new ActionSequence(touchDevice, 0);
sequence.AddAction(touchDevice.CreatePointerMove(appNameTitle, 0, 0, TimeSpan.Zero));
sequence.AddAction(touchDevice.CreatePointerDown(PointerButton.TouchContact));
sequence.AddAction(touchDevice.CreatePointerMove(CoordinateOrigin.Pointer, offset, offset, TimeSpan.FromSeconds(1)));
sequence.AddAction(touchDevice.CreatePointerUp(PointerButton.TouchContact));
session.PerformActions(new List<ActionSequence> { sequence });
Thread.Sleep(TimeSpan.FromSeconds(1));
// Verify that application window is now re-positioned from the original location
Assert.AreNotEqual(originalPosition, session.Manage().Window.Position);
Assert.IsTrue(originalPosition.Y < session.Manage().Window.Position.Y);
// Restore application window original position
session.Manage().Window.Position = originalPosition;
Assert.AreEqual(originalPosition, session.Manage().Window.Position);
}
[TestMethod]
public void Touch_Flick()
{
// Navigate to add alarm page
session.FindElementByAccessibilityId("AddAlarmButton").Click();
session.FindElementByAccessibilityId("AlarmTimePicker").Click();
Thread.Sleep(TimeSpan.FromSeconds(1));
WindowsElement minuteSelector = session.FindElementByAccessibilityId("MinuteLoopingSelector");
WindowsElement minute00 = session.FindElementByName("00");
Assert.IsNotNull(minuteSelector);
Assert.IsNotNull(minute00);
Assert.IsTrue(minute00.Displayed);
// Perform touch flick down action to scroll the minute hiding 00 minutes that was shown
PointerInputDevice touchDevice = new PointerInputDevice(PointerKind.Touch);
ActionSequence sequence = new ActionSequence(touchDevice, 0);
sequence.AddAction(touchDevice.CreatePointerMove(minuteSelector, 0, 0, TimeSpan.Zero));
sequence.AddAction(touchDevice.CreatePointerDown(PointerButton.TouchContact));
sequence.AddAction(touchDevice.CreatePointerMove(minuteSelector, 0, 500, TimeSpan.Zero));
sequence.AddAction(touchDevice.CreatePointerUp(PointerButton.TouchContact));
session.PerformActions(new List<ActionSequence> { sequence });
Thread.Sleep(TimeSpan.FromSeconds(2));
Assert.IsFalse(minute00.Displayed);
}
[TestMethod]
public void Touch_LongClick()
{
// Create a new test alarm
string alarmName = "LongTapTest";
DeletePreviouslyCreatedAlarmEntry(alarmName);
AddAlarmEntry(alarmName);
Thread.Sleep(TimeSpan.FromSeconds(3));
var alarmEntries = session.FindElementsByXPath($"//ListItem[starts-with(@Name, \"{alarmName}\")]");
Assert.IsNotNull(alarmEntries);
Assert.AreEqual(1, alarmEntries.Count);
// Open a the context menu on the alarm entry using long tap (press and hold) action and click delete
PointerInputDevice touchDevice = new PointerInputDevice(PointerKind.Touch);
ActionSequence sequence = new ActionSequence(touchDevice, 0);
sequence.AddAction(touchDevice.CreatePointerMove(alarmEntries[0], 0, 0, TimeSpan.Zero));
sequence.AddAction(touchDevice.CreatePointerDown(PointerButton.TouchContact));
sequence.AddAction(touchDevice.CreatePointerMove(alarmEntries[0], 0, 0, TimeSpan.FromSeconds(3)));
sequence.AddAction(touchDevice.CreatePointerUp(PointerButton.TouchContact));
session.PerformActions(new List<ActionSequence> { sequence });
Thread.Sleep(TimeSpan.FromSeconds(1));
session.FindElementByName("Delete").Click();
Thread.Sleep(TimeSpan.FromSeconds(1));
alarmEntries = session.FindElementsByXPath($"//ListItem[starts-with(@Name, \"{alarmName}\")]");
Assert.IsNotNull(alarmEntries);
Assert.AreEqual(0, alarmEntries.Count);
}
[TestMethod]
public void Touch_Scroll_Horizontal()
{
// Different Alarm & Clock application version uses different UI elements
if (AlarmTabClassName == "ListViewItem")
{
// The latest Alarms & Clock application no longer has horizontal scroll UI elements
}
else
{
WindowsElement homePagePivot = session.FindElementByAccessibilityId("HomePagePivot");
WindowsElement alarmPivotItem = session.FindElementByAccessibilityId(AlarmTabAutomationId);
WindowsElement worldClockPivotItem = session.FindElementByAccessibilityId(WorldClockTabAutomationId);
Assert.IsNotNull(homePagePivot);
Assert.IsNotNull(alarmPivotItem);
Assert.IsNotNull(worldClockPivotItem);
Assert.IsTrue(alarmPivotItem.Selected);
Assert.IsFalse(worldClockPivotItem.Selected);
// Perform scroll left touch action to switch from Alarm to WorldClock tab
PointerInputDevice touchDevice = new PointerInputDevice(PointerKind.Touch);
ActionSequence sequence = new ActionSequence(touchDevice, 0);
sequence.AddAction(touchDevice.CreatePointerMove(homePagePivot, 0, 0, TimeSpan.Zero));
sequence.AddAction(touchDevice.CreatePointerDown(PointerButton.TouchContact));
sequence.AddAction(touchDevice.CreatePointerMove(homePagePivot, -session.Manage().Window.Size.Width / 2, 0, TimeSpan.FromSeconds(.5)));
sequence.AddAction(touchDevice.CreatePointerUp(PointerButton.TouchContact));
session.PerformActions(new List<ActionSequence> { sequence });
Thread.Sleep(TimeSpan.FromSeconds(1));
Assert.IsFalse(alarmPivotItem.Selected);
Assert.IsTrue(worldClockPivotItem.Selected);
// Perform scroll right touch action to switch back from WorldClock to Alarm tab
sequence = new ActionSequence(touchDevice, 0);
sequence.AddAction(touchDevice.CreatePointerMove(homePagePivot, 0, 0, TimeSpan.Zero));
sequence.AddAction(touchDevice.CreatePointerDown(PointerButton.TouchContact));
sequence.AddAction(touchDevice.CreatePointerMove(homePagePivot, session.Manage().Window.Size.Width / 2, 0, TimeSpan.FromSeconds(.5)));
sequence.AddAction(touchDevice.CreatePointerUp(PointerButton.TouchContact));
session.PerformActions(new List<ActionSequence> { sequence });
Thread.Sleep(TimeSpan.FromSeconds(1));
Assert.IsTrue(alarmPivotItem.Selected);
Assert.IsFalse(worldClockPivotItem.Selected);
}
}
[TestMethod]
public void Touch_Scroll_Vertical()
{
// Navigate to add alarm page
session.FindElementByAccessibilityId("AddAlarmButton").Click();
session.FindElementByAccessibilityId("AlarmTimePicker").Click();
Thread.Sleep(TimeSpan.FromSeconds(1));
WindowsElement minuteSelector = session.FindElementByAccessibilityId("MinuteLoopingSelector");
WindowsElement minute00 = session.FindElementByName("00");
Assert.IsNotNull(minuteSelector);
Assert.IsNotNull(minute00);
Assert.IsTrue(minute00.Displayed);
// Perform scroll down touch action to scroll the minute hiding 00 minutes that was shown
PointerInputDevice touchDevice = new PointerInputDevice(PointerKind.Touch);
ActionSequence sequence = new ActionSequence(touchDevice, 0);
sequence.AddAction(touchDevice.CreatePointerMove(minuteSelector, 0, 0, TimeSpan.Zero));
sequence.AddAction(touchDevice.CreatePointerDown(PointerButton.TouchContact));
sequence.AddAction(touchDevice.CreatePointerMove(minuteSelector, 0, -200, TimeSpan.FromSeconds(.5)));
sequence.AddAction(touchDevice.CreatePointerUp(PointerButton.TouchContact));
session.PerformActions(new List<ActionSequence> { sequence });
Thread.Sleep(TimeSpan.FromSeconds(1));
Assert.IsFalse(minute00.Displayed);
// Perform scroll up touch action to scroll the the minute back showing 00 minutes that was hidden
sequence = new ActionSequence(touchDevice, 0);
sequence.AddAction(touchDevice.CreatePointerMove(minuteSelector, 0, 0, TimeSpan.Zero));
sequence.AddAction(touchDevice.CreatePointerDown(PointerButton.TouchContact));
sequence.AddAction(touchDevice.CreatePointerMove(minuteSelector, 0, 200, TimeSpan.FromSeconds(.5)));
sequence.AddAction(touchDevice.CreatePointerUp(PointerButton.TouchContact));
session.PerformActions(new List<ActionSequence> { sequence });
Thread.Sleep(TimeSpan.FromSeconds(1));
Assert.IsTrue(minute00.Displayed);
}
}
}