2

Hi in the below protractor-cucumber step-definition, I can modify the step definition variable a from before hook:

    "use strict"; 
        let { Given, Before } = require('cucumber');        
        let a=10;
       
        Before(async function (scenario) {
          a=12;  
        });        
        
        Given('I navigates to google', async () => {
          console.log(a)
        });

But if I declare before hook in a different file say, hook.js:

    'use strict';
    
    var { After, Before } = require('cucumber');
    
    Before(async function (scenario) {
        let a=12;  
      });

and call it from spec in protractor config:

    cucumberOpts: {
        require: ['./testsuites/test1.js','./commons/hooks.js'],

The step definition variable a is not getting affected, as the scope of 'a' in hook is local:

Is there a way to change the step definition variables from hook.js without needing to declare the variable as global?

Update:

I tried below code:

Created world.js

    const { setWorldConstructor } = require('cucumber')
    
    
    function CustomWorld() {
      this.a=1
    }
    
    setWorldConstructor(CustomWorld)

Call it from hooks.js

This prints `CustomWorld { a: 1 }`

    'use strict';
    var {  Before } = require('cucumber');
    
    Before(async function (scenario) {
        console.log("hooks")
        console.log(this)
    });

Call the same from step definition:

This prints empty property `{}`

    "use strict";
    console.log("feature head")
    console.log(this)
    let { Given } = require('cucumber');
    
    Given('I navigates to google', async () => {
      console.log("Given nav")
      console.log(this)
    });

Update:

Call the same from step definition:

Here I removed arrow function and this prints the World object properly'

    "use strict";
    console.log("feature head")
    console.log(this)
    let { Given } = require('cucumber');
    
    Given('I navigates to google', async function() {
      console.log("Given nav")
      console.log(this)
    });
1

In CucumberJS you can use a setWorldConstructor where you can create for example a this.context which can we used in each steps and hooks I think.

For an example code look here: https://github.com/cucumber/cucumber-js/blob/0b2d7e468cf10ecef37749efaddd2dfbea8c7808/features/generator_step_definitions.feature

Other reads:

7
  • Hi i am able to get the world constructor in the hooks.js but not in steps . In steps if i print console.log(this) i get empty property
    – Learner
    Jan 21 '20 at 14:53
  • Could you see the update question
    – Learner
    Jan 21 '20 at 14:58
  • 1
    The other read suggests you use the arrow notation in the hooks. I do not have a setup to reproduce your situation, but worst case you need to setup a global variable and clean it in the before. Personally I do not see to much issues with that. Jan 21 '20 at 15:46
  • 1
    It worked, I removed arrow function in step definition and it is working !!! thank you for pointing it out.
    – Learner
    Jan 21 '20 at 16:10
  • Using World isn't thread safe, is it? What if another thread is running the same step and modifies the variable? Mar 10 at 13:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.