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.

Pre-Requisites:

Cucumber has two basic components known as features and scenarios.

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

n1
$ npm init

Step 2: Create following folders shown below

n2

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

n3

Step 4: Creating a feature file

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

n4.PNG
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.

n5
Page Object File(google-search.js)

Step 6: Creating Step-Definition file

n6
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

n7
Edit package.json file

Step 7: Run the test

$ npm test

n8
run $npm test

Step 8: Check Reports in Reports folder

n9
Check reports folder

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

n10
Test Results(cucumber-report.html)

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

Hope you all found it interesting .

Try it yourself!

Let me know of any suggestions in the comments!

For official docs check here

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s