Setup Selenium WebDriver For xUnit Framework In C#
Tags: xunit, selenum, selenium, csharp
Published: 2021-05-07 07:56:49

In the on-going ‘Selenium xUnit testing tutorial’ series, we earlier had a look at setting up the xUnit framework in Visual Studio. It was a getting started guide with a major focus on the installation of the xUnit framework for the project. Before setting up Selenium WebDriver for xUnit, we recommend that you check out the installation guide on [xUnit Testing Tutorial: Environment Setup For Selenium Testing](https://www.lambdatest.com/blog/xunit-testing-tutorial-environment-setup/?utm_source=dev&utm_medium=Blog&utm_campaign=Himanshu-7052021&utm_term=Himanshu), in case you are starting up with the Selenium xUnit framework.

In this Selenium xUnit tutorial, we take a quick look at how to install Selenium WebDriver in Visual Studio for performing automation testing with C#.

## Creating a New C# Project in Visual Studio

Before installing Selenium WebDriver, we create a new project in C# by following the below-mentioned steps:

1. Open Visual Studio and create a new project of the type ‘**xUnit Test Project (.Net Core)**’.

![xUnit Test Project](https://www.lambdatest.com/blog/wp-content/uploads/2021/03/xUnit-Test-Project.png)

2. As the project uses the xUnit.net (or xUnit) framework, the C# file contains the [Fact] attribute. 

![Fact Attribute in xUnit](https://www.lambdatest.com/blog/wp-content/uploads/2021/03/unnamed-1-1.png)

## Steps to Download Selenium WebDriver

Before [running your first script in xUnit](https://www.lambdatest.com/blog/running-first-xunit-script/?utm_source=dev&utm_medium=Blog&utm_campaign=Himanshu-7052021&utm_term=Himanshu), you need to download and set up Selenium WebDriver.

Selenium WebDriver for the xUnit project can be installed using the NuGet Package Manager (PM). NuGet is a free and open-source Package Manager (PM) designed for the Microsoft Platform.

Installation of the Selenium WebDriver can be done either using the **Visual Studio IDE** , and **NuGet Package Manager (PM) Commands**.

### Using Visual Studio IDE

For installing Selenium WebDriver using the Visual Studio IDE, perform the following steps:

1. Go to **Tools -\> NuGet Package Manager** -\> **Manage NuGet Packages for Solution** and search for ‘Selenium.’

![NuGet Package Manager](https://www.lambdatest.com/blog/wp-content/uploads/2021/03/NuGet-Package-Manager.png)

2. Select **Selenium.WebDriver** from the search list and install the package by clicking the **Install** button.

![Selenium Webdriver Installation](https://www.lambdatest.com/blog/wp-content/uploads/2021/03/pasted-image-0-7.png)

![Selenium Webdriver License Acceptance](https://www.lambdatest.com/blog/wp-content/uploads/2021/03/image3-3.png)

3. For automated browser testing using Selenium xUnit, we use Google Chrome as the test browser. We install the Chrome WebDriver so that Chrome can be instantiated using the Selenium APIs. Select **Selenium.WebDriver.ChromeDriver** from the search list and click on the **Install** button.

![Selenium.WebDriver.ChromeDriver installation](https://www.lambdatest.com/blog/wp-content/uploads/2021/03/pasted-image-0-8.png)

4. As seen in the implementation below, we added the Selenium (OpenQA.Selenium) and Chrome WebDriver (OpenQA.Selenium.Chrome) references in the code.

![OpenQA.Selenium](https://www.lambdatest.com/blog/wp-content/uploads/2021/03/pasted-image-0-9.png)

 
```
using System;
using Xunit;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
 
namespace XUnitTestProject1
{
    public class UnitTest1
    {
        [Fact]
        public void Test1()
        {
        }
    }
}
```
 
### Using Package Manager (PM) Commands

If you are more comfortable with the terminal, you can install Selenium WebDriver by invoking the Package Manager (PM) commands on the terminal.

For executing commands from the PM console, go to **Tools -\> NuGet Package Manager -\> Package Manager Console**.

![NuGet Package Manager Console](https://www.lambdatest.com/blog/wp-content/uploads/2021/03/pasted-image-0-10.png)

The Package Manager (PM) command Install-Package is used to perform the installation. Package Name is passed as an argument to the command. We also install the Selenium.Chrome.WebDriver from the terminal.

 
```
Install-Package Selenium.WebDriver
Install-Package Selenium.Chrome.WebDriver
```

Shown below is the snapshot of the Package Manager Console:

![Install Package Selenium Webdriver](https://www.lambdatest.com/blog/wp-content/uploads/2021/03/image2-4.png)

![Install Package Selenium Chromedriver](https://www.lambdatest.com/blog/wp-content/uploads/2021/03/pasted-image-0-11.png)

Get-Package command is used to verify whether the command installation is successful or not.

```
PM> Get-Package
 
Id                                  Versions
--                                  --------                                          
Selenium.WebDriver                  {3.141.0}
Selenium.Chrome.WebDriver           {89.0.4389.2300}
...... ......
...... ......
```

## Conclusion

![Thank you](https://www.lambdatest.com/blog/wp-content/uploads/2021/03/image11.gif)

With the installation of Selenium WebDriver and the xUnit framework, you are all set to execute the first automation testing with C#, Selenium, and xUnit.net. The steps mentioned in this [Selenium C# tutorial](https://www.lambdatest.com/blog/selenium-c-sharp-tutorial-introduction/?utm_source=dev&utm_medium=Blog&utm_campaign=Himanshu-7052021&utm_term=Himanshu) for the installation of Selenium WebDriver in xUnit are easy to understand. Terminologies like Selenium WebDriver, Package Manager, etc., are uniform across all the C# test frameworks.

A thorough understanding of these essential things forms the base on which you can enhance automation testing skills with C# and xUnit.