Skip to content

Commit ff2e624

Browse files
committed
fix(webdriver): Pass in the control flow.
Fixex angular/protractor#3505, which was caused by Protractor and Jasminewd finding different webdriver instances through require(), and thus using different ControlFlows.
1 parent a8ca7e3 commit ff2e624

File tree

4 files changed

+43
-26
lines changed

4 files changed

+43
-26
lines changed

index.js

+33-20
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66

77
var webdriver = require('selenium-webdriver');
88

9-
var flow = webdriver.promise.controlFlow();
10-
119
/**
1210
* Wraps a function so that all passed arguments are ignored.
1311
* @param {!Function} fn The function to wrap.
@@ -67,7 +65,7 @@ function validateString(stringtoValidate) {
6765
* @param {!Function} globalFn The function to wrap.
6866
* @return {!Function} The new function.
6967
*/
70-
function wrapInControlFlow(globalFn, fnName) {
68+
function wrapInControlFlow(flow, globalFn, fnName) {
7169
return function() {
7270
var driverError = new Error();
7371
driverError.stack = driverError.stack.replace(/ +at.+jasminewd.+\n/, '');
@@ -141,18 +139,41 @@ function wrapInControlFlow(globalFn, fnName) {
141139
};
142140
}
143141

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+
}
150171

151172
var originalExpect = global.expect;
152173
global.expect = function(actual) {
153174
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()?');
156177
}
157178
return originalExpect(actual);
158179
};
@@ -268,12 +289,4 @@ OnTimeoutReporter.prototype.specDone = function(result) {
268289
}
269290
};
270291

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;

spec/adapterSpec.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
var webdriver = require('selenium-webdriver');
22
var common = require('./common.js');
3-
require('../index.js');
43

54
/**
65
* Tests for the WebDriverJS Jasmine-Node Adapter. These tests use
@@ -57,6 +56,11 @@ describe('webdriverJS Jasmine adapter', function() {
5756
beforeEachMsg = '';
5857
});
5958

59+
it('should only allow initializing once', function() {
60+
expect(require('../index.js').init).toThrow(
61+
Error('JasmineWd already initialized when init() was called'));
62+
});
63+
6064
it('should pass normal synchronous tests', function() {
6165
expect(true).toEqual(true);
6266
});
@@ -167,8 +171,8 @@ describe('webdriverJS Jasmine adapter', function() {
167171

168172
expect(function() {
169173
expect(webElement).toEqual(4);
170-
}).toThrow('expect called with WebElement argument, expected a Promise. ' +
171-
'Did you mean to use .getText()?');
174+
}).toThrow(Error('expect called with WebElement argument, expected a Promise. ' +
175+
'Did you mean to use .getText()?'));
172176
});
173177

174178
it('should pass after the timed out tests', function() {

spec/common.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
require('../index.js');
21
var webdriver = require('selenium-webdriver');
32

3+
var flow = webdriver.promise.controlFlow();
4+
require('../index.js').init(flow);
5+
46
exports.getFakeDriver = function() {
5-
var flow = webdriver.promise.controlFlow();
67
return {
78
controlFlow: function() {
89
return flow;

spec/errorSpec.js

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
var webdriver = require('selenium-webdriver');
22
var common = require('./common.js');
3-
require('../index.js');
43

54
/**
65
* Error tests for the WebDriverJS Jasmine-Node Adapter. These tests use

0 commit comments

Comments
 (0)