Sharing Selenium WebDriver Context in XUnit with DI
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 (shared setup/cleanup code without sharing object instances)
- Class Fixtures (shared object instance across tests in a single class)
- Collection Fixtures (shared object instances across multiple test classes)
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