Reputation: 1876
I have an AfterFeatures hook that I'm using to try to gracefully shut down an expressjs web server that is used for testing only. In this hook, I need to call the visit method, which has been added to World, but I apparently don't have access to World from within this hook. What can I do to gain access to things in World inside this and other hooks?
// features/support/after_hooks.js
var myAfterHooks = function () {
this.registerHandler('AfterFeatures', function (event, callback) {
this.visit('/quit', callback);
});
};
module.exports = myAfterHooks;
Upvotes: 4
Views: 1309
Reputation: 2276
I do not think you can. In the AfterFeatures the cucumber process has already finished, so this no longer references it.
But, if all you want is to visit a page, you can register your browser outside cucumber so that it is still accessible from the AfterFeatures hook. If you are using AngularJS + Protractor, Protractor handles the browser for you, and therefore it is still accessible in the AfterFeatures hook. It would be the same principle. This can be done with the following.
hooks.js
var myHooks = function () {
this.registerHandler('AfterFeatures', function (event, callback) {
console.log('----- AfterFeatures hook');
// This will not work as the World is no longer valid after the features
// outside cucumber
//this.visit('/quit', callback);
// But the browser is now handled by Protractor so you can do this
browser.get('/quit').then(callback);
});
};
module.exports = myHooks;
world.js
module.exports = function() {
this.World = function World(callback) {
this.visit = function(url) {
console.log('visit ' + url);
return browser.get(url);
};
callback();
};
}
The AfterFeatures example in the cucumber-js GitHub repository is a little misleading, as it looks like you can access the driver that you previously registered in the World. But if you are using pure cucumber-js only, I have not seen that work.
By the way, you can use just this instead of registerHandler.
this.AfterFeatures(function (event, callback) {
browser.get('/quit').then(callback);
});
Hope this helps.
Upvotes: 2