forked from microsoft/WinAppDriver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SendKeys.cs
187 lines (165 loc) · 6.82 KB
/
SendKeys.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
//******************************************************************************
//
// 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 Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium.Appium.Windows;
using OpenQA.Selenium;
using System.Threading;
using System;
namespace WebDriverAPI
{
[TestClass]
public class SendKeys
{
protected static WindowsDriver<WindowsElement> session;
protected static WindowsElement editBox;
[ClassInitialize]
public static void ClassInitialize(TestContext context)
{
session = Utility.CreateNewSession(CommonTestSettings.NotepadAppId);
Assert.IsNotNull(session);
editBox = session.FindElementByClassName("Edit");
Assert.IsNotNull(editBox);
}
[ClassCleanup]
public static void ClassCleanup()
{
if (session != null)
{
ClearEditBox();
session.Quit();
session = null;
}
}
[TestInitialize]
public void TestInitialize()
{
ClearEditBox();
}
public static void ClearEditBox()
{
// Select all text and delete using keyboard shortcut Ctrl + A and Delete
editBox.Click();
session.Keyboard.SendKeys(Keys.Control + "a" + Keys.Control);
session.Keyboard.SendKeys(Keys.Delete);
Assert.AreEqual(string.Empty, editBox.Text);
}
[TestMethod]
public void SendKeys_Alphabet()
{
session.Keyboard.SendKeys("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ");
Assert.AreEqual("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", editBox.Text);
}
[TestMethod]
public void SendKeys_EmptySequence()
{
session.Keyboard.SendKeys(string.Empty);
Assert.AreEqual(string.Empty, editBox.Text);
}
[TestMethod]
public void SendKeys_ModifierAlt()
{
session.Keyboard.SendKeys(Keys.Alt + "ED" + Keys.Alt); // Insert Time/Date
Assert.IsTrue(editBox.Text.Length > 0);
}
[TestMethod]
public void SendKeys_ModifierControl()
{
session.Keyboard.SendKeys("789");
session.Keyboard.SendKeys(Keys.Control + "a" + Keys.Control); // Select all
session.Keyboard.SendKeys(Keys.Control + "c" + Keys.Control); // Copy
session.Keyboard.SendKeys(Keys.Control + "vvv" + Keys.Control); // Paste 3 times
Assert.AreEqual("789789789", editBox.Text);
}
[TestMethod]
public void SendKeys_ModifierExplicitRelease()
{
// Keys persist all modifier between API call and requires ecplicit modifier release
session.Keyboard.SendKeys(Keys.Shift + "abcwxyz1237890"); // Shift modifier is still pressed
session.Keyboard.SendKeys("abcwxyz1237890" + Keys.Shift);
session.Keyboard.SendKeys("abcwxyz1237890");
Assert.AreEqual("ABCWXYZ!@#&*()ABCWXYZ!@#&*()abcwxyz1237890", editBox.Text);
}
[TestMethod]
public void SendKeys_ModifierShift()
{
session.Keyboard.SendKeys(Keys.Shift + "abcdefghijklmnopqrstuvwxyz\n1234567890\t`-=[]\\;',./" + Keys.Shift);
Assert.AreEqual("ABCDEFGHIJKLMNOPQRSTUVWXYZ\r\n!@#$%^&*()\t~_+{}|:\"<>?", editBox.Text); // Assumes 101 keys US Keyboard layout
}
[TestMethod]
public void SendKeys_ModifierWindowsKey()
{
WindowsDriver<WindowsElement> desktopSession = Utility.CreateNewSession(CommonTestSettings.DesktopAppId);
Assert.IsNotNull(desktopSession);
// Launch action center using Window Keys + A
session.Keyboard.SendKeys(Keys.Command + "a" + Keys.Command);
Thread.Sleep(TimeSpan.FromSeconds(2));
// Verify that Action Center pane is displayed
WindowsElement actionCenterElement = desktopSession.FindElementByName("Action center");
Assert.IsNotNull(actionCenterElement);
Assert.IsNotNull(actionCenterElement.FindElementByName("Tablet mode"));
// Dismiss action center and cleanup the temporary session
actionCenterElement.SendKeys(Keys.Escape);
editBox.Click();
desktopSession.Quit();
}
[TestMethod]
public void SendKeys_NonPrintableKeys()
{
session.Keyboard.SendKeys("9");
session.Keyboard.SendKeys(Keys.Home + "8");
session.Keyboard.SendKeys(Keys.Home + "7");
session.Keyboard.SendKeys(Keys.Home + Keys.Tab);
session.Keyboard.SendKeys(Keys.Home + Keys.Enter);
session.Keyboard.SendKeys(Keys.Up + Keys.Tab + "78");
session.Keyboard.SendKeys(Keys.Home + Keys.Enter);
session.Keyboard.SendKeys(Keys.Up + Keys.Tab + "7");
Assert.AreEqual("\t7\r\n\t78\r\n\t789", editBox.Text);
}
[TestMethod]
public void SendKeys_Number()
{
session.Keyboard.SendKeys("0123456789");
Assert.AreEqual("0123456789", editBox.Text);
}
[TestMethod]
public void SendKeys_SymbolsEscapedCharacter()
{
// Line endings such as \r or \n are replaced with \r\n
// form feeds (\f) or vertical tab feeds (\v) are removed
session.Keyboard.SendKeys("\a\b\f\n\r\t\v\'\"\\\r\n");
Assert.AreEqual("\r\n\r\n\t\'\"\\\r\n\r\n", editBox.Text);
}
[TestMethod]
public void SendKeys_SymbolsKeys()
{
session.Keyboard.SendKeys("`-=[]\\;',./~!@#$%^&*()_+{}|:\"<>?");
Assert.AreEqual("`-=[]\\;',./~!@#$%^&*()_+{}|:\"<>?", editBox.Text);
}
[TestMethod]
public void SendKeysError_NoSuchWindow()
{
try
{
Utility.GetOrphanedSession().Keyboard.SendKeys("keys");
Assert.Fail("Exception should have been thrown");
}
catch (InvalidOperationException exception)
{
Assert.AreEqual(ErrorStrings.NoSuchWindow, exception.Message);
}
}
}
}