I'm trying out CucumberJS with Selenium and PhantomJS. I've successfully built a World object using this StackOverflow answer as a guide.
So now I'm testing out some basic step definitions, but having some confusion about how to execute the callback at the end of the step. This works great:
module.exports = function () {
this.World = require("../support/world.js").World;
this.Given(/^I am visiting Google$/, function (callback) {
this.driver.get('http://www.google.com')
.then(function() {
callback();
});
});
};
The driver hits Google.com and the callback isn't fired until after the requested document is loaded. But I find this syntax to be a little wordy, so I thought maybe I could just pass callback straight to the then() after my first promise, like so:
module.exports = function () {
this.World = require("../support/world.js").World;
this.Given(/^I am visiting Google$/, function (callback) {
this.driver.get('http://www.google.com')
.then(callback);
});
};
This, however fails, and seems to console.log the callback. Here's the output:
Scenario: Googling # features/theGoogle.feature:6
Given I am visiting Google # features/theGoogle.feature:7
[object Object]
(::) failed steps (::)
[object Object]
What's going on here? I was expecting that callback could simply be passed to the then() function and executed after the promise is fulfilled. Why would wrapping it in an anonymous function make it work?