Nodejs Api request for cucumber test cases

To test the Nodejs API so please create the API in Nodejs.

Suppose you created a customer register API.


http://127.0.0.1:3333/api/register

Now, pass the requested data


   {  
         "first_name":"John",
	 "last_name":"Taylor",
	 "email":"john@gmail.com",
	 "password":"john123",
	 "cpass":"john123"
   }

Where first_name, last_name, email, password and cpass are request parameters.

Now, get the response


{  
    "status": 200,
    "message": "Customer Registered successfully."
}

To write the Cucumber test cases for Nodejs API in through the below steps

1) Firstly create a features folder in the root folder of your Nodejs project.

2) Now create the file name create_customer_via_jsonfile.feature extension and write the below code in this


# Create Customer API Testing Exercise via json file
Feature: Create new customer signup through json file
 Call API to create new customer through json file

  Scenario: Create new customer through json file
  
  Given create new customer through json file and data are
   """
   {  
         "first_name":"John",
	 "last_name":"Taylor",
	 "email":"john@gmail.com",
	 "password":"john123",
	 "cpass":"john123"
   }
  """

  When I send URL through POST method for JSON file http://127.0.0.1:3333/api/register

  Then response data is 
  """
  {  
    "status": 200,
    "message": "Customer Registered successfully."
   }
  """

  

in the create_customer_via_jsonfile.feature file, you have to create Feature: feature_name after that create the Scenario: scenario_name
after that, you have to create 3 steps
Given:- It is used for request data
When:- it is used for the condition
Then:- It is used for response data

3) Now create the create_customer_via_jsonfile_step folder and create create_customer_via_jsonfile_step.jsfile into this and put the below code.


// features/support/steps.js
const { Given, When, Then } = require("cucumber");
const assert = require("assert")
const got = require('got');

var output;
var jsonfile_body_request;


Given(/^create new customer through json file and data are$/, async function(request_data){
  jsonfile_body_request=request_data;
});

When('I send url through POST method for json file {}', async function(url) {

  const {body} = await got.post(url, {
		json: JSON.parse(jsonfile_body_request),
		responseType: 'json'
	});

  output={  
  "status": (body.status),
  "message": body.message
  }


});

Then(/^response data is$/, async function (expected_output) {
  var main_expected_output= JSON.parse(expected_output);
  assert.equal(JSON.stringify(output), JSON.stringify(main_expected_output));
});


4) Now, run the test cases


./node_modules/.bin/cucumber-js

Now, check the results and found the 1 scenarios, and 3 steps are passed.

Nodejs dynamic request for cucumber test cases

To write the dynamic request for cucumber test cases in Nodejs through the below steps

1) Firstly create a features folder in the root folder of your Nodejs project.

2) Now create the file name dynamic_req_calculation.feature extension and write the below code in this


# features/dynamic_req_calculation.feature

Feature: Dynamic request to add the variable
  
  I want to check the variable result

  Scenario Outline: dynamic request addition calculation
  Given a variable set to 
  When I increment the variable by 
  Then the variable should contain 

    Examples:
      | variable | increment_variable | result |
      | 10       |    5               |  15    |
      | 25       |    30              |  55    |
      | 100      |    10              |  110   |

in the dynamic_req_calculation.feature file, you have to create Feature: feature_name after that create the Scenario: scenario_name
after that, you have to create 3 steps
Given:- It is used for request data
When:- it is used for the condition
Then:- It is used for response data

Now define the dynamic request value in the Examples: like variable, increment_variable and the result will show the addition of these variables (variable+increment_variable).

3) Now create the calculation_support folder and create two files into this.

1) firstly create a world.js file which has below code


// features/support/world.js
const { setWorldConstructor } = require("cucumber");

class CustomWorld {
  constructor() {
    this.variable = 0;
  }

  setTo(number) {
    this.variable = number;
  }

  incrementBy(number) {
    this.variable += number;
  }

  multiplyBy(number) {
    this.variable *= number;
  }
}

setWorldConstructor(CustomWorld);

2) Now create the step.js file and write the below code in this.


// features/support/steps.js
const { Given, When, Then } = require("cucumber");
const assert = require("assert").strict

Given("a variable set to {int}", function(number) {
  this.setTo(number);
});

When("I increment the variable by {int}", function(number) {
  this.incrementBy(number);
});

Then("the variable should contain {int}", function(number) {
  assert.equal(this.variable, number);
});

3) Now, run the test cases


./node_modules/.bin/cucumber-js

Now, check the results and found the 3 scenarios and 9 steps are passed.

Nodejs simple request for cucumber test cases

To write the simple request for cucumber test cases in Nodejs through the below steps

1) Firstly create a features folder in the root folder of the Nodejs project.

2) Now create the file name simple_calculation.feature extension and write the below code in this


# features/simple_calculation.feature

Feature: Simple math calculation
  
  I want to check the increment variable result

  Scenario: easy math calculations
    Given a variable set to 4
    When I increment the variable by 5
    Then the variable should contain 9

in the simple_calculation.feature file create Feature: feature_name after that create the Scenario: scenario_name
after that, you have to create 3 steps
Given:- It is used for request data
When:- it is used for the condition
Then:- It is used for response data

3) Now, run the test cases


./node_modules/.bin/cucumber-js

Now, check the results 1 scenario and 3 steps are passed.

Install cucumber tool in Nodejs

To install and set up Cucumber in a Node.js project, you can follow the steps below. Cucumber is a tool for running automated tests written in plain language (Behavior-Driven Development, or BDD). It helps in writing test cases in a way that can be understood by non-technical stakeholders.

Firstly open the terminal after that move to the root folder of your Nodejs Project then write the below command.


npm install --save-dev cucumber

After installing the cucumber then you can check the package name in the package.json file.


  "devDependencies": {
    "cucumber": "^7.0.0-rc.0"
  }

After installing the package, firstly you have to create a features folder at the root of your project.
after creating the folder you can create a file with .feature extension.
like mytest.feature

Run the cucumber test cases

Now write the test cases and run the test cases through the below command


./node_modules/.bin/cucumber-js

What is Cucumber?

Cucumber is a software testing tool and nowadays it is famous in the market because it is based on BDD (Behavior Driven Development). Cucumber testing software runs automated acceptance tests written in a behavior-driven development (BDD) style.
It has a stable release on 13 July 2018.
It is basically written in Ruby programming language. Cucumber uses Gherkin language to define the test cases. It is used for cross platform and you can use this tool in many programming languages like Java, .Net, Nodejs, PHP, etc.

What is Behavior Driven Development?

It encourages collaboration between everyone involved in developing software like developers, testers, and business representatives such as product owners or business analysts.

What is the goal of BDD?

the main goal of BDD is to improve the collaboration between all stakeholders involved in developing software like developers, testers, and business representatives such as product owners or business analysts and form a shared understanding among them.

Cucumber has two basic components known as features and scenarios.

What are features?

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

What are Scenarios?

Scenarios are plain English statements that 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.

Features file has 3 sections.
Given – These steps are used to set up the initial state before you do your test.
When – These steps are the actual test that is to be executed.
Then – These steps are used to assert the outcome of the test.

Example:- to create the file name simple_calculation.feature extension and write the below code in this


# features/simple_calculation.feature

Feature: Simple math calculation
  
  I want to check the increment variable result

  Scenario: easy math calculations
    Given a variable set to 4
    When I increment the variable by 5
    Then the variable should contain 9