[cucumber-js] access World in hooks

2,011 views
Skip to first unread message

Jhonny E.

unread,
May 26, 2014, 7:46:53 AM5/26/14
to cu...@googlegroups.com
I have a BeforeScenario method which initializes some value. I want to use that value in my step definitions.
Naturally, I want to use the World construct for that.
How can I do that?
using this.something = x inside my BeforeScenario function doesn't work (this.something is undefined in the step definitions).
thanks
J

Artur Kania

unread,
May 26, 2014, 10:27:26 AM5/26/14
to cu...@googlegroups.com
BeforeScenario is a event - you can listen to it by defining your own listeners ( cucumberInstance.attachListener({ BeforeScenario : function () {});
To execute code before every scenario use Before ( this.Before ) - " Before hooks To run something before every scenario, use before hooks: "https://github.com/cucumber/cucumber-js )

var myHooks = function () {
  this.Before(function(callback) {
    // Just like inside step definitions, "this" is set to a World instance.
    // It's actually the same instance the current scenario step definitions
    // will receive.

    // Let's say we have a bunch of "maintenance" methods available on our World
    // instance, we can fire some to prepare the application for the next
    // scenario:

    this.bootFullTextSearchServer();
    this.createSomeUsers();

    // Don't forget to tell Cucumber when you're done:
    callback();
  });
};

module.exports = myHooks;

Jhonny E.

unread,
May 26, 2014, 10:48:06 AM5/26/14
to cu...@googlegroups.com
I think you misunderstood my question. I do know how to define listeners.
My issue is that if. in my 'Before' method, I set some value on World (i.e 'this.value = 5'), and I later want to read that value in my step definition code, then this.value is undefined.

Jhonny E.

unread,
May 27, 2014, 3:36:28 AM5/27/14
to cu...@googlegroups.com
Ho ok, re-read your answer, and I realized that there's a difference between this.BeforeScenario which I used (where this is not set to a world instance), and this.Before which you used, in which I have access to the world instance.
However now I face a different problem- I want to perform some operations based on which scenario is running. When using this.BeforeScenario, my callback would receive the event parameter which can be used to get that info.
BUT with the this.Before notation, the event parameter isn't passed.

So I guess my question would be- How can I execute code before each scenario with access to both the world instance and to the scenario data.

Artur Kania

unread,
May 27, 2014, 4:29:13 AM5/27/14
to cu...@googlegroups.com
so it looks like you need to add one more extra param to callback fn - according to doc 

You can access the scenario currently being run by adding a parameter to your function:

this.Before(function (scenario, callback) {
  console.log(scenario.getName(), "(" + scenario.getUri() + ":" + scenario.getLine() + ")");
  callback();
});

See Cucumber.Ast.Scenario for more information about the scenario object.


cheers

Jhonny E.

unread,
May 27, 2014, 7:44:57 AM5/27/14
to cu...@googlegroups.com
Yes, I thought of that myself, but, as I wrote, my Before function is being passed only the callback parameter.
Here's my code:
this.Before(function(scenario, callback){
        console
.log('now before scenario. world is',this,', scenario is ', scenario,', callback is ',callback);



And the output is:
now before scenario. world is {
  takeScreenShot: [Function],
  getSingleValueDatatable: [Function],
  expectMultipleMembers: [Function] } , scenario is  function () {
          iterate();
        } , callback is  undefined

So that means that only the callback parameter is being passed.
I'm using cucumberjs 0.4.0

Julien Biezemans

unread,
May 27, 2014, 4:45:13 PM5/27/14
to cu...@googlegroups.com
On Tue, May 27, 2014 at 1:44 PM, Jhonny E. <jhonny....@sisense.com> wrote:
Yes, I thought of that myself, but, as I wrote, my Before function is being passed only the callback parameter.
Here's my code:
this.Before(function(scenario, callback){
        console
.log('now before scenario. world is',this,', scenario is ', scenario,', callback is ',callback);



And the output is:
now before scenario. world is {
  takeScreenShot: [Function],
  getSingleValueDatatable: [Function],
  expectMultipleMembers: [Function] } , scenario is  function () {
          iterate();
        } , callback is  undefined

So that means that only the callback parameter is being passed.
I'm using cucumberjs 0.4.0



I tried and could access the scenario object in a before hook:


Are you 100% sure you're running 0.4.0?
 

--
Posting rules: http://cukes.info/posting-rules.html
---
You received this message because you are subscribed to the Google Groups "Cukes" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cukes+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Julien.

Founder, Cucumber Ltd.

Jhonny E.

unread,
May 28, 2014, 2:58:12 AM5/28/14
to cu...@googlegroups.com


On Tuesday, May 27, 2014 11:45:13 PM UTC+3, Julien Biezemans wrote:



On Tue, May 27, 2014 at 1:44 PM, Jhonny E. <jhonny....@sisense.com> wrote:
Yes, I thought of that myself, but, as I wrote, my Before function is being passed only the callback parameter.
Here's my code:
this.Before(function(scenario, callback){
        console
.log('now before scenario. world is',this,', scenario is ', scenario,', callback is ',callback);



And the output is:
now before scenario. world is {
  takeScreenShot: [Function],
  getSingleValueDatatable: [Function],
  expectMultipleMembers: [Function] } , scenario is  function () {
          iterate();
        } , callback is  undefined

So that means that only the callback parameter is being passed.
I'm using cucumberjs 0.4.0



I tried and could access the scenario object in a before hook:


Are you 100% sure you're running 0.4.0?

I was sure, but I was wrong. On closer inspection it turned out I had 0.3.3 globally installed :(.
Anyway, with 0.4.0 it works well.
Thanks for that, and for all the great work on cucumberJS.
J

 

Julien Biezemans

unread,
May 28, 2014, 4:02:36 AM5/28/14
to cu...@googlegroups.com
On Wed, May 28, 2014 at 8:58 AM, Jhonny E. <jhonny....@sisense.com> wrote:


On Tuesday, May 27, 2014 11:45:13 PM UTC+3, Julien Biezemans wrote:



On Tue, May 27, 2014 at 1:44 PM, Jhonny E. <jhonny....@sisense.com> wrote:
Yes, I thought of that myself, but, as I wrote, my Before function is being passed only the callback parameter.
Here's my code:
this.Before(function(scenario, callback){
        console
.log('now before scenario. world is',this,', scenario is ', scenario,', callback is ',callback);



And the output is:
now before scenario. world is {
  takeScreenShot: [Function],
  getSingleValueDatatable: [Function],
  expectMultipleMembers: [Function] } , scenario is  function () {
          iterate();
        } , callback is  undefined

So that means that only the callback parameter is being passed.
I'm using cucumberjs 0.4.0



I tried and could access the scenario object in a before hook:


Are you 100% sure you're running 0.4.0?

I was sure, but I was wrong. On closer inspection it turned out I had 0.3.3 globally installed :(.
Anyway, with 0.4.0 it works well.

Ha-ha! I'm glad you got it sorted out.
 
Thanks for that, and for all the great work on cucumberJS.

Thanks, I appreciate it :)
Reply all
Reply to author
Forward
0 new messages