/
inject.js
547 lines (471 loc) · 13.3 KB
/
inject.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
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
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
/**
* JavaScript file to included by the test suite page that it loaded
* inside the iframe on the "run" pages. This injection must be done
* by the guest page, it can't be loaded by TestSwarm.
* Example:
* - https://github.com/jquery/jquery/blob/2.0.0/test/data/testrunner.js
* - https://github.com/jquery/jquery/blob/2.0.0/test/index.html
*
* @author John Resig
* @author Timo Tijhof
* @since 0.1.0
* @package TestSwarm
*/
/* eslint-env browser */
/* global jQuery, QUnit, Test, jasmine, JSSpec, JsUnitTestManager, SeleniumTestResult, LOG, doh, Screw, mocha */
(function() {
var url, curHeartbeat, testFrameworks, onErrorFnPrev,
DEBUG = false,
doPost = false,
search = location.search,
index = search.indexOf( "swarmURL=" ),
submitTimeout = 5,
beatRate = 20,
setTimeout = window.setTimeout,
clearTimeout = window.clearTimeout;
try {
doPost = !!window.parent.postMessage;
} catch ( e ) {
// Ignore
}
if ( index !== -1 ) {
url = decodeURIComponent( search.slice( index + 9 ) );
}
if ( !DEBUG && ( !url || url.indexOf( "http" ) !== 0 ) ) {
return;
}
// Prevent blocking things from executing
if ( !DEBUG ) {
window.print = window.confirm = window.alert = window.open = function() {};
}
/** Utility functions **/
function debugObj( obj ) {
var i, str = "";
for ( i in obj ) {
str += ( str ? "\n" : "" ) + i + ":\n\t " + obj[i];
}
return str;
}
function remove( elem ) {
if ( typeof elem === "string" ) {
elem = document.getElementById( elem );
}
if ( elem ) {
elem.parentNode.removeChild( elem );
}
}
function trimSerialize( doc ) {
var scripts, root, cur, links, i, href;
doc = doc || document;
scripts = doc.getElementsByTagName( "script" );
while ( scripts.length ) {
remove( scripts[0] );
}
root = location.href.replace( /(https?:\/\/.*?)\/.*/, "$1" );
cur = location.href.replace( /[^/]*$/, "" );
links = doc.getElementsByTagName( "link" );
for ( i = 0; i < links.length; i += 1 ) {
href = links[i].href;
if ( href.indexOf( "/" ) === 0 ) {
href = root + href;
} else if ( !/^https?:\/\//.test( href ) ) {
href = cur + href;
}
links[i].href = href;
}
return ( "<html>" + doc.documentElement.innerHTML + "</html>" )
.replace( /\s+/g, " " );
}
function submit( params ) {
var form, i, input, key, paramItems, parts, query;
if ( curHeartbeat ) {
clearTimeout( curHeartbeat );
}
paramItems = ( url.split( "?" )[1] || "" ).split( "&" );
for ( i = 0; i < paramItems.length; i += 1 ) {
if ( paramItems[i] ) {
parts = paramItems[i].split( "=" );
if ( !params[ parts[0] ] ) {
params[ parts[0] ] = parts[1];
}
}
}
if ( !params.action ) {
params.action = "saverun";
}
if ( !params.report_html ) {
params.report_html = window.TestSwarm.serialize();
}
if ( DEBUG ) {
window.alert( debugObj( params ) ) ;
}
if ( doPost ) {
// Build Query String
query = "";
for ( key in params ) {
query += ( query ? "&" : "" ) + key + "=" + encodeURIComponent( params[key] );
}
if ( !DEBUG ) {
window.parent.postMessage( query, "*" );
}
} else {
form = document.createElement( "form" );
form.action = url;
form.method = "POST";
for ( i in params ) {
input = document.createElement( "input" );
input.type = "hidden";
input.name = i;
input.value = params[i];
form.appendChild( input );
}
if ( DEBUG ) {
window.alert( url );
} else {
// Watch for the result submission timing out
setTimeout(function() {
submit( params );
}, submitTimeout * 1000);
document.body.appendChild( form );
form.submit();
}
}
}
function detectAndInstall() {
var key;
for ( key in testFrameworks ) {
if ( testFrameworks[key].detect() ) {
testFrameworks[key].install();
return key;
}
}
return false;
}
// Preserve other handlers
onErrorFnPrev = window.onerror;
// Cover uncaught exceptions
// Returning true will suppress the default browser handler,
// returning false will let it run.
window.onerror = function( error, filePath, linerNr ) {
var report,
ret = false;
if ( onErrorFnPrev ) {
ret = onErrorFnPrev( error, filePath, linerNr );
}
// Treat return value as window.onerror itself does,
// Only do our handling if not suppressed.
if ( ret !== true ) {
report = document.createElement( "div" );
report.innerHTML = "<hr/><b>[TestSwarm] window.onerror:</b><br/>";
report.appendChild( document.createTextNode( error ) );
report.appendChild( document.createElement( "br" ) );
report.appendChild( document.createTextNode( "in " + filePath + " on line " + linerNr ) );
document.body.appendChild( report );
submit({ fail: 0, error: 1, total: 1 });
return false;
}
return ret;
};
// Expose the TestSwarm API
window.TestSwarm = {
submit: submit,
heartbeat: function() {
if ( curHeartbeat ) {
clearTimeout( curHeartbeat );
}
curHeartbeat = setTimeout(function() {
submit({
// ResultAction::STATE_ABORTED
status: 3
});
}, beatRate * 1000);
},
serialize: function() {
return trimSerialize();
}
};
testFrameworks = {
// QUnit (by jQuery)
// https://qunitjs.com
"QUnit": {
detect: function() {
return typeof QUnit !== "undefined";
},
install: function() {
QUnit.done(function( results ) {
submit({
fail: results.failed,
error: 0,
total: results.total
});
});
QUnit.log(window.TestSwarm.heartbeat);
window.TestSwarm.heartbeat();
window.TestSwarm.serialize = function() {
var ol, i;
// Show any collapsed results
ol = document.getElementsByTagName( "ol" );
for ( i = 0; i < ol.length; i += 1 ) {
ol[i].style.display = "block";
}
return trimSerialize();
};
}
},
// Jasmine v2.x
// https://jasmine.github.io/
"Jasmine": {
detect: function() {
return typeof jasmine !== "undefined" && typeof describe !== "undefined" && typeof it !== "undefined";
},
install: function() {
var jasmineEnv = jasmine.getEnv(),
result = {
fail: 0,
error: 0,
total: 0
};
jasmineEnv.addReporter({
jasmineStarted: function( info ) {
result.total = info.totalSpecsDefined;
window.TestSwarm.heartbeat();
},
suiteStarted: window.TestSwarm.heartbeat,
specStarted: window.TestSwarm.heartbeat,
specDone: window.TestSwarm.heartbeat,
suiteDone: window.TestSwarm.heartbeat,
jasmineDone: function( info ) {
result.fail = info.failedExpectations.length;
submit(result);
}
});
}
},
// UnitTestJS (Prototype, Scriptaculous)
// https://github.com/tobie/unittest_js
"UnitTestJS": {
detect: function() {
return typeof Test !== "undefined" && Test && Test.Unit && Test.Unit.runners;
},
install: function() {
/*jshint loopfunc:true */
var i,
total_runners = Test.Unit.runners.length,
cur_runners = 0,
total = 0,
fail = 0,
error = 0;
for ( i = 0; i < Test.Unit.runners.length; i += 1 ) {
// Need to proxy the i variable into a local scope,
// otherwise all the finish-functions created in this loop
// will refer to the same i variable..
(function( i ) {
var results,
finish = Test.Unit.runners[i].finish;
Test.Unit.runners[i].finish = function() {
finish.call( this );
results = this.getResult();
total += results.assertions;
fail += results.failures;
error += results.errors;
cur_runners += 1;
if ( cur_runners === total_runners ) {
submit({
fail: fail,
error: error,
total: total
});
}
};
}( i ) );
}
}
},
// JSSpec (MooTools)
// http://jania.pe.kr/aw/moin.cgi/JSSpec
// https://code.google.com/p/jsspec/
"JSSpec": {
detect: function() {
return typeof JSSpec !== "undefined" && JSSpec && JSSpec.Logger;
},
install: function() {
var onRunnerEnd = JSSpec.Logger.prototype.onRunnerEnd;
JSSpec.Logger.prototype.onRunnerEnd = function() {
var ul, i;
onRunnerEnd.call( this );
// Show any collapsed results
ul = document.getElementsByTagName( "ul" );
for ( i = 0; i < ul.length; i += 1 ) {
ul[i].style.display = "block";
}
submit({
fail: JSSpec.runner.getTotalFailures(),
error: JSSpec.runner.getTotalErrors(),
total: JSSpec.runner.totalExamples
});
};
window.TestSwarm.serialize = function() {
var i,
ul = document.getElementsByTagName( "ul" );
// Show any collapsed results
for ( i = 0; i < ul.length; i += 1 ) {
ul[i].style.display = "block";
}
return trimSerialize();
};
}
},
// JSUnit
// http://www.jsunit.net/
// Note: Injection file must be included before the frames
// are document.write()d into the page.
"JSUnit": {
detect: function() {
return typeof JsUnitTestManager !== "undefined";
},
install: function() {
var _done = JsUnitTestManager.prototype._done;
JsUnitTestManager.prototype._done = function() {
_done.call( this );
submit({
fail: this.failureCount,
error: this.errorCount,
total: this.totalCount
});
};
window.TestSwarm.serialize = function() {
return "<pre>" + this.log.join( "\n" ) + "</pre>";
};
}
},
// Selenium Core
// http://seleniumhq.org/projects/core/
"Selenium": {
detect: function() {
return typeof SeleniumTestResult !== "undefined" && typeof LOG !== "undefined";
},
install: function() {
// Completely overwrite the postback
SeleniumTestResult.prototype.post = function() {
submit({
fail: this.metrics.numCommandFailures,
error: this.metrics.numCommandErrors,
total: this.metrics.numCommandPasses + this.metrics.numCommandFailures + this.metrics.numCommandErrors
});
};
window.TestSwarm.serialize = function() {
var msg,
results = [];
while ( LOG.pendingMessages.length ) {
msg = LOG.pendingMessages.shift();
results.push( msg.type + ": " + msg.msg );
}
return "<pre>" + results.join( "\n" ) + "</pre>";
};
}
},
// Dojo Objective Harness
// http://docs.dojocampus.org/quickstart/doh
"DOH": {
detect: function() {
return typeof doh !== "undefined" && doh._report;
},
install: function() {
var _report = doh._report;
doh._report = function() {
_report.apply( this, arguments );
submit({
fail: doh._failureCount,
error: doh._errorCount,
total: doh._testCount
});
};
window.TestSwarm.serialize = function() {
return "<pre>" + document.getElementById( "logBody" ).innerHTML + "</pre>";
};
}
},
// Screw.Unit
// https://github.com/nathansobo/screw-unit
"Screw.Unit": {
detect: function() {
return typeof Screw !== "undefined" && typeof jQuery !== "undefined" && Screw && Screw.Unit;
},
install: function() {
/*global $ */
$( Screw ).bind( "after", function() {
var passed = $( ".passed" ).length,
failed = $( ".failed" ).length;
submit({
fail: failed,
error: 0,
total: failed + passed
});
});
$( Screw ).bind( "loaded", function() {
$( ".it" )
.bind( "passed", window.TestSwarm.heartbeat )
.bind( "failed", window.TestSwarm.heartbeat );
window.TestSwarm.heartbeat();
});
window.TestSwarm.serialize = function() {
return trimSerialize();
};
}
},
// Mocha
// https://mochajs.org/
"Mocha": {
detect: function() {
return typeof Mocha !== "undefined" && typeof mocha !== "undefined";
},
install: function() {
// Tab into the run method to install our hooks.
// Use the mocha instance instead of the prototype, because
// the mocha instance (HTMLReporter) also overloads .run.
// This ensures our code runs after HTMLReporter is done.
var run = mocha.run;
mocha.run = function(fn) {
var runner;
// Sometimes (in IE9?) the 'end' event has already fired.
// Registering on("end", fn) afterwards doesn't work.
// So we use the .run(fn) callback instead, which is called
// internally by Mocha right after the 'end' event.
runner = run.call(this, function() {
if (fn) {
// Call the original callback given to .run(fn)
fn.apply(this, arguments);
}
// `runner` can sometimes still be undefined here (at least in IE9).
// Let the function return and pick up asynchronously
// so the variable has been assigned.
setTimeout(function() {
submit({
fail: runner.failures,
total: runner.total,
error: 0
});
}, 1);
});
runner.on("start", window.TestSwarm.heartbeat);
runner.on("suite", window.TestSwarm.heartbeat);
runner.on("test end", window.TestSwarm.heartbeat);
runner.on("pass", window.TestSwarm.heartbeat);
runner.on("fail", window.TestSwarm.heartbeat);
return runner;
};
window.TestSwarm.serialize = function() {
var i, len, els;
els = document.getElementsByTagName("pre");
// Expand all source code sections, because we submit a static
// snapshot to TestSwarm, event handlers don"t survive.
for ( i = 0, len = els.length; i < len; i++ ) {
els[i].style.display = "inline-block";
}
return trimSerialize();
};
}
}
};
detectAndInstall();
}() );