-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Window.cs
487 lines (427 loc) · 18 KB
/
Window.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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
//******************************************************************************
//
// Copyright (c) 2016 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 System;
using System.Collections.Generic;
using System.Threading;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium.Appium.Windows;
using System.Drawing;
using OpenQA.Selenium;
namespace WebDriverAPI
{
[TestClass]
public class Window
{
private WindowsDriver<WindowsElement> session = null;
[TestCleanup]
public void TestCleanup()
{
if (session != null)
{
session.Quit();
session = null;
}
}
[TestMethod]
public void CloseWindow()
{
session = Utility.CreateNewSession(CommonTestSettings.AlarmClockAppId);
Assert.IsNotNull(session.SessionId);
// Close the application window without deleting the session
session.Close();
Assert.IsNotNull(session);
Assert.IsNotNull(session.SessionId);
// Delete the session
session.Quit();
session = null;
}
[TestMethod]
public void CloseWindowError_NoSuchWindow()
{
// Attempt to close the previously closed application window
try
{
Utility.GetOrphanedSession().Close();
Assert.Fail("Exception should have been thrown");
}
catch (InvalidOperationException exception)
{
Assert.AreEqual(ErrorStrings.NoSuchWindow, exception.Message);
}
}
[TestMethod]
public void GetWindowHandle()
{
session = Utility.CreateNewSession(CommonTestSettings.CalculatorAppId);
Assert.IsNotNull(session.SessionId);
string windowHandle = session.CurrentWindowHandle;
Assert.IsNotNull(windowHandle);
}
[TestMethod]
public void GetWindowHandleError_NoSuchWindow()
{
try
{
string windowHandle = Utility.GetOrphanedSession().CurrentWindowHandle;
Assert.Fail("Exception should have been thrown");
}
catch (InvalidOperationException exception)
{
Assert.AreEqual(ErrorStrings.NoSuchWindow, exception.Message);
}
}
[TestMethod]
public void GetWindowHandles_ClassicApp()
{
session = Utility.CreateNewSession(CommonTestSettings.NotepadAppId);
Assert.IsNotNull(session);
var handles = session.WindowHandles;
Assert.IsNotNull(handles);
Assert.IsTrue(handles.Count > 0);
}
[TestMethod]
public void GetWindowHandles_Desktop()
{
session = Utility.CreateNewSession(CommonTestSettings.DesktopAppId);
Assert.IsNotNull(session);
var handles = session.WindowHandles;
Assert.IsNotNull(handles);
Assert.AreEqual(0, handles.Count);
}
[TestMethod]
public void GetWindowHandles_ModernApp()
{
session = Utility.CreateNewSession(CommonTestSettings.EdgeAppId, "-private");
Assert.IsNotNull(session);
var windowHandlesBefore = session.WindowHandles;
Assert.IsNotNull(windowHandlesBefore);
Assert.IsTrue(windowHandlesBefore.Count > 0);
// Preserve previously opened Edge window(s) and only manipulate newly opened windows
List<string> previouslyOpenedEdgeWindows = new List<string>(windowHandlesBefore);
previouslyOpenedEdgeWindows.Remove(session.CurrentWindowHandle);
// Set focus on itself
session.SwitchTo().Window(session.CurrentWindowHandle);
// Open a new window in private mode
session.Keyboard.SendKeys(Keys.Control + Keys.Shift + "p" + Keys.Shift + Keys.Control);
Thread.Sleep(TimeSpan.FromSeconds(3));
var windowHandlesAfter = session.WindowHandles;
Assert.IsNotNull(windowHandlesAfter);
Assert.AreEqual(windowHandlesBefore.Count + 1, windowHandlesAfter.Count);
// Preserve previously opened Edge Windows by only closing newly opened windows
List<string> newlyOpenedEdgeWindows = new List<string>(windowHandlesAfter);
foreach (var previouslyOpenedEdgeWindow in previouslyOpenedEdgeWindows)
{
newlyOpenedEdgeWindows.Remove(previouslyOpenedEdgeWindow);
}
foreach (var windowHandle in newlyOpenedEdgeWindows)
{
session.SwitchTo().Window(windowHandle);
EdgeBase.CloseEdge(session);
}
}
[TestMethod]
public void SwitchWindows()
{
session = Utility.CreateNewSession(CommonTestSettings.EdgeAppId, CommonTestSettings.EdgeAboutBlankURL);
Assert.IsNotNull(session);
// Preserve previously opened Edge window(s) and only manipulate newly opened windows
List<string> previouslyOpenedEdgeWindows = new List<string>(session.WindowHandles);
previouslyOpenedEdgeWindows.Remove(session.CurrentWindowHandle);
// Set focus on itself
session.SwitchTo().Window(session.CurrentWindowHandle);
// Open a new window in private mode
session.Keyboard.SendKeys(Keys.Control + Keys.Shift + "p" + Keys.Shift + Keys.Control);
Thread.Sleep(TimeSpan.FromSeconds(3));
var multipleWindowHandles = session.WindowHandles;
Assert.IsTrue(multipleWindowHandles.Count > 1);
// Preserve previously opened Edge Windows by only operating on newly opened windows
List<string> newlyOpenedEdgeWindows = new List<string>(multipleWindowHandles);
foreach (var previouslyOpenedEdgeWindow in previouslyOpenedEdgeWindows)
{
newlyOpenedEdgeWindows.Remove(previouslyOpenedEdgeWindow);
}
string previousWindowHandle = string.Empty;
foreach (var windowHandle in newlyOpenedEdgeWindows)
{
session.SwitchTo().Window(windowHandle);
Assert.AreEqual(session.CurrentWindowHandle, windowHandle);
Assert.AreNotEqual(windowHandle, previousWindowHandle);
previousWindowHandle = windowHandle;
EdgeBase.CloseEdge(session);
}
}
[TestMethod]
public void SwitchWindowsError_EmptyValue()
{
session = Utility.CreateNewSession(CommonTestSettings.CalculatorAppId);
try
{
session.SwitchTo().Window(string.Empty);
Assert.Fail("Exception should have been thrown");
}
catch (InvalidOperationException exception)
{
Assert.AreEqual("Missing Command Parameter: name", exception.Message);
}
}
[TestMethod]
public void SwitchWindowsError_ForeignWindowHandle()
{
WindowsDriver<WindowsElement> mainSession = null;
WindowsDriver<WindowsElement> foreignSession = null;
try
{
mainSession = Utility.CreateNewSession(CommonTestSettings.CalculatorAppId);
foreignSession = Utility.CreateNewSession(CommonTestSettings.AlarmClockAppId);
Assert.IsNotNull(mainSession.SessionId);
Assert.IsNotNull(foreignSession.SessionId);
// Get a foreign window handle from a different application/process under foreignSession
var foreignTopLevelWindow = foreignSession.CurrentWindowHandle;
Assert.IsFalse(string.IsNullOrEmpty(foreignTopLevelWindow));
mainSession.SwitchTo().Window(foreignTopLevelWindow);
Assert.Fail("Exception should have been thrown");
}
catch (InvalidOperationException exception)
{
Assert.AreEqual("Window handle does not belong to the same process/application", exception.Message);
}
finally
{
if (foreignSession != null)
{
foreignSession.Quit();
}
if (foreignSession != null)
{
mainSession.Quit();
}
}
}
[TestMethod]
public void SwitchWindowsError_InvalidValue()
{
session = Utility.CreateNewSession(CommonTestSettings.CalculatorAppId);
try
{
session.SwitchTo().Window("-1");
Assert.Fail("Exception should have been thrown");
}
catch (InvalidOperationException exception)
{
Assert.AreEqual("String cannot contain a minus sign if the base is not 10.", exception.Message);
}
}
[TestMethod]
public void SwitchWindowsError_NonTopLevelWindowHandle()
{
session = Utility.CreateNewSession(CommonTestSettings.CalculatorAppId);
var nonTopLevelWindowHandle = session.FindElementByClassName("Windows.UI.Core.CoreWindow").GetAttribute("NativeWindowHandle");
var nonTopLevelWindowHandleHex = Convert.ToInt32(nonTopLevelWindowHandle).ToString("x");
try
{
session.SwitchTo().Window(nonTopLevelWindowHandleHex); // This needs to be in Hex e.g. 0x00880088
Assert.Fail("Exception should have been thrown");
}
catch (InvalidOperationException exception)
{
Assert.IsTrue(exception.Message.EndsWith("is not a top level window handle"));
}
}
[TestMethod]
public void SwitchWindowsError_NoSuchWindow()
{
session = Utility.CreateNewSession(CommonTestSettings.CalculatorAppId);
// Get an orphaned window handle from a closed application
var orphanedTopLevelWindow = Utility.GetOrphanedWindowHandle();
Thread.Sleep(TimeSpan.FromSeconds(3));
try
{
session.SwitchTo().Window(orphanedTopLevelWindow);
Assert.Fail("Exception should have been thrown");
}
catch (InvalidOperationException exception)
{
Assert.AreEqual("A request to switch to a window could not be satisfied because the window could not be found.", exception.Message);
}
}
}
[TestClass]
public class WindowTransform
{
protected static WindowsDriver<WindowsElement> WindowTransformSession;
protected static Size OriginalSize;
protected static Point OriginalPosition;
[ClassInitialize]
public static void Setup(TestContext context)
{
// Launch the Calculator app
WindowTransformSession = Utility.CreateNewSession(CommonTestSettings.CalculatorAppId);
Assert.IsNotNull(WindowTransformSession);
}
[ClassCleanup]
public static void TearDown()
{
// Close the application and delete the session
WindowTransformSession.Quit();
WindowTransformSession = null;
}
[TestInitialize]
public void TestInitialize()
{
// Save application window original size and position
OriginalSize = WindowTransformSession.Manage().Window.Size;
Assert.IsNotNull(OriginalSize);
OriginalPosition = WindowTransformSession.Manage().Window.Position;
Assert.IsNotNull(OriginalPosition);
}
[TestCleanup]
public void TestCleanup()
{
// Restore application window original size and position
WindowTransformSession.Manage().Window.Size = OriginalSize;
Assert.AreEqual(OriginalSize, WindowTransformSession.Manage().Window.Size);
WindowTransformSession.Manage().Window.Position = OriginalPosition;
Assert.AreEqual(OriginalPosition, WindowTransformSession.Manage().Window.Position);
}
[TestMethod]
public void GetWindowPosition()
{
var windowPosition = WindowTransformSession.Manage().Window.Position;
Assert.IsNotNull(windowPosition);
Assert.AreEqual(OriginalPosition.X, windowPosition.X);
Assert.AreEqual(OriginalPosition.Y, windowPosition.Y);
}
[TestMethod]
public void GetWindowPositionError_NoSuchWindow()
{
try
{
var windowPosition = Utility.GetOrphanedSession().Manage().Window.Position;
Assert.Fail("Exception should have been thrown");
}
catch (InvalidOperationException exception)
{
Assert.AreEqual(ErrorStrings.NoSuchWindow, exception.Message);
}
}
[TestMethod]
public void GetWindowSize()
{
var windowSize = WindowTransformSession.Manage().Window.Size;
Assert.IsNotNull(windowSize);
Assert.AreEqual(OriginalSize.Height, windowSize.Height);
Assert.AreEqual(OriginalSize.Width, windowSize.Width);
}
[TestMethod]
public void GetWindowSizeError_NoSuchWindow()
{
try
{
var windowSize = Utility.GetOrphanedSession().Manage().Window.Size;
Assert.Fail("Exception should have been thrown");
}
catch (InvalidOperationException exception)
{
Assert.AreEqual(ErrorStrings.NoSuchWindow, exception.Message);
}
}
[TestMethod]
public void MaximizeWindow()
{
WindowTransformSession.Manage().Window.Maximize();
var windowSize = WindowTransformSession.Manage().Window.Size;
Assert.IsNotNull(windowSize);
Assert.IsTrue(OriginalSize.Height <= windowSize.Height);
Assert.IsTrue(OriginalSize.Width <= windowSize.Width);
Assert.IsTrue(WindowTransformSession.FindElementByAccessibilityId("Maximize").Text.Contains("Restore"));
}
[TestMethod]
public void MaximizeWindowError_NoSuchWindow()
{
try
{
Utility.GetOrphanedSession().Manage().Window.Maximize();
Assert.Fail("Exception should have been thrown");
}
catch (InvalidOperationException exception)
{
Assert.AreEqual(ErrorStrings.NoSuchWindow, exception.Message);
}
}
[TestMethod]
public void SetWindowPosition()
{
int offset = 100;
WindowTransformSession.Manage().Window.Position = new Point(OriginalPosition.X + offset, OriginalPosition.Y + offset);
var windowPosition = WindowTransformSession.Manage().Window.Position;
Assert.IsNotNull(windowPosition);
Assert.AreEqual(OriginalPosition.X + offset, windowPosition.X);
Assert.AreEqual(OriginalPosition.Y + offset, windowPosition.Y);
}
[TestMethod]
public void SetWindowPosition_ToOrigin()
{
var origin = new Point(0, 0);
WindowTransformSession.Manage().Window.Position = origin;
var position = WindowTransformSession.Manage().Window.Position;
Assert.AreEqual(origin.X, position.X);
Assert.AreEqual(origin.Y, position.Y);
}
[TestMethod]
public void SetWindowPositionError_NoSuchWindow()
{
try
{
Utility.GetOrphanedSession().Manage().Window.Position = new Point(0, 0);
Assert.Fail("Exception should have been thrown");
}
catch (InvalidOperationException exception)
{
Assert.AreEqual(ErrorStrings.NoSuchWindow, exception.Message);
}
}
[TestMethod]
public void SetWindowSize()
{
// The calculator app has a minimum width and height. Setting below the minimum values will resize it to the
// minimum size instead of the new width and height. The values below are chosen to exceed this minimum size.
int offset = 100;
int newWidth = 350;
int newHeight = 550;
WindowTransformSession.Manage().Window.Size = new Size(newWidth, newHeight);
var windowSize = WindowTransformSession.Manage().Window.Size;
Assert.AreEqual(newWidth, windowSize.Width);
Assert.AreEqual(newHeight, windowSize.Height);
WindowTransformSession.Manage().Window.Size = new Size(newWidth + offset, newHeight + offset);
windowSize = WindowTransformSession.Manage().Window.Size;
Assert.AreEqual(newWidth + offset, windowSize.Width);
Assert.AreEqual(newHeight + offset, windowSize.Height);
}
[TestMethod]
public void SetWindowSizeError_NoSuchWindow()
{
try
{
Utility.GetOrphanedSession().Manage().Window.Size = new Size(1000, 1000);
Assert.Fail("Exception should have been thrown");
}
catch (InvalidOperationException exception)
{
Assert.AreEqual(ErrorStrings.NoSuchWindow, exception.Message);
}
}
}
}