Skip to content

Commit 534566c

Browse files
[dotnet] Modernize EnvironmentManager, standardize assembly teardown (#15551)
* [dotnet] Modernize `EnvironmentManager`, standardize assembly teardown * fix whitespace * Fix whitespace * Fix newline again
1 parent b1d3522 commit 534566c

File tree

14 files changed

+145
-158
lines changed

14 files changed

+145
-158
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// <copyright file="AssemblyTeardown.cs" company="Selenium Committers">
2+
// Licensed to the Software Freedom Conservancy (SFC) under one
3+
// or more contributor license agreements. See the NOTICE file
4+
// distributed with this work for additional information
5+
// regarding copyright ownership. The SFC licenses this file
6+
// to you under the Apache License, Version 2.0 (the
7+
// "License"); you may not use this file except in compliance
8+
// with the License. You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing,
13+
// software distributed under the License is distributed on an
14+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
// KIND, either express or implied. See the License for the
16+
// specific language governing permissions and limitations
17+
// under the License.
18+
// </copyright>
19+
20+
using NUnit.Framework;
21+
using OpenQA.Selenium.Environment;
22+
using System.Threading.Tasks;
23+
24+
[SetUpFixture]
25+
#pragma warning disable // Outside a namespace to affect the entire assembly
26+
public class AssemblyTeardown
27+
#pragma warning restore
28+
{
29+
[OneTimeSetUp]
30+
public async Task RunBeforeAnyTestAsync()
31+
{
32+
await EnvironmentManager.Instance.WebServer.StartAsync();
33+
}
34+
35+
[OneTimeTearDown]
36+
public async Task RunAfterAnyTestsAsync()
37+
{
38+
EnvironmentManager.Instance.CloseCurrentDriver();
39+
await EnvironmentManager.Instance.WebServer.StopAsync();
40+
}
41+
}

dotnet/test/chrome/ChromeSpecificTests.cs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,10 @@
1818
// </copyright>
1919

2020
using NUnit.Framework;
21-
using OpenQA.Selenium.Environment;
22-
using System.Threading.Tasks;
2321

2422
namespace OpenQA.Selenium.Chrome;
2523

2624
[TestFixture]
2725
public class ChromeSpecificTests : DriverTestFixture
2826
{
29-
[OneTimeTearDown]
30-
public async Task RunAfterAnyTestsAsync()
31-
{
32-
EnvironmentManager.Instance.CloseCurrentDriver();
33-
await EnvironmentManager.Instance.WebServer.StopAsync();
34-
}
3527
}

dotnet/test/common/CustomTestAttributes/NeedsFreshDriverAttribute.cs

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -25,39 +25,29 @@ namespace OpenQA.Selenium;
2525

2626
public class NeedsFreshDriverAttribute : TestActionAttribute
2727
{
28-
private bool isCreatedBeforeTest = false;
29-
private bool isCreatedAfterTest = false;
28+
public bool IsCreatedBeforeTest { get; set; } = false;
3029

31-
public bool IsCreatedBeforeTest
32-
{
33-
get { return isCreatedBeforeTest; }
34-
set { isCreatedBeforeTest = value; }
35-
}
36-
37-
public bool IsCreatedAfterTest
38-
{
39-
get { return isCreatedAfterTest; }
40-
set { isCreatedAfterTest = value; }
41-
}
30+
public bool IsCreatedAfterTest { get; set; } = false;
4231

4332
public override void BeforeTest(ITest test)
4433
{
45-
DriverTestFixture fixtureInstance = test.Fixture as DriverTestFixture;
46-
if (fixtureInstance != null && this.isCreatedBeforeTest)
34+
if (test.Fixture is DriverTestFixture fixtureInstance && this.IsCreatedBeforeTest)
4735
{
4836
EnvironmentManager.Instance.CreateFreshDriver();
49-
fixtureInstance.DriverInstance = EnvironmentManager.Instance.GetCurrentDriver();
37+
fixtureInstance.driver = EnvironmentManager.Instance.GetCurrentDriver();
5038
}
39+
5140
base.BeforeTest(test);
5241
}
5342

5443
public override void AfterTest(ITest test)
5544
{
56-
DriverTestFixture fixtureInstance = test.Fixture as DriverTestFixture;
57-
if (fixtureInstance != null && this.isCreatedAfterTest)
45+
if (test.Fixture is DriverTestFixture fixtureInstance && this.IsCreatedAfterTest)
5846
{
5947
EnvironmentManager.Instance.CreateFreshDriver();
60-
fixtureInstance.DriverInstance = EnvironmentManager.Instance.GetCurrentDriver();
48+
fixtureInstance.driver = EnvironmentManager.Instance.GetCurrentDriver();
6149
}
50+
51+
base.AfterTest(test);
6252
}
6353
}

dotnet/test/common/DriverTestFixture.cs

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -109,20 +109,15 @@ public abstract class DriverTestFixture
109109

110110
public string printPage = EnvironmentManager.Instance.UrlBuilder.WhereIs("printPage.html");
111111

112-
protected IWebDriver driver;
113-
114-
public IWebDriver DriverInstance
115-
{
116-
get { return driver; }
117-
set { driver = value; }
118-
}
112+
public IWebDriver driver { get; set; }
119113

120114
public bool IsNativeEventsEnabled
121115
{
122116
get
123117
{
124-
IHasCapabilities capabilitiesDriver = driver as IHasCapabilities;
125-
if (capabilitiesDriver != null && capabilitiesDriver.Capabilities.HasCapability(CapabilityType.HasNativeEvents) && (bool)capabilitiesDriver.Capabilities.GetCapability(CapabilityType.HasNativeEvents))
118+
if (driver is IHasCapabilities capabilitiesDriver &&
119+
capabilitiesDriver.Capabilities.HasCapability(CapabilityType.HasNativeEvents) &&
120+
(bool)capabilitiesDriver.Capabilities.GetCapability(CapabilityType.HasNativeEvents))
126121
{
127122
return true;
128123
}
@@ -154,12 +149,6 @@ protected void CreateFreshDriver()
154149
driver = EnvironmentManager.Instance.CreateFreshDriver();
155150
}
156151

157-
protected bool IsIeDriverTimedOutException(Exception e)
158-
{
159-
// The IE driver may throw a timed out exception
160-
return e.GetType().Name.Contains("TimedOutException");
161-
}
162-
163152
protected bool WaitFor(Func<bool> waitFunction, string timeoutMessage)
164153
{
165154
return WaitFor<bool>(waitFunction, timeoutMessage);
@@ -173,7 +162,7 @@ protected T WaitFor<T>(Func<T> waitFunction, string timeoutMessage)
173162
protected T WaitFor<T>(Func<T> waitFunction, TimeSpan timeout, string timeoutMessage)
174163
{
175164
DateTime endTime = DateTime.Now.Add(timeout);
176-
T value = default(T);
165+
T value = default;
177166
Exception lastException = null;
178167
while (DateTime.Now < endTime)
179168
{
@@ -207,6 +196,6 @@ protected T WaitFor<T>(Func<T> waitFunction, TimeSpan timeout, string timeoutMes
207196
}
208197

209198
Assert.Fail("Condition timed out: " + timeoutMessage);
210-
return default(T);
199+
return default;
211200
}
212201
}

dotnet/test/common/Environment/EnvironmentManager.cs

Lines changed: 18 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,9 @@ namespace OpenQA.Selenium.Environment;
3131
public class EnvironmentManager
3232
{
3333
private static EnvironmentManager instance;
34-
private Type driverType;
35-
private Browser browser;
34+
private readonly Type driverType;
3635
private IWebDriver driver;
37-
private UrlBuilder urlBuilder;
38-
private TestWebServer webServer;
39-
private DriverFactory driverFactory;
40-
private RemoteSeleniumServer remoteServer;
41-
private string remoteCapabilities;
36+
private readonly DriverFactory driverFactory;
4237

4338
private EnvironmentManager()
4439
{
@@ -94,10 +89,10 @@ private EnvironmentManager()
9489
throw new ArgumentOutOfRangeException($"Unable to find driver type {driverConfig.DriverTypeName}");
9590
}
9691

97-
browser = driverConfig.BrowserValue;
98-
remoteCapabilities = driverConfig.RemoteCapabilities;
92+
Browser = driverConfig.BrowserValue;
93+
RemoteCapabilities = driverConfig.RemoteCapabilities;
9994

100-
urlBuilder = new UrlBuilder(websiteConfig);
95+
UrlBuilder = new UrlBuilder(websiteConfig);
10196

10297
// When run using the `bazel test` command, the following environment
10398
// variable will be set. If not set, we're running from a build system
@@ -188,48 +183,28 @@ private EnvironmentManager()
188183
// Use the default one.
189184
}
190185

191-
webServer = new TestWebServer(projectRoot, webServerConfig);
186+
WebServer = new TestWebServer(projectRoot, webServerConfig);
192187
bool autoStartRemoteServer = false;
193-
if (browser == Browser.Remote)
188+
if (Browser == Browser.Remote)
194189
{
195190
autoStartRemoteServer = driverConfig.AutoStartRemoteServer;
196191
}
197192

198-
remoteServer = new RemoteSeleniumServer(projectRoot, autoStartRemoteServer);
193+
RemoteServer = new RemoteSeleniumServer(projectRoot, autoStartRemoteServer);
199194
}
200195

201196
~EnvironmentManager()
202197
{
203-
if (remoteServer != null)
204-
{
205-
remoteServer.StopAsync().Wait();
206-
}
207-
if (webServer != null)
208-
{
209-
webServer.StopAsync().Wait();
210-
}
198+
RemoteServer?.StopAsync().Wait();
199+
WebServer?.StopAsync().Wait();
211200
CloseCurrentDriver();
212201
}
213202

214203
public event EventHandler<DriverStartingEventArgs> DriverStarting;
215204

216-
public static EnvironmentManager Instance
217-
{
218-
get
219-
{
220-
if (instance == null)
221-
{
222-
instance = new EnvironmentManager();
223-
}
224-
225-
return instance;
226-
}
227-
}
205+
public static EnvironmentManager Instance => instance ??= new EnvironmentManager();
228206

229-
public Browser Browser
230-
{
231-
get { return browser; }
232-
}
207+
public Browser Browser { get; }
233208

234209
public string CurrentDirectory
235210
{
@@ -245,39 +220,17 @@ public string CurrentDirectory
245220
}
246221
}
247222

248-
public TestWebServer WebServer
249-
{
250-
get { return webServer; }
251-
}
223+
public TestWebServer WebServer { get; }
252224

253-
public RemoteSeleniumServer RemoteServer
254-
{
255-
get { return remoteServer; }
256-
}
225+
public RemoteSeleniumServer RemoteServer { get; }
257226

258-
public string RemoteCapabilities
259-
{
260-
get { return remoteCapabilities; }
261-
}
227+
public string RemoteCapabilities { get; }
262228

263-
public UrlBuilder UrlBuilder
264-
{
265-
get
266-
{
267-
return urlBuilder;
268-
}
269-
}
229+
public UrlBuilder UrlBuilder { get; }
270230

271231
public IWebDriver GetCurrentDriver()
272232
{
273-
if (driver != null)
274-
{
275-
return driver;
276-
}
277-
else
278-
{
279-
return CreateFreshDriver();
280-
}
233+
return driver ?? CreateFreshDriver();
281234
}
282235

283236
public IWebDriver CreateDriverInstance()
@@ -299,10 +252,7 @@ public IWebDriver CreateFreshDriver()
299252

300253
public void CloseCurrentDriver()
301254
{
302-
if (driver != null)
303-
{
304-
driver.Quit();
305-
}
255+
driver?.Quit();
306256
driver = null;
307257
}
308258

dotnet/test/common/StubDriver.cs

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -28,34 +28,19 @@ public class StubDriver : IWebDriver
2828

2929
public string Url
3030
{
31-
get
32-
{
33-
throw new NotImplementedException();
34-
}
31+
get => throw new NotImplementedException();
3532
set
3633
{
3734
}
3835
}
3936

40-
public string Title
41-
{
42-
get { throw new NotImplementedException(); }
43-
}
37+
public string Title => throw new NotImplementedException();
4438

45-
public string PageSource
46-
{
47-
get { throw new NotImplementedException(); }
48-
}
39+
public string PageSource => throw new NotImplementedException();
4940

50-
public string CurrentWindowHandle
51-
{
52-
get { throw new NotImplementedException(); }
53-
}
41+
public string CurrentWindowHandle => throw new NotImplementedException();
5442

55-
public ReadOnlyCollection<string> WindowHandles
56-
{
57-
get { throw new NotImplementedException(); }
58-
}
43+
public ReadOnlyCollection<string> WindowHandles => throw new NotImplementedException();
5944

6045
public void Close()
6146
{
@@ -82,7 +67,7 @@ public ITargetLocator SwitchTo()
8267
throw new NotImplementedException();
8368
}
8469

85-
public System.Collections.ObjectModel.ReadOnlyCollection<string> GetWindowHandles()
70+
public ReadOnlyCollection<string> GetWindowHandles()
8671
{
8772
throw new NotImplementedException();
8873
}
@@ -101,7 +86,7 @@ public IWebElement FindElement(By by)
10186
throw new NotImplementedException();
10287
}
10388

104-
public System.Collections.ObjectModel.ReadOnlyCollection<IWebElement> FindElements(By by)
89+
public ReadOnlyCollection<IWebElement> FindElements(By by)
10590
{
10691
throw new NotImplementedException();
10792
}

dotnet/test/common/TestUtilities.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public class TestUtilities
2525
{
2626
private static IJavaScriptExecutor GetExecutor(IWebDriver driver)
2727
{
28-
return driver as IJavaScriptExecutor;
28+
return (IJavaScriptExecutor)driver;
2929
}
3030

3131
private static string GetUserAgent(IWebDriver driver)

dotnet/test/edge/AssemblyTeardown.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,10 @@
2121
using OpenQA.Selenium.Environment;
2222
using System.Threading.Tasks;
2323

24-
namespace OpenQA.Selenium.Edge;
25-
2624
[SetUpFixture]
27-
// Outside a namespace to affect the entire assembly
28-
public class MySetUpClass
25+
#pragma warning disable // Outside a namespace to affect the entire assembly
26+
public class AssemblyTeardown
27+
#pragma warning restore
2928
{
3029
[OneTimeSetUp]
3130
public async Task RunBeforeAnyTestAsync()

0 commit comments

Comments
 (0)