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!