Which is Selenium?
Selenium is a top web testing automation tool. It's an open-source framework for programmatic browser control. Selenium lets you develop test scripts that click buttons, fill out forms, and validate website content like real users.
Why Selenium in C# are powerful and efficient automated testing tools:
- C# Integration with.NET: Selenium works well with Visual Studio and NUnit, making it ideal for.NET developers. The connection boosts productivity and lets developers use existing tools and frameworks.
- The C# ecosystem and Selenium have substantial community support. Developers may find many tools, guides, and libraries to debug and improve their testing frameworks.
- Due to its performance-optimized compilation, C# runs tests faster. C# also simplifies test execution with better error handling and debugging. Parallel Execution: NUnit and MSTest make scaling tests easy in C#.
- The simultaneous execution of tests in these frameworks greatly reduces test execution time and enhances test efficiency.
Perquisites
- Install Selenium WebDriver
- NuGet Package Manager > Manage NuGet Packages for Solution
- Search for Selenium.WebDriver in the NuGet package manager.
- Click Install to add the Selenium WebDriver to your project.
- To configure the WebDriver to interact with a specific browser:
- For Chrome, install Selenium.WebDriver.ChromeDriver.
- For Firefox, install Selenium.WebDriver.GeckoDriver.
- For Edge, install Selenium.WebDriver.MicrosoftWebDriver.
Selenium C# Automation Framework using Visual Studio
- Create New Project
- 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.
- The default framework contains dependencies for NUnit and NUnit Test Adapter. One file with .cs extension will be created by default i.e UnitTest1.cs.
- NUnit Framework is the best for Selenium C#. NUnit provides a rich set of features for organizing and structuring tests, making it easy to write and maintain automated tests.
- 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
- Selenium Webdriver
- Selenium Support
- Configure/installed WebDriver - desired browser driver
- Tests require the desired browser driver. Testers require Chromedriver to run Chrome tests. Tests on Firefox require GeckoDriver, etc.
- Install ChromeDriver for Selenium
- Navigate to Solution Explorer in Visual Studio.
- Right click on the Project (ex: SeleniumUiTests)
Note: Right Click on the Project Name, not on the Solution Name. - Click Add.
- Click New Folder.
- Name the new folder drivers.
- Download Browser Drivers:
- Chrome: Download ChromeDriver matching your Chrome version.
- Firefox: Download GeckoDriver matching your Firefox version.
- Extract the zip folder to the desired project location:
- Create Test Class
- UnitTest1.cs is a default file in the project. Rename to EmployeesUiTests.cs.
- Double click on the EmployeesUiTests.cs file and write a few tests.