Codementor Events

BDD Web Automation 04: Hooks and TimeOut

Published Sep 05, 2018Last updated Mar 03, 2019
BDD Web Automation 04: Hooks and TimeOut

The following are previous posts in this series:

  1. Create and Run the First Sample
  2. Cucumber Feature File Syntax and Editing
  3. Introducing Tags, Scenario, Scenario Outlines, Background

In the last post, we explained the differences between different types of scenarios that can be used when filling a feature file. Today we will introduce the execution part of Cucumber.

Content

  1. Hooks introduction
  2. Custom hooks
  3. "Skip" run settings
  4. TimeOut Introduction
  5. Custom TimeOut
  6. Disable TimeOut

Hooks introduction

Hooks (hooks) are used to add operations before and after each scenario. For example, in the web automation test, before a scenario runs, a browser window can be opened and may also be maximized. After the scenario run, we can take a screenshot of the web page. We can define all these actions in hooks.

In Cucumber there are 4 kind of hooks:

  1. BeforeAll
  2. Before
  3. After
  4. AfterAll

BeforeAll

Perform operations before any scenario, such as opening a browser and maximizing the window. Each BeforeAll function will only be executed once.

const {BeforeAll} = require('cucumber');

BeforeAll(async function(){
    await driver.manage().window().maximize();
})

Before

Before each scenario runs, such as clearing the browser cookie before performing operations

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

Before(async function () {
    await driver.manage().deleteAllCookies();
})

After

Operations after each scenario is run, such as taking a screenshot after performing an operation

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

After(async function () {
    //After Scenario Hook

    //capture screenshot after each scenario
    let screenshot = await driver.takeScreenshot();
    this.attach(screenshot, 'image/png');
});

AfterAll

After all the scenarios have been run, such as closing the browser instance after the run.

const {AfterAll} = require('cucumber')

AfterAll(function () {
    //perform some shared teardown
    return driver.quit();
})

Custom hooks

The hook function can set tags to apply to, therefore apply to a set of scenarios that match the tag expression. The following shows the example:

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

Before(function () {
  // This hook will be executed before all scenarios
});

Before({tags: "@foo"}, function () {
  // This hook will be executed before scenarios tagged with @foo
});

Before({tags: "@foo and @bar"}, function () {
  // This hook will be executed before scenarios tagged with @foo and @bar
});

Before({tags: "@foo or @bar"}, function () {
  // This hook will be executed before scenarios tagged with @foo or @bar
});

// You can use the following shorthand when only specifying tags
Before("@foo", function () {
  // This hook will be executed before scenarios tagged with @foo
});

In the last post, we talked about how to use tag expressions in filter scenarios to run, here the hooks is another place that tag expressions can be used.

"Skip" run settings

If some scenarios need to be skipped, just return "skipped" in the hooks. The usage is as follows:

Before("@foo", function() {
  // perform some runtime check to decide whether to skip the proceeding scenario
  return 'skipped';
});

TimeOut introduction

Some step may take longer than expected to return, or some step function never return, both of these cases are highly likely to indicate errors in application or in automation script. A timeout setting exists in Cucumber to handle these situation, and the default timeout is 5 second.

However, there are cases that the step may take longer than 5 seconds to execute, and you don’t want Cucumber to report error when running it. You can then change the default global timeout by calling the setDefaultTimeout method.

const {setDefaultTimeout} = require('cucumber');

setDefaultTimeout(60 * 1000);

Custom timeout

If you change the default timeout, it will affect all hooks and step functions. If you only want to apply it to a particular hook or step function, you can define a timeout for some hooks or step functions.

const {Before, Given} = require('cucumber');

Before({timeout: 60 * 1000}, function() {
  // Does some slow browser/filesystem/network actions
});

Given(/^a slow step$/, {timeout: 60 * 1000}, function() {
  // Does some slow browser/filesystem/network actions
});

Disable timeout

Under normal circumstances, it is not recommended for everyone to use

In some special scenarios, you may need to disable the timeout period, or wait indefinitely for execution to complete. This can be done by setting {timeout:-1}.

const {Before, Given} = require('cucumber');
const Promise = require('bluebird');

Given('the operation completes within {n} minutes', {timeout: -1}, function(minutes) {
  const milliseconds = (minutes + 1) * 60 * 1000
  const message = `operation did not complete within ${minutes} minutes`
  return Promise(this.verifyOperationComplete()).timeout(milliseconds, message);
});

For more information about hook, read Hooks in Cucumber.js document.

Previous Post: 03. Introducing Tags, Scenario, Scenario Outlines, Background
Next Post: 05. Create Data Driven Automation Script with Doc String & Data Table
First Post: 01. Create and Run the First Sample

Discover and read more posts from CukeTest
get started
post commentsBe the first to share your opinion
Show more replies