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

Commit a379b33

Browse files
authored
feat(plugins): support onPrepare in plugins (#3483)
1 parent 4252000 commit a379b33

File tree

5 files changed

+45
-5
lines changed

5 files changed

+45
-5
lines changed

docs/plugins.md

+16
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,22 @@ Plugins are node modules which export an object with the following API:
9494
*/
9595
exports.setup = function() {};
9696

97+
/**
98+
* This is called before the test have been run but after the test framework has
99+
* been set up. Analogous to a config file's `onPreare`.
100+
*
101+
* Very similar to using `setup`, but allows you to access framework-specific
102+
* variables/funtions (e.g. `jasmine.getEnv().addReporter()`)
103+
*
104+
* @throws {*} If this function throws an error, a failed assertion is added to
105+
* the test results.
106+
*
107+
* @return {Q.Promise=} Can return a promise, in which case protractor will wait
108+
* for the promise to resolve before continuing. If the promise is
109+
* rejected, a failed assertion is added to the test results.
110+
*/
111+
exports.onPrepare = function() {};
112+
97113
/**
98114
* This is called after the tests have been run, but before the WebDriver
99115
* session has been terminated.

lib/plugins.ts

+17
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,22 @@ export interface ProtractorPlugin {
3434
*/
3535
setup?: () => Q.Promise<any>;
3636

37+
/**
38+
* This is called before the test have been run but after the test framework has
39+
* been set up. Analogous to a config file's `onPreare`.
40+
*
41+
* Very similar to using `setup`, but allows you to access framework-specific
42+
* variables/funtions (e.g. `jasmine.getEnv().addReporter()`)
43+
*
44+
* @throws {*} If this function throws an error, a failed assertion is added to
45+
* the test results.
46+
*
47+
* @return {Q.Promise=} Can return a promise, in which case protractor will wait
48+
* for the promise to resolve before continuing. If the promise is
49+
* rejected, a failed assertion is added to the test results.
50+
*/
51+
onPrepare?: () => Q.Promise<any>;
52+
3753
/**
3854
* This is called after the tests have been run, but before the WebDriver
3955
* session has been terminated.
@@ -396,6 +412,7 @@ export class Plugins {
396412
* @see docs/plugins.md#writing-plugins for information on these functions
397413
*/
398414
setup: Function = pluginFunFactory('setup', PromiseType.Q);
415+
onPrepare: Function = pluginFunFactory('onPrepare', PromiseType.Q);
399416
teardown: Function = pluginFunFactory('teardown', PromiseType.Q);
400417
postResults: Function = pluginFunFactory('postResults', PromiseType.Q);
401418
postTest: Function = pluginFunFactory('postTest', PromiseType.Q);

lib/runner.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export class Runner extends EventEmitter {
3131
preparer_: any;
3232
driverprovider_: DriverProvider;
3333
o: any;
34+
plugins_: Plugins;
3435

3536
constructor(config: Config) {
3637
super();
@@ -76,7 +77,9 @@ export class Runner extends EventEmitter {
7677
* are finished.
7778
*/
7879
runTestPreparer(): q.Promise<any> {
79-
return helper.runFilenameOrFn_(this.config_.configDir, this.preparer_);
80+
return this.plugins_.onPrepare().then(() => {
81+
return helper.runFilenameOrFn_(this.config_.configDir, this.preparer_);
82+
});
8083
}
8184

8285
/**
@@ -268,7 +271,7 @@ export class Runner extends EventEmitter {
268271
*/
269272
run(): q.Promise<any> {
270273
let testPassed: boolean;
271-
let plugins = new Plugins(this.config_);
274+
let plugins = this.plugins_ = new Plugins(this.config_);
272275
let pluginPostTestPromises: any;
273276
let browser_: any;
274277
let results: any;

spec/plugins/plugins/basic_plugin.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
module.exports = {
22
setup: function() {
3-
protractor.__BASIC_PLUGIN_RAN = true;
3+
protractor.__BASIC_PLUGIN_RAN_SETUP = true;
4+
},
5+
onPrepare: function() {
6+
protractor.__BASIC_PLUGIN_RAN_ON_PREPARE = true;
47
}
58
};

spec/plugins/specs/smoke_spec.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
describe('check if plugin setup ran', function() {
2-
it('should have set protractor.__BASIC_PLUGIN_RAN', function() {
3-
expect(protractor.__BASIC_PLUGIN_RAN).toBe(true);
2+
it('should have set protractor.__BASIC_PLUGIN_RAN_*', function() {
3+
expect(protractor.__BASIC_PLUGIN_RAN_SETUP).toBe(true);
4+
expect(protractor.__BASIC_PLUGIN_RAN_ON_PREPARE).toBe(true);
45
});
56
});

0 commit comments

Comments
 (0)