Skip to content

Commit 4ab4f2f

Browse files
committed
chore(promises): Wait for promises explicitly
See #68 for details
1 parent e05cd3c commit 4ab4f2f

File tree

2 files changed

+56
-12
lines changed

2 files changed

+56
-12
lines changed

index.js

+36-12
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,22 @@ function validateString(stringtoValidate) {
5959
}
6060
}
6161

62+
/**
63+
* Calls a function once the control flow is idle
64+
* @param {webdriver.promise.ControlFlow} flow The Web Driver control flow
65+
* @param {!Function} fn The function to call
66+
*/
67+
function callWhenIdle(flow, fn) {
68+
if (flow.isIdle()) {
69+
fn();
70+
} else {
71+
flow.once(webdriver.promise.ControlFlow.EventType.IDLE, function() {
72+
fn();
73+
});
74+
}
75+
}
76+
77+
6278
/**
6379
* Wraps a function so it runs inside a webdriver.promise.ControlFlow and
6480
* waits for the flow to complete before continuing.
@@ -78,30 +94,38 @@ function wrapInControlFlow(flow, globalFn, fnName) {
7894

7995
flow.execute(function controlFlowExecute() {
8096
return new webdriver.promise.Promise(function(fulfill, reject) {
97+
function wrappedReject(err) {
98+
var wrappedErr = new Error(err);
99+
reject(wrappedErr);
100+
}
81101
if (async) {
82102
// If testFn is async (it expects a done callback), resolve the promise of this
83103
// test whenever that callback says to. Any promises returned from testFn are
84104
// ignored.
85105
var proxyDone = fulfill;
86-
proxyDone.fail = function(err) {
87-
var wrappedErr = new Error(err);
88-
reject(wrappedErr);
89-
};
106+
proxyDone.fail = wrappedReject;
90107
testFn(proxyDone);
91108
} else {
92109
// Without a callback, testFn can return a promise, or it will
93110
// be assumed to have completed synchronously.
94-
fulfill(testFn());
111+
var ret = testFn();
112+
if (webdriver.promise.isPromise(ret)) {
113+
ret.then(fulfill, wrappedReject);
114+
} else {
115+
fulfill(ret);
116+
}
95117
}
96118
}, flow);
97-
}, 'Run ' + fnName + description + ' in control flow').then(seal(done), function(err) {
98-
if (!err) {
99-
err = new Error('Unknown Error');
100-
err.stack = '';
119+
}, 'Run ' + fnName + description + ' in control flow').then(
120+
callWhenIdle.bind(null, flow, done), function(err) {
121+
if (!err) {
122+
err = new Error('Unknown Error');
123+
err.stack = '';
124+
}
125+
err.stack = err.stack + '\nFrom asynchronous test: \n' + driverError.stack;
126+
callWhenIdle(flow, done.fail.bind(done, err));
101127
}
102-
err.stack = err.stack + '\nFrom asynchronous test: \n' + driverError.stack;
103-
done.fail(err);
104-
});
128+
);
105129
};
106130
}
107131

spec/adapterSpec.js

+20
Original file line numberDiff line numberDiff line change
@@ -242,4 +242,24 @@ describe('webdriverJS Jasmine adapter', function() {
242242
expect(spec3.description).toBe('test3');
243243
});
244244
});
245+
246+
describe('should handle returning native promises', () => {
247+
var currentTest = null;
248+
249+
it('test A', () => {
250+
currentTest = 'A';
251+
return new Promise((resolve) => {
252+
setTimeout(() => {
253+
fakeDriver.sleep(100).then(() => {
254+
expect(currentTest).toBe('A');
255+
});
256+
resolve();
257+
}, 100);
258+
});
259+
});
260+
261+
it('test B', () => {
262+
currentTest = 'B';
263+
});
264+
});
245265
});

0 commit comments

Comments
 (0)