-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
ElementSendKeys.cs
199 lines (179 loc) · 7.41 KB
/
ElementSendKeys.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
//******************************************************************************
//
// 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;
using System.Threading;
namespace WebDriverAPI
{
[TestClass]
public class ElementSendKeys : AlarmClockBase
{
private static WindowsElement alarmNameTextBox;
[ClassInitialize]
public static void ClassInitialize(TestContext context)
{
Setup(context);
}
[ClassCleanup]
public static void ClassCleanup()
{
TearDown();
}
[TestInitialize]
public override void TestInit()
{
// Open new alarm page if app is currently in different view
try
{
alarmNameTextBox = session.FindElementByAccessibilityId("AlarmNameTextBox");
}
catch
{
base.TestInit();
Thread.Sleep(TimeSpan.FromSeconds(1));
session.FindElementByAccessibilityId("AddAlarmButton").Click();
Thread.Sleep(TimeSpan.FromSeconds(1.5));
alarmNameTextBox = session.FindElementByAccessibilityId("AlarmNameTextBox");
}
// Select all text and delete using keyboard shortcut Ctrl + A and Delete
alarmNameTextBox.SendKeys(Keys.Control + "a");
alarmNameTextBox.SendKeys(Keys.Delete);
Assert.AreEqual(string.Empty, alarmNameTextBox.Text);
}
[TestMethod]
public void SendKeysToElement_Alphabet()
{
alarmNameTextBox.SendKeys("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ");
Assert.AreEqual("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", alarmNameTextBox.Text);
}
[TestMethod]
public void SendKeysToElement_EmptySequence()
{
alarmNameTextBox.SendKeys(string.Empty);
Assert.AreEqual(string.Empty, alarmNameTextBox.Text);
}
[TestMethod]
public void SendKeysToElement_ModifierAlt()
{
alarmNameTextBox.SendKeys(Keys.Space);
Assert.AreEqual("True", alarmNameTextBox.GetAttribute("HasKeyboardFocus"));
alarmNameTextBox.SendKeys(Keys.Alt + Keys.Enter + Keys.Alt); // Alt + Enter moves the focus to the next element
Assert.AreEqual("False", alarmNameTextBox.GetAttribute("HasKeyboardFocus"));
}
[TestMethod]
public void SendKeysToElement_ModifierControl()
{
alarmNameTextBox.SendKeys("789");
alarmNameTextBox.SendKeys(Keys.Control + "a" + Keys.Control); // Select all
alarmNameTextBox.SendKeys(Keys.Control + "c" + Keys.Control); // Copy
alarmNameTextBox.SendKeys(Keys.Control + "vvv" + Keys.Control); // Paste 3 times
Assert.AreEqual("789789789", alarmNameTextBox.Text);
}
[TestMethod]
public void SendKeysToElement_ModifierImplicitRelease()
{
// SendKeys implicitly depress all modifier at the end of the sequence (every API call)
alarmNameTextBox.SendKeys(Keys.Shift + "abcwxyz1237890"); // Implicit shift release at the end of the sequence
alarmNameTextBox.SendKeys("abcwxyz1237890" + Keys.Shift);
alarmNameTextBox.SendKeys("abcwxyz1237890");
Assert.AreEqual("ABCWXYZ!@#&*()abcwxyz1237890abcwxyz1237890", alarmNameTextBox.Text);
}
[TestMethod]
public void SendKeysToElement_ModifierShift()
{
alarmNameTextBox.SendKeys(Keys.Shift + "abcwxyz1237890`-=[]\\;',./" + Keys.Shift);
Assert.AreEqual("ABCWXYZ!@#&*()~_+{}|:\"<>?", alarmNameTextBox.Text); // Assumes 101 keys US Keyboard layout
}
[TestMethod]
public void SendKeysToElement_NonPrintableKeys()
{
Thread.Sleep(TimeSpan.FromSeconds(1));
alarmNameTextBox.SendKeys("9");
alarmNameTextBox.SendKeys(Keys.Home + "8");
alarmNameTextBox.SendKeys(Keys.Left + "7");
Assert.AreEqual("789", alarmNameTextBox.Text);
}
[TestMethod]
public void SendKeysToElement_Number()
{
alarmNameTextBox.SendKeys("0123456789");
Assert.AreEqual("0123456789", alarmNameTextBox.Text);
}
[TestMethod]
public void SendKeysToElement_SymbolsKeys()
{
alarmNameTextBox.SendKeys("`-=[]\\;',./~!@#$%^&*()_+{}|:\"<>?");
Assert.AreEqual("`-=[]\\;',./~!@#$%^&*()_+{}|:\"<>?", alarmNameTextBox.Text);
}
[TestMethod]
public void SendKeysToElementError_ElementNotVisible()
{
base.TestInit();
// Different Alarm & Clock application version uses different UI elements
if (AlarmTabClassName == "ListViewItem")
{
// The latest Alarms & Clock application destroys the previous view instead of hiding it
}
else
{
// Navigate to Stopwatch tab and attempt to click on addAlarmButton that is no longer displayed
WindowsElement addAlarmButton = session.FindElementByAccessibilityId("AddAlarmButton");
session.FindElementByAccessibilityId(StopwatchTabAutomationId).Click();
Thread.Sleep(TimeSpan.FromSeconds(1));
Assert.IsFalse(addAlarmButton.Displayed);
try
{
addAlarmButton.SendKeys("keys");
Assert.Fail("Exception should have been thrown");
}
catch (InvalidOperationException exception)
{
Assert.AreEqual(ErrorStrings.ElementNotVisible, exception.Message);
}
}
}
[TestMethod]
public void SendKeysToElementError_NoSuchWindow()
{
try
{
Utility.GetOrphanedElement().SendKeys("keys");
Assert.Fail("Exception should have been thrown");
}
catch (InvalidOperationException exception)
{
Assert.AreEqual(ErrorStrings.NoSuchWindow, exception.Message);
}
}
[TestMethod]
public void SendKeysToElementError_StaleElement()
{
try
{
DismissAddAlarmPage();
Thread.Sleep(TimeSpan.FromSeconds(1));
alarmNameTextBox.SendKeys("keys");
Assert.Fail("Exception should have been thrown");
}
catch (InvalidOperationException exception)
{
Assert.AreEqual(ErrorStrings.StaleElementReference, exception.Message);
}
}
}
}