
π Selenium C# Tutorial with NUnit Framework β Step-by-Step Guide
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.
πΉπ What is Selenium?
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.
πΉπ What is C#?
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
π Setting Up Selenium WebDriver with C# in Visual Studio
β Step-by-Step Setup Instructions:
- Download Visual Studio: Visit and install the Community Edition.
- Install Workloads:
- Universal Windows Platform Development
- .NET Desktop Development
- Universal Windows Platform Development
- Create New Console Project:
- File β New β Project
- Choose Console App (.NET Framework)
- Name the project and click OK
- File β New β Project
- Install Selenium WebDriver:
- Navigate to Tools β NuGet Package Manager β Manage NuGet Packages for Solution
- Search for Selenium.WebDriver
- Select and install the package
- Navigate to Tools β NuGet Package Manager β Manage NuGet Packages for Solution
π Installing and Integrating NUnit Framework
β Installing NUnit and NUnit Test Adapter
- Open NuGet Package Manager
- Search for NUnit and install it
- 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
- Change Output Type to Class Library
- Build the solution (Build β Build Solution)
- Open Test Explorer (Test β Windows β Test Explorer)
- Run the tests and validate output
πSelenium WebDriver Commands in C#
πΈ Browser Commands
Command | Description | Syntax |
driver.Url | Opens a specific URL | driver.Url = “https://site” |
driver.Title | Gets the page title | String title = driver.Title |
driver.Close() | Closes current browser | driver.Close(); |
driver.Quit() | Closes all browsers | driver.Quit(); |
driver.Navigate().Back() | Navigates to previous page |
πΈ WebElement Commands
Command | Description | Syntax |
Click | Clicks on an element | element.Click(); |
Clear | Clears text from textbox | element.Clear(); |
SendKeys | Enters text into a field | element.SendKeys(“text”); |
Displayed | Checks if element is visible | bool status = element.Displayed; |
Enabled | Checks if element is enabled | bool status = element.Enabled; |
Selected | Checks if element is selected | bool status = element.Selected; |
Text | Gets the inner text of an element | string text = element.Text; |
TagName | Gets the HTML tag name | string tag = element.TagName; |
GetCSSValue | Gets CSS property value (e.g., color) | string color = element.GetCssValue(“color”); |
πΈ Dropdown Handling using SelectElement
Command | Description | Syntax |
SelectByText | Selects by visible text | select.SelectByText(“value”); |
SelectByValue | Selects by value attribute | select.SelectByValue(“val”); |
SelectByIndex | Selects by index | select.SelectByIndex(0); |
Options | Gets list of dropdown options | var options = select.Options; |
IsMultiple | Checks if multi-select is supported | bool isMulti = select.IsMultiple; |
DeselectAll | Deselects all selected items | select.DeselectAll(); |
π Sample Test Case Scenarios
β 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”);
π Summary
- 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.