Setup Selenium Test Automation Project Using C#

Introduction

In this article, we will understand how to setup a selenium automation project using C# language.

In order to setup Selenium Automation Project using c# we need to install the dotnet sdk first in your system.

To install dotnet sdk visit this link.

Download the dotnet sdk from the above link and then set the environment variable in system environment path.

Now download the visual studio community edition which is free

https://visualstudio.microsoft.com/downloads/

It will take some time to download depending on the network speed.

Open the visual studio.

  • Click Create a New Project Option
  • Search Nunit Test project Template and then click the next option
  • Provide the Project Name and click Next 
  • Select the dotnet SDK Environment
  • Your project will get created with a unittest.cs sample file.
  • Now Goto Solution Explorer
  • Expand the package manager
  • Right-click on the packages option and select the option manage packages.
  • A NuGet package manager explorer will get opened. Here search selenium in the browse tab.
  • You will get the packages related to selenium
  • Download the selenium and selenium support packages from NuGet package manager
  • Search WebDrivermanager in Browser tab
  • Download the webdrivermanager (Download the package which is developed by webdrivermanager) from NuGet package manager

Once downloaded these entries will get updated in the csproj file in your project.

Now we can go ahead and write our selenium Tests.

For writing tests in selenium c#, we write using Nunit framework.

NUnit is a test Framework like JUnit.

We can use NUnit to define your test cases, test suites, and assertions.

NUnit can run all the tests and show you a report.

NUnit, like JUnit, is a test-driven development framework.

Advantages of using NUint

  • It is open source and it is free.
  • It is easy to integrate it with testing projects.
  • NUnit works with many integrated runners including Resharper and TestDriven .NET.
  • NUnit has frequent version updates.
  • NUnit has a graphical user interface.
  • Very easy integration with Visual Studio and other IDEs.

Nunit tests are written using [test] annotations. This is equivalent to @test in TestNG framework

Annotations

  1. OneTimeSetUp - Identifies methods to be called once prior to any child tests
  2. OneTimeTearDown - Identifies methods to be called once after all child tests
  3. Repeat - Specifies that the decorated method should be executed multiple times
  4. Retry - Causes a test to rerun if it fails, up to a maximum number of times
  5. TearDown - Indicates a method of a TestFixture called immediately after each test method
  6. Test - Marks a method of a TestFixture that represents a test
  7. TestCase - Marks a method with parameters as a test and provides inline arguments
  8. Timeout - Provides a timeout value in milliseconds for test cases
  9. Setup - Indicates a method of a TestFixture called immediately before each test method

Browser Methods in C# 

We are using Webdrivermanager to create the instance of the browser.

We need to create the instance of the Webdriver outside of the test so that we can use that instance across all the tests in your test file.

IWebDriver driver;

We need to call SetUpDriver method and pass the required browser config. If it's chrome then pass chromeconfig, if it's edge pass edgeconfig.

new WebDriverManager.DriverManager().SetUpDriver(new EdgeConfig());

This config comes from the namespace using WebDriverManager.DriverConfigs.Impl.

Now create an instance of ChromeDriver class/Edgedriver, and import the necessary chromedriver/edgedriver namespace.

driver = new EdgeDriver();

To maximize the window of the browser when launched then use the below method.

driver.Manage().Window.Maximize();

To minimize the window of the browser when launched then use the below method

driver.Manage().Window.Minimize();

To open an application then use the below driver.url property

driver.Url="url of the application"

To get the title of the application then use the driver.title property

String title=driver.title;

To close the current window then use the close method

driver.Close();

To close all instances of the browser opened then use the quit method.

driver.Quit();

To refresh the browser then use the refresh method

driver.Navigate().Refresh();

We can open URL of the application using navigate class as well by calling the GoToUrl method

Using this way of opening the browser we enable the user to navigate back and forward in the application. It is equivalent to clicking back and forward arrows in the browser.

driver.Navigate().GoToUrl("https://test.com");
driver.Navigate().Back();
driver.Navigate().Forward();

To print values in the console in nuint framework then use the writeline method in the testcontext class.

TestContext.Progress.WriteLine(driver.Url);

Note - To execute the test we can switch to test explorer section which is present in the solution explorer section.

Select the required tests and then click the run button.

We will see the results of the tests in the tests console section. By default, it will show the build related console log. To view the tests logs of the tests ran then select the tests option in the console section.

Entire code

using OpenQA.Selenium;
using OpenQA.Selenium.Edge;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WebDriverManager.DriverConfigs.Impl;
namespace Seleniumautomation {
    public class Firstselenium {
        IWebDriver driver;
        [SetUp]
        public void Setup() {
                new WebDriverManager.DriverManager().SetUpDriver(new EdgeConfig());
                driver = new EdgeDriver();
                driver.Manage().Window.Maximize();
                driver.Url = "url of application to be tested";
            }
            [Test]
        public void Test1() {
            TestContext.Progress.WriteLine(driver.Title);
            TestContext.Progress.WriteLine(driver.Url);
            driver.Close();
        }
    }
}

Summary

This article gives you an understanding of how to setup the selenium automation project using C# and we learned a few browser methods which help us to interact with the browser.

Thanks..............


Similar Articles