forked from SeleniumHQ/selenium
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestcase.js
More file actions
168 lines (146 loc) · 5 KB
/
Copy pathtestcase.js
File metadata and controls
168 lines (146 loc) · 5 KB
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
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
/**
* @fileoverview Defines a special test case that runs each test inside of a
* {@code webdriver.Application}. This allows each phase to schedule
* asynchronous actions that run to completion before the next phase of the
* test.
*
* This file requires the global {@code G_testRunner} to be initialized before
* use. This can be accomplished by also importing
* {@link webdriver.testing.jsunit}. This namespace is not required by default
* to improve interoperability with other namespaces that may initialize
* G_testRunner.
*/
goog.provide('webdriver.testing.TestCase');
goog.require('goog.testing.TestCase');
goog.require('webdriver.promise');
/** @suppress {extraRequire} Imported for user convenience. */
goog.require('webdriver.testing.asserts');
/**
* Constructs a test case that synchronizes each test case with the singleton
* {@code webdriver.promise.ControlFlow}.
*
* @param {!webdriver.testing.Client} client The test client to use for
* reporting test results.
* @param {string=} opt_name The name of the test case, defaults to
* 'Untitled Test Case'.
* @constructor
* @extends {goog.testing.TestCase}
*/
webdriver.testing.TestCase = function(client, opt_name) {
goog.base(this, opt_name);
/** @private {!webdriver.testing.Client} */
this.client_ = client;
};
goog.inherits(webdriver.testing.TestCase, goog.testing.TestCase);
/**
* Executes the next test inside its own {@code webdriver.Application}.
* @override
*/
webdriver.testing.TestCase.prototype.cycleTests = function() {
var test = this.next();
if (!test) {
this.finalize();
return;
}
goog.testing.TestCase.currentTestName = test.name;
this.result_.runCount++;
this.log('Running test: ' + test.name);
this.client_.sendTestStartedEvent(test.name);
var self = this;
var hadError = false;
var app = webdriver.promise.controlFlow();
this.runSingleTest_(test, onError).then(function() {
hadError || self.doSuccess(test);
self.timeout(function() {
self.cycleTests();
}, 100);
});
function onError(e) {
hadError = true;
self.doError(test, e);
// Note: result_ is a @protected field but still uses the trailing
// underscore.
var err = self.result_.errors[self.result_.errors.length - 1];
self.client_.sendErrorEvent(err.toString());
}
};
/** @override */
webdriver.testing.TestCase.prototype.logError = function(name, opt_e) {
var errMsg = null;
var stack = null;
if (opt_e) {
this.log(opt_e);
if (goog.isString(opt_e)) {
errMsg = opt_e;
} else {
errMsg = opt_e.toString();
stack = opt_e.stack.substring(errMsg.length + 1);
}
} else {
errMsg = 'An unknown error occurred';
}
var err = new goog.testing.TestCase.Error(name, errMsg, stack);
// Avoid double logging.
if (!opt_e || !opt_e['isJsUnitException'] ||
!opt_e['loggedJsUnitException']) {
this.saveMessage(err.toString());
}
if (opt_e && opt_e['isJsUnitException']) {
opt_e['loggedJsUnitException'] = true;
}
return err;
};
/**
* Executes a single test, scheduling each phase with the global application.
* Each phase will wait for the application to go idle before moving on to the
* next test phase. This function models the follow basic test flow:
*
* try {
* this.setUp.call(test.scope);
* test.ref.call(test.scope);
* } catch (ex) {
* onError(ex);
* } finally {
* try {
* this.tearDown.call(test.scope);
* } catch (e) {
* onError(e);
* }
* }
*
* @param {!goog.testing.TestCase.Test} test The test to run.
* @param {function(*)} onError The function to call each time an error is
* detected.
* @return {!webdriver.promise.Promise} A promise that will be resolved when the
* test has finished running.
* @private
*/
webdriver.testing.TestCase.prototype.runSingleTest_ = function(test, onError) {
var flow = webdriver.promise.controlFlow();
return execute(test.name + '.setUp()', this.setUp)().
then(execute(test.name + '()', test.ref)).
thenCatch(onError).
then(execute(test.name + '.tearDown()', this.tearDown)).
thenCatch(onError);
function execute(description, fn) {
return function() {
return flow.execute(goog.bind(fn, test.scope), description);
}
}
};