How to implement hooks in specflow c#

In this Sepcflow tutorial, Let’s have a deep dive to understand hooks in Specflow C#. We will see step by step implementation of hooks and uses of the hooks during the BDD automation in Specflow.

The process will be the same for any BDD framework Like Specflow or cucumber the only difference is with programming language syntax. Check examples of more real-time Automation problems in Specflow c#. 

Before starting the implementation, let’s discuss what is hooks and why we need hooks in BDD automation with Specflow c#

What is Hooks in Specflow C#

Hooks in Specflow c# are used to manage the automation workflow. Hooks can add logic at a specific time. Like before every step or after every step. We need to add the “[binding]” attribute in order to create a hooks class.

There are multiple tags available the will help us to inject the logic at a specific time. In below code will see an example of the “[AfterTestRun]” attribute.

Example: In this below code. We are going to close and quit the browser after the complete execution of all the test cases.

[AfterTestRun]
      public static void AfterTestRun()
      {
          DriverContext.Driver.Close();
          DriverContext.Driver.Dispose();
      }

List of attributes in hooks

  • BeforeTestRun / AfterTestRun
  • BeforeFeature / AfterFeature
  • BeforeScenario / AfterScenario
  • BeforeScenarioBlock / AfterScenarioBlock
  • BeforeStep / AfterStep

Let’s see one by one with a real-time example

[BeforeTestRun] [AfterTestRun]

 As the name suggests “BeforeTestRun” will trigger before the test run and “AfterTestRun” will trigger after complete test run.

So In case you need to Initialize browser driver or want to open the browser you can use the “[BeforeTestRun]”. Similarly, If you want to close or quite a web browser after the complete suite run. You can use “[AfterTestRun]”.

Example:

[BeforeTestRun]
       public static void BeforeTestRun()
       {
           DriverContext.Initialize();
           Page.Initialize();
       }

[AfterTestRun]
       public static void AfterTestRun()
       {
           DriverContext.Driver.Close();
           DriverContext.Driver.Dispose();
       }

[BeforeFeature] [AfterFeature]

“[BeforeFeature]” Will trigger before the execution of every feature file and in a similar way “[AfterFeature]” after the execution of a feature file.

Example and Syntax:

 [BeforeFeature]
        public static void BeforeFeature()
        {
  //Enter your code logic………….
}
 
 [AfterFeature]
        public static void AfterFeature()
        {
            //Enter your code logic………….
 
 }

[BeforeScenario] [AfterScenario]

        After and before every scenario or test cases. It will execute before your text cases and after test case.

Example and Syntax:

 [BeforeScenario]
        public static void BeforeScenario()
        {
  //Enter your code logic………….
}
 
 [AfterScenario]
        public static void AfterScenario()
        {
            //Enter your code logic………….
 
 }

 [BeforeScenarioBlock] [AfterScenarioBlock]

Before and after of every block step Like for example if you are going to write a step with GIVEN and continue the same step with multiple AND so It will be a block. I guess block is confusing you let have a look of block code.

ScenarioBlock

Given User is on Login Page
And User enter username
And User enter password

Example and Syntax:

 [BeforeScenarioBlock]
        public static void AfterScenarioBlock()
        {
  //Enter your code logic………….
}
 
 [AfterScenarioBlock]
        public static void AfterScenarioBlock()
        {
            //Enter your code logic………….
 
}

[BeforeStep] [AfterStep]

BeforeStep and AfterStep will trigger after every step of the Scenario.

Example and Syntax:

 [BeforeStep]
        public static void BeforeStep()
        {
  //Enter your code logic………….
}
 
 [AfterStep]
        public static void  AfterStep()
        {
            //Enter your code logic………….
 
}

Implement hooks in Specflow

 Create a class “Hooks” in your framework and add the “[binding]” attribute to bind it. Below is the complete implementation of the hooks class. Then in next step will check a real-time example to generate the extent report using hooks.

[Binding]
    public sealed class Hooks
    {
 
        [BeforeTestRun]
        public static void BeforeTestRun()
        {
          //Enter code before testrun
        }
 
 
        [BeforeFeature]
        public static void BeforeFeature()
        {
            //Enter code before feature
        }
 
        [BeforeScenario]
        public void BeforeScenario()
        {
             //Enter code before Scenario
        }
 
           [BeforeScenarioBlock]
        public void BeforeScenarioBlock()
        {
//Enter code before Scenario block
        }
      
 [BeforeStep]
        public void BeforeStep()
        {
          //Enter code before Step
        }
 
 
        [AfterStep]
        public void AfterStep()
        {
          //Enter code .. After Step
        }
 
          [AfterScenarioBlock]
        public void AfterScenarioBlock()
        {
//Enter code .. After Scenario block
        }
      
 
        [AfterScenario]
        public void AfterScenario()
        {
           //Enter code .. After Scenario
        }
 
[AfterFeature]
        public static void AfterFeature()
        {
            //Enter code .. After Feature

        }
 
 
        [AfterTestRun]
        public static void AfterTestRun()
        {
            DriverContext.Driver.Close();
            DriverContext.Driver.Dispose();
        }
    }

 Create an extent report in selenium with SpecFlow and NUnit using HOOKS.

Specflow tutorial in detail

Leave a Reply

Your email address will not be published.

two × two =