nexus site path corrected
[portal.git] / ecomp-portal-FE / client / bower_components / jqTree / phantomjs / runner.js
1 /*
2  * QtWebKit-powered headless test runner using PhantomJS
3  *
4  * PhantomJS binaries: http://phantomjs.org/download.html
5  * Requires PhantomJS 1.6+ (1.7+ recommended)
6  *
7  * Run with:
8  *   phantomjs runner.js [url-of-your-qunit-testsuite]
9  *
10  * e.g.
11  *   phantomjs runner.js http://localhost/qunit/test/index.html
12  */
13
14 /*global phantom:false, require:false, console:false, window:false, QUnit:false */
15
16 (function() {
17     'use strict';
18
19     var url, page, timeout,
20         args = require('system').args;
21
22     // arg[0]: scriptName, args[1...]: arguments
23     if (args.length < 2 || args.length > 3) {
24         console.error('Usage:\n  phantomjs runner.js [url-of-your-qunit-testsuite] [timeout-in-seconds]');
25         phantom.exit(1);
26     }
27
28     url = args[1];
29     page = require('webpage').create();
30     if (args[2] !== undefined) {
31         timeout = parseInt(args[2], 10);
32     }
33
34     // Route `console.log()` calls from within the Page context to the main Phantom context (i.e. current `this`)
35     page.onConsoleMessage = function(msg) {
36         console.log(msg);
37     };
38
39     page.onInitialized = function() {
40         page.evaluate(addLogging);
41     };
42
43     page.onCallback = function(message) {
44         var result,
45             failed;
46
47         if (message) {
48             if (message.name === 'QUnit.done') {
49                 result = message.data;
50                 failed = !result || !result.total || result.failed;
51
52                 if (!result.total) {
53                     console.error('No tests were executed. Are you loading tests asynchronously?');
54                 }
55
56                 // Work-around to avoid "Unsafe JavaScript attempt to access frame" warning in PhantomJS 1.9.8.
57                 // See: https://github.com/ariya/phantomjs/issues/12697
58                 page.close();
59                 setTimeout(function () { phantom.exit(failed ? 1 : 0) }, 0);
60             }
61             else if (message.name == 'Blanket.done') {
62                 console.log('Saving coverage data to data.lcov.');
63
64                 var fs = require('fs');
65                 var f = fs.open('data.lcov', 'w');
66                 f.write(message.data);
67                 f.close();
68             }
69
70         }
71     };
72
73     page.open(url, function(status) {
74         if (status !== 'success') {
75             console.error('Unable to access network: ' + status);
76             phantom.exit(1);
77         } else {
78             // Cannot do this verification with the 'DOMContentLoaded' handler because it
79             // will be too late to attach it if a page does not have any script tags.
80             var qunitMissing = page.evaluate(function() { return (typeof QUnit === 'undefined' || !QUnit); });
81             if (qunitMissing) {
82                 console.error('The `QUnit` object is not present on this page.');
83                 phantom.exit(1);
84             }
85
86             // Set a timeout on the test running, otherwise tests with async problems will hang forever
87             if (typeof timeout === 'number') {
88                 setTimeout(function() {
89                     console.error('The specified timeout of ' + timeout + ' seconds has expired. Aborting...');
90                     phantom.exit(1);
91                 }, timeout * 1000);
92             }
93
94             // Do nothing... the callback mechanism will handle everything!
95         }
96     });
97
98     function addLogging() {
99         window.document.addEventListener('DOMContentLoaded', function() {
100             var currentTestAssertions = [];
101
102             QUnit.log(function(details) {
103                 var response;
104
105                 // Ignore passing assertions
106                 if (details.result) {
107                     return;
108                 }
109
110                 response = details.message || '';
111
112                 if (typeof details.expected !== 'undefined') {
113                     if (response) {
114                         response += ', ';
115                     }
116
117                     response += 'expected: ' + details.expected + ', but was: ' + details.actual;
118                 }
119
120                 if (details.source) {
121                     response += "\n" + details.source;
122                 }
123
124                 currentTestAssertions.push('Failed assertion: ' + response);
125             });
126
127             QUnit.testDone(function(result) {
128                 var i,
129                     len,
130                     name = result.module + ': ' + result.name;
131
132                 if (result.failed) {
133                     console.log('Test failed: ' + name);
134
135                     for (i = 0, len = currentTestAssertions.length; i < len; i++) {
136                         console.log('    ' + currentTestAssertions[i]);
137                     }
138                 }
139
140                 currentTestAssertions.length = 0;
141             });
142
143             QUnit.done(function(result) {
144                 console.log('Took ' + result.runtime +  'ms to run ' + result.total + ' tests. ' + result.passed + ' passed, ' + result.failed + ' failed.');
145
146                 if (typeof window.callPhantom === 'function') {
147                     window.callPhantom({
148                         'name': 'QUnit.done',
149                         'data': result
150                     });
151                 }
152             });
153
154             blanket.options(
155                 'reporter',
156                 function(coverage) {
157                     var result = '';
158
159                     function addLine() {
160                         for (var i=0; i<arguments.length; i++) {
161                             result += arguments[i];
162                         }
163
164                         result += '\n';
165                     }
166
167                     for (var filename in coverage.files) {
168                         var data = coverage.files[filename];
169
170                         addLine('SF:', filename.replace('http://localhost:8000/', ''));
171
172                         data.source.forEach(function(line, num) {
173                             num++;
174
175                             if (data[num] !== undefined) {
176                                 addLine('DA:', num, ',', data[num]);
177                             }
178                         });
179
180                         addLine('end_of_record');
181                     }
182
183                     if (typeof window.callPhantom === 'function') {
184                         window.callPhantom({
185                             'name': 'Blanket.done',
186                             'data': result
187                         });
188                     }
189                 }
190             );
191         }, false);
192     }
193 })();