This is the basic gist (and why) of adding the WebDriverManager.Net Package to your C#-Selenium web browser automation project.
Laziness is a virtue in computer science. Why should you develop and maintain something that someone else has developed and is maintaining?
This value of "laziness" in computer science is also why you automate. Why install and manage your own web browser drivers like ChromeDriver and GeckoDriver when you can use a utility that someone else has developed and is maintaining?
Most Selenium language frameworks have some WebDriver utility that installs (and versions), sets the execution path, and manages the drivers for the various major web browsers. Of course, this does all assume that the desired web browser is installed locally.
It is pretty easy to add the WebDriverManager.Net Package to your C#-Selenium web browser automation project.
Just...
- Add the WebDriverManager.Net Package to your C#-Selenium project
- Add the
WebDriverManager
namespace to the file(s) where you want to use it - Create (and Setup) a new
DriverManager
instance for the desired web browser - Continue to create the desired C# Selenium Driver (e.g.
IWebDriver
) as normal
Here you are adding the WebDriverManager.Net Package to your project which is represented
by the .csproj
file.
You can add/install the package through Visual Studio Code or...
- Using the command line
dotnet add package
command (e.g.
dotnet add package WebDriverManager
to install the latest) - Editing the
.csproj
file directly and adding the package reference (e.g.<PackageReference Include="WebDriverManager" Version="2.12.1" />
)
Here you are adding the WebDriverManager
Namespace to the file
or files where you intend to use it to manage your web browser
drivers.
Add the WebDriverManager
Namespace(s) by adding the following lines
to the top of your file with the other external namespaces ...
using WebDriverManager;
using WebDriverManager.DriverConfigs.Impl;
Here you are creating an instance of the managed web browser using the
DriverManager
and setting it up with the with desired driver config.
Generally, you should be good with the defaults.
Here are some examples...
FOR BROWSER... | CREATE DRIVER MANAGER LIKE THIS... |
---|---|
Chrome | new DriverManager().SetUpDriver(new ChromeConfig()); |
Firefox | new DriverManager().SetUpDriver(new FirefoxConfig()); |
Edge | new DriverManager().SetUpDriver(new EdgeConfig()); |
See https://github.com/rosolko/WebDriverManager.Net for complete details like I did.
WebDriverManager only deals with the web browser drivers that need to be installed locally so the rest of your Selenium local web browser driver code should just work.
Here's some sample code putting this all together using the WebDriverManager with a local Chrome browser...
...
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using System;
using WebDriverManager;
using WebDriverManager.DriverConfigs.Impl;
using Xunit;
using Xunit.Abstractions;
...
IWebDriver driver;
...
// Create the WebDriverManager for Chrome
new DriverManager().SetUpDriver(new ChromeConfig());
// Create the Local Selenium Chrome (with options)
ChromeOptions chromeOptions = new ChromeOptions();
driver = new ChromeDriver(chromeOptions);
...