Skip to content

Instantly share code, notes, and snippets.

@lucasconstantino
Last active March 14, 2016 18:48
Show Gist options
  • Save lucasconstantino/269e24094898832068f3 to your computer and use it in GitHub Desktop.
Save lucasconstantino/269e24094898832068f3 to your computer and use it in GitHub Desktop.
Helper method to simulate beforeAll behavior for tests using protractor.
/**
* @file
* Protractor beforeAll implementation as helper.
*/
var history = [];
/**
* BeforeAll implementation.
*/
function beforeAll(callback) {
(callback.length ? beforeAllAsync : beforeAllSync).call(null, callback);
}
/**
* BeforeAll Synchronous: used when there is no "done" argument
* defined in the callback.
*/
function beforeAllSync(callback) {
// Avoid registering multiple beforeEach callbacks.
if (!~history.indexOf(callback)) {
beforeEach(function () {
// Only execute if never executed before.
if (!~history.indexOf(callback)) {
history.push(callback);
callback();
}
});
}
}
/**
* BeforeAll Asynchronous: used when there is a "done" argument
* defined in the callback.
*/
function beforeAllAsync(callback) {
// Avoid registering multiple beforeEach callbacks.
if (!~history.indexOf(callback)) {
beforeEach(function (done) {
// Only execute if never executed before.
if (!~history.indexOf(callback)) {
history.push(callback);
callback(done);
}
});
}
}
module.exports = beforeAll;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment