NUnit Test Automation Using Selenium C# (with Example)
Tags: selenium, csharp, testing
Published: 2019-11-21 12:15:08

---
title: NUnit Test Automation Using Selenium C# (with Example)
published: true
description: In this NUnit testing tutorial, we execute Selenium automation scripts using Selenium C# framework through local and remote webdriver for parallel testing.
tags: selenium, csharp, testing
canonical_url: https://www.lambdatest.com/blog/nunit-testing-tutorial-for-selenium-csharp/
cover_image: https://www.lambdatest.com/blog/wp-content/uploads/2019/11/NUnit.jpg
---

C# is objected-oriented and provides all the popular features supported by OOPS namely encapsulation, polymorphism, and inheritance. Using C#, you can develop web-based, develop windows, and console applications. Development is not all you can do with C#, you can also leverage C# for test automation with top frameworks like NUnit, along with language bindings for Selenium.

Selenium is an open-source test automation framework for automated cross browser testing. It supports popular web browsers – Firefox, Chrome, Microsoft Edge, Internet Explorer, Safari, etc. To interact with the underlying web browser, a collection of language specific bindings to drive the browser called Selenium WebDriver is used. If this is the first time you are hearing about Selenium, then I recommend you follow our earlier blog on [Selenium WebDriver tutorial](https://www.lambdatest.com/blog/selenium-webdriver-tutorial-for-cross-browser-testing/?utm_source=dev&utm_medium=Blog&utm_campaign=Himanshu-21112019&utm_term=Himanshu) where we talked about Selenium architecture in great detail for automated browser testing.

Today, I am going to help you run your first test automation script for Selenium C# framework i.e. the NUnit testing framework. By the end of this NUnit testing tutorial, you will be well versed with:

- Overview and Advantages of using NUnit as Selenium C# framework.
- Installation and setup for NUnit testing framework.
- Setting up Selenium WebDriver with Visual Studio in C#.
- Running your first automation script for NUnit testing with Selenium using local WebDriver.
- Leveraging parallel testing with Selenium C# framework using a Remote Selenium WebDriver. 

## Testing With NUnit – A Selenium C# Framework 

[NUnit](https://nunit.org/) is an open-source unit testing framework in C# that is ported from [JUnit automated testing framework](https://www.lambdatest.com/blog/automated-testing-with-junit-and-selenium-for-browser-compatibility/?utm_source=dev&utm_medium=Blog&utm_campaign=Himanshu-21112019&utm_term=Himanshu). It is a member of the .Net Foundation and is used for development and execution of unit tests with .Net programming language. As of October, 2019; the latest version of NUnit is NUnit3.

## Advantages Of NUnit Testing For Selenium C# Automation

NUnit is preferred over other test frameworks as it is compatible with Selenium testsuite. NUnit testing for Selenium automation is widely used due to the following advantages over other test frameworks:

- Annotations used in NUnit help in speeding up test development & execution as tests can be executed with numerous input values.
- TDD is primarily useful as unit tests are instrumental in finding issues/bugs during the early stages of product development. NUnit test framework can be used with Selenium if you plan to use TDD (Test Driven Development) for the test activity.
- Provides you the ability to run your test cases in parallel.
- Using NUnit, you can execute test cases from console runner by either a third-party automation testing tool or by the NUnit Test Adapter inside the Visual Studio.

Now, in this Nunit testing tutorial, we will look into installation and set up required to run your first test automation script in Selenium C# framework.

{% youtube ZU85Mjwgv54 %}

[![Web UI Testing with Python and LambdaTest](https://www.lambdatest.com/webinar/blog-cta/webinar-image.png)](https://www.lambdatest.com/webinar/cross-browser-testing-with-Selenified-and-LambdaTest?utm_source=dev&utm_medium=Blog&utm_campaign=Himanshu-21112019&utm_term=Himanshu)


## NUnit and Selenium – Installation and Setting up with Visual Studio

Since this is the first article focusing on C# and Selenium, we have a look at the detailed steps for installation of Visual Studio IDE. To execute NUnit testing for Selenium automation scripts, we make use of the Visual Studio IDE for development and execution. Using Visual Studio, developers can come up with test cases/test suites for different platforms like Windows, Android, iOS, Web, Cloud, etc.

***This tutorial dive deep into*** [***web testing***](https://www.lambdatest.com/learning-hub/web-testing?utm_source=devto&utm_medium=organic&utm_campaign=mar10_bh&utm_term=bh&utm_content=learning_hub) ***to help you understand its life cycle, elements, angles, the role of automation, and more.***

### Set Up Visual Studio for Development

**Step 1:** Download the latest version of Visual Studio i.e. Visual Studio 2019 from here. Feature comparison of different Visual Studio 2019 editions can be used for reference. Depending on the requirement, you can download the Community/Professional/Enterprise edition of Visual Studio 2019.

![Set Up Visual Studio](https://cdn.lambdatest.com/blog/wp-content/uploads/2019/11/Set-Up-Visual-Studio.png)

**Step 2:** Since the intent with this NUnit testing tutorial is to use Visual Studio for Selenium C# automation, we select the required packages for development on Windows operating system.

![visual studio installer](https://cdn.lambdatest.com/blog/wp-content/uploads/2019/11/visual-studio-installer.png)

![visual studio installer guide](https://cdn.lambdatest.com/blog/wp-content/uploads/2019/11/visual-studio-installer-guide.png)

**Step 3:** Once the installation is complete, you will be prompted to restart the machine before Visual Studio 2019 can be used.

**Step 4:** You may opt to sign-in to publish code to private Git repository, sync settings, unlock the IDE, etc.

Now, that the Visual Studio is setup for development. Next in this NUnit testing tutorial, we need to install the Selenium C# framework i.e. NUnit, along with the NUnit Test Adapter for Selenium automation.

{% youtube 2fN6GcZ3-fs %}

***Upload your app for testing within seconds using the LambdaTest cloud and perform*** [***mobile app test***](https://www.lambdatest.com/mobile-app-testing?utm_source=devto&utm_medium=organic&utm_campaign=mar10_bh&utm_term=bh&utm_content=webpage) ***right away. Find bugs early on, improve performance, quality, user experience and make the most of mobile application testing on LambdaTest.***

### Installation of NUnit Framework & NUnit Test Adapter

**Step 1:** To start off with NUnit testing, you need to first create a new project in Visual Studio. Go to **Visual Studio → New → Project**.

**Step 2:** Add **Console.WriteLine(“Selenium C#”)** to the newly created .cs file.

**Step 3:** Navigate to **Tools→ NuGet Package Manager→ Manager NuGet Packages for Solution** and search for “ **NUnit & NUnit Test Adapter** ”.

![NUnit Test Adapter](https://cdn.lambdatest.com/blog/wp-content/uploads/2019/11/image9.png)

 

![Nunit test framework](https://cdn.lambdatest.com/blog/wp-content/uploads/2019/11/Nunit-test-framework.png)

**Step 4:** Click on Install and Press OK to confirm the installation. With this installation, NUnit can be used as a Selenium C# framework for[Web UI automation testing](https://www.lambdatest.com/webinar/web-ui-testing-with-python-and-lambdatest?utm_source=dev&utm_medium=Blog&utm_campaign=Himanshu-21112019&utm_term=Himanshu).

### Setup Selenium WebDriver with Visual Studio in C#

With NUnit testing framework configured, you can now create NUnit Test Project in Visual Studio for C#. Below are the steps to setup your Selenium and C# in Visual Studio.

**Step 1:** Create a new NUnit test Project in C# by navigating to **New → Project → NUnit Test Project (.Net Core)**.

![WebDriver with Visual Studio](https://cdn.lambdatest.com/blog/wp-content/uploads/2019/11/image6.png)

**Step 2:** The newly created project will have the basic test ready with NUnit annotations being used in the source code.

![WebDriver with Visual Studio](https://cdn.lambdatest.com/blog/wp-content/uploads/2019/11/WebDriver-with-Visual-Studio.png)

**Step 3:** Navigate to **Tools → NuGet Package Manager → Manager NuGet Packages for Solution** and search for ‘ **Selenium** ’.

![WebDriver with Visual Studio](https://cdn.lambdatest.com/blog/wp-content/uploads/2019/11/image9-1.png)

**Step 4:** Click on **Install** to setup the **Selenium WebDriver** interface.

![Selenium WebDriver](https://cdn.lambdatest.com/blog/wp-content/uploads/2019/11/Selenium-WebDriver.png)

***Get started with this complete*** [***Selenium automation testing***](https://www.lambdatest.com/selenium?utm_source=devto&utm_medium=organic&utm_campaign=mar10_bh&utm_term=bh&utm_content=webpage) ***tutorial. Learn what Selenium is, its architecture, advantages and more for automated cross browser testing.***

The latest stable version as of November 2019 is **3.141.0**.

  
 ![Selenium WebDriver](https://cdn.lambdatest.com/blog/wp-content/uploads/2019/11/Seleniu_-WebDriver.png)

**Step 5:** After the successful installation of Selenium WebDriver, you can start [automation testing](https://www.lambdatest.com/blog/17-key-benefits-of-automation-testing-for-a-successful-release/?utm_source=dev&utm_medium=Blog&utm_campaign=Himanshu-21112019&utm_term=Himanshu) using NUnit, a Selenium C# framework for test automation.

![Selenium C# framework](https://cdn.lambdatest.com/blog/wp-content/uploads/2019/11/image3.png)

Now, that we are done configuring the Visual Studio for Selenium C# framework in this NUnit testing tutorial. It is time to deep dive into action and runs our first automation scripts for automated browser testing.

## Automated Browser Testing With NUnit For Selenium C# Scripts

Automated browser testing is one of the key components of Web UI testing. It is pivotal to ensure that your website or web application offers a consistent UI or UX across different browsers. Verifying whether the functionalities would work as expected on different browsers and browser versions can be tricky and time-consuming. This is where [automation testing with Selenium](https://www.lambdatest.com/blog/automation-testing-with-selenium-cucumber-testng/?utm_source=dev&utm_medium=Blog&utm_campaign=Himanshu-21112019&utm_term=Himanshu) comes into play.

Now, in this NUnit testing tutorial, we will run our first automation script for the Selenium C# framework with examples. But before we do that, you need to have a look at the basic types of commands used for Selenium automation.

### Types Of Commands Used For Selenium Automation

The interaction of the Selenium C# Framework with the web browser happens via the Selenium WebDriver. Before you can make use of Selenium WebDriver commands in C#, the development environment should be setup i.e. Selenium WebDriver for the corresponding web browser should be installed on the machine.

Make sure to download the Selenium WebDriver file for the browsers with which you wish to [perform cross browser testing](https://www.lambdatest.com/blog/performing-cross-browser-testing-with-lambdatest/?utm_source=dev&utm_medium=Blog&utm_campaign=Himanshu-21112019&utm_term=Himanshu) from the links mentioned below:

![Capture32.PNG](https://ucarecdn.com/7c629b6b-faf2-402e-9fcd-eaf157977791/)

Like other programming languages such as Python, C# also uses a particular interface to interact with the browser. The interface used is called the **IWebDriver**. With that said, we now look into actions involved in Automated browser testing.

- Actions on the web browser (e.g. open, close, maximize, etc.)
- Actions on the web elements in the current window (e.g. checking a button, entering text in a textbox, click a button, etc.
- Actions on the drop-down elements in the current window (e.g. select particular value from drop-down window, deselect the selected value from the drop-down window, etc.).

Based on these distinct actions/interactions, Selenium WebDriver Commands in C# are divided in three broad categories:

1. Web Browser commands
2. Web Element commands
3. Drop-down commands

We will be working with such commands in this NUnit testing tutorial as we work our way with test automation using Selenium C# framework.

Now, we run our first NUnit testing automation script for web UI. Validation of the code and its corresponding functionalities can be done either locally or remotely.

### NUnit Testing With Local WebDriver

You have the key functionalities of the web product ready and want to test those features on a select set of browsers installed on your machine. Once you have tested the website over different browsers to ensure compatibility, you should make sure that any upcoming code change will not disturb the UI and UX?

Now, in this NUnit testing tutorial, we will execute Selenium C# automation from the perspective of automated browser testing using simple test cases.

**Test Case 1** – Below is the break-up of the things that we plan to achieve in the test:

1. Open DuckDuckGo in Chrome browser.
2. Locate search box.
3. Enter search query ‘LambdaTest’.
4. Execute the search operation.
5. Free up the resources.

As we are making use of local Selenium WebDriver, you should first download the Selenium WebDriver for Chrome and install/copy it in the location where the Chrome web browser (Chrome.exe) is located.

![chrome exe file](https://cdn.lambdatest.com/blog/wp-content/uploads/2019/11/chrome-exe-file.png)

If the WebDriver executable is present in the location where web browser executable is present, you need not specify the path when the WebDriver instance is created in the source code.

```
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
 
IWebDriver webDriver;
 
webDriver = new ChromeDriver();
```

 
### Running The NUnit Testing Script For Test Case 1 On Local WebDriver

```
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.IE;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace NUnit_Demo
{
    public class Browser_ops
    {
        IWebDriver webDriver;
        public void Init_Browser()
        {
            webDriver = new ChromeDriver();
            webDriver.Manage().Window.Maximize();
        }
        public string Title
        {
            get { return webDriver.Title; }
        }
        public void Goto(string url)
        {
            webDriver.Url = url;
        }
        public void Close()
        {
            webDriver.Quit();
        }
        public IWebDriver getDriver
        {
            get { return webDriver; }
        }
    }
    class NUnit_Demo_1
    {
        Browser_ops brow = new Browser_ops();
        String test_url = "https://www.duckduckgo.com";
        IWebDriver driver;
 
        [SetUp]
        public void start_Browser()
        {
            brow.Init_Browser();
        }
 
        [Test]
        public void test_Browserops()
        {
            brow.Goto(test_url);
            System.Threading.Thread.Sleep(4000);
 
            driver = brow.getDriver;
 
            IWebElement element = driver.FindElement(By.XPath("//*[@id='search_form_input_homepage']"));
 
            element.SendKeys("LambdaTest");
 
            /* Submit the Search */
            element.Submit();
 
            /* Perform wait to check the output */
            System.Threading.Thread.Sleep(2000);
        }
 
        [TearDown]
        public void close_Browser()
        {
            brow.Close();
        }
    }
}
```


Let’s have a code walkthrough to understand how we executed our Test Case 1.

A class named Browser\_ops of public scope is created with methods for creation of Chrome WebDriver instance, getting the Title of the page (under test), navigating to a particular URL, etc.

```
public class Browser_ops
    {
        IWebDriver webDriver;
        public void Init_Browser()
        {
            webDriver = new ChromeDriver();
            webDriver.Manage().Window.Maximize();
        }
 
	  ………………………………………………
	  ………………………………………………
	  public void Goto(string url)
        {
            webDriver.Url = url;
        } 
    }
```
 

***Automate*** [***Cypress testing***](https://www.lambdatest.com/cypress-testing?utm_source=devto&utm_medium=organic&utm_campaign=mar10_bh&utm_term=bh&utm_content=webpage) ***and perform browser automation testing with LambdaTest. Our cloud infrastructure has 3000+ desktop & mobile environments.***

A class named NUnit\_Demo\_1 uses the methods that were defined in the Browser\_ops() class. Chrome WebDriver instance for automated browser testing is created under the [SetUp] annotation. The implementation of locating the SearchBox on the DuckDuckGo homepage, entering LambdaTest i.e. search term and submitting the search is performed under [Test] annotation. XPath of the search box is found using the Inspect Tool in Chrome browser.

![Nunit test cases](https://cdn.lambdatest.com/blog/wp-content/uploads/2019/11/image15.png)

The resources used by the Chrome WebDriver are freed as a part of [TearDown].

```
class NUnit_Demo_1
{
    Browser_ops brow = new Browser_ops();
    String test_url = "https://www.duckduckgo.com";
    IWebDriver driver;
 
    ...............................................
    ...............................................
 
    [Test]
    public void test_Browserops()
    {
        brow.Goto(test_url);
        System.Threading.Thread.Sleep(4000);
 
        driver = brow.getDriver;
 
        IWebElement element = driver.FindElement(By.XPath("//*[@id='search_form_input_homepage']"));
 
        element.SendKeys("LambdaTest");
 
        /* Submit the Search */
        element.Submit();
 
        /* Perform wait to check the output */
        System.Threading.Thread.Sleep(2000);
    }
 
    ...............................................
    ...............................................
 
}
```


To build and execute the test/project, you can perform a Right click on the class name that contains the test implementation i.e. NUnit\_Demo\_1

![Test Explorer.](https://cdn.lambdatest.com/blog/wp-content/uploads/2019/11/image16.png)

Test status and details about test execution time are available in the Test Explorer. If there are build errors, the errors can be seen from the **Error List Window**.

![Error List Window](https://cdn.lambdatest.com/blog/wp-content/uploads/2019/11/image7.png)

**Test Case 2 –** Your website is working fine with one combination i.e. DuckDuckGo on Chrome. Great! What if you want to test using Google on Firefox? All you need to do is execute both the tests serially.

Below is the breakup of the activities that needs to be carried out as a part of the test.

![chart](https://cdn.lambdatest.com/blog/wp-content/uploads/2019/11/chart.png)

### Running The NUnit Testing Script For Test Case 2 On Local WebDriver

As tests need to be carried out in two different browsers, one instance of Chrome WebDriver and one instance of Firefox WebDriver is created. For simplifying this implementation that demonstrates NUnit testing for Selenium automation, we create two separate classes:

**NUnitDemo\_1 –** Implementation for performing automated browser testing on Chrome browser with DuckDuckGo as test URL.

**NUnitDemo\_2 –** Implementation for performing automated browser testing on Firefox browser with Google as test URL.

```
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Firefox;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenQA.Selenium.IE;
using System.Configuration;
 
namespace NUnit_Demo_1
{
    class NUnitDemo_1
    {
        IWebDriver driver;
 
        [SetUp]
        public void startBrowser()
        {
            driver = new ChromeDriver();
            driver.Manage().Window.Maximize();
        }
 
        [Test]
        public void test_duckduckgo()
        {
            driver.Url = "https://www.duckduckgo.com";
            System.Threading.Thread.Sleep(4000);
 
            IWebElement element = driver.FindElement(By.XPath("//*[@id='search_form_input_homepage']"));
 
            element.SendKeys("LambdaTest");
 
            /* Submit the Search */
            element.Submit();
 
            /* Perform wait to check the output */
            System.Threading.Thread.Sleep(2000);
        }
 
        [TearDown]
        public void closeBrowser()
        {
            driver.Quit();
        }
 
    }
}
 
namespace NUnit_Demo_2
{
    class NUnitDemo_2
    {
        IWebDriver driver;
 
        [SetUp]
        public void startBrowser()
        {
            driver = new FirefoxDriver();
            driver.Manage().Window.Maximize();
        }
 
        [Test]
        public void test_google()
        {
            driver.Url = "https://www.google.com";
            System.Threading.Thread.Sleep(4000);
 
            IWebElement element = driver.FindElement(By.XPath("//*[@id='tsf']/div[2]/div[1]/div[1]/div/div[2]/input"));
 
            element.SendKeys("LambdaTest");
 
            /* Submit the Search */
            element.Submit();
 
            /* Perform wait to check the output */
            System.Threading.Thread.Sleep(2000);
        }
 
        [TearDown]
        public void closeBrowser()
        {
            driver.Quit();
        }
 
    }
}
```
 

Both the test cases have their respective methods of initialization, testing on browser, and de-initialization/cleanup under separate **[SetUp], [Test], and [TearDown]** attributes as the tests have to be performed in a serial manner.

However, what happens when you have to run a test case of say 50 or more browser combinations? Relying on serial combination is going to take up a lot of your time. Local testing using serial execution will not be effective as it would require significant cost to setup the test infrastructure and execution time will be significantly higher!

If only we could run the same test case on 2, 5, or maybe 10 different browsers simultaneously then our overall test execution time could have trimmed down significantly, right?

Well, this is where the Selenium Grid comes in to rescue. Using a Selenium Grid, you can perform parallel testing with Selenium C# framework to validate your website’s browser compatibility.

[**Guide: Setup Selenium Grid For Parallel Execution In Different Browsers**](https://www.lambdatest.com/blog/setup-selenium-grid-for-parallel-execution/?utm_source=dev&utm_medium=Blog&utm_campaign=Himanshu-21112019&utm_term=Himanshu)

In the next section of this Nunit testing tutorial, we will leverage Parallel testing using a remote WebDriver on cloud.

***This*** [***Cypress test automation***](https://www.lambdatest.com/blog/cypress-test-automation-framework/?utm_source=devto&utm_medium=organic&utm_campaign=mar10_bh&utm_term=bh&utm_content=blog) ***tutorial will help you learn the benefits of Cypress automation, and how to install Cypress and execute Cypress automation testing over scores of browsers and operating systems online.***

## Parallel Testing Using A Remote WebDriver On Cloud

We now know that serial execution using Selenium Webdriver is not going to be enough to help you pass your release cycles on time with flying colors. However, a Selenium Grid is going to be a different story.

Using a Selenium Grid you can run a test case over different test configurations of browsers + OS, simultaneously. However, there is a downside to that as well. You can only access the browsers you have on your machine as you configure the hub and nodes of the Selenium Grid. Now, you can’t go ahead and download hundreds of browsers and buying a device lab can be a not so pocket-friendly option. So what can you do?

A scalable model is using a cloud-based [browser compatibility testing tool](https://www.lambdatest.com/feature?utm_source=dev&utm_medium=Blog&utm_campaign=Himanshu-21112019&utm_term=Himanshu) on which NUnit testing for Selenium C# automation can be performed on a remote, scalable, [online Selenium Grid](https://www.lambdatest.com/selenium-automation?utm_source=dev&utm_medium=Blog&utm_campaign=Himanshu-21112019&utm_term=Himanshu) with zero downtime. LambdaTest is one such cloud-based [automated browser testing](https://www.lambdatest.com/selenium-automation?utm_source=dev&utm_medium=Blog&utm_campaign=Himanshu-21112019&utm_term=Himanshu) platform on which offers 2000+ real browsers hosted on the cloud servers. That way, you don’t have to worry about downloading or maintaining any browser or device lab, as we do it for you to help you provide with a wider test coverage.

### How Do I Run My Selenium C# Automation Script On LambdaTest?

Porting an existing NUnit testing suite that makes use of Local Selenium WebDriver to LambdaTest infrastructure requires minimal effort. All you need are LambdaTest authentication credentials i.e. username and access key, and LambdaTest Hub URL. You can find all of these details from your [LambdaTest Automation Dashboard](https://automation.lambdatest.com/?utm_source=dev&utm_medium=Blog&utm_campaign=Himanshu-21112019&utm_term=Himanshu).

To declare your desired capabilities class, you can use LambdaTest Desired Capabilities Generator. It auto-generates the code for Capabilities Class based on your testing requirements, in all of the programming languages.

![capability generater](https://cdn.lambdatest.com/blog/wp-content/uploads/2019/11/pasted-image-0-1.png)

Apart from scalability, the other advantages of using NUnit testing for Selenium automation on a remote Selenium Grid is that you can perform Parallel Testing on the test cases. Parallel testing allows you to execute numerous automation test cases altogether simultaneously. Parallel testing with Selenium testing tool can reduce the overall execution time of the tests.

The number of parallel tests depends on the number of concurrent sessions under ![Capture12.PNG](https://ucarecdn.com/a37e5f7c-8e92-4860-b6a3-5bd2a9130f30/)the opted plan. More the number of concurrent plans, the better the throughput which eventually leads to shortened release cycles.

![lambdatest dashboard](https://cdn.lambdatest.com/blog/wp-content/uploads/2019/11/lambdatest-dashboard.png)

Automated browser testing with NUnit and Selenium when used in parallel testing mode can greatly reduce the test execution time.

**Test Case –** Leveraging the advantages of parallel testing with [Selenium testing tool](https://www.lambdatest.com/selenium-automation?utm_source=dev&utm_medium=Blog&utm_campaign=Himanshu-21112019&utm_term=Himanshu), we perform search test using DuckDuckGo on 4 different combinations of (browsers + browser versions + platform) using 2 parallel test sessions. This means that I would be running the test cases on 2 different combinations at the same time.

![Capture12.PNG](https://ucarecdn.com/dbe3d742-1acf-4b66-b562-f01d44b4701d/)

### Implementation

```
using System;
using OpenQA.Selenium;
using OpenQA.Selenium.Remote;
using NUnit.Framework;
using System.Threading;
using System.Collections.Generic;
 
namespace ParallelLTSelenium
{
    [TestFixture("chrome", "72.0", "Windows 10")]
    [TestFixture("internet explorer", "11.0", "Windows 10")]
    [TestFixture("Safari", "11.0", "macOS High Sierra")]
    [TestFixture("MicrosoftEdge", "18.0", "Windows 10")]
    [Parallelizable(ParallelScope.All)]
    public class ParallelLTTests
    {
        ThreadLocal<IWebDriver> driver = new ThreadLocal<IWebDriver>();
        private String browser;
        private String version;
        private String os;
 
        public ParallelLTTests(String browser, String version, String os)
        {
            this.browser = browser;
            this.version = version;
            this.os = os;
        }
 
        [SetUp]
        public void Init()
        {
            String username = "user-name";
            String accesskey = "access-key";
            String gridURL = "@hub.lambdatest.com/wd/hub";
 
            DesiredCapabilities capabilities = new DesiredCapabilities();
 
            capabilities.SetCapability("user", username);
            capabilities.SetCapability("accessKey", accesskey);
            capabilities.SetCapability("browserName", browser);
            capabilities.SetCapability("version", version);
            capabilities.SetCapability("platform", os);
 
            driver.Value = new RemoteWebDriver(new Uri("https://" + username + ":" + accesskey + gridURL), capabilities, TimeSpan.FromSeconds(600));
 
            System.Threading.Thread.Sleep(2000);
        }
 
        [Test]
        public void DuckDuckGo_Test()
        {
            {
                driver.Value.Url = "https://www.duckduckgo.com";
 
                IWebElement element = driver.Value.FindElement(By.XPath("//*[@id='search_form_input_homepage']"));
 
                element.SendKeys("LambdaTest");
 
                /* Submit the Search */
                element.Submit();
 
                /* Perform wait to check the output */
                System.Threading.Thread.Sleep(2000);
            }
        }
 
        [TearDown]
        public void Cleanup()
        {
            bool passed = TestContext.CurrentContext.Result.Outcome.Status == NUnit.Framework.Interfaces.TestStatus.Passed;
            try
            {
                // Logs the result to Lambdatest
                ((IJavaScriptExecutor)driver.Value).ExecuteScript("lambda-status=" + (passed ? "passed" : "failed"));
            }
            finally
            {
                // Terminates the remote webdriver session
                driver.Value.Quit();
            }
        }
    }
}
```


**Code Walkthrough**

Since Remote WebDriver is used, we first import the types defined in the OpenQA.Selenium.Remote namespace.

```
using OpenQA.Selenium.Remote;
```
 

The combination of user-name & access-token is used for logging on to the platform.

 ```
String username = "user-name";
String accesskey = "access-key";
```


The browser and platform capabilities for C# are generated using the LambdaTest capabilities generator.

```
DesiredCapabilities capabilities = new DesiredCapabilities();
 
capabilities.SetCapability("user", username);
capabilities.SetCapability("accessKey", accesskey);
capabilities.SetCapability("browserName", browser);
capabilities.SetCapability("version", version);
capabilities.SetCapability("platform", os);
```
 

The combination of user-name and access key are passed to the remote URL on which the test will be performed.

```
driver = new RemoteWebDriver(new Uri("https://user-name:access-key@hub.lambdatest.com/wd/hub"), capabilities, TimeSpan.FromSeconds(600));
```
 

Multiple parameters that represent the browser, browser version, & platform are passed to the TestFixture class. The TestFixture class is a part of the NUnit.Framework.Internal namespace.

The Parallelizable attribute is addressed with the ParallelScope as All. This implies that the test case will run in parallel with the descendant test cases, at the same level. Changing the ParallelScope at Children may impact the ability to execute test cases in parallel.

```
[TestFixture("chrome", "72.0", "Windows 10")]
[TestFixture("internet explorer", "11.0", "Windows 10")]
[TestFixture("Safari", "11.0", "macOS High Sierra")]
[TestFixture("MicrosoftEdge", "18.0", "Windows 10")]
[Parallelizable(ParallelScope.All)]
```
 

The **ThreadLocal** class that belongs to the **System.Threading** namespace provides **thread-local** storage of data. The instance of **IWebDriver** is the data that is stored per-thread.

```
public class ParallelLTTests
{
 ThreadLocal<IWebDriver> driver = new ThreadLocal<IWebDriver>();
 ...................
 ...................
}
```
 

As tests will be executed in parallel threads, value argument in **iWebDriver-Instance** i.e. **driver.Value** is used to get the value of the instance for the current thread.

```
............
............
public void Init()
{
    ............
    ............
    ............
    driver.Value = new RemoteWebDriver(new Uri("https://" + username + ":" + accesskey + gridURL), capabilities, TimeSpan.FromSeconds(600));
 
    System.Threading.Thread.Sleep(2000);
}
```
 

Rest of the implementation remains unchanged. Hence, you can achieve parallelism for the tests with minimal code changes. As per the current plan, we can have two concurrent sessions i.e. 4 tests should be executed in 2 test runs.

![](https://cdn.lambdatest.com/blog/wp-content/uploads/2019/11/image18.png)

You may have noticed that 2 of our test cases are in queue, as I was using 2 parallel sessions to run my Selenium C# automation script. Once the running test cases are passed, the ones queue are also tested.

![](https://cdn.lambdatest.com/blog/wp-content/uploads/2019/11/image17.png)

The overall execution time of these four tests is around 128 seconds, this is when I was going with 2 parallel sessions. If I had run the test cases with 4 parallel test sessions, my overall execution time would have been 64 seconds. As seen in the snapshot below, the parameters passed to the **TextFixture** class are considered as the **DesiredCapabilities** for automated browser testing.

![](https://www.lambdatest.com/blog/wp-content/uploads/2019/11/image14.png)

***Run your*** [***Playwright test***](https://www.lambdatest.com/playwright?utm_source=devto&utm_medium=organic&utm_campaign=mar10_bh&utm_term=bh&utm_content=webpage) ***scripts instantly on 50+ browser and OS combinations using the LambdaTest cloud.***

## Conclusion

Though there are a lot of test frameworks in C#, NUnit is one of the most popular ones. The main advantage of the NUnit test framework is that it is compatible with Selenium testsuite and is effective in performing TDD (Test Driven Development). NUnit testing for Selenium automation with C# is widely used for automated browser testing.

In this NUnit testing tutorial, we learned to automate web UI testing using Selenium C# framework called NUnit. We also learned that testing using Local Selenium WebDriver has scalability issues, hence it is better to use Selenium testing tool on a cloud based [cross browser testing](https://www.lambdatest.com/?utm_source=dev&utm_medium=Blog&utm_campaign=Himanshu-21112019&utm_term=Himanshu) platform like LambdaTest.

Parallel Testing should be used when testing is performed on a remote Selenium Grid since it reduces the overall execution time thereby shortening the release cycles. It is always better to use an effective test framework like NUnit on powerful and scalable Selenium Grid as it can have a huge impact on the deliverables of the project. Kudos on running your first NUnit testing script. Until next time. Ciao! 🙂

[![](https://www.lambdatest.com/blog/wp-content/uploads/2018/11/Adword-Cyber2.jpg)](https://accounts.lambdatest.com/register/?utm_source=dev&utm_medium=Blog&utm_campaign=Himanshu-21112019&utm_term=Himanshu)