Skip to main content

Cucumber JS Testing – Running Cucumberjs Automation Scripts On LambdaTest Selenium Grid

LambdaTest Selenium Automation Grid is a cloud-based Selenium testing platform which enables you to run your automation scripts on 2000+ different browsers and operating systems. You can now run your JavaScript Selenium automated test cases on a scalable Selenium infrastructure that is running real browsers and real operating systems.

This post will help you in getting started with configuring and running your JavaScript based automation test scripts on LambdaTest Selenium cloud platform using Cucumber js framework. This topic will help you to:

  • Run a sample Selenium Webdriver test using Cucumber js framework on LambdaTest Automation.
  • Comprehend and configure Selenium test suite with respect to your capabilities.
  • Select the specific browsers to run your test on.
  • Run your test cases in parallel to reduce build times.
  • Test your locally hosted pages.

Prerequisites To Run Cucumber.js Testing With Selenium#

All the code samples in this documentation can be found in the CucumberJS LambdaTest Repository on Image GitHub. You can either download or clone the repository to quickly run your tests.

Before getting started with Automated Scripts using Selenium with Cucumber js framework on LambdaTest Automation, you need to:

  • The first step is to download and install node.js and node package manager or npm. You should be having nodejs v6 or newer. Click here to download.
  • Make sure to use the latest version of JavaScript.
  • Make sure you have Cucumber js framework installed in your system, you can install it using the below command through npm:

npm install cucumber

  • Download Selenium JavaScript bindings from the official Selenium website.
  • Once you download the JavaScript bindings, extract the ZIP file which you’ve downloaded. After extracting the file, you need to add Selenium JavaScript bindings which is a JAR file and all the dependent libraries to your classpath.
  • Download LambdaTest tunnel binary file if you wish to test your locally hosted or privately hosted projects.

Installing Selenium Dependencies For Node.js#

Next step is to install Selenium dependencies for Node.js using npm. Here’s the command to run:

npm i selenium-webdriver

LambdaTest Authentication Credentials#

Be aware of your LambdaTest authentication credentials i.e. your LambdaTest username, access key and HubURL. You need to set them up as your environment variables. You can retrieve them from your LambdaTest automation dashboard by clicking on the key icon near the help button.

  • For Windows user:
  • For macOS/Linux user:

Basic Setup To Run Your Cucumber js Testing On LambdaTest#

First, you need to clone the code from our GitHub repository for Cucumber js framework. You can use this repository to find all the code that we will be covering in this topic.

//nodejs-cucumber-todo/features/todo.featureFeature: Automate a website    Scenario: perform click events      When visit url ""      When field with name "First Item" is present check the box      When field with name "Second Item" is present check the box      When select the textbox add "Let's add new to do item" in the box      Then click the "addbutton"      Then I must see title "Sample page -"

Now create step definition file.

//nodejs-cucumber-todo/features/step_definitions/todo.js/*This file contains the code which automate the sample app.It reads instructions form feature file and find matchingcase and execute it.*/  'use strict'; const assert = require('cucumber-assert');const webdriver = require('selenium-webdriver'); module.exports = function() {   this.When(/^visit url "([^"]*)"$/, function (url, next) {    this.driver.get('').then(next);  });   this.When(/^field with name "First Item" is present check the box$/, function (next) {      this.driver.findElement({ name: 'li1' })      .click().then(next);  });   this.When(/^field with name "Second Item" is present check the box$/, function (next) {      this.driver.findElement({ name: 'li3' })      .click().then(next);  });   this.When(/^select the textbox add "([^"]*)" in the box$/, function (text, next) {      this.driver.findElement({ id: 'sampletodotext' }).click();      this.driver.findElement({ id: 'sampletodotext' }).sendKeys(text).then(next);  });   this.Then(/^click the "([^"]*)"$/, function (button, next) {    this.driver.findElement({ id: button }).click().then(next);  });   this.Then(/^I must see title "([^"]*)"$/, function (titleMatch, next) {    this.driver.getTitle()      .then(function(title) {        assert.equal(title, titleMatch, next, 'Expected title to be ' + titleMatch);      });  });};

Now create cucumber js framework runner file.

//nodejs-cucumber-todo/scripts/cucumber-runner.js#!/usr/bin/env node/*This is parallel test runner file.It creates child processes equals the number oftest environments passed.*/let child_process = require('child_process');let config_file = '../conf/' + (process.env.CONFIG_FILE || 'single') + '.conf.js';let config = require(config_file).config; process.argv[0] = 'node';process.argv[1] = './node_modules/.bin/cucumber-js'; const getValidJson = function(jenkinsInput) {    let json = jenkinsInput;    json = json.replace(/\\n/g, "");    json = json.replace('\\/g', '');    return json;}; let lt_browsers = null;if(process.env.LT_BROWSERS) {    let jsonInput = getValidJson(process.env.LT_BROWSERS);    lt_browsers = JSON.parse(jsonInput);} for( let i in (lt_browsers || config.capabilities) ){  let env = Object.create( process.env );  env.TASK_ID = i.toString();  let p = child_process.spawn('/usr/bin/env', process.argv, { env: env } );   p.stdout.pipe(process.stdout);}

Now, we need to add username and accesskey in cred.conf.js file located in conf folder. You can export your username and accesskey using 2 ways mentioned below.

exports.cred = {    username: process.env.LT_USERNAME || 'YOUR_USERNAME',    access_key: process.env.LT_ACCESS_KEY || 'YOUR_ACCESS_KEY'}

Or, you can simply export username and accesskey using the below command as well.


Executing Single Test Of Cucumber js Testing Framework With Selenium Grid#

If you wish to run a single test, execute the below command.

npm test

If you wish to set up your Cucumber js testing through Jenkins, then refer to our Jenkins documentation.

Executing Parallel Tests Of Cucumber js testing framework With Selenium Grid#

One of the most important feature of LambdaTest Selenium Grid is the ability to run your test cases in parallel. What that means is that if you have more than one concurrent session, you can run your test cases on more than one machine at a time, which greatly cuts down your test times. To put it in perspective, if you have 100 test cases each with an average run time of 1 minute, without parallel testing it would take 100 minutes to execute. However with 2 concurrent session, you can run 2 test cases in parallel at a time and can cut down the build’s test time to 50 minutes. With four concurrent session it would cut down to 25 minutes. With eight, well you got the picture :)

npm run parallel

Test Your Locally Hosted Cucumber js Tests With LambdaTest#

To help you perform cross browser testing of your locally stored web pages, LambdaTest provides an SSH(Secure Shell) tunnel connection with the name LambdaTest tunnel. With LambdaTest tunnel, you can execute a test of your locally hosted Cucumber js testing scripts on cloud to perform automated cross browser testing on 2000+ browsers offered by Selenium Grid on LambdaTest. So you make sure how well your changes look, even before your customers. Curious to know more about LambdaTest tunnel?

Follow our documentation on LambdaTest tunnel to know it all. OS specific instructions to download and setup tunnel binary can be found at the following links.

Download the binary file of:

Once, the tunnel is successfully set up. You can add the below code to your capabilities for testing internal servers on your network.

Tunnel Capabilities- tunnel: true

Fast track your automated cross browser testing process with Selenide + LambdaTest integration. Happy testing!

Last updated on