Skip to content

[dotnet] Modernize EnvironmentManager, standardize assembly teardown #15551

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions dotnet/test/chrome/AssemblyTeardown.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// <copyright file="AssemblyTeardown.cs" company="Selenium Committers">
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
// </copyright>

using NUnit.Framework;
using OpenQA.Selenium.Environment;
using System.Threading.Tasks;

[SetUpFixture]
#pragma warning disable // Outside a namespace to affect the entire assembly
public class AssemblyTeardown
#pragma warning restore
{
[OneTimeSetUp]
public async Task RunBeforeAnyTestAsync()
{
await EnvironmentManager.Instance.WebServer.StartAsync();
}

[OneTimeTearDown]
public async Task RunAfterAnyTestsAsync()
{
EnvironmentManager.Instance.CloseCurrentDriver();
await EnvironmentManager.Instance.WebServer.StopAsync();
}
}
8 changes: 0 additions & 8 deletions dotnet/test/chrome/ChromeSpecificTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,11 @@
// </copyright>

using NUnit.Framework;
using OpenQA.Selenium.Environment;
using System.Threading.Tasks;

namespace OpenQA.Selenium.Chrome
{
[TestFixture]
public class ChromeSpecificTests : DriverTestFixture
{
[OneTimeTearDown]
public async Task RunAfterAnyTestsAsync()
{
EnvironmentManager.Instance.CloseCurrentDriver();
await EnvironmentManager.Instance.WebServer.StopAsync();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,40 +25,30 @@ namespace OpenQA.Selenium
{
public class NeedsFreshDriverAttribute : TestActionAttribute
{
private bool isCreatedBeforeTest = false;
private bool isCreatedAfterTest = false;
public bool IsCreatedBeforeTest { get; set; } = false;

public bool IsCreatedBeforeTest
{
get { return isCreatedBeforeTest; }
set { isCreatedBeforeTest = value; }
}

public bool IsCreatedAfterTest
{
get { return isCreatedAfterTest; }
set { isCreatedAfterTest = value; }
}
public bool IsCreatedAfterTest { get; set; } = false;

public override void BeforeTest(ITest test)
{
DriverTestFixture fixtureInstance = test.Fixture as DriverTestFixture;
if (fixtureInstance != null && this.isCreatedBeforeTest)
if (test.Fixture is DriverTestFixture fixtureInstance && this.IsCreatedBeforeTest)
{
EnvironmentManager.Instance.CreateFreshDriver();
fixtureInstance.DriverInstance = EnvironmentManager.Instance.GetCurrentDriver();
fixtureInstance.driver = EnvironmentManager.Instance.GetCurrentDriver();
}

base.BeforeTest(test);
}

public override void AfterTest(ITest test)
{
DriverTestFixture fixtureInstance = test.Fixture as DriverTestFixture;
if (fixtureInstance != null && this.isCreatedAfterTest)
if (test.Fixture is DriverTestFixture fixtureInstance && this.IsCreatedAfterTest)
{
EnvironmentManager.Instance.CreateFreshDriver();
fixtureInstance.DriverInstance = EnvironmentManager.Instance.GetCurrentDriver();
fixtureInstance.driver = EnvironmentManager.Instance.GetCurrentDriver();
}

base.AfterTest(test);
}
}
}
23 changes: 6 additions & 17 deletions dotnet/test/common/DriverTestFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,20 +109,15 @@ public abstract class DriverTestFixture

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

protected IWebDriver driver;

public IWebDriver DriverInstance
{
get { return driver; }
set { driver = value; }
}
public IWebDriver driver { get; set; }

public bool IsNativeEventsEnabled
{
get
{
IHasCapabilities capabilitiesDriver = driver as IHasCapabilities;
if (capabilitiesDriver != null && capabilitiesDriver.Capabilities.HasCapability(CapabilityType.HasNativeEvents) && (bool)capabilitiesDriver.Capabilities.GetCapability(CapabilityType.HasNativeEvents))
if (driver is IHasCapabilities capabilitiesDriver &&
capabilitiesDriver.Capabilities.HasCapability(CapabilityType.HasNativeEvents) &&
(bool)capabilitiesDriver.Capabilities.GetCapability(CapabilityType.HasNativeEvents))
{
return true;
}
Expand Down Expand Up @@ -154,12 +149,6 @@ protected void CreateFreshDriver()
driver = EnvironmentManager.Instance.CreateFreshDriver();
}

protected bool IsIeDriverTimedOutException(Exception e)
{
// The IE driver may throw a timed out exception
return e.GetType().Name.Contains("TimedOutException");
}

protected bool WaitFor(Func<bool> waitFunction, string timeoutMessage)
{
return WaitFor<bool>(waitFunction, timeoutMessage);
Expand All @@ -173,7 +162,7 @@ protected T WaitFor<T>(Func<T> waitFunction, string timeoutMessage)
protected T WaitFor<T>(Func<T> waitFunction, TimeSpan timeout, string timeoutMessage)
{
DateTime endTime = DateTime.Now.Add(timeout);
T value = default(T);
T value = default;
Exception lastException = null;
while (DateTime.Now < endTime)
{
Expand Down Expand Up @@ -207,7 +196,7 @@ protected T WaitFor<T>(Func<T> waitFunction, TimeSpan timeout, string timeoutMes
}

Assert.Fail("Condition timed out: " + timeoutMessage);
return default(T);
return default;
}
}
}
86 changes: 18 additions & 68 deletions dotnet/test/common/Environment/EnvironmentManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,9 @@ namespace OpenQA.Selenium.Environment
public class EnvironmentManager
{
private static EnvironmentManager instance;
private Type driverType;
private Browser browser;
private readonly Type driverType;
private IWebDriver driver;
private UrlBuilder urlBuilder;
private TestWebServer webServer;
private DriverFactory driverFactory;
private RemoteSeleniumServer remoteServer;
private string remoteCapabilities;
private readonly DriverFactory driverFactory;

private EnvironmentManager()
{
Expand Down Expand Up @@ -91,10 +86,10 @@ private EnvironmentManager()
throw new ArgumentOutOfRangeException($"Unable to find driver type {driverConfig.DriverTypeName}");
}

browser = driverConfig.BrowserValue;
remoteCapabilities = driverConfig.RemoteCapabilities;
Browser = driverConfig.BrowserValue;
RemoteCapabilities = driverConfig.RemoteCapabilities;

urlBuilder = new UrlBuilder(websiteConfig);
UrlBuilder = new UrlBuilder(websiteConfig);

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

webServer = new TestWebServer(projectRoot, webServerConfig);
WebServer = new TestWebServer(projectRoot, webServerConfig);
bool autoStartRemoteServer = false;
if (browser == Browser.Remote)
if (Browser == Browser.Remote)
{
autoStartRemoteServer = driverConfig.AutoStartRemoteServer;
}

remoteServer = new RemoteSeleniumServer(projectRoot, autoStartRemoteServer);
RemoteServer = new RemoteSeleniumServer(projectRoot, autoStartRemoteServer);
}

~EnvironmentManager()
{
if (remoteServer != null)
{
remoteServer.StopAsync().Wait();
}
if (webServer != null)
{
webServer.StopAsync().Wait();
}
RemoteServer?.StopAsync().Wait();
WebServer?.StopAsync().Wait();
CloseCurrentDriver();
}

public event EventHandler<DriverStartingEventArgs> DriverStarting;

public static EnvironmentManager Instance
{
get
{
if (instance == null)
{
instance = new EnvironmentManager();
}

return instance;
}
}
public static EnvironmentManager Instance => instance ??= new EnvironmentManager();

public Browser Browser
{
get { return browser; }
}
public Browser Browser { get; }

public string CurrentDirectory
{
Expand All @@ -242,39 +217,17 @@ public string CurrentDirectory
}
}

public TestWebServer WebServer
{
get { return webServer; }
}
public TestWebServer WebServer { get; }

public RemoteSeleniumServer RemoteServer
{
get { return remoteServer; }
}
public RemoteSeleniumServer RemoteServer { get; }

public string RemoteCapabilities
{
get { return remoteCapabilities; }
}
public string RemoteCapabilities { get; }

public UrlBuilder UrlBuilder
{
get
{
return urlBuilder;
}
}
public UrlBuilder UrlBuilder { get; }

public IWebDriver GetCurrentDriver()
{
if (driver != null)
{
return driver;
}
else
{
return CreateFreshDriver();
}
return driver ?? CreateFreshDriver();
}

public IWebDriver CreateDriverInstance()
Expand All @@ -296,10 +249,7 @@ public IWebDriver CreateFreshDriver()

public void CloseCurrentDriver()
{
if (driver != null)
{
driver.Quit();
}
driver?.Quit();
driver = null;
}

Expand Down
29 changes: 7 additions & 22 deletions dotnet/test/common/StubDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,34 +28,19 @@ public class StubDriver : IWebDriver

public string Url
{
get
{
throw new NotImplementedException();
}
get => throw new NotImplementedException();
set
{
}
}

public string Title
{
get { throw new NotImplementedException(); }
}
public string Title => throw new NotImplementedException();

public string PageSource
{
get { throw new NotImplementedException(); }
}
public string PageSource => throw new NotImplementedException();

public string CurrentWindowHandle
{
get { throw new NotImplementedException(); }
}
public string CurrentWindowHandle => throw new NotImplementedException();

public ReadOnlyCollection<string> WindowHandles
{
get { throw new NotImplementedException(); }
}
public ReadOnlyCollection<string> WindowHandles => throw new NotImplementedException();

public void Close()
{
Expand All @@ -82,7 +67,7 @@ public ITargetLocator SwitchTo()
throw new NotImplementedException();
}

public System.Collections.ObjectModel.ReadOnlyCollection<string> GetWindowHandles()
public ReadOnlyCollection<string> GetWindowHandles()
{
throw new NotImplementedException();
}
Expand All @@ -101,7 +86,7 @@ public IWebElement FindElement(By by)
throw new NotImplementedException();
}

public System.Collections.ObjectModel.ReadOnlyCollection<IWebElement> FindElements(By by)
public ReadOnlyCollection<IWebElement> FindElements(By by)
{
throw new NotImplementedException();
}
Expand Down
11 changes: 5 additions & 6 deletions dotnet/test/common/TestUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class TestUtilities
{
private static IJavaScriptExecutor GetExecutor(IWebDriver driver)
{
return driver as IJavaScriptExecutor;
return (IJavaScriptExecutor)driver;
}

private static string GetUserAgent(IWebDriver driver)
Expand Down Expand Up @@ -106,13 +106,12 @@ public static bool IsOldIE(IWebDriver driver)

public static bool IsNativeEventsEnabled(IWebDriver driver)
{
IHasCapabilities hasCaps = driver as IHasCapabilities;
if (hasCaps != null)
if (driver is IHasCapabilities hasCaps)
{
object cap = hasCaps.Capabilities.GetCapability(OpenQA.Selenium.CapabilityType.HasNativeEvents);
if (cap != null && cap is bool)
object cap = hasCaps.Capabilities.GetCapability(CapabilityType.HasNativeEvents);
if (cap != null && cap is bool b)
{
return (bool)cap;
return b;
}
}

Expand Down
Loading
Loading