Skip to content
Merged
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
4 changes: 2 additions & 2 deletions dotnet/private/dotnet_nunit_test_suite.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ _BROWSERS = {
"ActiveDriverConfig=IE",
],
"data": [],
"tags": [],
"tags": ["exclusive-if-local"], # IE cannot run in parallel.
"target_compatible_with": ["@platforms//os:windows"],
},
"safari": {
Expand All @@ -97,7 +97,7 @@ _BROWSERS = {
"ActiveDriverConfig=Safari",
],
"data": [],
"tags": [],
"tags": ["exclusive-if-local"], # Safari cannot run in parallel.
"target_compatible_with": ["@platforms//os:osx"],
},
"remote": {
Expand Down
3 changes: 3 additions & 0 deletions dotnet/test/remote/AssemblyTeardown.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
using NUnit.Framework;
using OpenQA.Selenium.Testing.NUnit;

[assembly: Parallelizable(ParallelScope.Fixtures)]
[assembly: SingleSessionBrowser(Browser.Safari, Browser.IE)]

[SetUpFixture]
#pragma warning disable // Outside a namespace to affect the entire assembly
public class AssemblyTeardown : AssemblyFixtureBase
Expand Down
2 changes: 1 addition & 1 deletion dotnet/test/remote/RemoteWebDriverSpecificTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public void Teardown()
[NeedsFreshDriver(IsCreatedAfterTest = true)]
public void ShouldBeAbleToCreateRemoteWebDriverWithNoSlashAtEndOfUri()
{
EnvironmentManager.Instance.CloseCurrentDriver();
CloseDriver();
DriverOptions options = OperatingSystem.IsWindows() ? new EdgeOptions() : new ChromeOptions();
RemoteWebDriver noSlashDriver = new RemoteWebDriver(RemoteSeleniumServer.ServerUri, options);
noSlashDriver.Url = Urls.JavascriptPage;
Expand Down
3 changes: 3 additions & 0 deletions dotnet/test/support/AssemblyFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
// under the License.
// </copyright>

[assembly: Parallelizable(ParallelScope.Fixtures)]
[assembly: SingleSessionBrowser(Browser.Safari, Browser.IE)]

namespace OpenQA.Selenium.Support.Tests;

[SetUpFixture]
Expand Down
1 change: 0 additions & 1 deletion dotnet/test/testing.nunit/AssemblyFixtureBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ public async Task RunBeforeAnyTestAsync()
[OneTimeTearDown]
public async Task RunAfterAnyTestsAsync()
{
EnvironmentManager.Instance.CloseCurrentDriver();
await EnvironmentManager.Instance.WebServer.StopAsync();
if (StartRemoteServer)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ namespace OpenQA.Selenium.Testing.NUnit.DriverConfigs;
// constructor.
public class DefaultSafariDriver : SafariDriver
{
// Required for dynamic setting with `EnvironmentManager.Instance.CreateDriverInstance(options)`
// Required for dynamic setting with `DriverFactory.CreateDriver(options)`
public DefaultSafariDriver(SafariOptions options)
: base(options)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public DevChannelChromeDriver()
{
}

// Required for dynamic setting with `EnvironmentManager.Instance.CreateDriverInstance(options)`
// Required for dynamic setting with `DriverFactory.CreateDriver(options)`
public DevChannelChromeDriver(ChromeOptions options)
: base(options)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public DevChannelEdgeDriver()
{
}

// Required for dynamic setting with `EnvironmentManager.Instance.CreateDriverInstance(options)`
// Required for dynamic setting with `DriverFactory.CreateDriver(options)`
public DevChannelEdgeDriver(EdgeOptions options)
: base(options)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public EdgeInternetExplorerModeDriver()
{
}

// Required for dynamic setting with `EnvironmentManager.Instance.CreateDriverInstance(options)`
// Required for dynamic setting with `DriverFactory.CreateDriver(options)`
public EdgeInternetExplorerModeDriver(InternetExplorerOptions options)
: base(options)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public NightlyChannelFirefoxDriver()
{
}

// Required for dynamic setting with `EnvironmentManager.Instance.CreateDriverInstance(options)`
// Required for dynamic setting with `DriverFactory.CreateDriver(options)`
public NightlyChannelFirefoxDriver(FirefoxOptions options)
: base(options)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public SafariTechnologyPreviewDriver()
{
}

// Required for dynamic setting with `EnvironmentManager.Instance.CreateDriverInstance(options)`
// Required for dynamic setting with `DriverFactory.CreateDriver(options)`
public SafariTechnologyPreviewDriver(SafariOptions options)
: base(options)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public StableChannelChromeDriver()
{
}

// Required for dynamic setting with `EnvironmentManager.Instance.CreateDriverInstance(options)`
// Required for dynamic setting with `DriverFactory.CreateDriver(options)`
public StableChannelChromeDriver(ChromeOptions options)
: base(options)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public StableChannelEdgeDriver()
{
}

// Required for dynamic setting with `EnvironmentManager.Instance.CreateDriverInstance(options)`
// Required for dynamic setting with `DriverFactory.CreateDriver(options)`
public StableChannelEdgeDriver(EdgeOptions options)
: base(options)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public StableChannelFirefoxDriver()
{
}

// Required for dynamic setting with `EnvironmentManager.Instance.CreateDriverInstance(options)`
// Required for dynamic setting with `DriverFactory.CreateDriver(options)`
public StableChannelFirefoxDriver(FirefoxOptions options)
: base(options)
{
Expand Down
46 changes: 31 additions & 15 deletions dotnet/test/testing.nunit/DriverTestFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,20 @@ public bool IsNativeEventsEnabled
}
}

protected DriverFactory DriverFactory { get; private set; }

[OneTimeSetUp]
public void SetUp()
public void InitFixtureDriver()
{
Driver = EnvironmentManager.Instance.GetCurrentDriver();
DriverFactory = CreateDriverFactory();
Driver = CreateDriverInstance();
}

[OneTimeTearDown]
public void TearDown()
public void DisposeFixtureDriver()
{
EnvironmentManager.Instance.CloseCurrentDriver();
Driver?.Dispose();
Driver = null;
}

[TearDown]
Expand All @@ -63,16 +66,30 @@ public void ResetOnError()
if (TestContext.CurrentContext.Result.Outcome == ResultState.Error)
{
Driver?.Dispose();
Driver = EnvironmentManager.Instance.CreateFreshDriver();
Driver = CreateDriverInstance();
}
}

/// <summary>
/// Exists because a given test might require a fresh driver.
/// </summary>
protected void CreateFreshDriver()
protected virtual DriverFactory CreateDriverFactory()
{
return new DriverFactory(EnvironmentManager.Instance);
}

protected IWebDriver CreateDriverInstance(DriverOptions options = null)
{
return DriverFactory.CreateDriver(options);
}

protected internal void CloseDriver()
{
Driver?.Dispose();
Driver = null;
}

protected internal void CreateFreshDriver()
{
Driver = EnvironmentManager.Instance.CreateFreshDriver();
CloseDriver();
Driver = CreateDriverInstance();
}

protected void WaitFor(Func<bool> waitFunction, string timeoutMessage)
Expand All @@ -87,17 +104,16 @@ protected T WaitFor<T>(Func<T> waitFunction, string timeoutMessage)

protected T WaitFor<T>(Func<T> waitFunction, TimeSpan timeout, string timeoutMessage)
{
var waiter = new WebDriverWait(Driver, timeout)
// Not bound to Driver: callers may wait on a local driver while the fixture driver is closed.
var waiter = new DefaultWait<Func<T>>(waitFunction)
{
Timeout = timeout,
PollingInterval = TimeSpan.FromMilliseconds(100),
Message = $"Condition timed out: {timeoutMessage}",
};

waiter.IgnoreExceptionTypes(typeof(Exception));

return waiter.Until((driver) =>
{
return waitFunction();
});
return waiter.Until(func => func());
}
}
37 changes: 25 additions & 12 deletions dotnet/test/testing.nunit/Environment/DriverFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
// under the License.
// </copyright>

using System.Collections.Concurrent;
using System.IO;
using System.Reflection;
using OpenQA.Selenium.Chrome;
Expand All @@ -30,20 +31,37 @@ namespace OpenQA.Selenium.Testing.NUnit.Environment;

public class DriverFactory
{
private static readonly ConcurrentDictionary<string, Type> driverTypes = new();

private readonly Type driverType;
private readonly string driverPath;
private readonly string browserBinaryLocation;
private readonly bool headless;
private readonly Dictionary<Browser, Type> serviceTypes = new Dictionary<Browser, Type>();
private readonly Dictionary<Browser, Type> optionsTypes = new Dictionary<Browser, Type>();

public DriverFactory(string driverPath, string browserBinaryLocation)
public DriverFactory(EnvironmentManager environment)
{
this.driverPath = driverPath;
this.browserBinaryLocation = browserBinaryLocation;
this.driverType = driverTypes.GetOrAdd(environment.DriverTypeName, ResolveDriverType);
this.driverPath = environment.DriverServiceLocation;
this.browserBinaryLocation = environment.BrowserLocation;
this.headless = environment.Headless;

this.PopulateServiceTypes();
this.PopulateOptionsTypes();
}

private static Type ResolveDriverType(string driverTypeName)
{
// Bazel uses unpredictable assembly names to execute tests, so search all loaded assemblies.
return AppDomain.CurrentDomain.GetAssemblies()
.AsEnumerable()
.Reverse()
.Select(assembly => assembly.GetType(driverTypeName))
.FirstOrDefault(t => t != null)
?? throw new ArgumentOutOfRangeException(nameof(driverTypeName), $"Unable to find driver type {driverTypeName}");
}

private void PopulateOptionsTypes()
{
this.optionsTypes[Browser.Chrome] = typeof(ChromeOptions);
Expand All @@ -64,12 +82,7 @@ private void PopulateServiceTypes()

public event EventHandler<DriverStartingEventArgs> DriverStarting;

public IWebDriver CreateDriver(Type driverType, bool logging = false)
{
return CreateDriverWithOptions(driverType, null, logging);
}

public IWebDriver CreateDriverWithOptions(Type driverType, DriverOptions driverOptions, bool logging = false)
public IWebDriver CreateDriver(DriverOptions driverOptions = null, bool logging = false)
{
Browser browser = Browser.All;
DriverService service = null;
Expand All @@ -84,7 +97,7 @@ public IWebDriver CreateDriverWithOptions(Type driverType, DriverOptions driverO

var chromeOptions = (ChromeOptions)options;
chromeOptions.AddArguments("--no-sandbox", "--disable-dev-shm-usage");
if (EnvironmentManager.Instance.Headless)
if (this.headless)
{
chromeOptions.AddArgument("--headless");
}
Expand All @@ -106,7 +119,7 @@ public IWebDriver CreateDriverWithOptions(Type driverType, DriverOptions driverO

var edgeOptions = (EdgeOptions)options;
edgeOptions.AddArguments("--no-sandbox", "--disable-dev-shm-usage");
if (EnvironmentManager.Instance.Headless)
if (this.headless)
{
edgeOptions.AddArgument("--headless");
}
Expand Down Expand Up @@ -135,7 +148,7 @@ public IWebDriver CreateDriverWithOptions(Type driverType, DriverOptions driverO
{
browser = Browser.Firefox;
options = GetDriverOptions<FirefoxOptions>(driverType, driverOptions);
if (EnvironmentManager.Instance.Headless)
if (this.headless)
{
((FirefoxOptions)options).AddArgument("-headless");
}
Expand Down
Loading
Loading