Sharing Selenium WebDriver Context in XUnit with DI

ExecuteAutomation
ExecuteAutomation
Published in
2 min readAug 11, 2021

In this short and sweet post, I going to show how XUnit shares context across classes using Dependency Injection built into XUnit

There are three types of sharing context, according to XUnit documentation

Constructor and Dispose

xUnit.net creates a new instance of the test class for every test that is run, so any code which is placed into the constructor of the test class will be run for every single test. This makes the constructor a convenient place to put reusable context setup code where you want to share the code without sharing object instances.

Selenium WebDriver Context

Here is an example Selenium WebDriver context which uses the power of XUnit to share the context of WebDriver among classes to invoke ChromeDriver and quit driver once testing completes

Class Fixture

Class fixtures greatly simplifies the way we access the above fixture in the test class. This below code explains all very easily

The below line is code is where we invoke via IClassFixture

public class SecondSeleniumTest : IClassFixture<WebDriverFixture>

Video demo

Complete Series

Here is the complete XUnit series available free to watch and learn in YouTube

--

--

ExecuteAutomation
ExecuteAutomation

ExecuteAutomation Ltd is a Software testing and its related information service company founded in 2020. Info available in YouTube and Udemy as video courses .