-
Notifications
You must be signed in to change notification settings - Fork 0
v2.x Documentation
A Utility library to ease the launching of Selenium WebDriver instances in .NET Core projects.
Version 1.x documentation is available here.
The standard WebDriver nuget packages install the binaries on the path when instantiating IWebDriver instances from .NET Framework projects allowing simple calls of the form
IWebDriver driver = new FirefoxDriver();
When attempting the same from a .NET Core project however, the call fails to locate the WebDriver binary for most browsers. (All except the most recent versions of Safari and Microsoft Edge) The required call becomes
IWebDriver driver = new FirefoxDriver(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
but only for browsers that require a separate WebDriver binary.
This library provides a consistent interface for cross browser testing.
This is not really intended for production use, but is a quick starter for your .NET Core experiments. My hope is that it will ease the creation of WebDriver instances, particularly by providing helpful Exception messages to find the required driver path.
The recommendation is to instantiate an appropriate IWebDriverFactory for generation of IWebDriver instances. The provided example implementations generate sessions with useful default DriverOptions set. To control the DriverOptions used you can pass them in directly instead of the Browser enum or create your own IDriverOptionsFactory implementation. All public methods in the default implementations of both interfaces are overridable for simple changes, or the code here can be used as a base to write your own.
There is little benefit to using the static factories directly and I advise against it.
For usage ideas I recommend you to look through the test sources e.g. for Windows
A .NET Core project referencing the following nuget packages: Selenium.WebDriver.NetCoreWebDriverFactory The appropriate WebDriver binary nuget packages for your OS e.g. Selenium.WebDriver.GeckoDriver.Win64 by J Sakamoto
IWebDriverFactory webDriverFactory = new NetCoreWebDriverFactory(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location));
IWebDriver driver = webDriverFactory.GetLocalWebDriver(Browser.xxx);
A .NET Core project referencing the following nuget packages: Selenium.WebDriver.NetCoreWebDriverFactory
IWebDriverFactory webDriverFactory = new NetCoreWebDriverFactory(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), {GridUrl});
IWebDriver driver = webDriverFactory.GetRemoteWebDriver(Browser.xxx, PlatformType.xxx);
- PlatformType.Windows
- PlatformType.Linux
- PlatformType.Mac
- Browser.Chrome
- Browser.Edge
- Browser.Firefox
- Browser.InternetExplorer
- Browser.Safari
- Hd (1366 x 768 - default)
- Fhd (1920 x 1080)
- Maximise
- Unchanged
Chrome and Firefox both support headless browser instances. These can be requested from a local WebDriver using an optional bool. Requesting this for other Browsers will throw an Exception.
using System;
using AlexanderOnTest.NetCoreWebDriverFactory;
using NUnit.Framework;
using OpenQA.Selenium;
namespace QuickTests
{
public class RemoteTests
{
private IWebDriverFactory DriverFactory { get ; set; }
private IWebDriver Driver { get; set; }
[OneTimeSetUp]
public void Prepare()
{
DriverFactory = new DefaultWebDriverFactory(null, new Uri("http://192.168.0.200:4444/wd/hub"));
}
[SetUp]
public void Setup()
{
Driver = DriverFactory.GetRemoteWebDriver(Browser.Firefox, PlatformType.Linux, WindowSize.Fhd);
}
[Test]
public void Test1()
{
Driver.Url = "https://www.bbc.co.uk";
Assert.IsTrue(true);
}
// Add more tests here
[TearDown]
public void Teardown()
{
Driver.Quit();
}
}
}
using System.IO;
using System.Reflection;
using AlexanderOnTest.NetCoreWebDriverFactory;
using NUnit.Framework;
using OpenQA.Selenium;
namespace QuickTests
{
public class LocalTests
{
private IWebDriverFactory DriverFactory { get ; set; }
private IWebDriver Driver { get; set; }
[OneTimeSetUp]
public void Prepare()
{
DriverFactory = new DefaultWebDriverFactory(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location));
}
[Test]
public void OnScreenTest()
{
Driver = DriverFactory.GetLocalWebDriver(Browser.Chrome, WindowSize.Maximise);
Driver.Url = "https://www.bbc.co.uk";
Assert.IsTrue(true);
}
[Test]
public void HeadlessTest()
{
Driver = DriverFactory.GetLocalWebDriver(Browser.Chrome, WindowSize.Hd, true);
Driver.Url = "https://www.bbc.co.uk";
Assert.IsTrue(true);
}
// Add more tests here
[TearDown]
public void Teardown()
{
Driver.Quit();
}
}
}
To simplify cross platform testing, a WebDriverManager has been implemented.
Recommended usage: (from v2.3.0)
-
Instantiate an appropriate WebDriverFactory as above.
-
Instantiate a WebDriverConfiguration object of your preferred using the constructor:
public WebDriverConfiguration(
Browser browser = Browser.Firefox,
Uri gridUri = null,
bool headless = false,
bool isLocal = true,
PlatformType platformType = PlatformType.Any,
WindowSize windowSize = WindowSize.Hd)
- Use both to instantiate a WebDriverManager instance:
public WebDriverManager(IWebDriverFactory factory, IWebDriverConfiguration configuration)
- The WebDriverManager implements the following generic interface
/// <summary>
/// Return a singleton WebDriver instance;
/// </summary>
/// <returns></returns>
IWebDriver Get();
/// <summary>
/// Quit and clear the current singleton WebDriver instance;
/// </summary>
IWebDriver Quit();
/// <summary>
/// Return a new WebDriver instance independent of the singleton instance;
/// </summary>
/// <returns></returns>
IWebDriver GetAdditionalWebDriver();
The linked Selenium.WebDriver.WebDriverFactoryNunitConfig package can be used to apply settings from runsettings files or local json configuration files. Details of usage are in this post on my blog.