Table of Contents
Getting Started
Introduction
In this series, we will set up an automation framework from scratch to test against the frontend of a web application.
The completed repository can be found at https://gitlab.com/testifyqa/CSharp-Selenium-Web
We will use the following in our test framework:
- Selenium WebDriver
- Selenium is a portable software-testing framework for web applications. Selenium provides a playback (formerly also recording) tool for authoring tests without the need to learn a test scripting language (Selenium IDE). It also provides a test domain-specific language (Selenese) to write tests in a number of popular programming languages, including C#, Groovy, Java, Perl, PHP, Python, Ruby and Scala. The tests can then run against most modern web browsers. Selenium deploys on Windows, Linux, and macOS platforms. It is open-source software, released under the Apache 2.0 license: web developers can download and use it without charge.
- We will set up WebDrivers for automation in Google Chrome and Mozilla Firefox
- Selenium is a portable software-testing framework for web applications. Selenium provides a playback (formerly also recording) tool for authoring tests without the need to learn a test scripting language (Selenium IDE). It also provides a test domain-specific language (Selenese) to write tests in a number of popular programming languages, including C#, Groovy, Java, Perl, PHP, Python, Ruby and Scala. The tests can then run against most modern web browsers. Selenium deploys on Windows, Linux, and macOS platforms. It is open-source software, released under the Apache 2.0 license: web developers can download and use it without charge.
- SpecFlow
- Use SpecFlow to define, manage and automatically execute human-readable acceptance tests in .NET projects. Writing easily understandable tests (in Gherkin language) is a cornerstone of the BDD paradigm and also helps build up a living documentation of your system.
- NUnit
- NUnit is a unit-testing framework for all .Net languages. Initially ported from JUnit, the current production release, version 3, has been completely rewritten with many new features and support for a wide range of .NET platforms.
Setup / Installation
Setting up Visual Studio Code
- Download Visual Studio Code for your Operating System from https://code.visualstudio.com/download
- Complete installation of Visual Studio Code on your machine
Add the C# Extension to Visual Studio Code
- Launch Visual Studio Code on your machine
- Click the Extensions icon (the 4 squares with one square being slightly separated)
- Search for ‘C#’ and click the green ‘Install’ button
Enable ability to launch Visual Studio Code from the command line
- Make sure Visual Studio Code is launched
- Open the command palette (CMD/Ctrl + SHIFT + P)
- Type in ‘shell’
- Select the option “Shell command: Install ‘code’ command in PATH”
You can now launch Visual Studio Code from the directory you are in from Terminal / CMD by entering the following command…
code .
Installing .NET Core SDK
.NET Core SDK is a set of libraries and tools that allow developers to create . NET Core applications and libraries. We require it to create our test automation framework in C#
- Download the .NET Core SDK Installer for your Operating System from https://dotnet.microsoft.com/download/dotnet-core/3.1
- Launch the installer once downloaded and follow any instructions to install it
- Verify DotNet installed successfully by opening a CMD / Terminal window and entering the following command…
dotnet --version
Creating an Automation Test Project in Visual Studio Code
- Open up a Terminal window
- change directory in to a suitable folder (e.g. Documents)…
cd Documents
- Make a new directory for your project and then change directory in to it…
mkdir csharp-selenium-framework
cd csharp-selenium-framework
- Launch Visual Studio Code from the command line in the current project directory…
code .
- Click ‘Terminal’ -> ‘New Terminal’ to display the Terminal window integrated within Visual Studio Code
- Enter the following command to add a new NUnit project…
dotnet new nunit --name ProductAutomation
- Next, let’s add our Solution file (again, name it something suitable) by entering the following command in the integrated terminal again…
dotnet new sln --name ProductAutomationSolution
- Finally, let’s add our NUnit project to our solution…
dotnet sln add ProductAutomation
Installing Visual Studio Code Extensions
- With Visual Studio Code open, click the Extensions icon
- Search for ‘SpecFlow’ and click ‘Install’ on “Specflow Tools”
- Next, search for ‘Power Tools’ and click the green ‘Install’ button
- Next, search for ‘Cucumber’ and install ‘Cucumber (Gherkin) Full Support’
- Next, search for ‘.NET Core test Explorer’ and click the green ‘Install’ button
Installing NuGet Packages
- In the Terminal window integrated within Visual Studio Code, enter the following commands for each package to install it successfully…
NUnit Packages
NUnit Test Adapter
dotnet add ProductAutomation/ProductAutomation.csproj package NUnit3TestAdapter
Selenium Packages
Chrome WebDriver
dotnet add ProductAutomation/ProductAutomation.csproj package Selenium.WebDriver.Chrome
Firefox WebDriver
dotnet add ProductAutomation/ProductAutomation.csproj package Selenium.WebDriver.GeckoDriver
Selenium Support
dotnet add ProductAutomation/ProductAutomation.csproj package Selenium.Support
Selenium WebDriver
dotnet add ProductAutomation/ProductAutomation.csproj package Selenium.WebDriver
SpecFlow Packages
SpecFlow
dotnet add ProductAutomation/ProductAutomation.csproj package SpecFlow
SpecFlow Assist Dynamic
dotnet add ProductAutomation/ProductAutomation.csproj package SpecFlow.Assist.Dynamic
SpecFlow.NUnit
dotnet add ProductAutomation/ProductAutomation.csproj package SpecFlow.NUnit
SpecFlow.Tools.MsBuild.Generation
dotnet add ProductAutomation/ProductAutomation.csproj package SpecFlow.Tools.MsBuild.Generation
Building the Framework
- In the Terminal window integrated within Visual Studio Code, enter the following command to build the project and all of its dependencies…
dotnet build
- Once the build has run successfully, go in to one of the generated .cs files (e.g. Program.cs) and click in to a line of code
- You should get a prompt to add required assets that are missing for C#…
- Click ‘Yes’ and it will add some necessary asset files and folders (e.g. ‘.vscode’ folder)
- You should get a prompt to add required assets that are missing for C#…