It would we good to have an asynchronous called init hook.
I tried to implement a helper for the selenium-standalone package. Ideally it should use the _init hook of a helper. So my code looked like this:
const selenium = require('selenium-standalone');
'use strict';
class SeleniumHelper extends Helper {
_init() {
return selenium.install({logger: function(message) {
console.log(message);
}}, function() {
selenium.start(() => {console.log('blub')});
});
}
}
module.exports = SeleniumHelper;
The problem is here, that the _init hook is called synchronously in the lib/listener/helpers.js
event.dispatcher.on(event.all.before, function () {
runHelpersHook('_init');
});
This means, that the init will not wait for the selenium helper to be correctly initialized, so further steps will fails as selenium wasn't started. Also the beforeSuite step is not helping as it is also called synchronously. Only the before hook would work.
By changing the above dispatch event to
event.dispatcher.on(event.all.before, function () {
runAsyncHelpersHook('_init');
});
The init would wait for the initialization of the selenium standalone to complete. But as there may be code in the wild relying on synchronous initialization, you may introduce a new hook which is called asynchronously.
Somehow it's not working anymore now. So I'm still testing a little bit more to get it correctly working.
Somehow it's not working anymore now. So I'm still testing a little bit more to get it correctly working.
Yes, it failed for me now. The reason is that recorder, a singleton that records promise chains runs only inside a test. So init can't be async because recorder is not started at that point. However, I will try to think on how it can be implemented in different way
I also need this!
Looks like _init hook was removed in https://github.com/Codeception/CodeceptJS/commit/632dafb14c636d654da9e9f700a005441ae53df2 revision to fix https://github.com/Codeception/CodeceptJS/issues/1036 issue.
Most helpful comment
I also need this!