Selenium with Cucumber JS

Your guide to running Selenium Webdriver tests with Cucumber JS on BrowserStack.

CucumberJS repo on Github


BrowserStack gives you instant access to our Selenium Grid of 2000+ real devices and desktop browsers. Running your Selenium tests with Cucumber JS on BrowserStack is simple. This guide will help you:

  1. Run your first test
  2. Mark tests as pass / fail
  3. Debug your app


Before you can start running your Selenium tests with Cucumber JS, Install Cucumber JS using npm

# Install using npm

npm install cucumber

Run your first test

To understand how to integrate with BrowserStack, we will look at two things:

  1. A sample test case written in Cucumber JS with NodeJS
  2. Integration of this sample test case with BrowserStack

Sample Test Case

The sample Cucumber JS test case below searches for the string “BrowserStack” on Google, and checks if the title of the resulting page is “BrowserStack - Google Search”

// Google Feature
Feature: Google Search Functionality

  Scenario: Can find search results
    When I type query as "BrowserStack"
    Then I submit
    Then I should see title "BrowserStack - Google Search"

Once we have defined the feature file, which contains the test case, we now need to create the step definition. The step definition for the Cucumber JS test case shown above is:

// Google Steps
module.exports = function() {

  this.When(/^I type query as "([^"]*)"$/, function (searchQuery, next) {
    this.driver.findElement({ name: 'q' })
      .sendKeys(searchQuery + '\n').then(next);

  this.Then(/^I submit$/, function (next) {
    var self = this;
    this.driver.findElement({ name: 'btnG' })
      .then(function() {
        self.driver.wait(function () {
          return self.driver.isElementPresent("top_nav"));
        }, 5000);

  this.Then(/^I should see title "([^"]*)"$/, function (titleMatch, next) {
      .then(function(title) {
        assert.equal(title, titleMatch, next, 'Expected title to be ' + titleMatch);

Now that we have created the feature file and the step definition, we are ready to integrate this Cucumber JS test case into BrowserStack.

Integrating with BrowserStack

Note: Running your Selenium tests on BrowserStack requires a BrowserStack Username and Access Key.

To obtain your username and access keys, sign up for a Free Trial or purchase a plan.

Now that we have created a feature file and step definitions, we can integrate our Cucumber JS test case into BrowserStack. We now create a hooks.js and env.js file, as shown below:

// hooks.js
var config_file = '../../conf/' + (process.env.CONFIG_FILE || 'single') + '.conf.js';
var config = require(config_file).config;

var username = process.env.BROWSERSTACK_USERNAME || config.user;
var accessKey = process.env.BROWSERSTACK_ACCESS_KEY || config.key;

var createBrowserStackSession = function(config, caps){
  return new webdriver.Builder().

var myHooks = function () {
  var bs_local = null;

  this.Before(function (scenario, callback) {
    var world = this;
    var task_id = parseInt(process.env.TASK_ID || 0);
    var caps = config.capabilities[task_id];
    caps['browserstack.user'] = username;
    caps['browserstack.key'] = accessKey;

      // Code to start browserstack local before start of test and stop browserstack local after end of test
      bs_local = new browserstack.Local();
      bs_local.start({'key': accessKey }, function(error) {
        if (error) return console.log(;

        world.driver = createBrowserStackSession(config, caps);
    else {
      world.driver = createBrowserStackSession(config, caps);

  this.After(function(scenario, callback){
      else callback();

module.exports = myHooks;
// env.js
var configure = function () {
    this.setDefaultTimeout(60 * 1000);

module.exports = configure;

We now need to create a config file, which contains the BrowserStack Hub URL and credentials required to connect to the BrowserStack Selenium grid.

exports.config = {
  user: 'YOUR_USERNAME',
  server: '',

  capabilities: [{
    browserName: 'chrome',

We are now ready to run the test on BrowserStack, using the following command:

CONFIG_FILE=single ./node_modules/cucumber/bin/cucumber.js features/single.feature

Mark tests as pass / fail

BrowserStack provides a comprehensive REST API to access and update information about your tests. Shown below is a sample code snippet which allows you to mark your tests as pass or fail based on the assertions in your Cucumber JS test cases.

var request = require("request");
request({uri: "<session-id>.json", method:"PUT", form:{"status":"completed","reason":""}})

A full reference of our REST API can be found here.

Debug your app

BrowserStack provides a range of debugging tools to help you quickly identify and fix bugs you discover through your automated tests.

  • Text Logs

Text Logs are a comprehensive record of your test. They are used to identify all the steps executed in the test and troubleshoot errors for the failed step. Text Logs are accessible from the Automate dashboard or via our REST API.

  • Visual Logs

Visual Logs automatically capture the screenshots generated at every Selenium command run through your Cucumber JS tests. Visual logs help with debugging the exact step and the page where failure occurred. They also help identify any layout or design related issues with your web pages on different browsers.

Visual Logs are disabled by default. In order to enable Visual Logs you will need to set browserstack.debug capability to true.

capabilities: [{
  'browserstack.debug': true
  • Video recording

Every test run on the BrowserStack Selenium grid is recorded exactly as it is executed on our remote machine. This feature is particularly helpful whenever a browser test fails. You can access videos from Automate Dashboard for each session. You can also download the videos from the Dashboard or retrieve a link to download the video using our REST API.

Note: Video recording increases test execution time slightly. You can disable this feature by setting the capability to false.

To disable video recording, add the following code snippet:

capabilities: [{
  '': false

In addition to these logs BrowserStack also provides Raw logs, Network logs, Console logs, Selenium logs, Appium logs and Interactive session. Complete details to enable all the debugging options can be found here.

We're sorry to hear that. Please share your feedback so we can do better

Contact our Support team for immediate help while we work on improving our docs.

We're continuously improving our docs. We'd love to know what you liked

Thank you for your valuable feedback

Is this page helping you?


We're sorry to hear that. Please share your feedback so we can do better

Contact our Support team for immediate help while we work on improving our docs.

We're continuously improving our docs. We'd love to know what you liked

Talk to automation expert