Framework Structure

NOTE: If you have followed the C# Selenium Blog Series, then you can also skip this blog post and simply do the following only…

  • Add an ‘Apis’ folder (namespace) to the project root
    • Add the following class file in the ‘Apis’ folder (namespace)…
      • BaseApiTests.cs
    • Add a ‘Models’ subfolder (nested namespace) within the ‘Apis’ folder
      • Add the following class file in the ‘Models’ folder (nested namespace)…
        • HomeTimeline.cs
    • Add an ‘ApiSteps’ subfolder (nested namespace) in the ‘Steps’ folder
      • Add the following class file in the ‘ApiSteps’ folder (namespace)…
        • BaseApiScenariosSteps.cs
  • Add a sub-directory in the ‘Features’ folder called ‘ApiFeatures’
    • Add the following file in the ‘ApiFeatures’ directory…
      • BaseApiScenarios.feature

If you are instead starting from scratch, please continue reading below.

Directory Structure

  1. In the Solution Explorer, right-click on the project name and select ‘Add’ –> ‘New Folder’
    • Name the folder ‘Features’
  2. Repeat the above steps and add the following top-level folders…
    • Apis
    • Steps
    • Utils
  3. Add the following sub-folders to the ‘Utils’ directory…
    • Hooks
    • Settings
  4. Add the following sub-folder to the ‘Apis’ directory…
    • Models

Initial Files in each Directory

Features namespace

  1. Right-click the ‘Features’ folder and select ‘Add’ –> ‘New Item’
  2. In the window that appears, select ‘SpecFlow Feature File’
  3. Add the following ‘Feature’ file…
    • BaseApiScenarios.feature

APIs namespace

  1. Right-click on the ‘Apis’ folder, select ‘Add’ –> ‘Class’ and add the following classes to the namespace (the ‘Apis’ namespace will contain all our class files for our RESTful API tests)…
    • BaseApiTests.cs

Models nested namespace

  1. Right-click on the ‘Models’ folder, select ‘Add’ –> ‘Class’ and add the following class to the nested namespace (the ‘Models’ nested namespace will contain all our class files for the keys and values in JSON responses that we want to deserialise and assert against)…
    1. HomeTimeline.cs

Steps namespace

  1. Right-click on the ‘Steps’ folder, select ‘New’ –> ‘Class’ and add the following classes to the namespace (the ‘Steps’ namespace will contain all our class files for our step definitions, the glue that connects our SpecFlow scenarios to our C# test code)…
    • BaseApiScenariosSteps.cs

Utils namespace

Hooks nested namespace

  1. Right-click on the ‘Hooks’ folder, select ‘New’ –> ‘Class’ and add the following classes to the nested namespace (the ‘Hooks’ namespace will contain classes for all the different hooks we can run before and/or after test runs/features/scenarios etc.)…
    • FeatureHooks.cs
    • ScenarioHooks.cs

Settings nested namespace

  1. Right-click on the ‘Settings’ folder, select ‘New’ –> ‘Class’ and add the following classes to the nested namespace (the ‘Settings’ namespace will contain a class file for all our settings, e.g. our BaseUrl and any login credentials / parameters we might need to use in our tests. Due to the fact these shouldn’t ever really change, we can set everything in the Settings class to be ‘public static readonly‘)…
    1. Settings.cs

GitIgnore

We will now add a .gitignore file to ignore certain files and packages if/when we push to a remote repository.

Good practice is to have all of our NuGet packages listed in the ‘packages.config’ file and then git ignore the actual NuGet packages folders. When the project is cloned from the remote repository, NuGet Package Restore will be run when we build the solution and will restore all the packages that we need for us.

Additionally, we will git ignore the SpecFlow designer code.  All our SpecFlow Feature files auto-generate designer code, which we can also exclude from the remote repository. When the project is cloned from the remote repository, we can right-click on the project in the Solution Explorer (in Visual Studio) and select ‘Regenerate Feature Files’ 🙂

Finally, we will ignore Visual Studio specific folders that are not needed.

  1. Open up a text/file editor program like Notepad++ or Sublime Text (google, download and install if need be)
  2. Add the following into a blank file…
    # NuGet Packages
    *.nupkg
    # The packages folder can be ignored because of Package Restore
    **/packages/*
    # except build/, which is used as an MSBuild target.
    !**/packages/build/
    # Uncomment if necessary however generally it will be regenerated when needed
    #!**/packages/repositories.config
    
    # SpecFlow specific
    *.feature.cs
    *.feature.xlsx.*
    *.Specs_*.html
    
    # Visual Studio temp something
    .vs/
  3. Save the file as ‘.gitignore’ in your project root (where the .sln file exists)
Digiprove sealCopyright secured by Digiprove © 2018
Liked it? Take a second to support Thomas on Patreon!

Previous Article

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.