forked from microsoft/WinUI-Gallery
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSessionManager.cs
145 lines (130 loc) · 4.03 KB
/
SessionManager.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
//******************************************************************************
//
// Copyright (c) 2024 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;
using OpenQA.Selenium.Appium.Windows;
using System;
using System.Diagnostics;
using System.IO;
using System.Threading;
namespace UITests
{
[TestClass]
public class SessionManager
{
private const string WindowsApplicationDriverUrl = "http://127.0.0.1:4723";
private static readonly string[] WinUIGalleryAppIDs = new string[]{
"Microsoft.WinUI3ControlsGallery.Debug_grv3cx5qrw0gp!App",
"Microsoft.WinUI3ControlsGallery_grv3cx5qrw0gp!App"
};
private static uint appIdIndex = 0;
private static WindowsDriver<WindowsElement> _session;
public static WindowsDriver<WindowsElement> Session
{
get
{
if (_session is null)
{
Setup(null);
}
return _session;
}
}
[AssemblyInitialize]
public static void Setup(TestContext _)
{
if (_session is null)
{
int timeoutCount = 50;
TryInitializeSession();
if (_session is null)
{
// WinAppDriver is probably not running, so lets start it!
if (File.Exists(@"C:\Program Files (x86)\Windows Application Driver\WinAppDriver.exe"))
{
Process.Start(@"C:\Program Files (x86)\Windows Application Driver\WinAppDriver.exe");
}
else if (File.Exists(@"C:\Program Files\Windows Application Driver\WinAppDriver.exe"))
{
Process.Start(@"C:\Program Files\Windows Application Driver\WinAppDriver.exe");
}
else
{
throw new Exception("Unable to start WinAppDriver since no suitable location was found.");
}
Thread.Sleep(10000);
TryInitializeSession();
}
while (_session is null && timeoutCount < 1000 * 4)
{
TryInitializeSession();
Thread.Sleep(timeoutCount);
timeoutCount *= 2;
}
Thread.Sleep(3000);
Assert.IsNotNull(_session);
Assert.IsNotNull(_session.SessionId);
AxeHelper.InitializeAxe();
// Dismiss the disclaimer window that may pop up on the very first application launch
// If the disclaimer is not found, this throws an exception, so lets catch that
try
{
_session.FindElementByName("Disclaimer").FindElementByName("Accept").Click();
}
catch (OpenQA.Selenium.WebDriverException) { }
// Wait if something is still animating in the visual tree
_session.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(3);
_session.Manage().Window.Maximize();
}
}
[AssemblyCleanup()]
public static void TestRunTearDown()
{
TearDown();
}
public static void TearDown()
{
if (_session is not null)
{
_session.CloseApp();
_session.Quit();
_session = null;
}
}
private static void TryInitializeSession()
{
AppiumOptions appiumOptions = new AppiumOptions();
appiumOptions.AddAdditionalCapability("app", WinUIGalleryAppIDs[appIdIndex]);
appiumOptions.AddAdditionalCapability("deviceName", "WindowsPC");
try
{
_session = new WindowsDriver<WindowsElement>(new Uri(WindowsApplicationDriverUrl), appiumOptions);
}
catch (OpenQA.Selenium.WebDriverException exc)
{
// Use next app ID since the current one was failing
if (exc.Message.Contains("Package was not found"))
{
appIdIndex++;
}
else
{
Console.WriteLine("Failed to update start driver, got exception:" + exc.Message);
}
}
}
}
}