Skip to content

Commit fe10406

Browse files
committed
Update AlarmClockBase and CalculatorBase to always re-use existing session when possible
Create a new helper function to create new session in the Utility class Add a new function to get orphaned session needed to simulate No Such Window error scenario Add missing test scenario in Element, ElementAttribute, ElementClick, ElementElement, ElementElements, Elements, and Window tests Add useful common test strings and error messages to CommonTestSettings
1 parent f76c032 commit fe10406

24 files changed

+347
-284
lines changed

Tests/W3CWebDriver/AppSessionBase/AlarmClockBase.cs

+33-13
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,18 @@ public class AlarmClockBase
2929

3030
public static void Setup(TestContext context)
3131
{
32-
// Cleanup leftover objects from previous test if exists
33-
TearDown();
34-
35-
// Launch Alarm Clock
36-
DesiredCapabilities appCapabilities = new DesiredCapabilities();
37-
appCapabilities.SetCapability("app", CommonTestSettings.AlarmClockAppId);
38-
session = new WindowsDriver<WindowsElement>(new Uri(CommonTestSettings.WindowsApplicationDriverUrl), appCapabilities);
39-
Assert.IsNotNull(session);
40-
Assert.IsNotNull(session.SessionId);
41-
42-
// Initialize touch screen object
43-
touchScreen = new RemoteTouchScreen(session);
44-
Assert.IsNotNull(touchScreen);
32+
// Launch Alarm Clock if it is not yet launched
33+
if (session == null || touchScreen == null || !CurrentWindowIsAlive())
34+
{
35+
TearDown();
36+
session = Utility.CreateNewSession(CommonTestSettings.AlarmClockAppId);
37+
Assert.IsNotNull(session);
38+
Assert.IsNotNull(session.SessionId);
39+
40+
// Initialize touch screen object
41+
touchScreen = new RemoteTouchScreen(session);
42+
Assert.IsNotNull(touchScreen);
43+
}
4544
}
4645

4746
public static void TearDown()
@@ -83,6 +82,27 @@ protected void AddAlarmEntry(string alarmName)
8382
session.FindElementByAccessibilityId("AlarmSaveButton").Click();
8483
}
8584

85+
private static bool CurrentWindowIsAlive()
86+
{
87+
bool windowIsAlive = false;
88+
89+
if (session != null)
90+
{
91+
try
92+
{
93+
windowIsAlive = !String.IsNullOrEmpty(session.CurrentWindowHandle) && session.CurrentWindowHandle != "0";
94+
windowIsAlive = true;
95+
}
96+
catch
97+
{
98+
session.Quit();
99+
session = null;
100+
}
101+
}
102+
103+
return windowIsAlive;
104+
}
105+
86106
protected void DeletePreviouslyCreatedAlarmEntry(string alarmName)
87107
{
88108
while (true)

Tests/W3CWebDriver/AppSessionBase/CalculatorBase.cs

+12-19
Original file line numberDiff line numberDiff line change
@@ -14,32 +14,32 @@
1414
//
1515
//******************************************************************************
1616

17-
using System;
1817
using Microsoft.VisualStudio.TestTools.UnitTesting;
1918
using OpenQA.Selenium.Appium.Windows;
20-
using OpenQA.Selenium.Remote;
2119

2220
namespace W3CWebDriver
2321
{
2422
public class CalculatorBase
2523
{
2624
protected static WindowsDriver<WindowsElement> session;
27-
protected static WindowsElement header;
25+
private static WindowsElement header;
2826

2927
public static void Setup(TestContext context)
3028
{
31-
// Cleanup leftover objects from previous test if exists
32-
TearDown();
29+
// Launch Calculator if it is not yet launched
30+
if (session == null)
31+
{
32+
session = Utility.CreateNewSession(CommonTestSettings.CalculatorAppId);
33+
Assert.IsNotNull(session);
34+
Assert.IsNotNull(session.SessionId);
35+
header = session.FindElementByAccessibilityId("Header");
36+
Assert.IsNotNull(header);
37+
}
3338

34-
// Launch Calculator
35-
session = CreateNewCalculatorSession();
36-
Assert.IsNotNull(session);
37-
Assert.IsNotNull(session.SessionId);
39+
// Set focus on the calculator window
40+
header.Click();
3841

3942
// Ensure that calculator is in standard mode
40-
header = session.FindElementByAccessibilityId("Header");
41-
Assert.IsNotNull(header);
42-
4343
if (!header.Text.Equals("Standard"))
4444
{
4545
session.FindElementByAccessibilityId("NavButton").Click();
@@ -63,13 +63,6 @@ public static void TearDown()
6363
}
6464
}
6565

66-
public static WindowsDriver<WindowsElement> CreateNewCalculatorSession()
67-
{
68-
DesiredCapabilities appCapabilities = new DesiredCapabilities();
69-
appCapabilities.SetCapability("app", CommonTestSettings.CalculatorAppId);
70-
return new WindowsDriver<WindowsElement>(new Uri(CommonTestSettings.WindowsApplicationDriverUrl), appCapabilities);
71-
}
72-
7366
protected static WindowsElement GetStaleElement()
7467
{
7568
session.FindElementByAccessibilityId("ClearMemoryButton").Click();

Tests/W3CWebDriver/CommonTestSettings.cs renamed to Tests/W3CWebDriver/AppSessionBase/CommonTestSettings.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,14 @@
1414
//
1515
//******************************************************************************
1616

17-
using Microsoft.VisualStudio.TestTools.UnitTesting;
18-
1917
namespace W3CWebDriver
2018
{
2119
public class CommonTestSettings
2220
{
2321
public const string WindowsApplicationDriverUrl = "http://127.0.0.1:4723";
2422
public const string AlarmClockAppId = "Microsoft.WindowsAlarms_8wekyb3d8bbwe!App";
2523
public const string CalculatorAppId = "Microsoft.WindowsCalculator_8wekyb3d8bbwe!App";
24+
public const string DesktopAppId = "Root";
2625
public const string EdgeAppId = "Microsoft.MicrosoftEdge_8wekyb3d8bbwe!MicrosoftEdge";
2726
public const string ExplorerAppId = @"C:\Windows\System32\explorer.exe";
2827
public const string NotepadAppId = @"C:\Windows\System32\notepad.exe";
@@ -32,6 +31,7 @@ public class CommonTestSettings
3231

3332
public class ErrorStrings
3433
{
34+
public const string ElementNotVisible = "An element command could not be completed because the element is not pointer- or keyboard interactable.";
3535
public const string NoSuchElement = "An element could not be located on the page using the given search parameters.";
3636
public const string NoSuchWindow = "Currently selected window has been closed";
3737
public const string StaleElementReference = "An element command failed because the referenced element is no longer attached to the DOM.";

Tests/W3CWebDriver/AppSessionBase/EdgeBase.cs

+1-3
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,7 @@ public static void Setup(TestContext context)
3939
TearDown();
4040

4141
// Launch the Edge browser app
42-
DesiredCapabilities appCapabilities = new DesiredCapabilities();
43-
appCapabilities.SetCapability("app", CommonTestSettings.EdgeAppId);
44-
session = new WindowsDriver<WindowsElement>(new Uri(CommonTestSettings.WindowsApplicationDriverUrl), appCapabilities);
42+
session = Utility.CreateNewSession(CommonTestSettings.EdgeAppId);
4543
session.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(5));
4644
Assert.IsNotNull(session);
4745
Assert.IsNotNull(session.SessionId);

Tests/W3CWebDriver/AppSessionBase/Utility.cs

+37-11
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@
1414
//
1515
//******************************************************************************
1616

17-
using Microsoft.VisualStudio.TestTools.UnitTesting;
1817
using OpenQA.Selenium.Appium.Windows;
18+
using OpenQA.Selenium.Remote;
19+
using System;
1920

2021
namespace W3CWebDriver
2122
{
@@ -24,25 +25,41 @@ public class Utility
2425
private static WindowsDriver<WindowsElement> orphanedSession;
2526
private static WindowsElement orphanedElement;
2627

28+
~Utility()
29+
{
30+
CleanupOrphanedSession();
31+
}
32+
33+
public static WindowsDriver<WindowsElement> CreateNewSession(string appId)
34+
{
35+
DesiredCapabilities appCapabilities = new DesiredCapabilities();
36+
appCapabilities.SetCapability("app", appId);
37+
return new WindowsDriver<WindowsElement>(new Uri(CommonTestSettings.WindowsApplicationDriverUrl), appCapabilities);
38+
}
39+
2740
public static WindowsElement GetOrphanedElement()
2841
{
29-
// Create new calculator session and close the window to get an orphaned element
42+
// Re-initialize orphaned session and element if they are compromised
3043
if (orphanedSession == null || orphanedElement == null)
3144
{
32-
if (orphanedSession == null)
33-
{
34-
orphanedSession = CalculatorBase.CreateNewCalculatorSession();
35-
}
36-
37-
Assert.IsNotNull(orphanedSession);
38-
orphanedElement = orphanedSession.FindElementByAccessibilityId("Header");
39-
orphanedSession.Close();
45+
InitializeOrphanedSession();
4046
}
4147

4248
return orphanedElement;
4349
}
4450

45-
~Utility()
51+
public static WindowsDriver<WindowsElement> GetOrphanedSession()
52+
{
53+
// Re-initialize orphaned session and element if they are compromised
54+
if (orphanedSession == null || orphanedElement == null)
55+
{
56+
InitializeOrphanedSession();
57+
}
58+
59+
return orphanedSession;
60+
}
61+
62+
private static void CleanupOrphanedSession()
4663
{
4764
orphanedElement = null;
4865

@@ -53,5 +70,14 @@ public static WindowsElement GetOrphanedElement()
5370
orphanedSession = null;
5471
}
5572
}
73+
74+
private static void InitializeOrphanedSession()
75+
{
76+
// Create new calculator session and close the window to get an orphaned element
77+
CleanupOrphanedSession();
78+
orphanedSession = CreateNewSession(CommonTestSettings.CalculatorAppId);
79+
orphanedElement = orphanedSession.FindElementByAccessibilityId("Header");
80+
orphanedSession.Close();
81+
}
5682
}
5783
}

Tests/W3CWebDriver/Back.cs

+4-19
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,7 @@ public class Back
2929
[TestMethod]
3030
public void NavigateBackBrowser()
3131
{
32-
DesiredCapabilities appCapabilities = new DesiredCapabilities();
33-
appCapabilities.SetCapability("app", CommonTestSettings.EdgeAppId);
34-
session = new WindowsDriver<WindowsElement>(new Uri(CommonTestSettings.WindowsApplicationDriverUrl), appCapabilities);
32+
session = Utility.CreateNewSession(CommonTestSettings.EdgeAppId);
3533
Assert.IsNotNull(session);
3634
System.Threading.Thread.Sleep(3000); // Sleep for 3 seconds
3735

@@ -59,9 +57,7 @@ public void NavigateBackBrowser()
5957
[TestMethod]
6058
public void NavigateBackModernApp()
6159
{
62-
DesiredCapabilities appCapabilities = new DesiredCapabilities();
63-
appCapabilities.SetCapability("app", CommonTestSettings.AlarmClockAppId);
64-
session = new WindowsDriver<WindowsElement>(new Uri(CommonTestSettings.WindowsApplicationDriverUrl), appCapabilities);
60+
session = Utility.CreateNewSession(CommonTestSettings.AlarmClockAppId);
6561
Assert.IsNotNull(session);
6662

6763
// Navigate to New Alarm view
@@ -80,9 +76,7 @@ public void NavigateBackModernApp()
8076
[TestMethod]
8177
public void NavigateBackSystemApp()
8278
{
83-
DesiredCapabilities appCapabilities = new DesiredCapabilities();
84-
appCapabilities.SetCapability("app", CommonTestSettings.ExplorerAppId);
85-
session = new WindowsDriver<WindowsElement>(new Uri(CommonTestSettings.WindowsApplicationDriverUrl), appCapabilities);
79+
session = Utility.CreateNewSession(CommonTestSettings.ExplorerAppId);
8680
Assert.IsNotNull(session);
8781

8882
System.Threading.Thread.Sleep(1000); // Sleep for 1 second
@@ -109,24 +103,15 @@ public void NavigateBackSystemApp()
109103
[TestMethod]
110104
public void ErrorNavigateBackNoSuchWindow()
111105
{
112-
DesiredCapabilities appCapabilities = new DesiredCapabilities();
113-
appCapabilities.SetCapability("app", CommonTestSettings.AlarmClockAppId);
114-
session = new WindowsDriver<WindowsElement>(new Uri(CommonTestSettings.WindowsApplicationDriverUrl), appCapabilities);
115-
Assert.IsNotNull(session);
116-
117106
try
118107
{
119-
session.Close();
120-
session.Navigate().Back();
108+
Utility.GetOrphanedSession().Navigate().Back();
121109
Assert.Fail("Exception should have been thrown");
122110
}
123111
catch (System.InvalidOperationException exception)
124112
{
125113
Assert.AreEqual(ErrorStrings.NoSuchWindow, exception.Message);
126114
}
127-
128-
session.Quit();
129-
session = null;
130115
}
131116
}
132117
}

Tests/W3CWebDriver/Element.cs

+14
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,20 @@ public void ErrorFindElementByUnsupportedLocatorPartialLinkText()
146146
Assert.Fail("Exception should have been thrown");
147147
}
148148

149+
[TestMethod]
150+
public void ErrorFindElementNoSuchWindow()
151+
{
152+
try
153+
{
154+
WindowsElement element = Utility.GetOrphanedSession().FindElementByAccessibilityId("An accessibility id") as WindowsElement;
155+
Assert.Fail("Exception should have been thrown");
156+
}
157+
catch (System.InvalidOperationException exception)
158+
{
159+
Assert.AreEqual(ErrorStrings.NoSuchWindow, exception.Message);
160+
}
161+
}
162+
149163
[TestMethod]
150164
public void FindElementByTagName()
151165
{

Tests/W3CWebDriver/ElementActive.cs

+1-6
Original file line numberDiff line numberDiff line change
@@ -37,20 +37,15 @@ public static void ClassCleanup()
3737
[TestMethod]
3838
public void ErrorGetActiveElementNoSuchWindow()
3939
{
40-
WindowsDriver<WindowsElement> newSession = CreateNewCalculatorSession();
41-
newSession.Close();
42-
4340
try
4441
{
45-
WindowsElement activeElement = newSession.SwitchTo().ActiveElement() as WindowsElement;
42+
WindowsElement activeElement = Utility.GetOrphanedSession().SwitchTo().ActiveElement() as WindowsElement;
4643
Assert.Fail("Exception should have been thrown");
4744
}
4845
catch (System.InvalidOperationException exception)
4946
{
5047
Assert.AreEqual(ErrorStrings.NoSuchWindow, exception.Message);
5148
}
52-
53-
newSession.Quit();
5449
}
5550

5651
[TestMethod]

Tests/W3CWebDriver/ElementAttribute.cs

+28
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,34 @@ public static void ClassCleanup()
3333
TearDown();
3434
}
3535

36+
[TestMethod]
37+
public void ErrorGetElementAttributeNoSuchWindow()
38+
{
39+
try
40+
{
41+
var attribute = Utility.GetOrphanedElement().GetAttribute("Attribute");
42+
Assert.Fail("Exception should have been thrown");
43+
}
44+
catch (System.InvalidOperationException exception)
45+
{
46+
Assert.AreEqual(ErrorStrings.NoSuchWindow, exception.Message);
47+
}
48+
}
49+
50+
[TestMethod]
51+
public void ErrorGetElementAttributeStaleElement()
52+
{
53+
try
54+
{
55+
var attribute = GetStaleElement().GetAttribute("Attribute");
56+
Assert.Fail("Exception should have been thrown");
57+
}
58+
catch (System.InvalidOperationException exception)
59+
{
60+
Assert.AreEqual(ErrorStrings.StaleElementReference, exception.Message);
61+
}
62+
}
63+
3664
[TestMethod]
3765
public void GetValidElementAttribute()
3866
{

Tests/W3CWebDriver/ElementClick.cs

+20
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,26 @@ public static void ClassCleanup()
3434
TearDown();
3535
}
3636

37+
[TestMethod]
38+
public void ErrorClickElementNotVisible()
39+
{
40+
try
41+
{
42+
// Navigate to Stopwatch tab and attempt to click on addAlarmButton that is no longer displayed
43+
WindowsElement addAlarmButton = session.FindElementByAccessibilityId("AddAlarmButton");
44+
Assert.IsTrue(addAlarmButton.Displayed);
45+
WindowsElement stopwatchPivotItem = session.FindElementByAccessibilityId("StopwatchPivotItem");
46+
stopwatchPivotItem.Click();
47+
Assert.IsFalse(addAlarmButton.Displayed);
48+
addAlarmButton.Click();
49+
Assert.Fail("Exception should have been thrown");
50+
}
51+
catch (System.InvalidOperationException exception)
52+
{
53+
Assert.AreEqual(ErrorStrings.ElementNotVisible, exception.Message);
54+
}
55+
}
56+
3757
[TestMethod]
3858
public void ErrorClickElementNoSuchWindow()
3959
{

0 commit comments

Comments
 (0)