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…
    using Microsoft.VisualStudio.TestTools.UITesting;
    using TechTalk.SpecFlow;
    
    namespace ProductAutomation.Hooks //replace ProductAutomation with name of your CodedUI Test Project
    {
        [Binding]
        public sealed class TestRunHooks
        {
            //Before & After hooks and handlers
            [BeforeTestRun]
            public static void InitializePlayback()
            {
                Playback.Initialize();          
            }
    
            [AfterTestRun]
            public static void CleanupPlayback()
            {
                Playback.Cleanup();
            }
        }
    }

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…
    using Microsoft.VisualStudio.TestTools.UITesting;
    using TechTalk.SpecFlow;
    
    namespace ProductAutomation.Hooks //replace ProductAutomation with name of your CodedUI Test Project
    {
        [Binding]
        public sealed class ScenarioHooks
        {
            public static ApplicationUnderTest App;
    
            //Before & After hooks and handlers
            [BeforeScenario]
            public static void LaunchApplication()
            {
                if (ScenarioContext.Current.ScenarioInfo.Tags.Contains("App"))
                {
                    App = ApplicationUnderTest.Launch("\\path\\to\\exe"); //change path to the path of your exe file (or use an environment variable). If using the Sample C# Calculator, the exe can be found at "<location you unzipped to>//MyCalculatorv1//bin//Debug//MyCalculatorv1.exe"
            }
    
            [AfterScenario]
            public static void QuitApplication()
            {
                if (ScenarioContext.Current.ScenarioInfo.Tags.Contains("App"))
                {
                    App.Close();
                }
            }
        }
    }

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…
    using Microsoft.VisualStudio.TestTools.UITesting;
    using TechTalk.SpecFlow;
    
    namespace ProductAutomation.Hooks //replace ProductAutomation with name of your CodedUI Test project
    {
        [Binding]
        public sealed class FeatureHooks
        {
            public static ApplicationUnderTest App;
    
            //Before & After hooks and handlers
            [BeforeFeature]
            public static void LaunchApplication()
            {
                if (FeatureContext.Current.FeatureInfo.Tags.Contains("App"))
                {
                    App = ApplicationUnderTest.Launch("\\path\\to\\exe");//change path to the path of your exe file (or use an environment variable). If using the Sample C# Calculator, the exe can be found at "<location you unzipped to>//MyCalculatorv1//bin//Debug//MyCalculatorv1.exe"
                }
            }
    
            [AfterFeature]
            public static void QuitApplication()
            {
                if (FeatureContext.Current.FeatureInfo.Tags.Contains("App"))
                {
                    App.Close();
                }
            }
        }
    }

    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!

Previous Article

Next Article