forked from angular/jasminewd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
299 lines (269 loc) · 9.61 KB
/
index.js
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
/**
* Adapts Jasmine-Node tests to work better with WebDriverJS. Borrows
* heavily from the mocha WebDriverJS adapter at
* https://code.google.com/p/selenium/source/browse/javascript/node/selenium-webdriver/testing/index.js
*/
var webdriver = require('selenium-webdriver');
var flow = webdriver.promise.controlFlow();
/**
* Wraps a function so that all passed arguments are ignored.
* @param {!Function} fn The function to wrap.
* @return {!Function} The wrapped function.
*/
function seal(fn) {
return function() {
fn();
};
}
/**
* Validates that the parameter is a function.
* @param {Object} functionToValidate The function to validate.
* @throws {Error}
* @return {Object} The original parameter.
*/
function validateFunction(functionToValidate) {
if (functionToValidate && Object.prototype.toString.call(functionToValidate) === '[object Function]') {
return functionToValidate;
} else {
throw Error(functionToValidate + ' is not a function');
}
}
/**
* Validates that the parameter is a number.
* @param {Object} numberToValidate The number to validate.
* @throws {Error}
* @return {Object} The original number.
*/
function validateNumber(numberToValidate) {
if (!isNaN(numberToValidate)) {
return numberToValidate;
} else {
throw Error(numberToValidate + ' is not a number');
}
}
/**
* Validates that the parameter is a string.
* @param {Object} stringToValidate The string to validate.
* @throws {Error}
* @return {Object} The original string.
*/
function validateString(stringtoValidate) {
if (typeof stringtoValidate == 'string' || stringtoValidate instanceof String) {
return stringtoValidate;
} else {
throw Error(stringtoValidate + ' is not a string');
}
}
/**
* Wraps a function so it runs inside a webdriver.promise.ControlFlow and
* waits for the flow to complete before continuing.
* @param {!Function} globalFn The function to wrap.
* @return {!Function} The new function.
*/
function wrapInControlFlow(globalFn, fnName) {
return function() {
var driverError = new Error();
driverError.stack = driverError.stack.replace(/ +at.+jasminewd.+\n/, '');
function asyncTestFn(fn, desc) {
return function(done) {
var desc_ = 'Asynchronous test function: ' + fnName + '(';
if (desc) {
desc_ += '"' + desc + '"';
}
desc_ += ')';
// deferred object for signaling completion of asychronous function within globalFn
var asyncFnDone = webdriver.promise.defer();
if (fn.length === 0) {
// function with globalFn not asychronous
asyncFnDone.fulfill();
} else if (fn.length > 1) {
throw Error('Invalid # arguments (' + fn.length + ') within function "' + fnName +'"');
}
var flowFinished = flow.execute(function() {
fn.call(jasmine.getEnv().currentSpec, function(userError) {
if (userError) {
asyncFnDone.reject(new Error(userError));
} else {
asyncFnDone.fulfill();
}
});
}, desc_);
webdriver.promise.all([asyncFnDone, flowFinished]).then(function() {
seal(done)();
}, function(e) {
e.stack = e.stack + '==== async task ====\n' + driverError.stack;
done(e);
});
};
}
var description, func, timeout;
switch (fnName) {
case 'it':
case 'iit':
description = validateString(arguments[0]);
func = validateFunction(arguments[1]);
if (!arguments[2]) {
globalFn(description, asyncTestFn(func));
} else {
timeout = validateNumber(arguments[2]);
globalFn(description, asyncTestFn(func), timeout);
}
break;
case 'beforeEach':
case 'afterEach':
func = validateFunction(arguments[0]);
if (!arguments[1]) {
globalFn(asyncTestFn(func));
} else {
timeout = validateNumber(arguments[1]);
globalFn(asyncTestFn(func), timeout);
}
break;
default:
throw Error('invalid function: ' + fnName);
}
};
}
global.it = wrapInControlFlow(global.it, 'it');
global.iit = wrapInControlFlow(global.iit, 'iit');
global.beforeEach = wrapInControlFlow(global.beforeEach, 'beforeEach');
global.afterEach = wrapInControlFlow(global.afterEach, 'afterEach');
/**
* Wrap a Jasmine matcher function so that it can take webdriverJS promises.
* @param {!Function} matcher The matcher function to wrap.
* @param {webdriver.promise.Promise} actualPromise The promise which will
* resolve to the actual value being tested.
* @param {boolean} not Whether this is being called with 'not' active.
*/
function wrapMatcher(matcher, actualPromise, not) {
return function() {
var originalArgs = arguments;
var matchError = new Error("Failed expectation");
matchError.stack = matchError.stack.replace(/ +at.+jasminewd.+\n/, '');
actualPromise.then(function(actual) {
var expected = originalArgs[0];
var expectation = originalExpect(actual);
if (not) {
expectation = expectation.not;
}
var originalAddMatcherResult = expectation.spec.addMatcherResult;
var error = matchError;
expectation.spec.addMatcherResult = function(result) {
result.trace = error;
jasmine.Spec.prototype.addMatcherResult.call(this, result);
};
if (webdriver.promise.isPromise(expected)) {
if (originalArgs.length > 1) {
throw error('Multi-argument matchers with promises are not ' +
'supported.');
}
expected.then(function(exp) {
expectation[matcher].apply(expectation, [exp]);
expectation.spec.addMatcherResult = originalAddMatcherResult;
});
} else {
expectation.spec.addMatcherResult = function(result) {
result.trace = error;
originalAddMatcherResult.call(this, result);
};
expectation[matcher].apply(expectation, originalArgs);
expectation.spec.addMatcherResult = originalAddMatcherResult;
}
});
};
}
/**
* Return a chained set of matcher functions which will be evaluated
* after actualPromise is resolved.
* @param {webdriver.promise.Promise} actualPromise The promise which will
* resolve to the actual value being tested.
*/
function promiseMatchers(actualPromise) {
var promises = {not: {}};
var env = jasmine.getEnv();
var matchersClass = env.currentSpec.matchersClass || env.matchersClass;
for (var matcher in matchersClass.prototype) {
promises[matcher] = wrapMatcher(matcher, actualPromise, false);
promises.not[matcher] = wrapMatcher(matcher, actualPromise, true);
}
return promises;
}
var originalExpect = global.expect;
global.expect = function(actual) {
if (actual instanceof webdriver.WebElement) {
throw 'expect called with WebElement argument, expected a Promise. ' +
'Did you mean to use .getText()?';
}
if (webdriver.promise.isPromise(actual)) {
return promiseMatchers(actual);
} else {
return originalExpect(actual);
}
};
// Wrap internal Jasmine function to allow custom matchers
// to return promises that resolve to truthy or falsy values
var originalMatcherFn = jasmine.Matchers.matcherFn_;
jasmine.Matchers.matcherFn_ = function(matcherName, matcherFunction) {
var matcherFnThis = this;
var matcherFnArgs = jasmine.util.argsToArray(arguments);
return function() {
var matcherThis = this;
var matcherArgs = jasmine.util.argsToArray(arguments);
var result = matcherFunction.apply(this, arguments);
if (webdriver.promise.isPromise(result)) {
result.then(function(resolution) {
matcherFnArgs[1] = function() {
return resolution;
};
originalMatcherFn.apply(matcherFnThis, matcherFnArgs).
apply(matcherThis, matcherArgs);
});
} else {
originalMatcherFn.apply(matcherFnThis, matcherFnArgs).
apply(matcherThis, matcherArgs);
}
};
};
/**
* A Jasmine reporter which does nothing but execute the input function
* on a timeout failure.
*/
var OnTimeoutReporter = function(fn) {
this.callback = fn;
};
OnTimeoutReporter.prototype.reportRunnerStarting = function() {};
OnTimeoutReporter.prototype.reportRunnerResults = function() {};
OnTimeoutReporter.prototype.reportSuiteResults = function() {};
OnTimeoutReporter.prototype.reportSpecStarting = function() {};
OnTimeoutReporter.prototype.reportSpecResults = function(spec) {
if (!spec.results().passed()) {
var result = spec.results();
var failureItem = null;
var items_length = result.getItems().length;
for (var i = 0; i < items_length; i++) {
if (result.getItems()[i].passed_ === false) {
failureItem = result.getItems()[i];
var jasmineTimeoutRegexp =
/timed out after \d+ msec waiting for spec to complete/;
if (failureItem.toString().match(jasmineTimeoutRegexp)) {
this.callback();
}
}
}
}
};
OnTimeoutReporter.prototype.log = function() {};
// On timeout, the flow should be reset. This will prevent webdriver tasks
// from overflowing into the next test and causing it to fail or timeout
// as well. This is done in the reporter instead of an afterEach block
// to ensure that it runs after any afterEach() blocks with webdriver tasks
// get to complete first.
jasmine.getEnv().addReporter(new OnTimeoutReporter(function() {
console.warn('A Jasmine spec timed out. Resetting the WebDriver Control Flow.');
console.warn('The last active task was: ');
console.warn(
(flow.activeFrame_ && flow.activeFrame_.getPendingTask() ?
flow.activeFrame_.getPendingTask().toString() :
'unknown'));
flow.reset();
}));