Start an automation testing project with Selenium C#

Table Of Contents

Overview

Selenium is a popular automation testing tool to stimulate user action for browser. It supports many modern browser like: Chrome, Firefox, MsEdge, Safari…and also has so many language bindings (Ruby, Python, Java, Javascript…)

C# may be not the best choice for Selenium but it still rocks. The most advantage of choosing C# instead of another language is the combination of Nunit (Nunit Test Adapter) with Visual Studio. It makes running test cases in GUI Mode so breezily.

GUI mode is very convenient, especially for debug. Below are some easy steps to start a new automation project using Selenium C#.

Setup

1. Download and install visual studio (choose community version if you do not have license)

https://visualstudio.microsoft.com/vs/community/

2. Start visual studio and choose “Create New Project” option in the right sidebar

3. Choose to create project with “Nunit Test Project” template and click “Next”. If you do not see this option, just type “nunit” in search textbox:

tomatoqa - create selenium csharp

4. Choose whatever project name as you wish and click “Next”:

tomatoqa - create selenium csharp

5. After that, a Nunit project is created for you with a dumb sample test case. Now you should focus on two things:

a. TestExplorer: where you can choose testcase to run.

b. Packages: all default packages which Visual Studio helps us install using Nuget Package Manager (Nuget is popular package manager to install and manage dependencies like npm for Nodejs, maven/gradle for Java…)

tomatoqa - create selenium csharp

* Note: If you do not see “Test Explorer”, maybe it is hidden. Drag it out as below image:

tomatoqa - create selenium csharp

6. To start writing testscripts with Selenium, below are recommend packages I think you should consider to install.

tomatoqa - create selenium csharp

1. Selenium and other related packages (Selenium Support, Selenium Extra Wait Helpers): of course this is the must-use package

2. WebDriverManager: to automatically manage version of browser driver. Doc here.

3. Nhamcrest and Nhamcrest for Nunit: if you want rich and flexible assertions. Doc here.

4. Extent reports: for pretty auto-generated HTML report after running testcases (you will need some code to make it work)

5. Nunit console runner: native test runner for Nunit support. Doc here.

Some other useful packages:

1. RestSharp: http/https library to sendAPI request and get response. It is useful if you want to write some API testcase

2. HtmlAgilityPack: verfy useful lib for crawling data and also to quickly extract information from DOM HTML

3. Configuration Manager: very helpful if you want to store/read data from configuration file.

Conclusion

I think these above packages are enough for you to start an automation testing project using Selenium C#. Selenium is an old popular automation tools for web browser for such a long time. Although there are some modern tools like Cypress, Playwright… but I think Selenium still becomes a great option if you want to choose a tool for your testing project.

Ask me anything