Open In App

Can Selenium be used for .NET applications?

Last Updated : 13 Nov, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Selenium, a leading platform in the field of web application testing, promises to carry out automation across multiple browsers. When implemented on .NET, it enhances web application testing.

In this article, we will see set up of selenium in Visual Studio 2022, and a simple test case will also be created using the NUnit framework.

Prerequisites

  1. Visual Studio 2022: That is, the most accommodating integrated development environment for any .NET tasks.
  2. Selenium WebDriver: The authoritative drivers for the browsers.
  3. To start using Selenium there are several packages that you have to download.

Step By Step Process of Selenium for .NET applications

Step 1: Create a New Project

  1. Launch Visual Studio.
  2. Click on Create a new project.
Step 1: Create a New Project
Step 1: Create a New Project
  • In the search box of the Create a new project pop-up, type NUnit.
  • Select NUnit Test Project (.NET Core) from the search results.
Select NUnit Test Project (.NET Core) from the search results.
Select NUnit Test Project (.NET Core) from the search results
  • Enter a Project name and select a Location. Click Create to proceed.
Enter a Project name and select a Location. Click Create to proceed
Enter a Project name and select a Location. Click Create to proceed

Step 2: Install Required Packages

  • Once your NUnit (.NET Core) project is set up, you will have the Setup and Test methods provided by default. Choose the Tools and then the NuGet Package Manager and subsequently the Package Manager Console.
  • Go to the Tools menu and then select the NuGet Package Manager followed by the Package Manager Console.
 Install Required Packages
Install Required Packages
  • In the Package Manager Console, execute the following commands to install Selenium WebDriver and the NUnit framework.

For Firefox:

Install-Package Selenium.WebDriver Install-Package Selenium.Firefox.WebDriver

For Chrome:

Install-Package Selenium.WebDriver Install-Package Selenium.Chrome.WebDriver

For NUnit:

Install-Package NUnit Install-Package NUnit3TestAdapter

To check the installation status of the packages, run the following command:

Get-Package

Step 3: Write Your First Selenium Test

  • Now that all necessary packages are installed, you can write your first test case.
  • Replace the contents of your test class with the following code:
C#
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox; // Change to Chrome if using ChromeDriver
using System;

namespace FirstSeleniumTest
{
    public class Tests
    {
        private string u = "https://www.geeksforgeeks.com/index.htm";
        private IWebDriver d;

        [SetUp]
        public void Setup()
        {
            // Creating object of FirefoxDriver
            d = new FirefoxDriver();
        }

        [Test]
        public void Test1()
        {
            // Launching URL
            d.Navigate().GoToUrl(u);
            Console.WriteLine("URL launched");
            // Add an assertion to verify the URL or page title
            Assert.That(d.Url, Is.EqualTo(u), "The URL did not launch as expected.");
        }

        [TearDown]
        public void CloseBrowser()
        {
            if (d != null)
            {
                d.Quit();
                d.Dispose(); // Ensures the WebDriver instance is properly disposed
            }
        }
    }
}
  • This code sets up the Firefox browser, navigates to the specified URL, and then closes the browser after the test.

Step 4: Build the Solution

  • Go to terminal and run this command.
Go to terminal and run this command.
Build the Solution

Step 5: Run the Tests

  • Open the Test Explorer by going to Test > Test Explorer.
  • In the Test Explorer, you should see your tests listed. Click on Run All to execute the tests.
Run the Tests
Run the Tests

Step 6: View Test Results

  • After running the tests, check the output in the Test Explorer.
imresizer-1729826190157
View Test Results
View Test Results
View Test Results

Conclusion

Using Selenium for .NET applications is a powerful approach for browser automation and testing. It allows developers and testers to ensure application compatibility across browsers, making it ideal for maintaining quality in web applications. By utilizing Selenium in Visual Studio with C#, teams can deliver robust test automation solutions that significantly improve the testing process and application quality across .NET environments.


Next Article
Article Tags :

Similar Reads