---Advertisement---

Selenium C# Tutorial: Complete Guide with NUnit & WebDriver Examples Great 2025

By Manisha

Updated On:

---Advertisement---
Selenium C# Tutorial

Selenium C# Tutorial: Selenium is a powerful open-source automation tool that allows testers to automate web applications across various browsers and platforms. When paired with C# and NUnit, it becomes a robust testing framework ideal for .NET developers.


Selenium C# Tutorial: Selenium is an automation testing tool that supports multiple programming languages including Java, Python, C#, Ruby, and PHP. It can run on various browsers such as Chrome, Firefox, Safari, and Edge, making it a flexible and reliable tool for web testing.


Selenium C# Tutorial: C# (C-Sharp) is a modern, object-oriented programming language developed by Microsoft. It is widely used for building desktop, web, and cloud-based applications using the .NET Framework.

Key Features of C#:

  • Object-Oriented Programming
  • Rich Library Support
  • Easy Integration with Visual Studio
  • Supports Inheritance, Polymorphism, and Encapsulation

βœ… Step-by-Step Setup Instructions:

  1. Download Visual Studio: Visit and install the Community Edition.
  2. Install Workloads:
    • Universal Windows Platform Development
    • .NET Desktop Development
  3. Create New Console Project:
    • File β†’ New β†’ Project
    • Choose Console App (.NET Framework)
    • Name the project and click OK
  4. Install Selenium WebDriver:
    • Navigate to Tools β†’ NuGet Package Manager β†’ Manage NuGet Packages for Solution
    • Search for Selenium.WebDriver
    • Select and install the package

βœ… Installing NUnit and NUnit Test Adapter

  1. Open NuGet Package Manager
  2. Search for NUnit and install it
  3. Then, search and install NUnit3TestAdapter (for 64-bit systems)

βœ… Create NUnit Test Class

  • Add a new class to the project
  • Decorate test methods using NUnit attributes like [SetUp], [Test], and [TearDown]
  • Ensure the chromedriver.exe path is correctly set

βœ… Building and Running NUnit Tests

  1. Change Output Type to Class Library
  2. Build the solution (Build β†’ Build Solution)
  3. Open Test Explorer (Test β†’ Windows β†’ Test Explorer)
  4. Run the tests and validate output

πŸ”Έ Browser Commands

CommandDescriptionSyntax
driver.UrlOpens a specific URLdriver.Url = “https://site”
driver.TitleGets the page titleString title = driver.Title
driver.Close()Closes current browserdriver.Close();
driver.Quit()Closes all browsersdriver.Quit();
driver.Navigate().Back()Navigates to previous page

πŸ”Έ WebElement Commands

CommandDescriptionSyntax
ClickClicks on an elementelement.Click();
ClearClears text from textboxelement.Clear();
SendKeysEnters text into a fieldelement.SendKeys(“text”);
DisplayedChecks if element is visiblebool status = element.Displayed;
EnabledChecks if element is enabledbool status = element.Enabled;
SelectedChecks if element is selectedbool status = element.Selected;
TextGets the inner text of an elementstring text = element.Text;
TagNameGets the HTML tag namestring tag = element.TagName;
GetCSSValueGets CSS property value (e.g., color)string color = element.GetCssValue(“color”);

πŸ”Έ Dropdown Handling using SelectElement

CommandDescriptionSyntax
SelectByTextSelects by visible textselect.SelectByText(“value”);
SelectByValueSelects by value attributeselect.SelectByValue(“val”);
SelectByIndexSelects by indexselect.SelectByIndex(0);
OptionsGets list of dropdown optionsvar options = select.Options;
IsMultipleChecks if multi-select is supportedbool isMulti = select.IsMultiple;
DeselectAllDeselects all selected itemsselect.DeselectAll();

βœ… Example 1: Click Using XPath

Selenium C# Tutorial: csharp

IWebElement link = driver.FindElement(By.XPath(“//a[text()=’Testing’]”));

link.Click();

βœ… Example 2: Enter Email & Submit

csharp

IWebElement email = driver.FindElement(By.XPath(“//input[@id=’email’]”));

email.SendKeys(“test@example.com”);

email.Submit();

βœ… Example 3: Dropdown Selection

csharp

SelectElement course = new SelectElement(driver.FindElement(By.XPath(“//select[@id=’course’]”)));

course.SelectByText(“Automation”);


  • Selenium C# Tutorial: Selenium with C# and NUnit provides a strong framework for automated testing.
  • Visual Studio setup is required to work with C# Selenium.
  • Use NUnit for writing and managing test cases with attributes like [Test], [SetUp], and [TearDown].
  • Selenium supports various commands for browser control, element interaction, and dropdown handling.
  • Code samples and test scenarios make it easy to get started with real-world applications.

Visual Studio Downloads

Drag and Drop in Selenium 

Leave a Comment

Index