|
24 | 24 |
|
25 | 25 | namespace UITests
|
26 | 26 | {
|
27 |
| - [TestClass] |
28 |
| - public class SessionManager |
29 |
| - { |
30 |
| - private const string WindowsApplicationDriverUrl = "http://127.0.0.1:4723"; |
31 |
| - private static readonly string[] WinUIGalleryAppIDs = new string[]{ |
32 |
| - // WinUI 3 Gallery apps built in the lab |
| 27 | + [TestClass] |
| 28 | + public class SessionManager |
| 29 | + { |
| 30 | + private const string WindowsApplicationDriverUrl = "http://127.0.0.1:4723"; |
| 31 | + private static readonly string[] WinUIGalleryAppIDs = new string[]{ |
| 32 | + // WinUI 3 Gallery apps built in the lab |
33 | 33 | "Microsoft.WinUI3ControlsGallery.Debug_grv3cx5qrw0gp!App",
|
34 | 34 | "Microsoft.WinUI3ControlsGallery_grv3cx5qrw0gp!App",
|
35 |
| - // WinUI 3 Gallery apps built locally |
| 35 | + // WinUI 3 Gallery apps built locally |
36 | 36 | "Microsoft.WinUI3ControlsGallery.Debug_8wekyb3d8bbwe!App",
|
37 | 37 | "Microsoft.WinUI3ControlsGallery_8wekyb3d8bbwe!App"
|
38 | 38 | };
|
39 | 39 |
|
40 |
| - private static uint appIdIndex = 0; |
| 40 | + private static uint appIdIndex = 0; |
41 | 41 |
|
42 |
| - private static WindowsDriver<WindowsElement> _session; |
43 |
| - public static WindowsDriver<WindowsElement> Session |
44 |
| - { |
45 |
| - get |
46 |
| - { |
47 |
| - if (_session is null) |
48 |
| - { |
49 |
| - Setup(null); |
50 |
| - } |
51 |
| - return _session; |
52 |
| - } |
53 |
| - } |
| 42 | + private static WindowsDriver<WindowsElement> _session; |
| 43 | + public static WindowsDriver<WindowsElement> Session |
| 44 | + { |
| 45 | + get |
| 46 | + { |
| 47 | + if (_session is null) |
| 48 | + { |
| 49 | + Setup(null); |
| 50 | + } |
| 51 | + return _session; |
| 52 | + } |
| 53 | + } |
54 | 54 |
|
55 |
| - [AssemblyInitialize] |
56 |
| - public static void Setup(TestContext _) |
57 |
| - { |
58 |
| - if (_session is null) |
59 |
| - { |
| 55 | + [AssemblyInitialize] |
| 56 | + public static void Setup(TestContext _) |
| 57 | + { |
| 58 | + if (_session is null) |
| 59 | + { |
60 | 60 |
|
61 |
| - int timeoutCount = 50; |
| 61 | + int timeoutCount = 50; |
62 | 62 |
|
63 |
| - TryInitializeSession(); |
64 |
| - if (_session is null) |
65 |
| - { |
| 63 | + TryInitializeSession(); |
| 64 | + if (_session is null) |
| 65 | + { |
66 | 66 | // WinAppDriver is probably not running, so lets start it!
|
67 | 67 | string winAppDriverX64Path = Path.Join(Environment.GetEnvironmentVariable("ProgramFiles"), "Windows Application Driver", "WinAppDriver.exe");
|
68 | 68 | string winAppDriverX86Path = Path.Join(Environment.GetEnvironmentVariable("ProgramFiles(x86)"), "Windows Application Driver", "WinAppDriver.exe");
|
69 | 69 |
|
70 | 70 | if (File.Exists(winAppDriverX64Path))
|
71 |
| - { |
72 |
| - Process.Start(winAppDriverX64Path); |
73 |
| - } |
74 |
| - else if (File.Exists(winAppDriverX86Path)) |
75 |
| - { |
76 |
| - Process.Start(winAppDriverX86Path); |
77 |
| - } |
78 |
| - else |
79 |
| - { |
80 |
| - throw new Exception("Unable to start WinAppDriver since no suitable location was found."); |
81 |
| - } |
| 71 | + { |
| 72 | + Process.Start(winAppDriverX64Path); |
| 73 | + } |
| 74 | + else if (File.Exists(winAppDriverX86Path)) |
| 75 | + { |
| 76 | + Process.Start(winAppDriverX86Path); |
| 77 | + } |
| 78 | + else |
| 79 | + { |
| 80 | + throw new Exception("Unable to start WinAppDriver since no suitable location was found."); |
| 81 | + } |
82 | 82 |
|
83 |
| - Thread.Sleep(10000); |
84 |
| - TryInitializeSession(); |
85 |
| - } |
| 83 | + Thread.Sleep(10000); |
| 84 | + TryInitializeSession(); |
| 85 | + } |
86 | 86 |
|
87 |
| - while (_session is null && timeoutCount < 1000 * 4) |
88 |
| - { |
89 |
| - TryInitializeSession(); |
90 |
| - Thread.Sleep(timeoutCount); |
91 |
| - timeoutCount *= 2; |
92 |
| - } |
| 87 | + while (_session is null && timeoutCount < 1000 * 4) |
| 88 | + { |
| 89 | + TryInitializeSession(); |
| 90 | + Thread.Sleep(timeoutCount); |
| 91 | + timeoutCount *= 2; |
| 92 | + } |
93 | 93 |
|
94 |
| - Thread.Sleep(3000); |
95 |
| - Assert.IsNotNull(_session); |
96 |
| - Assert.IsNotNull(_session.SessionId); |
97 |
| - AxeHelper.InitializeAxe(); |
98 |
| - |
99 |
| - // Dismiss the disclaimer window that may pop up on the very first application launch |
100 |
| - // If the disclaimer is not found, this throws an exception, so lets catch that |
101 |
| - try |
102 |
| - { |
103 |
| - _session.FindElementByName("Disclaimer").FindElementByName("Accept").Click(); |
104 |
| - } |
105 |
| - catch (OpenQA.Selenium.WebDriverException) { } |
| 94 | + Thread.Sleep(3000); |
| 95 | + Assert.IsNotNull(_session); |
| 96 | + Assert.IsNotNull(_session.SessionId); |
| 97 | + AxeHelper.InitializeAxe(); |
| 98 | + |
| 99 | + // Dismiss the disclaimer window that may pop up on the very first application launch |
| 100 | + // If the disclaimer is not found, this throws an exception, so lets catch that |
| 101 | + try |
| 102 | + { |
| 103 | + _session.FindElementByName("Disclaimer").FindElementByName("Accept").Click(); |
| 104 | + } |
| 105 | + catch (OpenQA.Selenium.WebDriverException) { } |
106 | 106 |
|
107 |
| - // Wait if something is still animating in the visual tree |
108 |
| - _session.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(3); |
109 |
| - _session.Manage().Window.Maximize(); |
110 |
| - } |
111 |
| - } |
| 107 | + // Wait if something is still animating in the visual tree |
| 108 | + _session.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(3); |
| 109 | + _session.Manage().Window.Maximize(); |
| 110 | + } |
| 111 | + } |
112 | 112 |
|
113 |
| - [AssemblyCleanup()] |
114 |
| - public static void TestRunTearDown() |
115 |
| - { |
116 |
| - TearDown(); |
117 |
| - } |
| 113 | + [AssemblyCleanup()] |
| 114 | + public static void TestRunTearDown() |
| 115 | + { |
| 116 | + TearDown(); |
| 117 | + } |
118 | 118 |
|
119 |
| - public static void TearDown() |
120 |
| - { |
121 |
| - if (_session is not null) |
122 |
| - { |
123 |
| - _session.CloseApp(); |
124 |
| - _session.Quit(); |
125 |
| - _session = null; |
126 |
| - } |
127 |
| - } |
| 119 | + public static void TearDown() |
| 120 | + { |
| 121 | + if (_session is not null) |
| 122 | + { |
| 123 | + _session.CloseApp(); |
| 124 | + _session.Quit(); |
| 125 | + _session = null; |
| 126 | + } |
| 127 | + } |
128 | 128 |
|
129 |
| - private static void TryInitializeSession() |
130 |
| - { |
131 |
| - AppiumOptions appiumOptions = new AppiumOptions(); |
132 |
| - appiumOptions.AddAdditionalCapability("app", WinUIGalleryAppIDs[appIdIndex]); |
133 |
| - appiumOptions.AddAdditionalCapability("deviceName", "WindowsPC"); |
134 |
| - try |
135 |
| - { |
136 |
| - _session = new WindowsDriver<WindowsElement>(new Uri(WindowsApplicationDriverUrl), appiumOptions); |
137 |
| - } |
138 |
| - catch (OpenQA.Selenium.WebDriverException exc) |
139 |
| - { |
140 |
| - // Use next app ID since the current one was failing |
141 |
| - if (exc.Message.Contains("Package was not found")) |
142 |
| - { |
143 |
| - appIdIndex++; |
144 |
| - } |
145 |
| - else |
146 |
| - { |
147 |
| - Console.WriteLine("Failed to update start driver, got exception:" + exc.Message); |
148 |
| - } |
149 |
| - } |
150 |
| - } |
151 |
| - } |
| 129 | + private static void TryInitializeSession() |
| 130 | + { |
| 131 | + AppiumOptions appiumOptions = new AppiumOptions(); |
| 132 | + appiumOptions.AddAdditionalCapability("app", WinUIGalleryAppIDs[appIdIndex]); |
| 133 | + appiumOptions.AddAdditionalCapability("deviceName", "WindowsPC"); |
| 134 | + try |
| 135 | + { |
| 136 | + _session = new WindowsDriver<WindowsElement>(new Uri(WindowsApplicationDriverUrl), appiumOptions); |
| 137 | + } |
| 138 | + catch (OpenQA.Selenium.WebDriverException exc) |
| 139 | + { |
| 140 | + // Use next app ID since the current one was failing |
| 141 | + if (exc.Message.Contains("Package was not found")) |
| 142 | + { |
| 143 | + appIdIndex++; |
| 144 | + } |
| 145 | + else |
| 146 | + { |
| 147 | + Console.WriteLine("Failed to update start driver, got exception:" + exc.Message); |
| 148 | + } |
| 149 | + } |
| 150 | + } |
| 151 | + } |
152 | 152 | }
|
0 commit comments