Selenium with C# : How to start running Automated Tests

Getting Started with Selenium and C#

  1. Install Visual Studio
  2. Create Selenium C# automation framework .NET Core 5.0 from scratch using Visual Studio
  3. Add Chromedriver to Selenium C# Project using Visual Studio
  4. Add GeckoDriver to Selenium C# Project

How to Install Visual Studio

  • Click on Free Download, and the .exe file will be downloaded.
  • Click on the .exe file to start the Visual Studio Installer.
  • Click on the installer file, the Privacy and License Terms window appears, click on Continue to start installation.
  • Visual Studio Installer downloads and prepares Visual Studio for installation.
    Note: This is Visual Studio Installer, not the Actual Visual Studio
  • Once the Preparation is complete, the Visual Studio installation window appears. Users will have to choose a set of tools. To build an automation framework one can choose the Asp.net and Web development option. Click on Install to proceed.
  • Installation begins and its progress percentage will be shown accordingly.
    Note: The installation takes some time depending on the internet speed.
  • Once the installation is complete, the Restart pop up window appears. Click on Restart.
  • After a successful restart of the system, the Visual Studio setup pop up will appear. Click on Not now, Maybe later.
  • Note: Sometimes, after restart the above window doesn’t appear. If it doesn’t appear automatically just Open Visual Studio from the Windows Start Menu.
  • Choose a Theme for Visual Studio, and click on Start Visual studio.
  • Visual Studio prepares settings for First Use.
    Note: This stage takes some time to finish.
  • Once the above step is completed, the Get Started Window opens.
  • Visual Studio Installation is complete.

How to create Selenium C# automation framework .NET Core 5.0 from scratch using Visual Studio

  • Open Visual Studio from the Windows Start Menu.
  • Click on Create New Project.
  • Search for NUnit Template. From the Search Results choose C# NUnit Test Project (.NET core).
  • Note: Selenium C# project can be created using MSTest libraries as well, but Nunit is most widely used with C# and Selenium.
  • In the Configuration Section, enter the required details:
  • In the , Visual Studio creates Selenium C# framework with NUnit Test Runner.additional Information section, choose Target Framework : .NET 5.0 (Current)Click on Create.
  • After clicking Create .cs extension will be created by default i.e
    The default framework contains dependencies for NUnit and NUnit Test Adapter. One file with UnitTest1.cs.
  • Add Selenium Dependencies for the project. To run Selenium tests using C# and NUnit, add Selenium dependencies:- Click on the Tools Menu- Click on NuGet Package Manager
    - Click on Manage NuGet Package for Solution
  • Search for ,Selenium Webdriver and add the package. In the NuGet Package Explorer window:- Click on Browse- In the Search box type Selenium
    — Choose Selenium.Webdriver by Selenium
    - On the window to the right, select the checkbox which shows Project Name
    - Click on Install
  • If it prompts for the License Acceptance Window, click on Accept.
  • Wait for the installation to finish.
  • Install the Selenium Support Package from NuGet: (Tools > NuGet Package Manager > Manage NuGet Package for Solution) checkbox (the current project)
    — Navigate to NuGet Package Manager
    — Type Selenium Support
    - Click on Selenium Support from Search Results
    - Choose the Project Name- Click on Install
  • Wait for the installation to finish.
  • Once Selenium Webdriver and Selenium Support NuGet package is installed one should see the dependencies added in the project:

How to Add Chromedriver to Selenium C# Project using Visual Studio

  • Navigate to Solution Explorer in Visual Studio.
  • Right click on the Project (ex: SeleniumCsharp)
    Note: Right Click on the Project Name, not on the Solution Name.
  • Click Add.
  • Click New Folder.
  • Name the new folder drivers.
  • Click on ChromeDriver <XX.XX.XXX>.
  • A new web page will open up and ask for the target Operating System (Windows, in this case).
  • Click on the right option and ChromeDriver will be downloaded as a Zip file.

How to Add GeckoDriver to Selenium & C# Project using Visual Studio

  • Navigate Gecko driver Download web page:
    – Navigate to Mozilla Github
    – Click on Releases under Downloads
  • Choose the correct platform for GeckoDriver and download.
    - In the Releases page, scroll down to the Assets section.
    - Choose the desired GeckoDriver for the Operating System being used (Windows, in this case).
  • After completion of Step 2, the zip file containing geckodriver.exe will be downloaded.
  • Extract the GeckoDriver to the drivers folder created earlier.Note: Step 4 is similar to Chromedriver download. Simply extract geckodriver.exe and place it in drivers folder.After completing this step, the drivers folder should contain two .exe files namely chromedriver.exe and geckodriver.exe.

How to write the first test using Selenium, C# and Nunit using Visual Studio IDE

  • Test 1: Navigates to the BrowserStack homepage and verifies the company logo.
  • Test 2: Verifies for all menu items count on the BrowserStack homepage.
  • Test 3: Navigates to the BrowserStack pricing section and verifies the header.
//Inside SeleniumTest.cs

using NUnit.Framework;

using OpenQA.Selenium;

using OpenQA.Selenium.Chrome;

using OpenQA.Selenium.Firefox;

using System;

using System.Collections.ObjectModel;

using System.IO;

namespace SeleniumCsharp

{

public class Tests

{

IWebDriver driver;

[OneTimeSetUp]

public void Setup()

{

//Below code is to get the drivers folder path dynamically.

//You can also specify chromedriver.exe path dircly ex: C:/MyProject/Project/drivers

string path = Directory.GetParent(Environment.CurrentDirectory).Parent.Parent.FullName;

//Creates the ChomeDriver object, Executes tests on Google Chrome

driver = new ChromeDriver(path+@"\drivers\");

//If you want to Execute Tests on Firefox uncomment the below code

// Specify Correct location of geckodriver.exe folder path. Ex: C:/Project/drivers

//driver= new FirefoxDriver(path + @"\drivers\");

}

[Test]

public void verifyLogo()

{

driver.Navigate().GoToUrl("https://www.browserstack.com/");

Assert.IsTrue(driver.FindElement(By.Id("logo")).Displayed);

}

[Test]

public void verifyMenuItemcount()

{

ReadOnlyCollection<IWebElement> menuItem = driver.FindElements(By.XPath("//ul[contains(@class,'horizontal-list product-menu')]/li"));

Assert.AreEqual(menuItem.Count, 4);

}

[Test]

public void verifyPricingPage()

{

driver.Navigate().GoToUrl("https://browserstack.com/pricing");

IWebElement contactUsPageHeader = driver.FindElement(By.TagName("h1"));

Assert.IsTrue(contactUsPageHeader.Text.Contains("Replace your device lab and VMs with any of these plans"));

}




[OneTimeTearDown]

public void TearDown()

{

driver.Quit();

}

}

}

How to run Selenium C# Tests with Example

  • Tests start executing.
  • Once all your tests finish executing, the results will be marked as shown below:
  • Tests start executing.
  • Once all your tests finish executing, the results will be marked as shown below:

How to Execute C# Selenium Tests on Firefox

driver = new ChromeDriver(path+@"\drivers\");
driver= new FirefoxDriver(path + @"\drivers\");
driver = new FirefoxDriver(@'C:/MyProject/Project/driver');

--

--

--

https://www.linkedin.com/in/ganeshsirsi/

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Reusing Variables in Golang

Java 8 | StringTokenizer Practice 2

Migrating Data from Amazon Redshift to MaxCompute

Java Multi Threading : Basics and Beyond

Java threads : Image

Two ways to create a Live Demo for your Github project

How to Install Parse Server on Ubuntu 16.04

The Latest Features of DataWorks: How to Choose the Right Edition of DataWorks

Apache Spark 3 — Real-time Stream Processing using Scala

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Ganesh Hegde

Ganesh Hegde

https://www.linkedin.com/in/ganeshsirsi/

More from Medium

Safe type definition for .NET APIs

End to End Unit Testing for .NET 6 Web API

【C#】Behavioral Patterns Command Mode

Understanding .Net Framework 4.5 Architecture