Skip to content

Commit fa5a0cb

Browse files
committed
[dotnet] Modernize EnvironmentManager, standardize assembly teardown
1 parent 2f25394 commit fa5a0cb

File tree

14 files changed

+193
-211
lines changed

14 files changed

+193
-211
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,19 +18,11 @@
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
}
3628
}

dotnet/test/common/CustomTestAttributes/NeedsFreshDriverAttribute.cs

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -25,40 +25,30 @@ 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
}
6454
}

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,7 +196,7 @@ 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
}
213202
}

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
{
@@ -91,10 +86,10 @@ private EnvironmentManager()
9186
throw new ArgumentOutOfRangeException($"Unable to find driver type {driverConfig.DriverTypeName}");
9287
}
9388

94-
browser = driverConfig.BrowserValue;
95-
remoteCapabilities = driverConfig.RemoteCapabilities;
89+
Browser = driverConfig.BrowserValue;
90+
RemoteCapabilities = driverConfig.RemoteCapabilities;
9691

97-
urlBuilder = new UrlBuilder(websiteConfig);
92+
UrlBuilder = new UrlBuilder(websiteConfig);
9893

9994
// When run using the `bazel test` command, the following environment
10095
// variable will be set. If not set, we're running from a build system
@@ -185,48 +180,28 @@ private EnvironmentManager()
185180
// Use the default one.
186181
}
187182

188-
webServer = new TestWebServer(projectRoot, webServerConfig);
183+
WebServer = new TestWebServer(projectRoot, webServerConfig);
189184
bool autoStartRemoteServer = false;
190-
if (browser == Browser.Remote)
185+
if (Browser == Browser.Remote)
191186
{
192187
autoStartRemoteServer = driverConfig.AutoStartRemoteServer;
193188
}
194189

195-
remoteServer = new RemoteSeleniumServer(projectRoot, autoStartRemoteServer);
190+
RemoteServer = new RemoteSeleniumServer(projectRoot, autoStartRemoteServer);
196191
}
197192

198193
~EnvironmentManager()
199194
{
200-
if (remoteServer != null)
201-
{
202-
remoteServer.StopAsync().Wait();
203-
}
204-
if (webServer != null)
205-
{
206-
webServer.StopAsync().Wait();
207-
}
195+
RemoteServer?.StopAsync().Wait();
196+
WebServer?.StopAsync().Wait();
208197
CloseCurrentDriver();
209198
}
210199

211200
public event EventHandler<DriverStartingEventArgs> DriverStarting;
212201

213-
public static EnvironmentManager Instance
214-
{
215-
get
216-
{
217-
if (instance == null)
218-
{
219-
instance = new EnvironmentManager();
220-
}
221-
222-
return instance;
223-
}
224-
}
202+
public static EnvironmentManager Instance => instance ??= new EnvironmentManager();
225203

226-
public Browser Browser
227-
{
228-
get { return browser; }
229-
}
204+
public Browser Browser { get; }
230205

231206
public string CurrentDirectory
232207
{
@@ -242,39 +217,17 @@ public string CurrentDirectory
242217
}
243218
}
244219

245-
public TestWebServer WebServer
246-
{
247-
get { return webServer; }
248-
}
220+
public TestWebServer WebServer { get; }
249221

250-
public RemoteSeleniumServer RemoteServer
251-
{
252-
get { return remoteServer; }
253-
}
222+
public RemoteSeleniumServer RemoteServer { get; }
254223

255-
public string RemoteCapabilities
256-
{
257-
get { return remoteCapabilities; }
258-
}
224+
public string RemoteCapabilities { get; }
259225

260-
public UrlBuilder UrlBuilder
261-
{
262-
get
263-
{
264-
return urlBuilder;
265-
}
266-
}
226+
public UrlBuilder UrlBuilder { get; }
267227

268228
public IWebDriver GetCurrentDriver()
269229
{
270-
if (driver != null)
271-
{
272-
return driver;
273-
}
274-
else
275-
{
276-
return CreateFreshDriver();
277-
}
230+
return driver ?? CreateFreshDriver();
278231
}
279232

280233
public IWebDriver CreateDriverInstance()
@@ -296,10 +249,7 @@ public IWebDriver CreateFreshDriver()
296249

297250
public void CloseCurrentDriver()
298251
{
299-
if (driver != null)
300-
{
301-
driver.Quit();
302-
}
252+
driver?.Quit();
303253
driver = null;
304254
}
305255

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: 5 additions & 6 deletions
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)
@@ -106,13 +106,12 @@ public static bool IsOldIE(IWebDriver driver)
106106

107107
public static bool IsNativeEventsEnabled(IWebDriver driver)
108108
{
109-
IHasCapabilities hasCaps = driver as IHasCapabilities;
110-
if (hasCaps != null)
109+
if (driver is IHasCapabilities hasCaps)
111110
{
112-
object cap = hasCaps.Capabilities.GetCapability(OpenQA.Selenium.CapabilityType.HasNativeEvents);
113-
if (cap != null && cap is bool)
111+
object cap = hasCaps.Capabilities.GetCapability(CapabilityType.HasNativeEvents);
112+
if (cap != null && cap is bool b)
114113
{
115-
return (bool)cap;
114+
return b;
116115
}
117116
}
118117

0 commit comments

Comments
 (0)