Skip to content

Commit f76c032

Browse files
committed
Create unit test for element clear, click, text, and displayed API endpoints
Add error scenario for GET /session/:sessionId/element/:id/selected API endpoint test Create a helper function in AlarmClockBase to recreate stale element scenario
1 parent 8def0bd commit f76c032

File tree

7 files changed

+391
-2
lines changed

7 files changed

+391
-2
lines changed

Tests/W3CWebDriver/AppSessionBase/AlarmClockBase.cs

+10
Original file line numberDiff line numberDiff line change
@@ -120,5 +120,15 @@ protected void CreateStopwatchLapEntries(uint numberOfEntry)
120120
}
121121
stopwatchPlayPauseButton.Click();
122122
}
123+
124+
protected static WindowsElement GetStaleElement()
125+
{
126+
// Open the add alarm page, locate the cancel button, and click it to get a stale cancel button
127+
session.FindElementByAccessibilityId("AddAlarmButton").Click();
128+
WindowsElement staleElement = session.FindElementByAccessibilityId("CancelButton");
129+
staleElement.Click();
130+
System.Threading.Thread.Sleep(1000);
131+
return staleElement;
132+
}
123133
}
124134
}

Tests/W3CWebDriver/ElementClear.cs

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
//******************************************************************************
2+
//
3+
// Copyright (c) 2017 Microsoft Corporation. All rights reserved.
4+
//
5+
// This code is licensed under the MIT License (MIT).
6+
//
7+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
8+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
9+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
10+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
11+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
12+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
13+
// THE SOFTWARE.
14+
//
15+
//******************************************************************************
16+
17+
using Microsoft.VisualStudio.TestTools.UnitTesting;
18+
using OpenQA.Selenium.Appium.Windows;
19+
20+
namespace W3CWebDriver
21+
{
22+
[TestClass]
23+
public class ElementClear : AlarmClockBase
24+
{
25+
[ClassInitialize]
26+
public static void ClassInitialize(TestContext context)
27+
{
28+
Setup(context);
29+
}
30+
31+
[ClassCleanup]
32+
public static void ClassCleanup()
33+
{
34+
TearDown();
35+
}
36+
37+
[TestMethod]
38+
public void ErrorClearElementNoSuchWindow()
39+
{
40+
try
41+
{
42+
Utility.GetOrphanedElement().Clear();
43+
Assert.Fail("Exception should have been thrown");
44+
}
45+
catch (System.InvalidOperationException exception)
46+
{
47+
Assert.AreEqual(ErrorStrings.NoSuchWindow, exception.Message);
48+
}
49+
}
50+
51+
[TestMethod]
52+
public void ErrorClearElementStaleElement()
53+
{
54+
try
55+
{
56+
GetStaleElement().Clear();
57+
Assert.Fail("Exception should have been thrown");
58+
}
59+
catch (System.InvalidOperationException exception)
60+
{
61+
Assert.AreEqual(ErrorStrings.StaleElementReference, exception.Message);
62+
}
63+
}
64+
65+
[TestMethod]
66+
public void ClearElement()
67+
{
68+
// Open a new alarm page and clear the alarm name repeatedly
69+
session.FindElementByAccessibilityId("AddAlarmButton").Click();
70+
WindowsElement textBox = session.FindElementByAccessibilityId("AlarmNameTextBox");
71+
Assert.AreNotEqual(string.Empty, textBox.Text);
72+
textBox.Clear();
73+
Assert.AreEqual(string.Empty, textBox.Text);
74+
75+
textBox.SendKeys("Test alarm name text box!");
76+
Assert.AreNotEqual(string.Empty, textBox.Text);
77+
textBox.Clear();
78+
Assert.AreEqual(string.Empty, textBox.Text);
79+
}
80+
}
81+
}

Tests/W3CWebDriver/ElementClick.cs

+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
//******************************************************************************
2+
//
3+
// Copyright (c) 2017 Microsoft Corporation. All rights reserved.
4+
//
5+
// This code is licensed under the MIT License (MIT).
6+
//
7+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
8+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
9+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
10+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
11+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
12+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
13+
// THE SOFTWARE.
14+
//
15+
//******************************************************************************
16+
17+
using Microsoft.VisualStudio.TestTools.UnitTesting;
18+
using OpenQA.Selenium.Appium.Windows;
19+
20+
namespace W3CWebDriver
21+
{
22+
[TestClass]
23+
public class ElementClick : AlarmClockBase
24+
{
25+
[ClassInitialize]
26+
public static void ClassInitialize(TestContext context)
27+
{
28+
Setup(context);
29+
}
30+
31+
[ClassCleanup]
32+
public static void ClassCleanup()
33+
{
34+
TearDown();
35+
}
36+
37+
[TestMethod]
38+
public void ErrorClickElementNoSuchWindow()
39+
{
40+
try
41+
{
42+
Utility.GetOrphanedElement().Click();
43+
Assert.Fail("Exception should have been thrown");
44+
}
45+
catch (System.InvalidOperationException exception)
46+
{
47+
Assert.AreEqual(ErrorStrings.NoSuchWindow, exception.Message);
48+
}
49+
}
50+
51+
[TestMethod]
52+
public void ErrorClickElementStaleElement()
53+
{
54+
try
55+
{
56+
GetStaleElement().Click();
57+
Assert.Fail("Exception should have been thrown");
58+
}
59+
catch (System.InvalidOperationException exception)
60+
{
61+
Assert.AreEqual(ErrorStrings.StaleElementReference, exception.Message);
62+
}
63+
}
64+
65+
[TestMethod]
66+
public void ClickElement()
67+
{
68+
// Open a new alarm page and try clicking on visible and non-visible element in the time picker
69+
session.FindElementByAccessibilityId("AddAlarmButton").Click();
70+
71+
// initially visible element
72+
WindowsElement hourSelector = session.FindElementByAccessibilityId("HourLoopingSelector");
73+
hourSelector.FindElementByName("8").Click();
74+
Assert.AreEqual("8", hourSelector.Text);
75+
76+
// initially non-visible element that is implicitly scrolled into view once clicked
77+
WindowsElement minuteSelector = session.FindElementByAccessibilityId("MinuteLoopingSelector");
78+
minuteSelector.FindElementByName("30").Click();
79+
Assert.AreEqual("30", minuteSelector.Text);
80+
81+
// Return to main page and click on pivot items to switch between tabs
82+
session.FindElementByAccessibilityId("CancelButton").Click();
83+
84+
WindowsElement worldPivot = session.FindElementByAccessibilityId("WorldClockPivotItem");
85+
WindowsElement alarmPivot = session.FindElementByAccessibilityId("AlarmPivotItem");
86+
87+
worldPivot.Click();
88+
Assert.IsTrue(worldPivot.Selected);
89+
Assert.IsFalse(alarmPivot.Selected);
90+
91+
alarmPivot.Click();
92+
Assert.IsFalse(worldPivot.Selected);
93+
Assert.IsTrue(alarmPivot.Selected);
94+
}
95+
}
96+
}
+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
//******************************************************************************
2+
//
3+
// Copyright (c) 2017 Microsoft Corporation. All rights reserved.
4+
//
5+
// This code is licensed under the MIT License (MIT).
6+
//
7+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
8+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
9+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
10+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
11+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
12+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
13+
// THE SOFTWARE.
14+
//
15+
//******************************************************************************
16+
17+
using Microsoft.VisualStudio.TestTools.UnitTesting;
18+
using OpenQA.Selenium.Appium.Windows;
19+
20+
namespace W3CWebDriver
21+
{
22+
[TestClass]
23+
public class ElementDisplayed : AlarmClockBase
24+
{
25+
[ClassInitialize]
26+
public static void ClassInitialize(TestContext context)
27+
{
28+
Setup(context);
29+
}
30+
31+
[ClassCleanup]
32+
public static void ClassCleanup()
33+
{
34+
TearDown();
35+
}
36+
37+
[TestMethod]
38+
public void ErrorGetElementDisplayedStateNoSuchWindow()
39+
{
40+
try
41+
{
42+
var displayed = Utility.GetOrphanedElement().Displayed;
43+
Assert.Fail("Exception should have been thrown");
44+
}
45+
catch (System.InvalidOperationException exception)
46+
{
47+
Assert.AreEqual(ErrorStrings.NoSuchWindow, exception.Message);
48+
}
49+
}
50+
51+
[TestMethod]
52+
public void ErrorGetElementDisplayedStateStaleElement()
53+
{
54+
try
55+
{
56+
var displayed = GetStaleElement().Displayed;
57+
Assert.Fail("Exception should have been thrown");
58+
}
59+
catch (System.InvalidOperationException exception)
60+
{
61+
Assert.AreEqual(ErrorStrings.StaleElementReference, exception.Message);
62+
}
63+
}
64+
65+
[TestMethod]
66+
public void GetElementDisplayed()
67+
{
68+
WindowsElement alarmPivotItem = session.FindElementByAccessibilityId("AlarmPivotItem");
69+
WindowsElement addAlarmButton = session.FindElementByAccessibilityId("AddAlarmButton");
70+
Assert.IsTrue(addAlarmButton.Displayed);
71+
72+
// Navigate to Stopwatch tab
73+
WindowsElement stopwatchPivotItem = session.FindElementByAccessibilityId("StopwatchPivotItem");
74+
stopwatchPivotItem.Click();
75+
WindowsElement stopwatchResetButton = session.FindElementByAccessibilityId("StopWatchResetButton");
76+
Assert.IsTrue(stopwatchResetButton.Displayed);
77+
Assert.IsFalse(addAlarmButton.Displayed);
78+
79+
// Navigate back to Alarm tab
80+
alarmPivotItem.Click();
81+
Assert.IsTrue(addAlarmButton.Displayed);
82+
Assert.IsFalse(stopwatchResetButton.Displayed);
83+
84+
// Open a new alarm page and verify that 00 minute is displayed while 30 minute is hidden in the time picker
85+
addAlarmButton.Click();
86+
WindowsElement minuteLoopingSelector = session.FindElementByAccessibilityId("MinuteLoopingSelector");
87+
Assert.IsTrue(minuteLoopingSelector.FindElementByName("00").Displayed);
88+
Assert.IsFalse(minuteLoopingSelector.FindElementByName("30").Displayed);
89+
}
90+
}
91+
}

Tests/W3CWebDriver/ElementSelected.cs

+16-2
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public void ErrorGetElementSelectedStateNoSuchWindow()
3939
{
4040
try
4141
{
42-
var enabled = Utility.GetOrphanedElement().Enabled;
42+
var selected = Utility.GetOrphanedElement().Enabled;
4343
Assert.Fail("Exception should have been thrown");
4444
}
4545
catch (System.InvalidOperationException exception)
@@ -48,6 +48,20 @@ public void ErrorGetElementSelectedStateNoSuchWindow()
4848
}
4949
}
5050

51+
[TestMethod]
52+
public void ErrorGetElementSelectedStateStaleElement()
53+
{
54+
try
55+
{
56+
var selected = GetStaleElement().Selected;
57+
Assert.Fail("Exception should have been thrown");
58+
}
59+
catch (System.InvalidOperationException exception)
60+
{
61+
Assert.AreEqual(ErrorStrings.StaleElementReference, exception.Message);
62+
}
63+
}
64+
5165
[TestMethod]
5266
public void GetElementSelectedState()
5367
{
@@ -64,7 +78,7 @@ public void GetElementSelectedState()
6478
}
6579

6680
[TestMethod]
67-
public void ErrorFindUnselectableElement()
81+
public void GetElementSelectedStateUnselectableElement()
6882
{
6983
WindowsElement elementAddButton = session.FindElementByAccessibilityId("AddAlarmButton");
7084
Assert.IsFalse(elementAddButton.Selected);

0 commit comments

Comments
 (0)