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

Commit

Permalink
feat(plugins): support onPrepare in plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
sjelin committed Aug 18, 2016
1 parent 6350050 commit b05421e
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 5 deletions.
16 changes: 16 additions & 0 deletions docs/plugins.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,22 @@ Plugins are node modules which export an object with the following API:
*/
exports.setup = function() {};

/**
* This is called before the test have been run but after the test framework has
* been set up. Analogous to a config file's `onPreare`.
*
* Very similar to using `setup`, but allows you to access framework-specific
* variables/funtions (e.g. `jasmine.getEnv().addReporter()`)
*
* @throws {*} If this function throws an error, a failed assertion is added to
* the test results.
*
* @return {Q.Promise=} Can return a promise, in which case protractor will wait
* for the promise to resolve before continuing. If the promise is
* rejected, a failed assertion is added to the test results.
*/
explorts.onPrepare = function() {};

/**
* This is called after the tests have been run, but before the WebDriver
* session has been terminated.
Expand Down
17 changes: 17 additions & 0 deletions lib/plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,22 @@ export interface ProtractorPlugin {
*/
setup?: () => Q.Promise<any>;

/**
* This is called before the test have been run but after the test framework has
* been set up. Analogous to a config file's `onPreare`.
*
* Very similar to using `setup`, but allows you to access framework-specific
* variables/funtions (e.g. `jasmine.getEnv().addReporter()`)
*
* @throws {*} If this function throws an error, a failed assertion is added to
* the test results.
*
* @return {Q.Promise=} Can return a promise, in which case protractor will wait
* for the promise to resolve before continuing. If the promise is
* rejected, a failed assertion is added to the test results.
*/
onPrepare?: () => Q.Promise<any>;

/**
* This is called after the tests have been run, but before the WebDriver
* session has been terminated.
Expand Down Expand Up @@ -396,6 +412,7 @@ export class Plugins {
* @see docs/plugins.md#writing-plugins for information on these functions
*/
setup: Function = pluginFunFactory('setup', PromiseType.Q);
onPrepare: Function = pluginFunFactory('onPrepare', PromiseType.Q);
teardown: Function = pluginFunFactory('teardown', PromiseType.Q);
postResults: Function = pluginFunFactory('postResults', PromiseType.Q);
postTest: Function = pluginFunFactory('postTest', PromiseType.Q);
Expand Down
7 changes: 5 additions & 2 deletions lib/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export class Runner extends EventEmitter {
preparer_: any;
driverprovider_: DriverProvider;
o: any;
plugins_: Plugins;

constructor(config: Config) {
super();
Expand Down Expand Up @@ -76,7 +77,9 @@ export class Runner extends EventEmitter {
* are finished.
*/
runTestPreparer(): q.Promise<any> {
return helper.runFilenameOrFn_(this.config_.configDir, this.preparer_);
return this.plugins_.onPrepare().then(() => {
return helper.runFilenameOrFn_(this.config_.configDir, this.preparer_);
});
}

/**
Expand Down Expand Up @@ -268,7 +271,7 @@ export class Runner extends EventEmitter {
*/
run(): q.Promise<any> {
let testPassed: boolean;
let plugins = new Plugins(this.config_);
let plugins = this.plugins_ = new Plugins(this.config_);
let pluginPostTestPromises: any;
let browser_: any;
let results: any;
Expand Down
5 changes: 4 additions & 1 deletion spec/plugins/plugins/basic_plugin.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
module.exports = {
setup: function() {
protractor.__BASIC_PLUGIN_RAN = true;
protractor.__BASIC_PLUGIN_RAN_SETUP = true;
},
onPrepare: function() {
protractor.__BASIC_PLUGIN_RAN_ON_PREPARE = true;
}
};
5 changes: 3 additions & 2 deletions spec/plugins/specs/smoke_spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
describe('check if plugin setup ran', function() {
it('should have set protractor.__BASIC_PLUGIN_RAN', function() {
expect(protractor.__BASIC_PLUGIN_RAN).toBe(true);
it('should have set protractor.__BASIC_PLUGIN_RAN_*', function() {
expect(protractor.__BASIC_PLUGIN_RAN_SETUP).toBe(true);
expect(protractor.__BASIC_PLUGIN_RAN_ON_PREPARE).toBe(true);
});
});

0 comments on commit b05421e

Please sign in to comment.