Selenium C# and NUnit Pain Free Start Guide


In this article, I will walk you through the initial steps of automated testing with Selenium Webdriver using C#. For this purpose we will need:

  • Visual Studio
  • NUnit Framework
  • Selenium Webdriver

First, let’s see how we obtain these elements if we do not have them already.

Install Visual Studio

The only professional IDE available for C# at the time is Visual Studio (JetBrains is coming with a project called ‘JetBrains Rider’ and will release it soon). The most recent version of Visual Studio at the time I am writing this article is VS2017. The community edition can be downloaded here: https://www.visualstudio.com/downloads/

It is a lightweight installer. Click on it and then click ‘Yes’ when asked for permission. After clicking on ‘Continue’ you will end up with below screen:

C#

For this tutorial, let’s choose .NET desktop development option and click install. After the installation, a reboot might be required. Once you run the Visual Studio 2017, you will be asked to sign in. You can create an account or you might continue to use it without one. For doing that so:

C#

On next screen, you will be asked to opt a color theme. Choose as you will ( I like the blue better) and click on ‘Start Visual Studio’.

selenium

To get started with Visual Studio we need to create a new project. Press ‘File’ on the top left and then ‘New’ and ‘Project’.

shadow-black

Select Class Library (.NET Framework) and name the project as you will before you click ‘OK’.

webdriver

You may want to change your class name to something relative to your test (i.e LoginTest, HomePageTest etc..). For achieving this, right-click on your class on solution explorer window located at the right-hand side of the screen.

.net

Click “Rename

visual-studio

Then rename it.

And that’s it! We created it a new C# project using Visual Studio 2017. Next step is to download NUnit Framework and Selenium Webdriver.

Install NUnit Framework

NUnit is a free to use unit-testing framework for all .NET languages. It makes the unit testing lot easier. Hence we will use it on our Selenium tests.

For downloading NUnit Framework, we will use the NuGet Package Manager built into Visual Studio. NuGet is the package manager for the Microsoft development platform including .NET.

There are two ways for installing via NuGet:

First way is to click on ‘Tools’ on top menus and then hover on ‘NuGet Package Manager’ option and select ‘Manage NuGet Packager for Solution…’.

Click on ‘Browse’, then type ‘NUnit’ in the search box and hit enter. Click on NUnit ( most recent version is v3.6.1 at the time).

Select the project that you would like to install NUnit to and then click ‘Install’.

Click ‘OK’ once you are asked to review changes.

You have installed NUnit! But we need install Test Adapter for NUnit in order to run the tests inside Visual Studio. Therefore search for Nunit3TestAdapter, once you found it click on it and click ‘Install’.

Once again, click ‘OK’ when you are asked to review changes.

We now installed NUnit and NUnit Test Adapter and ready move on installing the Selenium WebDriver.

Install Selenium WebDriver

The second way to use NuGet Package Manager ( First way is mentioned during the installation of Nunit), is to right click on your solution on solution explorer and click ‘Manage NuGet Packages for Solution…’

This time type ‘Selenium’ on the search box and hit enter.

Select the Selenium.WebDriver and continue with the installation.

Again click ‘OK’ if asked for on review changes screen.

Well done! You have installed Selenium WebDriver! But for driving the latest versions of browsers, we need to install the driver explicitly for that browser.

In this tutorial, I am going automate Google Chrome. Therefore, I need to install ChromeDriver. You may download it via the internet or easy way to obtain it is to use NuGet again!

Since we previously searched ‘Selenium’, ChromeDriver is displayed on search results. Click on ‘Selenium.WebDriver.ChromeDriver’ and select the project you want to install it and click ‘Install’ again.

Yet again click ‘OK’ on review changes screen.

Congratulations! Since all components are installed, we are ready to write our first test!

Writing First Selenium Test with C#

Now, it is time to write our Selenium test.

For automating chrome using selenium webdriver, we need to create an instance of webdriver as a new chrome driver.

Add the following the code inside your class in order to create an instance of webdriver called ‘driver’.

You might notice there is an error in our code. It occurred because, though we installed Selenium Webdriver and ChromeDriver, we have not imported the necessary libraries.

An easy way to do it instead of typing the libraries explicitly in Visual Studio is to press ‘Ctrl + .’ on the words missing the references. Once you have done it below screen will pop-up:

Click on ‘using OpenQA.Selenium’. Then do the same thing on ‘ChromeDriver()’.

Notice the two libraries added on top of our class.

Since the driver is implemented, we can now write our first test.

All test methods must be written under ‘[Test]’ annotation provided by NUnit. Add the mentioned annotation and again import the necessary library.

Name your method (it will be the name of your test), and we can start writing the tests.

Now we can start writing our test!

First thing first, let’s navigate to the website which we would like to test.

Don’t forget to type ‘http://’ before your URL, otherwise, Selenium would not recognise it. Note that alternative to driver.Navigate().GoToUrl(), you may use driver.Url() only.

After navigating to the website, let’s assert the title of the page is true.

Since we are done, we can close the browser and dispose our driver.

We just wrote our first test!

But how do we run it? Before running it lets build our project. Click on ‘Build’ on the top menu and ‘Build Solution’ option.

Once the build is done. You can it is built successfully on output console.

Now head back to the top menu and click ‘Test’ this time. Hover on ‘Windows’ option and click ‘Test Explorer’.

You will see the test we recently have written on the list.

Click on ‘Run All’ and our your tests will start to run.

Once it is finished we can see if it passed or not.

The End! 🙂 If you like this article please write a comment and share it on social media.

GitHub Linkhttps://github.com/swtestacademy/NewSeleniumProjectVS

Spoiler: Stay tuned for the advance C# Selenium and Selenium with Python articles 😉

Happy Testing!

javafx

Database Operations in JavaFX

By Onur Baskirt / Apr 1, 2016 / 56 Comments
Before started this section, please check the first article and learn How to Start JAVAFX! http://www.swtestacademy.com/getting-started-with-javafx/ At first, part of JavaFX tutorial series, we created a sample JavaFX project, designed the draft version of the UI and set up an...
rest assured

REST API Testing with Rest Assured

By Onur Baskirt / Mar 8, 2016 / 32 Comments
Outline In this post, I will explain what is API and API testing, what is the difference between SOAP and REST services, and how to test REST APIs with Rest Assured Library. What is API? API stands for Application Programming...
extentreports

How to Write Effective CSS Locators

By Onur Baskirt / Oct 1, 2017 / 0 Comments
Hi all, in this tutorial, I will describe you how to write effective CSS locators to interrogate web elements for your automation projects. As a rule of thumb, your interrogation strategy should be in below order: First try to use...
extentreports

Selenium-11: Execute JavaScript with JavascriptExecutor

By Onur Baskirt / Jan 27, 2016 / 15 Comments
Outline Sometimes we cannot handle some conditions or problems with Webdriver, web controls don't react well against selenium commands. In this kind of situations, we use Javascript. It is useful for custom synchronizations, hide or show the web elements, change...
javafx

Getting Started with JavaFX

By Onur Baskirt / Mar 25, 2016 / 0 Comments
When I started to work in my current position, one of my task is to do manual operations for campaign products  every week. After the second week, I thought that I have to automate this task using a GUI based...
extentreports

How to Write Smart XPath Locators

By Onur Baskirt / Sep 24, 2017 / 4 Comments
Hi all, in this tutorial, I will describe you how to write smart and non-brittle XPath locators. When we write our test scripts, we generally prefer to use id, name, class, etc. these kinds of locators. However, sometimes we could not...
extentreports

Selenium-1: Quick Start to Automation with Selenium WebDriver & JAVA & JUnit & Maven & IntelliJ

By Onur Baskirt / Sep 8, 2015 / 26 Comments
Outline Selenium Webdriver is the most popular open source web test automation framework across wide range of browsers and platforms. In this tutorial you will learn how to do web test automation with Selenium Webdriver and the related tools. Audience...
page object model

Page Object Model with C#

By Ege Aksoz / Jun 18, 2017 / 8 Comments
In the previous tutorial, we’ve taken the initial steps and entered the world of automated testing. We also wrote our first automated test. From this point on, since we are not just going to write one test, we need to...
extentreports

How to Select a Date From DatePicker Using Selenium

By Onur Baskirt / Aug 13, 2016 / 6 Comments
When you need to automate a airway, hotel, or similar websites you need to deal with Datepickers and some times it is a little bit cumbersome to select a specific date on the Datepicker or calendar.  In this post, I...
extentreports

Selenium Webdriver Performance testing with Jmeter and Selenium Grid

By Ozgur Kaya / Aug 9, 2016 / 2 Comments
In this post, we will complete Selenium Webdriver Performance testing scenario using Jmeter and Selenium Grid. 1- Install Java 7 or later If necessary https://java.com/tr/download/ 2- Download latest Jmeter version 3.0 or higher. http://jmeter.apache.org/download_jmeter.cgi 3- Download Jmeter PluginsManager JAR file and...
By | 2017-03-28T11:06:10+00:00 March 28th, 2017|Selenium, Test Automation, Web Automation|3 Comments

About the Author:

Ege Aksoz
Holding a BSc in Mechatronics, Ege loves to automate. He is now working as a Software Test Automation Engineer at Tallinn, Estonia. He is also passionate about Artificial Neural Networks and he is a master student on AI at Tallinn Technical University.

3 Comments

  1. Srinivas July 31, 2017 at 3:14 pm - Reply

    Useful Article

  2. Mani October 15, 2017 at 8:55 pm - Reply

    Really Help full

    • Onur Baskirt
      Onur Baskirt October 15, 2017 at 9:26 pm - Reply

      Thanks. Our technical author Ege Aksoz did his best.

Leave A Comment