1

The approach I figure out is this

Given(
  `Step1`,
  async function() {
    const IwantToUseThisObj = {
      A: 'a',
      B: 'b'
    }

    this.IwantToUseThisObj = IwantToUseThisObj
  }
)

Then(`Step2`, async function() {
  IwantToUseThisObj = this.IwantToUseThisObj
})

but I am not sure it's the best practice or not, and if I need to use it a lot of time it looks very repetitive,

any better approach? I just want to use the value I use from the Given step

adiga
  • 34,372
  • 9
  • 61
  • 83
hrabal
  • 155
  • 1
  • 6

2 Answers2

0

Yes, storing data in the world class for reuse is best practice with cucumber

Ray
  • 1,134
  • 10
  • 27
0

The most reliable and accepted way of passing data between steps in a scenario is to use the scenario context or "world" object this. Here is an example:

Feature File

Feature: Passing data between steps

  Scenario: Passing data
    Given I set the value to "test"
    Then the value should be "test"

Step Definitions

const { Given, Then } = require('cucumber');
const assert = require('assert');


Given('I set the value to {string}', function (value) {
    this.value = value;
});

Then('the value should be {string}', function (value) {
    assert.ok(this.value === value);
});

Online Example: https://testjam.io/?p=cHsYgzrkRiI9dmkm1IyR

Steven Hunt
  • 2,321
  • 19
  • 18