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 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#.
Testing With NUnit – A Selenium C# Framework
NUnit is an open-source unit testing framework in C# that is ported from JUnit automated testing framework. 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.
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.
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.
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.
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.
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.
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 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 from the links mentioned below:
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.).