Part 1 — XUnit with Selenium

ExecuteAutomation
ExecuteAutomation
Published in
2 min readJul 28, 2021

xUnit.net is a free, open source, community-focused unit testing tool for the .NET Framework. Written by the original inventor of NUnit v2, xUnit.net is the latest technology for unit testing C#, F#, VB.NET and other .NET languages.

Creating XUnit Project Template

As XUnit is .NET Core compatible, it runs on MacOS, Windows and Linux OS

The project contains following references within its project

<PackageReference Include=”Microsoft.NET.Test.Sdk” Version=”16.9.4" /> 
<PackageReference Include=”xunit” Version=”2.4.1" /> <PackageReference Include=”xunit.runner.visualstudio” Version=”2.4.3">

The default test class generated with the project template will contain the following test method

As you can see the above method is decorated by [Fact] attribute, which tells its a XUnit test, since NUnit tests are decorated by [Test] and MSTest test method are decorated by [TestMethod] attributes.

Adding Logger for output in console

Lets add Logging output to console via ITestOutputHelper

Here is how we need to initialize the logger via dependency injection of XUnit

public SeleniumWithOutContext(ITestOutputHelper testOutputHelper)
{
this.testOutputHelper = testOutputHelper;
}

Adding Selenium References

Next up, we will add Selenium reference in the project via Nuget. Here are the following packages we are going to add. This includes WebDriverManager as well, as it resolves all the browser driver problems for us.

<PackageReference Include=”Selenium.WebDriver” Version=”3.141.0" /> <PackageReference Include=”WebDriverManager” Version=”2.11.2" />

Once added references, we can start writing the Selenium code with XUnit as shown below

 [Fact]
public void Test1()
{
Console.WriteLine(“First test”);
testOutputHelper.WriteLine(“First test”);
chromeDriver.Navigate().GoToUrl(“http://eaapp.somee.com”);
}

The `ChromeDriver ` is initialized in the constructor as shown below

private readonly ITestOutputHelper testOutputHelper;
private readonly ChromeDriver chromeDriver;
public SeleniumWithOutContext(ITestOutputHelper testOutputHelper)
{
this.testOutputHelper = testOutputHelper;
//WebDriverManager
var driver = new DriverManager().SetUpDriver(new ChromeConfig());
chromeDriver = new ChromeDriver();
}

Here is the complete video of the above discussion

Here is the complete code is available in the github repo here

Thanks,

--

--

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 .