Skip to content

Commit

Permalink
feat(async/await): Support overwriting native promise with WebDriver'…
Browse files Browse the repository at this point in the history
…s version

See angular#61
  • Loading branch information
sjelin committed Nov 1, 2016
1 parent 35d2ac8 commit 393cf83
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
*.log
node_modules
spec/asyncAwaitSpec.js
spec/asyncAwaitSpec*.js
10 changes: 9 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,13 +144,21 @@ function wrapInControlFlow(flow, globalFn, fnName) {
* pass webdriver here instead of using require() in order to ensure Protractor
* and Jasminews are using the same webdriver instance.
* @param {Object} flow. The ControlFlow to wrap tests in.
* @param {function=} WDPromise The class for promises managed by the
* ControlFlow. This is an optional parameter. If you use it, jasminewd
* will overwrite the global `Promise` class with this so that tools like
* async/await work. This might cause other unexpected problems however.
*/
function initJasmineWd(flow) {
function initJasmineWd(flow, WDPromise) {
if (jasmine.JasmineWdInitialized) {
throw Error('JasmineWd already initialized when init() was called');
}
jasmine.JasmineWdInitialized = true;

if (WDPromise) {
WDPromise.prototype = global.Promise || WDPromise.prototype;
global.Promise = WDPromise;
}
global.it = wrapInControlFlow(flow, global.it, 'it');
global.fit = wrapInControlFlow(flow, global.fit, 'fit');
global.beforeEach = wrapInControlFlow(flow, global.beforeEach, 'beforeEach');
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
},
"main": "index.js",
"scripts": {
"pretest": "node_modules/.bin/jshint index.js spec --exclude spec/asyncAwaitSpec.js; tsc -t ES2015 spec/asyncAwaitSpec.ts",
"pretest": "node_modules/.bin/jshint index.js spec --exclude spec/asyncAwaitSpec*.js; tsc -t ES2015 spec/asyncAwaitSpec*.ts",
"test": "scripts/test.sh"
},
"license": "MIT",
Expand Down
9 changes: 9 additions & 0 deletions scripts/test.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
PASSING_SPECS="spec/support/passing_specs.json"
FAILING_SPECS="spec/support/failing_specs.json"
EXPERIMENTAL_SPECS="spec/support/experimental_specs.json"
CMD_BASE="node node_modules/.bin/jasmine JASMINE_CONFIG_PATH="

echo "### running passing specs"
Expand All @@ -18,4 +19,12 @@ results_line=`echo "$res" | tail -2 | head -1`
echo "result: $results_line"
[ "$results_line" = "$EXPECTED_RESULTS" ] || exit 1

echo "### running experimental specs"
CMD=$CMD_BASE$EXPERIMENTAL_SPECS
echo "### $CMD"
$CMD
# We don't care if the experimental specs pass.
# [ "$?" -eq 0 ] || exit 1
echo

echo "all pass"
28 changes: 28 additions & 0 deletions spec/asyncAwaitSpec_experimental.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/* This is a test for an experimental feature to make async/await work with the
* webdriver control flow. You should not use this.
*/
"use strict";

// Declare globals
declare var describe;
declare var it;
declare var expect;
declare var require;

let webdriver = require('selenium-webdriver'),
flow = webdriver.promise.controlFlow();
require('../index.js').init(flow, webdriver.promise.Promise);

describe('async function', function() {
let sharedVal: any;
it('should wait on async functions', async function() {
sharedVal = await webdriver.promise.fulfilled('a');
expect(sharedVal).toBe('a');
flow.timeout(1000); // The control flow needs to work for this.
sharedVal = await webdriver.promise.fulfilled('b');
});

it('should have waited until the end of the last it() block', function() {
expect(sharedVal).toBe('b');
});
});
6 changes: 6 additions & 0 deletions spec/support/experimental_specs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"spec_dir": "spec",
"spec_files": [
"asyncAwaitSpec_experimental.js"
]
}

0 comments on commit 393cf83

Please sign in to comment.