Skip to main content
Triggering Actions With Hooks

Learn how to use hooks to setup and teardown state and trigger actions

Alan Parkinson avatar
Written by Alan Parkinson
Updated over 3 years ago

When creating automated acceptance tests we need to manage a lot different items outside of the executed steps, such as:

  • Setting up frameworks

  • Connecting to environments

  • Setting up and tearing down state

We may need to carry out one or more of these actions sometimes before/after each scenario is executed or before/after the whole test run begins. Fortunately we can manage these actions using hooks that are triggered either or before/after scenarios.

Before hooks

Before hooks are useful when you have to manage repetitive steps for example setting up specific data that will be used with a series of automated acceptance tests. By adding in the actions you want to carry out before the scenario is triggered it will ensure that those actions are completed before the scenario starts.

Java

@Before
public void setup(){
  // Do something before each scenario
}

Ruby

Before do
  # Do something before each scenario
end 

JavaScript

var {Before} = require('cucumber');

// Synchronous
Before(function () {
  // Do something before each scenario
});

After hooks

Just as Before hooks are triggered before a scenario is run, After hooks run straight after the scenario. This can be useful for removing data or resetting the state of the application you are interacting with. This is useful because then you can assume your application is a specific state at the start of every scenario that is run.

Java

@After
public void teardown(){
  // Do something after each scenario
}

Ruby

After do
  # Do something after each scenario
end 

JavaScript

var {After} = require('cucumber');

// Synchronous
After(function () {
  // Do something after each scenario
});

Before all

The examples above for Before hooks are used when you want to execute actions before and after each scenario. But what if you want to execute an action just once before all scenarios are run? By adding in a boolean we can allow the hook to run once and then prevent it from running again. Like so:

Java

private boolean runHook = true;

@Before
public void setup(){
  if(runHook){
    // Do something before all scenarios

    runHook = false;
  }
}

Ruby

runHook = true;

Before do
  if runHook
    # Do something before all scenarios
    runHook = true;
  end
end 

JavaScript

var {Before} = require('cucumber');
var runHook = true;

// Synchronous
Before(function () {
  if(runHook){
    // Do something before all scenarios
    runHook = false;
  }
});

Hooks and tags

We can also assign specific hooks to run depending on which tags you want to run. For example, in the hooks below they will only run if we set the tag @tag1 as a tag to run:

Java

@Before("@tag1")
public void runForTag1Only(){
  // Do something
}

Ruby

Before('@tag1') do
  # Do something after each scenario
end 

JavaScript

var {Before} = require('cucumber');

// Synchronous
Before({tags : "@tag1"}, function () {
  // Do something after each scenario
});
Did this answer your question?