This repository has been archived by the owner on Jul 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
Copy pathplugins.ts
499 lines (465 loc) · 18.1 KB
/
plugins.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
import {ProtractorBrowser} from './browser';
import {Config} from './config';
import {ConfigParser} from './configParser';
import {Logger} from './logger';
let logger = new Logger('plugins');
export interface PluginConfig {
path?: string;
package?: string;
inline?: ProtractorPlugin;
name?: string;
[key: string]: any;
}
export interface ProtractorPlugin {
/**
* Sets up plugins before tests are run. This is called after the WebDriver
* session has been started, but before the test framework has been set up.
*
* @this {Object} bound to module.exports.
*
* @throws {*} If this function throws an error, a failed assertion is added to
* the test results.
*
* @return {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.
*/
setup?(): void|Promise<void>;
/**
* This is called before the test have been run but after the test framework has
* been set up. Analogous to a config file's `onPrepare`.
*
* Very similar to using `setup`, but allows you to access framework-specific
* variables/functions (e.g. `jasmine.getEnv().addReporter()`).
*
* @this {Object} bound to module.exports.
*
* @throws {*} If this function throws an error, a failed assertion is added to
* the test results.
*
* @return {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?(): void|Promise<void>;
/**
* This is called after the tests have been run, but before the WebDriver
* session has been terminated.
*
* @this {Object} bound to module.exports.
*
* @throws {*} If this function throws an error, a failed assertion is added to
* the test results.
*
* @return {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.
*/
teardown?(): void|Promise<void>;
/**
* Called after the test results have been finalized and any jobs have been
* updated (if applicable).
*
* @this {Object} bound to module.exports.
*
* @throws {*} If this function throws an error, it is outputted to the console.
* It is too late to add a failed assertion to the test results.
*
* @return {Promise=} Can return a promise, in which case protractor will wait
* for the promise to resolve before continuing. If the promise is
* rejected, an error is logged to the console.
*/
postResults?(): void|Promise<void>;
/**
* Called after each test block (in Jasmine, this means an `it` block)
* completes.
*
* @param {boolean} passed True if the test passed.
* @param {Object} testInfo information about the test which just ran.
*
* @this {Object} bound to module.exports.
*
* @throws {*} If this function throws an error, a failed assertion is added to
* the test results.
*
* @return {Promise=} Can return a promise, in which case protractor will wait
* for the promise to resolve before outputting test results. Protractor
* will *not* wait before executing the next test; however, if the promise
* is rejected, a failed assertion is added to the test results.
*/
postTest?(passed: boolean, testInfo: any): void|Promise<void>;
/**
* This is called inside browser.get() directly after the page loads, and before
* angular bootstraps.
*
* @param {ProtractorBrowser} browser The browser instance which is loading a page.
*
* @this {Object} bound to module.exports.
*
* @throws {*} If this function throws an error, a failed assertion is added to
* the test results.
*
* @return {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.
*/
onPageLoad?(browser: ProtractorBrowser): void|Promise<void>;
/**
* This is called inside browser.get() directly after angular is done
* bootstrapping/synchronizing. If `await browser.waitForAngularEnabled()`
* is `false`, this will not be called.
*
* @param {ProtractorBrowser} browser The browser instance which is loading a page.
*
* @this {Object} bound to module.exports.
*
* @throws {*} If this function throws an error, a failed assertion is added to
* the test results.
*
* @return {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.
*/
onPageStable?(browser: ProtractorBrowser): void|Promise<void>;
/**
* Between every webdriver action, Protractor calls browser.waitForAngular() to
* make sure that Angular has no outstanding $http or $timeout calls.
* You can use waitForPromise() to have Protractor additionally wait for your
* custom promise to be resolved inside of browser.waitForAngular().
*
* @param {ProtractorBrowser} browser The browser instance which needs invoked `waitForAngular`.
*
* @this {Object} bound to module.exports.
*
* @throws {*} If this function throws an error, a failed assertion is added to
* the test results.
*
* @return {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, and
* protractor will continue onto the next command. If nothing is returned or
* something other than a promise is returned, protractor will continue
* onto the next command.
*/
waitForPromise?(browser: ProtractorBrowser): Promise<void>;
/**
* Between every webdriver action, Protractor calls browser.waitForAngular() to
* make sure that Angular has no outstanding $http or $timeout calls.
* You can use waitForCondition() to have Protractor additionally wait for your
* custom condition to be truthy. If specified, this function will be called
* repeatedly until truthy.
*
* @param {ProtractorBrowser} browser The browser instance which needs invoked `waitForAngular`.
*
* @this {Object} bound to module.exports.
*
* @throws {*} If this function throws an error, a failed assertion is added to
* the test results.
*
* @return {Promise<boolean>|boolean} If truthy, Protractor
* will continue onto the next command. If falsy, webdriver will
* continuously re-run this function until it is truthy. If a rejected promise
* is returned, a failed assertion is added to the test results, and Protractor
* will continue onto the next command.
*/
waitForCondition?(browser: ProtractorBrowser): Promise<boolean>|boolean;
/**
* Used to turn off default checks for angular stability
*
* Normally Protractor waits for all $timeout and $http calls to be processed
* before executing the next command. This can be disabled using
* browser.ignoreSynchronization, but that will also disable any
* <Plugin>.waitForPromise or <Plugin>.waitForCondition checks. If you want
* to disable synchronization with angular, but leave intact any custom plugin
* synchronization, this is the option for you.
*
* This is used by plugin authors who want to replace Protractor's
* synchronization code with their own.
*
* @type {boolean}
*/
skipAngularStability?: boolean;
/**
* The name of the plugin. Used when reporting results.
*
* If you do not specify this property, it will be filled in with something
* reasonable (e.g. the plugin's path) by Protractor at runtime.
*
* @type {string}
*/
name?: string;
/**
* The plugin's configuration object.
*
* Note: this property is added by Protractor at runtime. Any pre-existing
* value will be overwritten.
*
* Note: that this is not the entire Protractor config object, just the entry
* in the `plugins` array for this plugin.
*
* @type {Object}
*/
config?: PluginConfig;
/**
* Adds a failed assertion to the test's results.
*
* Note: this property is added by Protractor at runtime. Any pre-existing
* value will be overwritten.
*
* @param {string} message The error message for the failed assertion
* @param {specName: string, stackTrace: string} options Some optional extra
* information about the assertion:
* - specName The name of the spec which this assertion belongs to.
* Defaults to `PLUGIN_NAME + ' Plugin Tests'`.
* - stackTrace The stack trace for the failure. Defaults to undefined.
* Defaults to `{}`.
*
* @throws {Error} Throws an error if called after results have been reported
*/
addFailure?(message?: string, info?: {specName?: string, stackTrace?: string}): void;
/**
* Adds a passed assertion to the test's results.
*
* Note: this property is added by Protractor at runtime. Any pre-existing
* value will be overwritten.
*
* @param {specName: string} options Extra information about the assertion:
* - specName The name of the spec which this assertion belongs to.
* Defaults to `PLUGIN_NAME + ' Plugin Tests'`.
* Defaults to `{}`.
*
* @throws {Error} Throws an error if called after results have been reported
*/
addSuccess?(info?: {specName?: string}): void;
/**
* Warns the user that something is problematic.
*
* Note: this property is added by Protractor at runtime. Any pre-existing
* value will be overwritten.
*
* @param {string} message The message to warn the user about
* @param {specName: string} options Extra information about the assertion:
* - specName The name of the spec which this assertion belongs to.
* Defaults to `PLUGIN_NAME + ' Plugin Tests'`.
* Defaults to `{}`.
*/
addWarning?(message?: string, info?: {specName?: string}): void;
}
/**
* The plugin API for Protractor. Note that this API is unstable. See
* plugins/README.md for more information.
*
* @constructor
* @param {Object} config parsed from the config file
*/
export class Plugins {
pluginObjs: ProtractorPlugin[];
assertions: {[key: string]: AssertionResult[]};
resultsReported: boolean;
constructor(config: Config) {
this.pluginObjs = [];
this.assertions = {};
this.resultsReported = false;
if (config.plugins) {
config.plugins.forEach((pluginConf, i) => {
let path: string;
if (pluginConf.path) {
path = ConfigParser.resolveFilePatterns(pluginConf.path, true, config.configDir)[0];
if (!path) {
throw new Error('Invalid path to plugin: ' + pluginConf.path);
}
} else {
path = pluginConf.package;
}
let pluginObj: ProtractorPlugin;
if (path) {
pluginObj = require(path) as ProtractorPlugin;
} else if (pluginConf.inline) {
pluginObj = pluginConf.inline;
} else {
throw new Error(
'Plugin configuration did not contain a valid path or ' +
'inline definition.');
}
this.annotatePluginObj(pluginObj, pluginConf, i);
logger.debug('Plugin "' + pluginObj.name + '" loaded.');
this.pluginObjs.push(pluginObj);
});
}
}
/**
* Adds properties to a plugin's object
*
* @see docs/plugins.md#provided-properties-and-functions
*/
private annotatePluginObj(obj: ProtractorPlugin, conf: PluginConfig, i: number): void {
let addAssertion =
(info: {specName?: string, stackTrace?: string}, passed: boolean, message?: string) => {
if (this.resultsReported) {
throw new Error(
'Cannot add new tests results, since they were already ' +
'reported.');
}
info = info || {};
const specName = info.specName || (obj.name + ' Plugin Tests');
const assertion: AssertionResult = {passed: passed};
if (!passed) {
assertion.errorMsg = message;
if (info.stackTrace) {
assertion.stackTrace = info.stackTrace;
}
}
this.assertions[specName] = this.assertions[specName] || [];
this.assertions[specName].push(assertion);
};
obj.name = obj.name || conf.name || conf.path || conf.package || ('Plugin #' + i);
obj.config = conf;
obj.addFailure = (message?, info?) => {
addAssertion(info, false, message);
};
obj.addSuccess = (options?) => {
addAssertion(options, true);
};
obj.addWarning = (message?, options?) => {
options = options || {};
logger.warn(
'Warning ' +
(options.specName ? 'in ' + options.specName : 'from "' + obj.name + '" plugin') + ': ' +
message);
};
}
private printPluginResults(specResults: SpecResult[]) {
const green = '\x1b[32m';
const red = '\x1b[31m';
const normalColor = '\x1b[39m';
const printResult = (message: string, pass: boolean) => {
logger.info(pass ? green : red, '\t', pass ? 'Pass: ' : 'Fail: ', message, normalColor);
};
for (const specResult of specResults) {
const passed = specResult.assertions.map(x => x.passed).reduce((x, y) => (x && y), true);
printResult(specResult.description, passed);
if (!passed) {
for (const assertion of specResult.assertions) {
if (!assertion.passed) {
logger.error('\t\t' + assertion.errorMsg);
if (assertion.stackTrace) {
logger.error('\t\t' + assertion.stackTrace.replace(/\n/g, '\n\t\t'));
}
}
}
}
}
}
/**
* Gets the tests results generated by any plugins
*
* @see lib/frameworks/README.md#requirements for a complete description of what
* the results object must look like
*
* @return {Object} The results object
*/
getResults() {
const results = {failedCount: 0, specResults: [] as SpecResult[]};
for (const specName in this.assertions) {
results.specResults.push({description: specName, assertions: this.assertions[specName]});
results.failedCount +=
this.assertions[specName].filter(assertion => !assertion.passed).length;
}
this.printPluginResults(results.specResults);
this.resultsReported = true;
return results;
}
/**
* Returns true if any loaded plugin has skipAngularStability enabled.
*
* @return {boolean}
*/
skipAngularStability() {
const result = this.pluginObjs.some(pluginObj => pluginObj.skipAngularStability);
return result;
}
/**
* @see docs/plugins.md#writing-plugins for information on these functions
*/
setup = this.pluginFunFactory('setup');
onPrepare = this.pluginFunFactory('onPrepare');
teardown = this.pluginFunFactory('teardown');
postResults = this.pluginFunFactory('postResults');
postTest = this.pluginFunFactory('postTest');
onPageLoad = this.pluginFunFactory('onPageLoad');
onPageStable = this.pluginFunFactory('onPageStable');
waitForPromise = this.pluginFunFactory('waitForPromise');
waitForCondition = this.pluginFunFactory('waitForCondition', true);
/**
* Calls a function from a plugin safely. If the plugin's function throws an
* exception or returns a rejected promise, that failure will be logged as a
* failed test result instead of crashing protractor. If the tests results have
* already been reported, the failure will be logged to the console.
*
* @param {Object} pluginObj The plugin object containing the function to be run
* @param {string} funName The name of the function we want to run
* @param {*[]} args The arguments we want to invoke the function with
* @param {boolean} resultsReported If the results have already been reported
* @param {*} failReturnVal The value to return if the function fails
*
* @return {Promise} A promise which resolves to the
* function's return value
*/
private safeCallPluginFun(
pluginObj: ProtractorPlugin, funName: string, args: any[], failReturnVal: any): Promise<any> {
const resolver = async (done: (result: any) => void) => {
const logError = (e: any) => {
if (this.resultsReported) {
this.printPluginResults([{
description: pluginObj.name + ' Runtime',
assertions: [{
passed: false,
errorMsg: 'Failure during ' + funName + ': ' + (e.message || e),
stackTrace: e.stack
}]
}]);
} else {
pluginObj.addFailure(
'Failure during ' + funName + ': ' + e.message || e, {stackTrace: e.stack});
}
done(failReturnVal);
};
try {
const result = await(pluginObj as any)[funName].apply(pluginObj, args);
done(result);
} catch (e) {
logError(e);
}
};
return new Promise(resolver);
}
/**
* Generates the handler for a plugin function (e.g. the setup() function)
*
* @param {string} funName The name of the function to make a handler for
* @param {boolean=} failReturnVal The value that the function should return if the plugin crashes
*
* @return The handler
*/
private pluginFunFactory(funName: string, failReturnVal?: boolean):
(...args: any[]) => Promise<any[]>;
private pluginFunFactory(funName: string, failReturnVal?: boolean):
(...args: any[]) => Promise<any[]>;
private pluginFunFactory(funName: string, failReturnVal?: boolean) {
return (...args: any[]) => {
const promises =
this.pluginObjs.filter(pluginObj => typeof(pluginObj as any)[funName] === 'function')
.map(pluginObj => this.safeCallPluginFun(pluginObj, funName, args, failReturnVal));
return Promise.all(promises);
};
}
}
export interface SpecResult {
description: string;
assertions: AssertionResult[];
}
export interface AssertionResult {
passed: boolean;
errorMsg?: string;
stackTrace?: string;
}