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.