Skip to content

Commit 3f1679f

Browse files
committed
chore(async/await): document async/await better
1 parent 4408ae4 commit 3f1679f

File tree

2 files changed

+35
-5
lines changed

2 files changed

+35
-5
lines changed

README.md

+12
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ Features
1515

1616
- If a `done` function is passed to the test, waits for both the control flow and until done is called.
1717

18+
- If a test returns a promise, waits for both the the control flow and the promise to resolve.
19+
1820
- Enhances `expect` so that it automatically unwraps promises before performing the assertion.
1921

2022
Installation
@@ -58,3 +60,13 @@ describe('tests with webdriver', function() {
5860
});
5961
})
6062
```
63+
64+
`async` functions / `await`
65+
---------------------------
66+
67+
`async` functions and the `await` keyword are likely coming in ES7, and
68+
available via several compilers. At the moment, they often break the WebDriver
69+
control flow.
70+
([GitHub issue](https://github.com/SeleniumHQ/selenium/issues/3037)). You can
71+
still use them, but if you do then you will have to use `await`/Promises for
72+
almost all your synchronization. See `spec/asyncAwaitSpec.ts` for details.

spec/asyncAwaitSpec.ts

+23-5
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,34 @@
1+
/* Here we have an example of using async/await with jasminewd. We are using
2+
* typescript to gain access to async/await, but mostly this should look like
3+
* normal javascript to you.
4+
*
5+
* The key thing to note here is that once you use async/await, the webdriver
6+
* control flow is no longer reliable. This means you have to use async/await
7+
* or promises for all your asynchronous behavior. In Protractor this would
8+
* mean putting `await` before every line interacting with the browser. In this
9+
* example, we have to put `await` before `driver.sleep()`.
10+
*/
111
"use strict";
212

13+
// Declare globals
314
declare var describe;
415
declare var it;
516
declare var expect;
17+
declare var require;
618

7-
async function asyncHelper() {
8-
return 7;
9-
}
19+
let driver = require('./common.js').getFakeDriver();
1020

1121
describe('async function', function() {
22+
let sharedVal: any;
1223
it('should wait on async functions', async function() {
13-
let helperVal = await asyncHelper();
14-
expect(helperVal).toBe(7);
24+
sharedVal = await driver.getValueA(); // Async unwraps this to 'a'
25+
expect(sharedVal).toBe('a');
26+
await driver.sleep(1000); // Normally you wouldn't need to `await` this, but
27+
// the control flow is broken for async functions.
28+
sharedVal = await driver.getValueB();
29+
});
30+
31+
it('should have waited until the end of the last it() block', function() {
32+
expect(sharedVal).toBe('b');
1533
});
1634
});

0 commit comments

Comments
 (0)