Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
seleniumhq
GitHub Repository: seleniumhq/selenium
Path: blob/trunk/third_party/closure/goog/testing/jsunit.js
4501 views
1
/**
2
* @license
3
* Copyright The Closure Library Authors.
4
* SPDX-License-Identifier: Apache-2.0
5
*/
6
7
/**
8
* @fileoverview Utilities for working with JsUnit. Writes out the JsUnit file
9
* that needs to be included in every unit test.
10
*
11
* Testing code should not have dependencies outside of goog.testing so as to
12
* reduce the chance of masking missing dependencies.
13
*/
14
15
goog.setTestOnly('goog.testing.jsunit');
16
goog.provide('goog.testing.jsunit');
17
18
goog.require('goog.dom.TagName');
19
goog.require('goog.testing.TestCase');
20
goog.require('goog.testing.TestRunner');
21
goog.require('goog.testing.asserts');
22
23
24
/**
25
* @define {boolean} If this code is being parsed by JsTestC, we let it disable
26
* the onload handler to avoid running the test in JsTestC.
27
*/
28
goog.testing.jsunit.AUTO_RUN_ONLOAD =
29
goog.define('goog.testing.jsunit.AUTO_RUN_ONLOAD', true);
30
31
32
/**
33
* @define {number} Sets a delay in milliseconds after the window onload event
34
* and running the tests. See goog.testing.jsunit.AUTO_RUN_DELAY_IN_MS.
35
*/
36
goog.testing.jsunit.AUTO_RUN_DELAY_IN_MS_DEFAULT =
37
goog.define('goog.testing.jsunit.AUTO_RUN_DELAY_IN_MS_DEFAULT', 0);
38
39
/**
40
* @type {number} Sets a delay in milliseconds after the window onload event
41
* and running the tests. Used as a workaround for IE failing to report load
42
* event if the page has iframes. The appropriate value is zero;
43
* maximum should be 500. Do not use this value to support asynchronous tests.
44
*/
45
goog.testing.jsunit.AUTO_RUN_DELAY_IN_MS =
46
goog.testing.jsunit.AUTO_RUN_DELAY_IN_MS_DEFAULT;
47
48
49
(function() {
50
'use strict';
51
// Only allow one global test runner to be created on a page.
52
if (goog.global['G_testRunner'] instanceof goog.testing.TestRunner) {
53
return;
54
}
55
56
// Increases the maximum number of stack frames in Google Chrome from the
57
// default 10 to 50 to get more useful stack traces.
58
Error.stackTraceLimit = 50;
59
60
// Store a reference to the window's timeout so that it can't be overridden
61
// by tests.
62
/** @type {!Function} */
63
var realTimeout = window.setTimeout;
64
65
// Create a test runner.
66
var tr = new goog.testing.TestRunner();
67
68
// Export it so that it can be queried by Selenium and tests that use a
69
// compiled test runner.
70
goog.exportSymbol('G_testRunner', tr);
71
goog.exportSymbol('G_testRunner.initialize', tr.initialize);
72
goog.exportSymbol('G_testRunner.isInitialized', tr.isInitialized);
73
goog.exportSymbol('G_testRunner.isFinished', tr.isFinished);
74
goog.exportSymbol('G_testRunner.getUniqueId', tr.getUniqueId);
75
goog.exportSymbol('G_testRunner.isSuccess', tr.isSuccess);
76
goog.exportSymbol('G_testRunner.getReport', tr.getReport);
77
goog.exportSymbol('G_testRunner.getRunTime', tr.getRunTime);
78
goog.exportSymbol('G_testRunner.getNumFilesLoaded', tr.getNumFilesLoaded);
79
goog.exportSymbol('G_testRunner.setStrict', tr.setStrict);
80
goog.exportSymbol('G_testRunner.logTestFailure', tr.logTestFailure);
81
goog.exportSymbol('G_testRunner.getTestResults', tr.getTestResults);
82
goog.exportSymbol('G_testRunner.getTestResultsAsJson', tr.getTestResultsAsJson);
83
84
// Export debug as a global function for JSUnit compatibility. This just
85
// calls log on the current test case.
86
if (!goog.global['debug']) {
87
goog.exportSymbol('debug', goog.bind(tr.log, tr));
88
}
89
90
// If the application has defined a global error filter, set it now. This
91
// allows users who use a base test include to set the error filter before
92
// the testing code is loaded.
93
if (goog.global['G_errorFilter']) {
94
tr.setErrorFilter(goog.global['G_errorFilter']);
95
}
96
97
var maybeGetStack = function(error) {
98
'use strict';
99
var stack = error && error.stack;
100
return typeof stack === 'string' ? stack : '';
101
};
102
103
// Add an error handler to report errors that may occur during
104
// initialization of the page.
105
var onerror = window.onerror;
106
window.onerror = function(messageOrEvent, url, line) {
107
'use strict';
108
// TODO(johnlenz): fix this function parameters once the "onerror"
109
// definition has been corrected.
110
// colno and errObj were added later.
111
var colno = arguments[3];
112
var errObj = arguments[4];
113
// Call any existing onerror handlers, except our boot handler.
114
if (onerror && onerror != window['__onerror_at_boot']) {
115
onerror.apply(window, arguments);
116
}
117
var stack = maybeGetStack(errObj || messageOrEvent);
118
if (stack) {
119
tr.logError(String(messageOrEvent) + '\n' + stack);
120
} else if (typeof messageOrEvent == 'object') {
121
var error = /** @type {{target: ?}} */ (messageOrEvent);
122
// Some older webkit browsers pass an event object as the only argument
123
// to window.onerror. It doesn't contain an error message, url or line
124
// number. We therefore log as much info as we can.
125
if (error.target && error.target.tagName == goog.dom.TagName.SCRIPT) {
126
tr.logError('UNKNOWN ERROR: Script ' + error.target.src);
127
} else {
128
tr.logError('UNKNOWN ERROR: No error information available.');
129
}
130
} else {
131
// Add the column if it is available, older browsers won't have it.
132
var colstr = colno != null ? '\nColumn: ' + colno : '';
133
tr.logError(
134
'JS ERROR: ' + messageOrEvent + '\nURL: ' + url + '\nLine: ' + line +
135
colstr);
136
}
137
};
138
139
/**
140
* The onerror handler that may have been set by the test runner.
141
* @type {?function(string, string=, number=, number=, Object=)}
142
*/
143
window['__onerror_at_boot'] = window['__onerror_at_boot'] || null;
144
/**
145
* The arguments for any call to window.onerror occuring before this point.
146
* @type {?Array<!Array<?>>}
147
*/
148
window['__errors_since_boot'] = window['__errors_since_boot'] || null;
149
150
if (window['__onerror_at_boot']) {
151
if (window['__errors_since_boot']) {
152
for (var i = 0; i < window['__errors_since_boot'].length; i++) {
153
var args = window['__errors_since_boot'][i];
154
window.onerror.apply(window, args);
155
}
156
}
157
// http://perfectionkills.com/understanding-delete/#ie_bugs
158
window['__onerror_at_boot'] = null;
159
}
160
161
// Create an onload handler, if the test runner hasn't been initialized then
162
// no test has been registered with the test runner by the test file. We
163
// then create a new test case and auto discover any tests in the global
164
// scope. If this code is being parsed by JsTestC, we let it disable the
165
// onload handler to avoid running the test in JsTestC.
166
if (goog.testing.jsunit.AUTO_RUN_ONLOAD) {
167
var onload = window.onload;
168
window.onload = function(e) {
169
'use strict';
170
// Call any existing onload handlers.
171
if (onload) {
172
onload(e);
173
}
174
// Execute the test on the next turn, to allow the WebDriver.get()
175
// operation to return to the test runner and begin polling.
176
var executionDelayAfterLoad = goog.testing.jsunit.AUTO_RUN_DELAY_IN_MS;
177
178
realTimeout(function() {
179
'use strict';
180
if (!tr.initialized) {
181
var testCase = new goog.testing.TestCase(document.title);
182
goog.testing.TestCase.initializeTestCase(testCase);
183
tr.initialize(testCase);
184
}
185
tr.execute();
186
}, executionDelayAfterLoad);
187
window.onload = null;
188
};
189
}
190
})();
191
192