Hooks in Specflow framework

These are block codes that run before and after each scenario, between each step definitions, before and after each scenario block , before and after test run in the feature file. This permits us to deal with the code work process better and assists with decreasing code excess.

Hooks can be characterized anyplace in the task or step definition layers utilizing the strategies [Before] and [After]. They are typically used for setup and tear-down of the environment then after the fact every situation.

Where we need Hooks - 

Hooks are often wont to perform background tasks that aren't a part of business functionality. Such tasks could be before the test and after a test like 

- Starting up a browser

- Setting or clearing cookies

- Connecting to a database

- Checking the state of the system

- Monitoring

- Navigating to certain page

- Logging out from the application

 

Hooks Attributes available in Specflow Framework
 

Scenario Hook :

Before scenario - It will run before each scenario

Syntax: [BeforeScenario] or [Before]

 

[Before]
public void BeforeScenarios()
{
}

 

After scenario - It will run after each scenario

Syntax: [AfterScenario] or [After]

 

[After]
public void AfterScenarios()
{
}
 

[Before] - Before hooks run before every scenario. This is often commonly used for prerequisite steps that require to be performed before the particular test scenario. For instance , this will be as follows.

1. Initialize a web driver

2. Establish DB connections

3. To set up test data

4. To set browser cookies

5. Navigate to default page

 

[After] -  It starts execution after the last stage of the scenario.

1. Quit the web driver

2. To close DB connections

3. To clear the test data/browser cookies

4. Sign out from the application

5. Take screenshots for fail/pass scenarios

 

Step Hook : 

Before Step - It will run before each step

Syntax: [BeforeStep]

 

[BeforeStep]
public void BeforeSteps()
{
    
}
 

After step - It will run after each step

Syntax: [AfterStep]

 

[AfterStep]
public void AfterSteps()
{
}
 
Scenario Block Hook:

To run before/after executing each scenario block (e.g. between the "givens" and the "whens")

 

[BeforeScenarioBlock]
public void BeforeScenarioBlocks()
{

}
[AfterScenarioBlock]
public void AfterScenarioBlocks()
{

}

 

Feature Hook:

Feature Hook will run before/after executing each feature and the method should be static.

 

[BeforeFeature]
public static void BeforeFeatureScenarios()
{
}
[AfterFeature]
public static void AfterFeatureScenarios()
{
}

 

Test Run Hook: 

Before Test Run and after Test Run is used and the method should be static.

 

[BeforeTestRun]
public static void BeforeTestRuns()
{
}
[AfterTestRun]
public static void AfterTestRuns()
{
}

 

Hooks.cs

 

using System;
using TechTalk.SpecFlow;
namespace SpecFlowProject1.StepDefinitions
{
    [Binding]
    public class AddCustomerStepDefinitions
    {
        [Before]
        public void BeforeScenarios()
        {
            Console.WriteLine("Before scenarios .................");
        }
        [After]
        public void AfterScenarios()
        {
            Console.WriteLine("After scenarios ..................");
        }
        [BeforeStep]
        public void BeforeSteps()
        {
            Console.WriteLine("Before step .................");
        }
        [AfterStep]
        public void AfterSteps()
        {
            Console.WriteLine("After step ..................");
        }
        [BeforeTestRun]
        public static void BeforeTestRuns()
        {
            Console.WriteLine("Before Test Runs .................");
        }
        [AfterTestRun]
        public static void AfterTestRuns()
        {
            Console.WriteLine("After Test Runs ..................");
        }
        [BeforeScenarioBlock]
        public void BeforeScenarioBlocks()
        {
            Console.WriteLine("Before scenario blocks .................");
        }
        [AfterScenarioBlock]
        public void AfterScenarioBlocks()
        {
            Console.WriteLine("After scenario blocks ..................");
        }
        [BeforeFeature]
        public static void BeforeFeatureScenarios()
        {
            Console.WriteLine("Before feature scenarios .................");
        }
        [AfterFeature]
        public static void AfterFeatureScenarios()
        {
            Console.WriteLine("After feature scenarios ..................");
        }
    }
}

 

 

Related Tutorials