|
6 | 6 |
|
7 | 7 | var webdriver = require('selenium-webdriver');
|
8 | 8 |
|
9 |
| -var flow = webdriver.promise.controlFlow(); |
10 |
| - |
11 | 9 | /**
|
12 | 10 | * Wraps a function so that all passed arguments are ignored.
|
13 | 11 | * @param {!Function} fn The function to wrap.
|
@@ -67,7 +65,7 @@ function validateString(stringtoValidate) {
|
67 | 65 | * @param {!Function} globalFn The function to wrap.
|
68 | 66 | * @return {!Function} The new function.
|
69 | 67 | */
|
70 |
| -function wrapInControlFlow(globalFn, fnName) { |
| 68 | +function wrapInControlFlow(flow, globalFn, fnName) { |
71 | 69 | return function() {
|
72 | 70 | var driverError = new Error();
|
73 | 71 | driverError.stack = driverError.stack.replace(/ +at.+jasminewd.+\n/, '');
|
@@ -141,18 +139,41 @@ function wrapInControlFlow(globalFn, fnName) {
|
141 | 139 | };
|
142 | 140 | }
|
143 | 141 |
|
144 |
| -global.it = wrapInControlFlow(global.it, 'it'); |
145 |
| -global.fit = wrapInControlFlow(global.fit, 'fit'); |
146 |
| -global.beforeEach = wrapInControlFlow(global.beforeEach, 'beforeEach'); |
147 |
| -global.afterEach = wrapInControlFlow(global.afterEach, 'afterEach'); |
148 |
| -global.beforeAll = wrapInControlFlow(global.beforeAll, 'beforeAll'); |
149 |
| -global.afterAll = wrapInControlFlow(global.afterAll, 'afterAll'); |
| 142 | +/** |
| 143 | + * Initialize the JasmineWd adapter with a particlar webdriver instance. We |
| 144 | + * pass webdriver here instead of using require() in order to ensure Protractor |
| 145 | + * and Jasminews are using the same webdriver instance. |
| 146 | + * @param {Object} flow. The ControlFlow to wrap tests in. |
| 147 | + */ |
| 148 | +function initJasmineWd(flow) { |
| 149 | + if (jasmine.JasmineWdInitialized) { |
| 150 | + throw Error('JasmineWd already initialized when init() was called'); |
| 151 | + } |
| 152 | + jasmine.JasmineWdInitialized = true; |
| 153 | + |
| 154 | + global.it = wrapInControlFlow(flow, global.it, 'it'); |
| 155 | + global.fit = wrapInControlFlow(flow, global.fit, 'fit'); |
| 156 | + global.beforeEach = wrapInControlFlow(flow, global.beforeEach, 'beforeEach'); |
| 157 | + global.afterEach = wrapInControlFlow(flow, global.afterEach, 'afterEach'); |
| 158 | + global.beforeAll = wrapInControlFlow(flow, global.beforeAll, 'beforeAll'); |
| 159 | + global.afterAll = wrapInControlFlow(flow, global.afterAll, 'afterAll'); |
| 160 | + |
| 161 | + // On timeout, the flow should be reset. This will prevent webdriver tasks |
| 162 | + // from overflowing into the next test and causing it to fail or timeout |
| 163 | + // as well. This is done in the reporter instead of an afterEach block |
| 164 | + // to ensure that it runs after any afterEach() blocks with webdriver tasks |
| 165 | + // get to complete first. |
| 166 | + jasmine.getEnv().addReporter(new OnTimeoutReporter(function() { |
| 167 | + console.warn('A Jasmine spec timed out. Resetting the WebDriver Control Flow.'); |
| 168 | + flow.reset(); |
| 169 | + })); |
| 170 | +} |
150 | 171 |
|
151 | 172 | var originalExpect = global.expect;
|
152 | 173 | global.expect = function(actual) {
|
153 | 174 | if (actual instanceof webdriver.WebElement) {
|
154 |
| - throw 'expect called with WebElement argument, expected a Promise. ' + |
155 |
| - 'Did you mean to use .getText()?'; |
| 175 | + throw Error('expect called with WebElement argument, expected a Promise. ' + |
| 176 | + 'Did you mean to use .getText()?'); |
156 | 177 | }
|
157 | 178 | return originalExpect(actual);
|
158 | 179 | };
|
@@ -268,12 +289,4 @@ OnTimeoutReporter.prototype.specDone = function(result) {
|
268 | 289 | }
|
269 | 290 | };
|
270 | 291 |
|
271 |
| -// On timeout, the flow should be reset. This will prevent webdriver tasks |
272 |
| -// from overflowing into the next test and causing it to fail or timeout |
273 |
| -// as well. This is done in the reporter instead of an afterEach block |
274 |
| -// to ensure that it runs after any afterEach() blocks with webdriver tasks |
275 |
| -// get to complete first. |
276 |
| -jasmine.getEnv().addReporter(new OnTimeoutReporter(function() { |
277 |
| - console.warn('A Jasmine spec timed out. Resetting the WebDriver Control Flow.'); |
278 |
| - flow.reset(); |
279 |
| -})); |
| 292 | +module.exports.init = initJasmineWd; |
0 commit comments