-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(SELENIUM_PROMISE_MANAGER=0): Improve support for SELENIUM_PROMIS…
…E_MANAGER=0 There are three major ways this was done in this change: * In `callWhenIdle`, if `flow.isIdle` is not defined, we assume we are working with a `SimpleScheduler` instance, and so the flow is effectively idle. * In `initJasmineWd`, if `flow.reset` is not defined, we assume we are working with a `SimpleScheduler` instance, and so don't bother resetting the flow. * In `wrapInControlFlow`, we use `flow.promise` to create a new promise if possible. Since `new webdriver.promise.Promise()` would have always made a `ManagedPromise`, but `flow.promise` will do the right thing. * In `wrapCompare`, we avoid the webdriver library entirely, and never instance any extra promises. Using `webdriver.promise.when` and `webdriver.promise.all` could have been a problem if our instance of `webdriver` had the control flow turned on, but another instance somewhere did not (or even the same instance, but just at a different point in time). Instead we use the new `maybePromise` tool, which is a mess but is also exactly what we want. * In `specs/*`, we replace `webdriver.promise.fulfilled` with `webdriver.promise.when`. * In `specs/*`, a new version of `adapterSpec.js` and `errorSpec.js` are created: `asyncAwaitAdapterSpec.ts` and `asyncAwaitErrorSpec.ts`. I also also fixed a minor bug where we weren't correctly checking for promises inside an array of expected results. Before we had ```js expected = Array.prototype.slice.call(arguments, 0) ... webdriver.promise.isPromise(expected) ``` I thought about it for a little while, and there's no way that's correct. `expected` is an `Array<any>`, there's no way it has a `.then` function. Closes #69
- Loading branch information
Showing
21 changed files
with
841 additions
and
106 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
*.log | ||
node_modules | ||
spec/asyncAwaitSpec.js | ||
spec/asyncAwait*Spec.js | ||
spec/common.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
./spec/asyncAwaitSpec.js | ||
./spec/asyncAwaitAdapterSpec.js | ||
./spec/asyncAwaitErrorSpec.js | ||
./spec/common.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/** | ||
* This file implements jasminewd's peculiar alternatives to Promise.resolve() | ||
* and Promise.all(). Do not use the code from this file as pollyfill for | ||
* Promise.resolve() or Promise.all(). There are a number of reasons why this | ||
* implementation will cause unexpected errors in most codebases. | ||
* | ||
* Called "maybePromise" because both the parameters and the return values may | ||
* or may not be promises, and code execution may or may not be synchronous. | ||
*/ | ||
|
||
/** | ||
* Runs a callback synchronously against non-promise values and asynchronously | ||
* against promises. Similar to ES6's `Promise.resolve` except that it is | ||
* synchronous when possible and won't wrap the return value. | ||
* | ||
* This is not what you normally want. Normally you want the code to be | ||
* consistently asynchronous, and you want the result wrapped into a promise. | ||
* But because of webdriver's control flow, we're better off not introducing any | ||
* extra layers of promises or asynchronous activity. | ||
* | ||
* @param {*} val The value to call the callback with. | ||
* @param {!Function} callback The callback function | ||
* @return {*} If val isn't a promise, the return value of the callback is | ||
* directly returned. If val is a promise, a promise (generated by val.then) | ||
* resolving to the callback's return value is returned. | ||
*/ | ||
var maybePromise = module.exports = function maybePromise(val, callback) { | ||
if (val && (typeof val.then == 'function')) { | ||
return val.then(callback); | ||
} else { | ||
return callback(val); | ||
} | ||
} | ||
|
||
/** | ||
* Like maybePromise() but for an array of values. Analogous to `Promise.all`. | ||
* | ||
* @param {!Array<*>} vals An array of values to call the callback with | ||
* @param {!Function} callback the callback function | ||
* @return {*} If nothing in vals is a promise, the return value of the callback | ||
* is directly returned. Otherwise, a promise (generated by the .then | ||
* functions in vals) resolving to the callback's return value is returned. | ||
*/ | ||
maybePromise.all = function all(vals, callback) { | ||
var resolved = new Array(vals.length); | ||
function resolveAt(i) { | ||
if (i >= vals.length) { | ||
return callback(resolved); | ||
} else { | ||
return maybePromise(vals[i], function(val) { | ||
resolved[i] = val; | ||
return resolveAt(i+1); | ||
}); | ||
} | ||
} | ||
return resolveAt(0); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,59 @@ | ||
LIB_SPECS="spec/support/lib_specs.json" | ||
PASSING_SPECS="spec/support/passing_specs.json" | ||
FAILING_SPECS="spec/support/failing_specs.json" | ||
NO_CF_PASSING_SPECS="spec/support/no_cf_passing_specs.json" | ||
NO_CF_FAILING_SPECS="spec/support/no_cf_failing_specs.json" | ||
CMD_BASE="node node_modules/.bin/jasmine JASMINE_CONFIG_PATH=" | ||
|
||
echo "### running passing specs" | ||
# Run unit tests | ||
|
||
echo "### running all unit tests" | ||
CMD=$CMD_BASE$LIB_SPECS | ||
echo "### $CMD" | ||
$CMD | ||
[ "$?" -eq 0 ] || exit 1 | ||
echo | ||
|
||
|
||
# Run all tests when the control flow is enabled | ||
|
||
export SELENIUM_PROMISE_MANAGER=1 | ||
|
||
echo "### running all passing specs" | ||
CMD=$CMD_BASE$PASSING_SPECS | ||
echo "### $CMD" | ||
$CMD | ||
[ "$?" -eq 0 ] || exit 1 | ||
echo | ||
|
||
EXPECTED_RESULTS="18 specs, 16 failures" | ||
echo "### running failing specs (expecting $EXPECTED_RESULTS)" | ||
EXPECTED_RESULTS="38 specs, 34 failures" | ||
echo "### running all failing specs (expecting $EXPECTED_RESULTS)" | ||
CMD=$CMD_BASE$FAILING_SPECS | ||
echo "### $CMD" | ||
res=`$CMD 2>/dev/null` | ||
results_line=`echo "$res" | tail -2 | head -1` | ||
echo "result: $results_line" | ||
[ "$results_line" = "$EXPECTED_RESULTS" ] || exit 1 | ||
|
||
# Run only the async/await tests when the control flow is disabled | ||
|
||
export SELENIUM_PROMISE_MANAGER=0 | ||
|
||
echo "### running async/await passing specs" | ||
CMD=$CMD_BASE$NO_CF_PASSING_SPECS | ||
echo "### $CMD" | ||
$CMD | ||
[ "$?" -eq 0 ] || exit 1 | ||
echo | ||
|
||
EXPECTED_RESULTS="19 specs, 17 failures" | ||
echo "### running async/await failing specs (expecting $EXPECTED_RESULTS)" | ||
CMD=$CMD_BASE$NO_CF_FAILING_SPECS | ||
echo "### $CMD" | ||
res=`$CMD 2>/dev/null` | ||
results_line=`echo "$res" | tail -2 | head -1` | ||
echo "result: $results_line" | ||
[ "$results_line" = "$EXPECTED_RESULTS" ] || exit 1 | ||
|
||
|
||
echo "all pass" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.