Test Automation: Running Cucumber feature files using Node.js

Cucumber.js is a Node.js library used for automation. Cucumber.js helps to test our site’s features using pure JavaScript and the Selenium WebDriver.

It is powerful library to setup your testing environment compared to java which requires you to install various packages and jars which seems to be hectic and takes time and sometimes prone to incompatibilities.

But node’s npm manages everything here.

The most interesting thing is that selenium-cucumber-js comes with the selenium driver for Node.js, as well as a reporting mechanism by default. No external setup is required for reporting and selenium driver.

What are features?

Features are a file with a .feature extension which has multiple scenarios.

What are Scenarios?

Scenarios are plain English statements which completely state what the feature is and what it is supposed to perform. They are actions taken by the user and the outcome of these actions in the form of a response from the website.

Let us see how we can execute a feature file using cucumber.js

Step 1: Initialize node.js project

$ npm init

Step 2: Create following folders shown below

step-definitions: step-definitions are normal JS files with all the feature testing logic inside. We can use any assertion library like Chai, Assert, etc

page-objects – page objects are global objects accessible in your feature files for using common functions. It is accessible inside your feature file as a page object. Page objects with a separate file are stored as a key in the page object, for example, google-search.js will be available as apage.googleSearch.url, and so on. Remember to use camelCase for key names.

reports: the reports folder is only used by the library for storing the reports

features: we place all the feature files here

Step 3: Install following packages:

$ npm install selenium-cucumber-js –save

$ npm install chai –save

Step 4: Creating a feature file

Our feature file will be testing google search name google-search.feature

feature file(google-search.feature)

Step 5: Creating a Page Object file

Create a file called google-search.js in your page-objects directory.

Step-definition file will access this file.

Page Object File(google-search.js)

Step 6: Creating Step-Definition file

Step-defs file(google-search-steps.js)

Step 6: Configure npm test

Configure npm test (This runs a package’s “test” script, if one was provided.) command in package.json

Add test script location

Edit package.json file

Step 7: Run the test

$ npm test

run $npm test

Step 8: Check Reports in Reports folder

Check reports folder

Step 9: Open HTML report file browser to see test results

Test Results(cucumber-report.html)

So that’s a short project for quickly getting started with selenium-cucumber-js.