Reputation: 2976
How to put contents into the World object before a scenario (e.g. in Before()
) in cucumber.js?
I want to inject a test context object and inject some initial values into it before the first step of the scenario.
But in the first step, this is not referring to the World object.
How can I access the World object in Before()
?
const { Given, Before } = require('cucumber');
Before((scenario) => {
const world = this;
world.put = 'hello';
world.myContext = {
fileName: null,
fileContent: null,
};
});
Given( /^step 1$/, { 90000 },
async function() {
const world = this;
console.log('step 1: world: ', world);
console.log('step 1: world.myContext: ', world.myContext);
});
Upvotes: 0
Views: 3320
Reputation: 514
As per this document, world can be accessed as 'this' in only world and hooks file- https://github.com/cucumber/cucumber-js/blob/master/docs/support_files/world.md. Also, 'this' will be a json object which expects key-value.
Create a seperate class called world.ts and add sample code like below:
import { setWorldConstructor } from 'cucumber';
const _defaultOptions = {
env: 'stage',
};
function World(input) {
this.World = input;
}
Object.assign(this, _defaultOptions);
}
setWorldConstructor(World);
Using this, if we call this.env in hooks file, you will get a value mapped above..
Upvotes: 1