wdio-cucumber-framework icon indicating copy to clipboard operation
wdio-cucumber-framework copied to clipboard

Step retries lose world context

Open si-mikey opened this issue 8 years ago • 6 comments

The Problem

When a retry is kicked off the functions that were added to the World are no longer available on. They are available in the first run ( as retry has not been kicked off) when the retry happens it looks like it removes the World context from the step definitions.

// worldjs
const expect = require('chai').expect;
const fixedUsers = require('../../../lib/fixtures/fixture.users');
const routes = require('../../../config/dist/routes');
const Brain = require('../../../lib/tools/dist/brain');
const users = require('../../../lib/api/dist/users');

function World () {
  this.fixedUsers = fixedUsers;
  this.expect = expect;
  this.routes = routes;
  this.brain = new Brain();
  this.users = users;
};

module.exports = function () {
  this.World = World;
};
//step defs
this.Given(/^some step definition$/, { retry: 2 }, () => {
        // Access to this.expect and others on runs run, 
        // when retry happens because something failed 
        // then the methods from world are undefined
    })

wdio-cuucmber-framework version: 0.2.5 wdio version: 4.2.11 cucumber version: 1.2.2

si-mikey avatar Oct 07 '16 16:10 si-mikey

Happened for me, too. Any workaround for this one?

shkaper avatar Oct 18 '16 14:10 shkaper

The line below seems to be related https://github.com/webdriverio/wdio-cucumber-framework/blob/004a1a25a2eae80788dd8ee50fc03abe171a42d3/lib/adapter.js#L83

si-mikey avatar Oct 18 '16 15:10 si-mikey

Any updates?

si-mikey avatar Mar 06 '17 21:03 si-mikey

@shkaper the work around I found for this was to store any function / class instances in some custom object inside of the browser object. You would do this in a beforeScenario hook so it gets refreshed on every scenario. So something like this,

    beforeScenario: function (scenario) {
      browser['tools'] = {};
      browser['tools']['stuffInstance'] = new Stuff();
    }

This will ensure you never use the below in your step defs.

this.stuffInstance...

Instead

browser['tools']['stuffInstance'].someMethod

si-mikey avatar Mar 09 '17 15:03 si-mikey

Can someone provide a reproducible example?

christian-bromann avatar Mar 11 '17 16:03 christian-bromann

@christian-bromann below is code to reproduce the error.
world.js

function fooBar() {
   this.getValue = function() {
     return  'FooBar';
   }
}

function World () {
   this.fooBar = new fooBar();
};

module.exports = function () {
  this.World = World;
};

step_defs.js

this.Given(/^I call a world function with a retry$/, { retry: 1 }, function() {
   var foobar = this.fooBar.getValue();
   console.log(foobar);
   // Triggger a failure so retry is ran
  throw new Error('BOOM!');
})

test.feature

Feature: Retry test suite

 Scenario: retrying step def should retain world context
   Given I call a world function with a retry

The above code will run fine the first time, the second time because of retry 1

 this.fooBar.getValue();

Will return undefined. More specifically this.fooBar is undefined.

si-mikey avatar Mar 13 '17 15:03 si-mikey