Selenium Automation using C# (NUnit)

DYLSVhtWAAA5qn9seleniumwithC

     In the previous post, we discussed about Selenium C# and MSTest framework. Another day, I got an opportunity to write selenium automation using C# language and NUnit framework. Prior to scripting, I started the configuration from the scratch to achieve my selenium scripting using C# and NUnit. Here I would like to share my experience with configuration and scripting part,

Steps for install and configure Visual Studio:

Step 1: Navigate to the URL https://www.visualstudio.com/downloads/ and Click on the ‘Free download’ button displayed on Visual Studio Community 2017 tab.
Step 2: Open the exe downloaded. Click on ‘Yes’ if asked for Admin Rights.
Step 3: A popup will appear. Click on ‘Continue’ button.
Step 4: In the next screen, select the check-boxes for

  • Universal Windows Platform development
  • .Net desktop development

Click on ‘Install.’ Wait for installation of each component to complete. Files are 16GB in size and will take time.
Step 5: A pop up will be displayed. Click on ‘Restart’ button (if required).
Step 6: Once the machine is restarted, search for “Visual Studio 2017” on the start menu and click on the search result. In the next screen, Click on “Not now, maybe later” link if you do not have an existing account.
Step 7: In the next screen,

  • Select color theme of your liking
  • Click the button “Start Visual Studio'”

Step 8: Visual Studio ‘Get Started’ screen will appear.

Set up Visual Studio with Selenium WebDriver:

Step 1: Navigate to Tools -> NuGet Package Manager -> Manage NuGet Packages for Solution
Step 2: In the next window,

  1. Search for “Selenium”.
  2. Select the first search result “Selenium.WebDriver”
  3. Check the project checkbox
  4. Select the desired version.
  5. Click on ‘Install’.
  6. Select second search result “Selenium.Support”
  7. Check the project checkbox.
  8. Select the desired version.
  9. Click on ‘Install’.

Set up Visual Studio with NUnit Framework:

Step 1: Navigate to Tools -> NuGet Package Manager -> Manage NuGet Packages for Solution
Step 2: In the next window,

  1. Search for “NUnit”
  2. Select the search result “NUnit”
  3. Check the project checkbox
  4. Select the desired version.
  5. Click Install.
  6. In the same window, select search result “NUnit3TestAdapter”
  7. Check the project checkbox.
  8. Select the desired version.
  9. Click on ‘Install’.

Create a new project in Visual Studio:

Step 1: In the File Menu, Click New > Project
Step 2: In the next screen,

  1. Select the option ‘Visual C#’
  2. Click on Console App (.Net Framework)
  3. Enter project name.
  4. Click OK

Step 3: You will be able to see the created project.

     Now the configurations and project is ready, next you can add one C# class file to project and start writing your first script for automation as below. I used this class file to start my C# scripting for selenium automation,

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using NUnit.Framework;
using NUnit.Framework.Interfaces;
using System.Threading;

namespace seleniumTestProject
{
[TestFixture]
public class SeleniumTest
{
public IWebDriver driver; //declare the driver object

[OneTimeSetUp]
public void BeforeClass()
{
driver = new ChromeDriver(“path_to_chromedriver.exe”); //initialize the ChromeDriver instance
driver.Url = “https://www.google.co.in/”; //load application URL
}

[SetUp]
public void BeforeTest()
{
//You can write any precondition to perform prior to each test cases
}

[Test]
public void GoogleSearchTest()
{
driver.FindElement(By.Id(“lst-ib”)).SendKeys(“Journey of Quality”);
}

[TearDown]
public void AfterTest()
{
driver.Close();
}

[OneTimeTearDown]
public void AfterClass()
{
driver.Quit();
}
}
}

     Try the above configuration and scripting using C# for Selenium Automation from your end. I recommend everyone to use NUnit framework than MSTest framework for Selenium C# scripting. NUnit provides straight-froward scripting when use their annotations. In case of MSTest, we must declare the method as static and should possess one parameter when use [AssemblyInitialize] or [ClassInitialize]. In case of NUnit, no need to declare the method as static when uses [OneTimeSetUp] or [SetUp].

make it perfect !

 

Advertisement

One thought on “Selenium Automation using C# (NUnit)

Add yours

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create a website or blog at WordPress.com

Up ↑

%d bloggers like this: