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

Commit 6a88642

Browse files
committed
feat(plugins): basic tools for adding plugins
1 parent 6b58e51 commit 6a88642

File tree

2 files changed

+65
-6
lines changed

2 files changed

+65
-6
lines changed

lib/plugins.js

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
var q = require('q');
2+
3+
/**
4+
* The plugin API for Protractor. Note that this API is extremely unstable
5+
* and current consists of only two functions:
6+
* <plugin>.setup - called before tests
7+
* <plugin>.teardown - called after tests
8+
* More information on plugins coming in the future
9+
* @constructor
10+
*/
11+
var Plugins = function(config) {
12+
this.pluginConfs = config.plugins || {};
13+
this.pluginObjs = {};
14+
for (var name in this.pluginConfs) {
15+
this.pluginObjs[name] = require(this.pluginConfs[name].path);
16+
}
17+
};
18+
19+
var noop = function() {};
20+
21+
function pluginFunFactory(funName) {
22+
return function() {
23+
var promises = [];
24+
for (var name in this.pluginConfs) {
25+
var pluginConf = this.pluginConfs[name];
26+
var pluginObj = this.pluginObjs[name];
27+
promises.push((pluginObj[funName] || noop)(pluginConf));
28+
}
29+
return q.all(promises);
30+
};
31+
}
32+
33+
/**
34+
* Sets up plugins before tests are run.
35+
* @return {q.Promise} A promise which resolves when the plugins have all been
36+
* set up.
37+
*/
38+
Plugins.prototype.setup = pluginFunFactory('setup');
39+
40+
/**
41+
* Tears down plugins after tests are run.
42+
* @return {q.Promise} A promise which resolves when the plugins have all been
43+
* torn down.
44+
*/
45+
Plugins.prototype.teardown = pluginFunFactory('teardown');
46+
47+
module.exports = Plugins;

lib/runner.js

+18-6
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ var protractor = require('./protractor'),
33
util = require('util'),
44
q = require('q'),
55
EventEmitter = require('events').EventEmitter,
6-
helper = require('./util');
6+
helper = require('./util'),
7+
Plugins = require('./plugins');
78

89
/*
910
* Runner is responsible for starting the execution of a test run and triggering
@@ -191,9 +192,11 @@ Runner.prototype.run = function() {
191192
var self = this,
192193
driver,
193194
specs,
194-
testPassed;
195+
testPassed,
196+
plugins;
195197

196198
specs = this.config_.specs;
199+
plugins = new Plugins(this.config_);
197200

198201
if (!specs.length) {
199202
throw new Error('Spec patterns did not match any files.');
@@ -225,10 +228,12 @@ Runner.prototype.run = function() {
225228
}).then(function() {
226229
return driver.manage().timeouts()
227230
.setScriptTimeout(self.config_.allScriptsTimeout);
228-
// 3) Execute test cases
231+
// 3) Setup globals and plugins
229232
}).then(function() {
230233
self.setupGlobals_.bind(self)(driver);
231-
234+
return plugins.setup();
235+
// 4) Execute test cases
236+
}).then(function() {
232237
// Do the framework setup here so that jasmine and mocha globals are
233238
// available to the onPrepare function.
234239
var frameworkPath = '';
@@ -245,7 +250,14 @@ Runner.prototype.run = function() {
245250
') is not a valid framework.');
246251
}
247252
return require(frameworkPath).run(self, specs);
248-
// 4) Teardown
253+
// 5) Teardown plugins
254+
}).then(function(result) {
255+
var deferred = q.defer();
256+
plugins.teardown().then(function() {
257+
deferred.resolve(result);
258+
});
259+
return deferred.promise;
260+
// 6) Teardown
249261
}).then(function(result) {
250262
self.emit('testsDone', result);
251263
testPassed = result.failedCount === 0;
@@ -258,7 +270,7 @@ Runner.prototype.run = function() {
258270
} else {
259271
return self.driverprovider_.teardownEnv();
260272
}
261-
// 5) Exit process
273+
// 7) Exit process
262274
}).then(function() {
263275
var exitCode = testPassed ? 0 : 1;
264276
return self.exit_(exitCode);

0 commit comments

Comments
 (0)