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

Commit 20ab5c9

Browse files
committed
change ProtractorPlugin to an interface
1 parent 9f40108 commit 20ab5c9

File tree

3 files changed

+235
-14
lines changed

3 files changed

+235
-14
lines changed

docs/plugins.md

+11-7
Original file line numberDiff line numberDiff line change
@@ -312,17 +312,21 @@ export function onPageLoad(): void {
312312
};
313313
```
314314

315-
However, if you want your code more heavily typed, you can write your plugin as
316-
an extension of the `ProtractorPlugin` class:
315+
If you want your code more heavily typed, you can write your plugin with
316+
the `ProtractorPlugin` interface:
317317

318318
```typescript
319-
import {ProtractorPlugin} from 'protractor/built/plugins';
319+
import {ProtractorPlugin} from 'protractor';
320320

321-
export class MyPlugin extends ProtractorPlugin {
322-
onPageLoad() {
323-
this.addSuccess({specName: 'Hello, World!'});
324-
};
321+
// required creation of a module object
322+
declare var module: any;
323+
324+
let myPlugin: ProtractorPlugin = {
325+
addSuccess({specName: 'Hello, World!'})
325326
};
327+
328+
// set the exports to the defined plugin
329+
module.exports = myPlugin;
326330
```
327331

328332
Then, in a separate file, export an instance of that class using an assignment

exampleTypescript/plugins.ts

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// a plugin example with the protractor plugin interface
2+
import { ProtractorPlugin } from 'protractor';
3+
4+
// creating a "var module: any" will help create a module.exports
5+
declare var module: any;
6+
7+
let myPlugin: ProtractorPlugin = {
8+
addSuccess(info: {specName: string}) {
9+
console.log('on success: ' + info.specName);
10+
},
11+
onPageLoad() {
12+
this.addSuccess({specName: 'Hello, World!'});
13+
}
14+
};
15+
16+
module.exports = myPlugin;

lib/plugins.ts

+208-7
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,217 @@ export interface PluginConfig {
1818
[key: string]: any;
1919
}
2020

21-
export class ProtractorPlugin {
22-
skipAngularStability: boolean;
21+
export interface ProtractorPlugin {
22+
/**
23+
* Sets up plugins before tests are run. This is called after the WebDriver
24+
* session has been started, but before the test framework has been set up.
25+
*
26+
* @this {Object} bound to module.exports
27+
*
28+
* @throws {*} If this function throws an error, a failed assertion is added to
29+
* the test results.
30+
*
31+
* @return {q.Promise=} Can return a promise, in which case protractor will wait
32+
* for the promise to resolve before continuing. If the promise is
33+
* rejected, a failed assertion is added to the test results.
34+
*/
35+
setup?: () => q.Promise<any>;
36+
37+
/**
38+
* This is called after the tests have been run, but before the WebDriver
39+
* session has been terminated.
40+
*
41+
* @this {Object} bound to module.exports
42+
*
43+
* @throws {*} If this function throws an error, a failed assertion is added to
44+
* the test results.
45+
*
46+
* @return {q.Promise=} Can return a promise, in which case protractor will wait
47+
* for the promise to resolve before continuing. If the promise is
48+
* rejected, a failed assertion is added to the test results.
49+
*/
50+
teardown?: () => q.Promise<any>;
51+
52+
/**
53+
* Called after the test results have been finalized and any jobs have been
54+
* updated (if applicable).
55+
*
56+
* @this {Object} bound to module.exports
57+
*
58+
* @throws {*} If this function throws an error, it is outputted to the console
59+
*
60+
* @return {q.Promise=} Can return a promise, in which case protractor will wait
61+
* for the promise to resolve before continuing. If the promise is
62+
* rejected, an error is logged to the console.
63+
*/
64+
postResults?: () => q.Promise<any>;
2365

24-
name: string;
25-
config: PluginConfig;
26-
addFailure:
66+
/**
67+
* Called after each test block (in Jasmine, this means an `it` block)
68+
* completes.
69+
*
70+
* @param {boolean} passed True if the test passed.
71+
* @param {Object} testInfo information about the test which just ran.
72+
*
73+
* @this {Object} bound to module.exports
74+
*
75+
* @throws {*} If this function throws an error, a failed assertion is added to
76+
* the test results.
77+
*
78+
* @return {q.Promise=} Can return a promise, in which case protractor will wait
79+
* for the promise to resolve before outputting test results. Protractor
80+
* will *not* wait before executing the next test, however. If the promise
81+
* is rejected, a failed assertion is added to the test results.
82+
*/
83+
postTest?: (passed: boolean, testInfo: any) => q.Promise<any>;
84+
85+
/**
86+
* This is called inside browser.get() directly after the page loads, and before
87+
* angular bootstraps.
88+
*
89+
* @this {Object} bound to module.exports
90+
*
91+
* @throws {*} If this function throws an error, a failed assertion is added to
92+
* the test results.
93+
*
94+
* @return {q.Promise=} Can return a promise, in which case protractor will wait
95+
* for the promise to resolve before continuing. If the promise is
96+
* rejected, a failed assertion is added to the test results.
97+
*/
98+
onPageLoad?: () => q.Promise<any>;
99+
100+
/**
101+
* This is called inside browser.get() directly after angular is done
102+
* bootstrapping/synchronizing. If browser.ignoreSynchronization is true, this
103+
* will not be called.
104+
*
105+
* @this {Object} bound to module.exports
106+
*
107+
* @throws {*} If this function throws an error, a failed assertion is added to
108+
* the test results.
109+
*
110+
* @return {q.Promise=} Can return a promise, in which case protractor will wait
111+
* for the promise to resolve before continuing. If the promise is
112+
* rejected, a failed assertion is added to the test results.
113+
*/
114+
onPageStable?: () => q.Promise<any>;
115+
116+
/**
117+
* Between every webdriver action, Protractor calls browser.waitForAngular() to
118+
* make sure that Angular has no outstanding $http or $timeout calls.
119+
* You can use waitForPromise() to have Protractor additionally wait for your
120+
* custom promise to be resolved inside of browser.waitForAngular().
121+
*
122+
* @this {Object} bound to module.exports
123+
*
124+
* @throws {*} If this function throws an error, a failed assertion is added to
125+
* the test results.
126+
*
127+
* @return {q.Promise=} Can return a promise, in which case protractor will wait
128+
* for the promise to resolve before continuing. If the promise is
129+
* rejected, a failed assertion is added to the test results, and protractor
130+
* will continue onto the next command. If nothing is returned or something
131+
* other than a promise is returned, protractor will continue onto the next
132+
* command.
133+
*/
134+
waitForPromise?: () => q.Promise<any>;
135+
136+
/**
137+
* Between every webdriver action, Protractor calls browser.waitForAngular() to
138+
* make sure that Angular has no outstanding $http or $timeout calls.
139+
* You can use waitForCondition() to have Protractor additionally wait for your
140+
* custom condition to be truthy.
141+
*
142+
* @this {Object} bound to module.exports
143+
*
144+
* @throws {*} If this function throws an error, a failed assertion is added to
145+
* the test results.
146+
*
147+
* @return {q.Promise<boolean>|boolean} If truthy, Protractor will continue onto
148+
* the next command. If falsy, webdriver will continuously re-run this
149+
* function until it is truthy. If a rejected promise is returned, a failed
150+
* assertion is added to the test results, and protractor will continue onto
151+
* the next command.
152+
*/
153+
waitForCondition?: () => q.Promise<any>;
154+
155+
/**
156+
* Used to turn off default checks for angular stability
157+
*
158+
* Normally Protractor waits for all $timeout and $http calls to be processed
159+
* before executing the next command. This can be disabled using
160+
* browser.ignoreSynchronization, but that will also disable any
161+
* <Plugin>.waitForPromise or <Plugin>.waitForCondition checks. If you want
162+
* to
163+
* disable synchronization with angular, but leave in tact any custom plugin
164+
* synchronization, this is the option for you.
165+
*
166+
* This is used by users who want to replace Protractor's synchronization code
167+
* This is used by users who want to replace Protractor's synchronization code
168+
* with their own.
169+
*
170+
* @type {boolean}
171+
*/
172+
skipAngularStability?: boolean;
173+
174+
/**
175+
* Used when reporting results.
176+
*
177+
* If you do not specify this property, it will be filled in with something
178+
* reasonable (e.g. the plugin's path)
179+
*
180+
* @type {string}
181+
*/
182+
name?: string;
183+
184+
/**
185+
* The plugin configuration object. Note that this is not the entire
186+
* Protractor config object, just the entry in the plugins array for this
187+
* plugin.
188+
*
189+
* @type {Object}
190+
*/
191+
config?: PluginConfig;
192+
193+
/**
194+
* Adds a failed assertion to the test's results.
195+
*
196+
* @param {string} message The error message for the failed assertion
197+
* @param {specName: string, stackTrace: string} options Some optional extra
198+
* information about the assertion:
199+
* - specName The name of the spec which this assertion belongs to.
200+
* Defaults to `PLUGIN_NAME + ' Plugin Tests'`.
201+
* - stackTrace The stack trace for the failure. Defaults to undefined.
202+
* Defaults to `{}`.
203+
*
204+
* @throws {Error} Throws an error if called after results have been reported
205+
*/
206+
addFailure?:
27207
(message?: string,
28208
info?: {specName?: string, stackTrace?: string}) => void;
29-
addSuccess: (info?: {specName?: string}) => void;
30-
addWarning: (message?: string, info?: {specName?: string}) => void;
209+
210+
/**
211+
* Adds a passed assertion to the test's results.
212+
*
213+
* @param {specName: string} options Extra information about the assertion:
214+
* - specName The name of the spec which this assertion belongs to.
215+
* Defaults to `PLUGIN_NAME + ' Plugin Tests'`.
216+
* Defaults to `{}`.
217+
*
218+
* @throws {Error} Throws an error if called after results have been reported
219+
*/
220+
addSuccess?: (info?: {specName?: string}) => void;
221+
222+
/**
223+
* Warns the user that something is problematic.
224+
*
225+
* @param {string} message The message to warn the user about
226+
* @param {specName: string} options Extra information about the assertion:
227+
* - specName The name of the spec which this assertion belongs to.
228+
* Defaults to `PLUGIN_NAME + ' Plugin Tests'`.
229+
* Defaults to `{}`.
230+
*/
231+
addWarning?: (message?: string, info?: {specName?: string}) => void;
31232
}
32233

33234
/**

0 commit comments

Comments
 (0)