Skip to content
This repository has been archived by the owner on Jul 29, 2024. It is now read-only.

Commit

Permalink
feat(plugins): basic tools for adding plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
sjelin committed Nov 7, 2014
1 parent 6b58e51 commit 6a88642
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 6 deletions.
47 changes: 47 additions & 0 deletions lib/plugins.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
var q = require('q');

/**
* The plugin API for Protractor. Note that this API is extremely unstable
* and current consists of only two functions:
* <plugin>.setup - called before tests
* <plugin>.teardown - called after tests
* More information on plugins coming in the future
* @constructor
*/
var Plugins = function(config) {
this.pluginConfs = config.plugins || {};
this.pluginObjs = {};
for (var name in this.pluginConfs) {
this.pluginObjs[name] = require(this.pluginConfs[name].path);
}
};

var noop = function() {};

function pluginFunFactory(funName) {
return function() {
var promises = [];
for (var name in this.pluginConfs) {
var pluginConf = this.pluginConfs[name];
var pluginObj = this.pluginObjs[name];
promises.push((pluginObj[funName] || noop)(pluginConf));
}
return q.all(promises);
};
}

/**
* Sets up plugins before tests are run.
* @return {q.Promise} A promise which resolves when the plugins have all been
* set up.
*/
Plugins.prototype.setup = pluginFunFactory('setup');

/**
* Tears down plugins after tests are run.
* @return {q.Promise} A promise which resolves when the plugins have all been
* torn down.
*/
Plugins.prototype.teardown = pluginFunFactory('teardown');

module.exports = Plugins;
24 changes: 18 additions & 6 deletions lib/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ var protractor = require('./protractor'),
util = require('util'),
q = require('q'),
EventEmitter = require('events').EventEmitter,
helper = require('./util');
helper = require('./util'),
Plugins = require('./plugins');

/*
* Runner is responsible for starting the execution of a test run and triggering
Expand Down Expand Up @@ -191,9 +192,11 @@ Runner.prototype.run = function() {
var self = this,
driver,
specs,
testPassed;
testPassed,
plugins;

specs = this.config_.specs;
plugins = new Plugins(this.config_);

if (!specs.length) {
throw new Error('Spec patterns did not match any files.');
Expand Down Expand Up @@ -225,10 +228,12 @@ Runner.prototype.run = function() {
}).then(function() {
return driver.manage().timeouts()
.setScriptTimeout(self.config_.allScriptsTimeout);
// 3) Execute test cases
// 3) Setup globals and plugins
}).then(function() {
self.setupGlobals_.bind(self)(driver);

return plugins.setup();
// 4) Execute test cases
}).then(function() {
// Do the framework setup here so that jasmine and mocha globals are
// available to the onPrepare function.
var frameworkPath = '';
Expand All @@ -245,7 +250,14 @@ Runner.prototype.run = function() {
') is not a valid framework.');
}
return require(frameworkPath).run(self, specs);
// 4) Teardown
// 5) Teardown plugins
}).then(function(result) {
var deferred = q.defer();
plugins.teardown().then(function() {
deferred.resolve(result);
});
return deferred.promise;
// 6) Teardown
}).then(function(result) {
self.emit('testsDone', result);
testPassed = result.failedCount === 0;
Expand All @@ -258,7 +270,7 @@ Runner.prototype.run = function() {
} else {
return self.driverprovider_.teardownEnv();
}
// 5) Exit process
// 7) Exit process
}).then(function() {
var exitCode = testPassed ? 0 : 1;
return self.exit_(exitCode);
Expand Down

0 comments on commit 6a88642

Please sign in to comment.