Hooks

Creating the Namespace and Classes

In this part of the series, we will add our Before and After hooks to perform certain actions before and after our test scenarios are run.

  1. Right-click on the CodedUI Test project in the Solution Explorer, select ‘Add’ –> ‘New Folder’ and name it ‘Hooks’
  2. Right-click on the ‘Hooks’ folder, select ‘Add’ –> ‘Class’ and add the following classes to the namespace (the ‘Hooks’ namespace will contain classes for all the different hooks we can run before and/or after test runs, features and/or scenarios)…
    • FeatureHooks.cs
      • (This is where all our hooks that happen before and/or after all our scenarios in a feature file have run, will be kept)
    • ScenarioHooks.cs
      • (This is where all our hooks that happen before and/or after each individual scenario, will be kept)
    • ScenarioBlockHooks.cs
      • (This is where all our hooks that happen before and/or after each logical block, e.g. all ‘Given’ steps, all ‘When’ steps, or all ‘Then’ steps, will be kept)
    • StepHooks.cs
      • (This is where all our hooks that happen before and/or after each individual step in a scenario, will be kept)
    • TestRunHooks.cs
      • (This is where all our hooks that happen before and/or after an entire test run, will be kept)

Writing the Actual Hooks

In this blog post, I will only be giving examples for ‘FeatureHooks’, ‘ScenarioHooks’ and ‘TestRunHooks’ as a starting place, but I do encourage you to do your own research and add extra hooks in the other class files if your project could find use for them

Editing the ‘TestRunHooks.cs’ class file

  1. Open up the ‘TestRunHooks.cs’ class file and edit the file to match like below, so that playback is initialised before our test run, and also that the playback is cleaned up after our test run…

Editing the ‘ScenarioHooks.cs’ class file

  1. Open up the ‘ScenarioHooks.cs’ class file and edit the file to match like below, so that the Application Under Test (AUT) is launched before a scenario is run, and that the AUT is closed down after a scenario is run, when we use a SpecFlow tag on a scenario…

Editing the ‘FeatureHooks.cs’ class file

  1. Open up the ‘FeatureHooks.cs’ class file and edit the file to match like below, so that the Application Under Test (AUT) is launched before a feature is run, and that the AUT is closed down after a feature is run, when we use a SpecFlow tag on at the top of a Feature file…

    We have now added some Hooks to our Test Framework.  In the next part, we will create our first test scenario 😀
Digiprove sealCopyright secured by Digiprove © 2018
Liked it? Take a second to support Thomas on Patreon!