Simple test result logger
Thomas Aylott committed
Oct 31, 2013 at 13:28 UTC
c4727944dff365a03f1ca8a58f25ceb7c7e6042f
2 files changed
+135
test/reportTestResults.browser.js
new
+75
@@ -0,0 +1,75 @@
1
+console._error = console.error;
2
+console._warn = console.warn;
3
+console._log = console.log;
4
+
5
+console.log = window.print = function(message){
6
+ console._log(message);
7
+ testImageURL('/print?type=log&message=' + encodeURIComponent(message) + '&_=' + Date.now().toString(36));
8
+};
9
+
10
+console.error = function(message){
11
+ console._error(message);
12
+ testImageURL('/print?type=error&message=' + encodeURIComponent(message) + '&_=' + Date.now().toString(36));
13
+};
14
+
15
+console.warn = function(message){
16
+ console._warn(message);
17
+ testImageURL('/print?type=warn&message=' + encodeURIComponent(message) + '&_=' + Date.now().toString(36));
18
+};
19
+
20
+;(function(env){
21
+
22
+ env.addReporter(new jasmine.JSReporter());
23
+ env.addReporter(new TAPReporter(window.print));
24
+
25
+ function report(){
26
+ if (typeof jasmine.getJSReport != 'function') return setTimeout(report, 100);
27
+ postDataToURL(JSON.stringify(jasmine.getJSReport()), '/reportTestResults', function(error, event){
28
+ if (error) return console.error(error);
29
+ console.log(event);
30
+ });
31
+ }
32
+
33
+ var oldCallback = env.currentRunner().finishCallback;
34
+ env.currentRunner().finishCallback = function(){
35
+ if (oldCallback) oldCallback.apply(this, arguments);
36
+ report();
37
+ };
38
+
39
+ function postDataToURL(data, url, callback){
40
+ var id = '$' + (+new Date()).toString(36);
41
+
42
+ var postReportingTarget = document.createElement('iframe');
43
+ postReportingTarget.id = postReportingTarget.name = 'postReportingTarget' + id;
44
+
45
+ var postReportingForm = document.createElement('form');
46
+ postReportingForm.method = 'POST';
47
+ postReportingForm.action = url;
48
+ postReportingForm.target = postReportingTarget.name;
49
+
50
+ var postReportingData = document.createElement('input');
51
+ postReportingData.type = 'hidden';
52
+ postReportingData.name = 'data';
53
+ postReportingData.value = data || '{"error":"unknown error in postReportingData"}';
54
+
55
+ postReportingForm.appendChild(postReportingData);
56
+ postReportingForm.appendChild(postReportingTarget);
57
+ postReportingForm.style.cssText = "visibility:hidden; position:absolute; bottom:100%; right:100%";
58
+
59
+ function done(error, event){
60
+ postReportingForm.parentNode.removeChild(postReportingForm);
61
+ callback(error, event);
62
+ }
63
+
64
+ postReportingTarget.onerror = function(error){
65
+ done(error);
66
+ }
67
+ postReportingTarget.onload = function(event){
68
+ done(null, event);
69
+ }
70
+ document.body.appendChild(postReportingForm);
71
+ postReportingForm.submit();
72
+ }
73
+
74
+}(window.jasmine.getEnv()));
75
+
test/testImageURL.browser.js
new
+60
@@ -0,0 +1,60 @@
1
+testImageURL._recycle = function(img){
2
+ console.log('_recycle', img);
3
+ try {
4
+ img.src = '';
5
+ img.onload = img.onerror = null;
6
+ } catch(e){}
7
+ testImageURL._recycleBin.push(img);
8
+}
9
+testImageURL.getImage = function(callback){
10
+ // if (!testImageURL._recycleBin) testImageURL._recycleBin = [new Image(),new Image(),new Image(),new Image()];
11
+ // function get(){
12
+ // if (testImageURL._recycleBin.length === 0) return setTimeout(get, 100);
13
+ // callback(testImageURL._recycleBin.shift(), testImageURL._recycle);
14
+ // }
15
+ // get();
16
+ callback(new Image(), function recycle(){});
17
+}
18
+
19
+testImageURL.defaultCallback = function(error, event){}
20
+
21
+function testImageURL(url, timeout, callback){
22
+ if (typeof timeout == 'function'){
23
+ callback = timeout;
24
+ timeout = testImageURL.timeout;
25
+ }
26
+ if (typeof callback != 'function') callback = testImageURL.defaultCallback;
27
+
28
+ testImageURL.getImage(function(img, done){
29
+ function callbackWrapper(error, event){
30
+ callbackWrapper = testImageURL.noop;
31
+ clearTimeout(timer);
32
+ done(img);
33
+ img = url = timeout = null;
34
+ callback(error, event);
35
+ error = event = callback = null;
36
+ }
37
+
38
+ var timer = setTimeout(function(){callbackWrapper(Error('timeout'));}, timeout);
39
+
40
+ try {
41
+ img.onload = function(event){ callbackWrapper(null, event || window.event); };
42
+ img.onerror = function(error){ callbackWrapper(error); };
43
+ img.src = url;
44
+
45
+ if (img.complete === true
46
+ || img.readyState == 4
47
+ || img.width > 0
48
+ || img.height > 0
49
+ || img.readyState == 'complete'
50
+ ) callbackWrapper(null, null);
51
+ }
52
+ catch(error){
53
+ callbackWrapper(error);
54
+ }
55
+ });
56
+}
57
+
58
+testImageURL.noop = function(){};
59
+
60
+testImageURL.timeout = 5000;