Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save brianjbayer/e143032cf570ceaae9f49930a8ca7809 to your computer and use it in GitHub Desktop.
Save brianjbayer/e143032cf570ceaae9f49930a8ca7809 to your computer and use it in GitHub Desktop.
A How To for adding the WebDriverManager package to a Selenium C# local browser automation project

Adding WebDriverManager.Net to your C#-Selenium Project

In the rear view mirror heading to Berlin Heights, Ohio - Wendy Bayer


What This Is

This is the basic gist (and why) of adding the WebDriverManager.Net Package to your C#-Selenium web browser automation project.


Why?

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.


How

It is pretty easy to add the WebDriverManager.Net Package to your C#-Selenium web browser automation project.

Just...

  1. Add the WebDriverManager.Net Package to your C#-Selenium project
  2. Add the WebDriverManager namespace to the file(s) where you want to use it
  3. Create (and Setup) a new DriverManager instance for the desired web browser
  4. Continue to create the desired C# Selenium Driver (e.g. IWebDriver) as normal

1. Adding the WebDriverManager.Net Package

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...

  1. Using the command line dotnet add package command (e.g. dotnet add package WebDriverManager to install the latest)
  2. Editing the .csproj file directly and adding the package reference (e.g. <PackageReference Include="WebDriverManager" Version="2.12.1" />)

2. Adding the WebDriverManager Namespace to your File

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;

3. Creating the DriverManager for the Desired Browser

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.

4. Do What You Have Been Doing (with a Sample)

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);

...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment