Debian Codegraph @

closure-library/closure/goog/testing/testrunner.js

raw
1 // Copyright 2007 The Closure Library Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS-IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 /**
16 * @fileoverview The test runner is a singleton object that is used to execute
17 * a goog.testing.TestCases, display the results, and expose the results to
18 * Selenium for automation. If a TestCase hasn't been registered with the
19 * runner by the time window.onload occurs, the testRunner will try to auto-
20 * discover JsUnit style test pages.
21 *
22 * The hooks for selenium are (see http://go/selenium-hook-setup):-
23 * - Boolean G_testRunner.isFinished()
24 * - Boolean G_testRunner.isSuccess()
25 * - String G_testRunner.getReport()
26 * - number G_testRunner.getRunTime()
27 * - Object<string, Array<string>> G_testRunner.getTestResults()
28 *
29 * Testing code should not have dependencies outside of goog.testing so as to
30 * reduce the chance of masking missing dependencies.
31 *
32 */
33
34 goog.setTestOnly('goog.testing.TestRunner');
35 goog.provide('goog.testing.TestRunner');
36
37 goog.require('goog.dom');
38 goog.require('goog.dom.TagName');
39 goog.require('goog.dom.safe');
40 goog.require('goog.testing.TestCase');
41
42
43
44 /**
45 * Construct a test runner.
46 *
47 * NOTE(user): This is currently pretty weird, I'm essentially trying to
48 * create a wrapper that the Selenium test can hook into to query the state of
49 * the running test case, while making goog.testing.TestCase general.
50 *
51 * @constructor
52 */
53 goog.testing.TestRunner = function() {
54 /**
55 * Errors that occurred in the window.
56 * @type {!Array<string>}
57 */
58 this.errors = [];
59
60 /**
61 * Reference to the active test case.
62 * @type {?goog.testing.TestCase}
63 */
64 this.testCase = null;
65
66 /**
67 * Whether the test runner has been initialized yet.
68 * @type {boolean}
69 */
70 this.initialized = false;
71
72 /**
73 * Element created in the document to add test results to.
74 * @private {?Element}
75 */
76 this.logEl_ = null;
77
78 /**
79 * Function to use when filtering errors.
80 * @private {(function(string))?}
81 */
82 this.errorFilter_ = null;
83
84 /**
85 * Whether an empty test case counts as an error.
86 * @private {boolean}
87 */
88 this.strict_ = true;
89
90 /**
91 * An id unique to this runner. Checked by the server during polling to
92 * verify that the page was not reloaded.
93 * @private {!string}
94 */
95 this.uniqueId_ = Math.random() + '';
96 };
97
98 /**
99 * The uuid is embedded in the URL search. This function allows us to mock
100 * the search in the test.
101 * @return {string}
102 */
103 goog.testing.TestRunner.prototype.getSearchString = function() {
104 return window.location.search;
105 };
106
107 /**
108 * Returns the unique id for this test page.
109 * @return {!string}
110 */
111 goog.testing.TestRunner.prototype.getUniqueId = function() {
112 return this.uniqueId_;
113 };
114
115 /**
116 * Initializes the test runner.
117 * @param {goog.testing.TestCase} testCase The test case to initialize with.
118 */
119 goog.testing.TestRunner.prototype.initialize = function(testCase) {
120 if (this.testCase && this.testCase.running) {
121 throw new Error(
122 'The test runner is already waiting for a test to complete');
123 }
124 this.testCase = testCase;
125 this.initialized = true;
126 };
127
128
129 /**
130 * By default, the test runner is strict, and fails if it runs an empty
131 * test case.
132 * @param {boolean} strict Whether the test runner should fail on an empty
133 * test case.
134 */
135 goog.testing.TestRunner.prototype.setStrict = function(strict) {
136 this.strict_ = strict;
137 };
138
139
140 /**
141 * @return {boolean} Whether the test runner should fail on an empty
142 * test case.
143 */
144 goog.testing.TestRunner.prototype.isStrict = function() {
145 return this.strict_;
146 };
147
148
149 /**
150 * Returns true if the test runner is initialized.
151 * Used by Selenium Hooks.
152 * @return {boolean} Whether the test runner is active.
153 */
154 goog.testing.TestRunner.prototype.isInitialized = function() {
155 return this.initialized;
156 };
157
158
159 /**
160 * Returns false if the test runner has not finished successfully.
161 * Used by Selenium Hooks.
162 * @return {boolean} Whether the test runner is not active.
163 */
164 goog.testing.TestRunner.prototype.isFinished = function() {
165 return this.errors.length > 0 || this.isComplete();
166 };
167
168
169 /**
170 * Returns true if the test runner is finished.
171 * @return {boolean} True if the test runner started and subsequently completed.
172 */
173 goog.testing.TestRunner.prototype.isComplete = function() {
174 return this.initialized && !!this.testCase && this.testCase.started &&
175 !this.testCase.running;
176 };
177
178 /**
179 * Returns true if the test case didn't fail.
180 * Used by Selenium Hooks.
181 * @return {boolean} Whether the current test returned successfully.
182 */
183 goog.testing.TestRunner.prototype.isSuccess = function() {
184 return !this.hasErrors() && !!this.testCase && this.testCase.isSuccess();
185 };
186
187
188 /**
189 * Returns true if the test case runner has errors that were caught outside of
190 * the test case.
191 * @return {boolean} Whether there were JS errors.
192 */
193 goog.testing.TestRunner.prototype.hasErrors = function() {
194 return this.errors.length > 0;
195 };
196
197
198 /**
199 * Logs an error that occurred. Used in the case of environment setting up
200 * an onerror handler.
201 * @param {string} msg Error message.
202 */
203 goog.testing.TestRunner.prototype.logError = function(msg) {
204 if (this.isComplete()) {
205 // Once the user has checked their code, subsequent errors can occur
206 // because of tearDown actions. For now, log these but do not fail the test.
207 this.log('Error after test completed: ' + msg);
208 return;
209 }
210 if (!this.errorFilter_ || this.errorFilter_.call(null, msg)) {
211 this.errors.push(msg);
212 }
213 };
214
215
216 /**
217 * Log failure in current running test.
218 * @param {Error} ex Exception.
219 */
220 goog.testing.TestRunner.prototype.logTestFailure = function(ex) {
221 var testName = /** @type {string} */ (goog.testing.TestCase.currentTestName);
222 if (this.testCase) {
223 this.testCase.logError(testName, ex);
224 } else {
225 // NOTE: Do not forget to log the original exception raised.
226 throw new Error(
227 'Test runner not initialized with a test case. Original ' +
228 'exception: ' + ex.message);
229 }
230 };
231
232
233 /**
234 * Sets a function to use as a filter for errors.
235 * @param {function(string)} fn Filter function.
236 */
237 goog.testing.TestRunner.prototype.setErrorFilter = function(fn) {
238 this.errorFilter_ = fn;
239 };
240
241
242 /**
243 * Returns a report of the test case that ran.
244 * Used by Selenium Hooks.
245 * @param {boolean=} opt_verbose If true results will include data about all
246 * tests, not just what failed.
247 * @return {string} A report summary of the test.
248 */
249 goog.testing.TestRunner.prototype.getReport = function(opt_verbose) {
250 var report = [];
251 if (this.testCase) {
252 report.push(this.testCase.getReport(opt_verbose));
253 }
254 if (this.errors.length > 0) {
255 report.push('JavaScript errors detected by test runner:');
256 report.push.apply(report, this.errors);
257 report.push('\n');
258 }
259 return report.join('\n');
260 };
261
262
263 /**
264 * Returns the amount of time it took for the test to run.
265 * Used by Selenium Hooks.
266 * @return {number} The run time, in milliseconds.
267 */
268 goog.testing.TestRunner.prototype.getRunTime = function() {
269 return this.testCase ? this.testCase.getRunTime() : 0;
270 };
271
272
273 /**
274 * Returns the number of script files that were loaded in order to run the test.
275 * @return {number} The number of script files.
276 */
277 goog.testing.TestRunner.prototype.getNumFilesLoaded = function() {
278 return this.testCase ? this.testCase.getNumFilesLoaded() : 0;
279 };
280
281
282 /**
283 * Executes a test case and prints the results to the window.
284 */
285 goog.testing.TestRunner.prototype.execute = function() {
286 if (!this.testCase) {
287 throw new Error(
288 'The test runner must be initialized with a test case ' +
289 'before execute can be called.');
290 }
291
292 if (this.strict_ && this.testCase.getCount() == 0) {
293 throw new Error(
294 'No tests found in given test case: ' + this.testCase.getName() + '. ' +
295 'By default, the test runner fails if a test case has no tests. ' +
296 'To modify this behavior, see goog.testing.TestRunner\'s ' +
297 'setStrict() method, or G_testRunner.setStrict()');
298 }
299
300 this.testCase.setCompletedCallback(goog.bind(this.onComplete_, this));
301 if (goog.testing.TestRunner.shouldUsePromises_(this.testCase)) {
302 this.testCase.runTestsReturningPromise();
303 } else {
304 this.testCase.runTests();
305 }
306 };
307
308
309 /**
310 * @param {!goog.testing.TestCase} testCase
311 * @return {boolean}
312 * @private
313 */
314 goog.testing.TestRunner.shouldUsePromises_ = function(testCase) {
315 return testCase.constructor === goog.testing.TestCase;
316 };
317
318
319 /** @const {string} The ID of the element to log output to. */
320 goog.testing.TestRunner.TEST_LOG_ID = 'closureTestRunnerLog';
321
322
323 /**
324 * Writes the results to the document when the test case completes.
325 * @private
326 */
327 goog.testing.TestRunner.prototype.onComplete_ = function() {
328 var log = this.testCase.getReport(true);
329 if (this.errors.length > 0) {
330 log += '\n' + this.errors.join('\n');
331 }
332
333 if (!this.logEl_) {
334 var el = document.getElementById(goog.testing.TestRunner.TEST_LOG_ID);
335 if (el == null) {
336 el = goog.dom.createElement(goog.dom.TagName.DIV);
337 el.id = goog.testing.TestRunner.TEST_LOG_ID;
338 document.body.appendChild(el);
339 }
340 this.logEl_ = el;
341 }
342
343 // Highlight the page to indicate the overall outcome.
344 this.writeLog(log);
345
346 // TODO(chrishenry): Make this work with multiple test cases (b/8603638).
347 var runAgainLink = goog.dom.createElement(goog.dom.TagName.A);
348 runAgainLink.style.display = 'inline-block';
349 runAgainLink.style.fontSize = 'small';
350 runAgainLink.style.marginBottom = '16px';
351 runAgainLink.href = '';
352 runAgainLink.onclick = goog.bind(function() {
353 this.execute();
354 return false;
355 }, this);
356 runAgainLink.innerHTML = 'Run again without reloading';
357 this.logEl_.appendChild(runAgainLink);
358 };
359
360
361 /**
362 * Writes a nicely formatted log out to the document.
363 * @param {string} log The string to write.
364 */
365 goog.testing.TestRunner.prototype.writeLog = function(log) {
366 var lines = log.split('\n');
367 for (var i = 0; i < lines.length; i++) {
368 var line = lines[i];
369 var color;
370 var isPassed = /PASSED/.test(line);
371 var isFailOrError =
372 /FAILED/.test(line) || /ERROR/.test(line) || /NO TESTS RUN/.test(line);
373 if (isPassed) {
374 color = 'darkgreen';
375 } else if (isFailOrError) {
376 color = 'darkred';
377 } else {
378 color = '#333';
379 }
380 var div = goog.dom.createElement(goog.dom.TagName.DIV);
381 if (line.substr(0, 2) == '> ') {
382 // The stack trace may contain links so it has to be interpreted as HTML.
383 div.innerHTML = line;
384 } else {
385 div.appendChild(document.createTextNode(line));
386 }
387
388 var testNameMatch = /(\S+) (\[[^\]]*] )?: (FAILED|ERROR|PASSED)/.exec(line);
389 if (testNameMatch) {
390 // Build a URL to run the test individually. If this test was already
391 // part of another subset test, we need to overwrite the old runTests
392 // query parameter. We also need to do this without bringing in any
393 // extra dependencies, otherwise we could mask missing dependency bugs.
394 var newSearch = 'runTests=' + testNameMatch[1];
395 var search = window.location.search;
396 if (search) {
397 var oldTests = /runTests=([^&]*)/.exec(search);
398 if (oldTests) {
399 newSearch = search.substr(0, oldTests.index) + newSearch +
400 search.substr(oldTests.index + oldTests[0].length);
401 } else {
402 newSearch = search + '&' + newSearch;
403 }
404 } else {
405 newSearch = '?' + newSearch;
406 }
407 var href = window.location.href;
408 var hash = window.location.hash;
409 if (hash && hash.charAt(0) != '#') {
410 hash = '#' + hash;
411 }
412 href = href.split('#')[0].split('?')[0] + newSearch + hash;
413
414 // Add the link.
415 var a = goog.dom.createElement(goog.dom.TagName.A);
416 a.innerHTML = '(run individually)';
417 a.style.fontSize = '0.8em';
418 a.style.color = '#888';
419 goog.dom.safe.setAnchorHref(a, href);
420 div.appendChild(document.createTextNode(' '));
421 div.appendChild(a);
422 }
423
424 div.style.color = color;
425 div.style.font = 'normal 100% monospace';
426 div.style.wordWrap = 'break-word';
427 if (i == 0) {
428 // Highlight the first line as a header that indicates the test outcome.
429 div.style.padding = '20px';
430 div.style.marginBottom = '10px';
431 if (isPassed) {
432 div.style.border = '1px solid ' + color;
433 div.style.backgroundColor = '#eeffee';
434 } else if (isFailOrError) {
435 div.style.border = '5px solid ' + color;
436 div.style.backgroundColor = '#ffeeee';
437 } else {
438 div.style.border = '1px solid black';
439 div.style.backgroundColor = '#eeeeee';
440 }
441 }
442
443 try {
444 div.style.whiteSpace = 'pre-wrap';
445 } catch (e) {
446 // NOTE(brenneman): IE raises an exception when assigning to pre-wrap.
447 // Thankfully, it doesn't collapse whitespace when using monospace fonts,
448 // so it will display correctly if we ignore the exception.
449 }
450
451 if (i < 2) {
452 div.style.fontWeight = 'bold';
453 }
454 this.logEl_.appendChild(div);
455 }
456 };
457
458
459 /**
460 * Logs a message to the current test case.
461 * @param {string} s The text to output to the log.
462 */
463 goog.testing.TestRunner.prototype.log = function(s) {
464 if (this.testCase) {
465 this.testCase.log(s);
466 }
467 };
468
469
470 // TODO(nnaze): Properly handle serving test results when multiple test cases
471 // are run.
472 /**
473 * @return {Object<string, !Array<!goog.testing.TestCase.IResult>>} A map of
474 * test names to a list of test failures (if any) to provide formatted data
475 * for the test runner.
476 */
477 goog.testing.TestRunner.prototype.getTestResults = function() {
478 if (this.testCase) {
479 return this.testCase.getTestResults();
480 }
481 return null;
482 };
483
484
485 /**
486 * Returns the test results as json.
487 * This is called by the testing infrastructure through G_testrunner.
488 * @return {?string} Tests results object.
489 */
490 goog.testing.TestRunner.prototype.getTestResultsAsJson = function() {
491 if (this.testCase) {
492 return this.testCase.getTestResultsAsJson();
493 }
494 return null;
495 };