UI Browser Automation with Selenium and .NET
Getting Started
This page explains how to setup a UI testing framework built with C# and Selenium.
Installing the template
To create a templated solution we can use the @amidostacks/scaffolding-cli npm package.
npx @amidostacks/scaffolding-cli run -i
Installation dependencies
We are using npx (Node Package Executor) to run the @amidostacks/scaffolding-cli.
Please ensure your environment has: node@12.
To download the supported version see nodejs.org.
Using the template on VSCode
It is recommended to use Visual Studio. Alternatively, VSCode has some custom plugins that can be configured to make debugging your tests easier.
For all downloads, see Visual Studio
Suggested VSCode extensions
- C# for Visual Studio Code (powered by OmniSharp)
- Cucumber (Gherkin) Full Support Extension for VSCode
- NXunit Test Explorer
Debugging in Visual Studio
Visual Studio will automatically attach to the Selenium process, using the XUnit Runner. Visual Studio also automatically discovers the Xunit tests for the project and displays them in the Test Explorer.
For more information on running and debugging in Visual Studio see: Run unit tests with test explorer
It's as simple as following these steps:
- Open the Solution in Visual Studio
- Open the test explorer from View > Test
- Right-click the test you wish to debug, and select Debug Test
- Set a breakpoint and start stepping through your code!
Building the solution
Please ensure you have the .NET Core 3.1 SDK installed.
MacOS/Linux: For Homebrew users, brew cask install dotnet-sdk
will install the long term support version.
Please install Visual Studio Code and ensure the extension C# for Visual Studio Code (powered by OmniSharp) is installed.
Restore the solution
In root (*.sln) directory, restore the dependencies the solution using: ) the solution:
dotnet restore
Run the tests
In root (*.sln) directory, build the solution using: ) the solution:
dotnet test
As a result of test execution - a 'BDDfy.html' test report will be automatically generated.
You don't need to run dotnet restore
because it's run implicitly by all commands that require a restore to occur, such as dotnet new, dotnet build and dotnet run._
Dependencies
- BDDfy is a simple and fully featured BDD framework that makes it easy to turn your unit tests into living documentation. It works with any testing framework and test runner. Documentation: TestStack.BDDfy
- XUnit is a free, open-source, community-focused unit testing tool for the .NET Framework. We will be configuring it to be used as a Selenium C# framework for Web UI automation testing. Documentation: XUnit
- Selenium C# Framework is an open-source test automation framework for automated cross-browser testing. It supports popular web browsers – Firefox, Chrome, Microsoft Edge, Internet Explorer, Safari, etc. To interact with the underlying web browser, a collection of language-specific bindings to drive the browser called Selenium WebDriver is used. Documentation: Selenium
- Selenium Webdriver allows for the interaction of the Selenium C# Framework with the web browser. Before you can make use of Selenium WebDriver commands in C#, the development environment should be setup i.e. Selenium WebDriver for the corresponding web browser should be installed on the machine. Documentation: Selenium Projects
- Selenium WebDriver ChromeDriver installs Chrome Driver (Win32, macOS, Linux64) for Selenium WebDriver. The chromedriver executable is not in the solution, but is copied into the output folder on the build process. This allows you to use Chrome as a default browser without having to install locally or in the pipeline, or commit a driver into the test repository. Documentation: Selenium Webdriver Chromedriver
- Shouldly is an assertion library. Think of it as the bit that enforces (and tests) the
Then
statement. Documentation: Shouldly - Microsoft Extensions Configuration is a provider used to build key/value-based configuration settings for use in an application. In this instance, builds the app with configured
appsettings.json
. Documentation: Microsoft Extensions Configuration
Selenium WebDriver Downloads
If you wish to use a local instance of Selenium Webdriver, be sure to download the Selenium WebDriver file for the browsers with which you wish to perform cross-browser testing from the links mentioned below: