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
  • 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

  1. Download Visual Studio Code for your Operating System from https://code.visualstudio.com/download
  2. Complete installation of Visual Studio Code on your machine

Add the C# Extension to Visual Studio Code

  1. Launch Visual Studio Code on your machine
  2. Click the Extensions icon (the 4 squares with one square being slightly separated)
  3. Search for ‘C#’ and click the green ‘Install’ button

Enable ability to launch Visual Studio Code from the command line

  1. Make sure Visual Studio Code is launched
  2. Open the command palette (CMD/Ctrl + SHIFT + P)
    1. Type in ‘shell’
    2. 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#

  1. Download the .NET Core SDK Installer for your Operating System from https://dotnet.microsoft.com/download/dotnet-core/3.1
  2. Launch the installer once downloaded and follow any instructions to install it
  3. 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

  1. Open up a Terminal window
  2. change directory in to a suitable folder (e.g. Documents)…
    cd Documents
  3. Make a new directory for your project and then change directory in to it…
    mkdir csharp-selenium-framework
    cd csharp-selenium-framework
  4. Launch Visual Studio Code from the command line in the current project directory…
    code .
  5. Click ‘Terminal’ -> ‘New Terminal’ to display the Terminal window integrated within Visual Studio Code
  6. Enter the following command to add a new NUnit project…
    dotnet new nunit --name ProductAutomation
  7. 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
  8. Finally, let’s add our NUnit project to our solution…
    dotnet sln add ProductAutomation

Installing Visual Studio Code Extensions

  1. With Visual Studio Code open, click the Extensions icon
  2. Search for ‘SpecFlow’ and click ‘Install’ on “Specflow Tools”
  3. Next, search for ‘Power Tools’ and click the green ‘Install’ button
  4. Next, search for ‘Cucumber’ and install ‘Cucumber (Gherkin) Full Support’
  5. Next, search for ‘.NET Core test Explorer’ and click the green ‘Install’ button

Installing NuGet Packages

  1. 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

  1. In the Terminal window integrated within Visual Studio Code, enter the following command to build the project and all of its dependencies…
    dotnet build
  2. 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
    1. You should get a prompt to add required assets that are missing for C#…
    2. Click ‘Yes’ and it will add some necessary asset files and folders (e.g. ‘.vscode’ folder)
Digiprove sealCopyright secured by Digiprove © 2018
Liked it? Take a second to support Thomas on Patreon!

Next Article

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.