This repository was archived by the owner on Jul 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(doc): update doc for testing with node 8 async/await and chrome i…
…nspector. (#4613)
- Loading branch information
Showing
10 changed files
with
276 additions
and
84 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 |
---|---|---|
@@ -0,0 +1,37 @@ | ||
describe('angularjs homepage', function() { | ||
it('should greet the named user', async function() { | ||
debugger; | ||
await browser.get('http://www.angularjs.org'); | ||
|
||
await element(by.model('yourName')).sendKeys('Julie'); | ||
|
||
var greeting = element(by.binding('yourName')); | ||
|
||
expect(await greeting.getText()).toEqual('Hello Julie!'); | ||
}); | ||
|
||
describe('todo list', function() { | ||
var todoList; | ||
|
||
beforeEach(async function() { | ||
await browser.get('http://www.angularjs.org'); | ||
todoList = element.all(by.repeater('todo in todoList.todos')); | ||
}); | ||
|
||
it('should list todos', async function() { | ||
expect(await todoList.count()).toEqual(2); | ||
expect(await todoList.get(1).getText()).toEqual('build an AngularJS app'); | ||
}); | ||
|
||
it('should add a todo', async function() { | ||
var addTodo = element(by.model('todoList.todoText')); | ||
var addButton = element(by.css('[value="add"]')); | ||
|
||
await addTodo.sendKeys('write a protractor test'); | ||
await addButton.click(); | ||
|
||
expect(await todoList.count()).toEqual(3); | ||
expect(await todoList.get(2).getText()).toEqual('write a protractor test'); | ||
}); | ||
}); | ||
}); |
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,25 @@ | ||
// An example configuration file for debugging test using async/await. | ||
exports.config = { | ||
directConnect: true, | ||
|
||
// Capabilities to be passed to the webdriver instance. | ||
capabilities: { | ||
'browserName': 'chrome' | ||
}, | ||
|
||
seleniumAddress: 'http://localhost:4444/wd/hub', | ||
|
||
// Framework to use. Jasmine is recommended. | ||
framework: 'jasmine', | ||
|
||
// Spec patterns are relative to the current working directory when | ||
// protractor is called. | ||
specs: ['async_await.js'], | ||
|
||
SELENIUM_PROMISE_MANAGER: false, | ||
|
||
// Options to be passed to Jasmine. | ||
jasmineNodeOpts: { | ||
defaultTimeoutInterval: 30000 | ||
} | ||
}; |
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,4 +1,86 @@ | ||
`async`/`await` | ||
=============== | ||
|
||
Please see [our TypeScript examples which use `async`/`await`](/exampleTypescript/asyncAwait/). | ||
**Background** | ||
|
||
- The Web Driver Control Flow is used to synchronize your commands so they reach | ||
the browser in the correct order (see | ||
[/docs/control-flow.md](/docs/control-flow.md) for details). In the future, the | ||
control flow is being removed (see [SeleniumHQ's github issue]( | ||
https://github.com/SeleniumHQ/selenium/issues/2969) for details). Instead of the | ||
control flow, you can synchronize your commands with promise chaining or the | ||
upcoming ES7 feature `async`/`await`. | ||
|
||
- Previously, we have Typescript support for `async`/`await`: Please see [TypeScript examples which use `async`/`await`](/exampleTypescript/asyncAwait/README.md). | ||
|
||
- The latest [Node.js](https://nodejs.org/en/) provides native async/await, | ||
which means we can get stable e2e test without using control flow in javascript test. | ||
|
||
**Note**: To write and run native async/await test, the node.js version should be greater than or equal to 8.0, and Jasmine version should be greater than or equal to 2.7 | ||
|
||
- If we disable control flow and use async/await to write tests, we can get a | ||
better debugging experience by using [chrome | ||
inspector](./debugging.md#disabled-control-flow) | ||
|
||
**How to use native async/await in test** | ||
|
||
We have a simple example to show how to use async/await in test. | ||
|
||
You can find the whole example in | ||
[here](/debugging/async_await.js) | ||
|
||
```javascript | ||
describe('angularjs homepage', function() { | ||
it('should greet the named user', async function() { | ||
await browser.get('http://www.angularjs.org'); | ||
|
||
await element(by.model('yourName')).sendKeys('Julie'); | ||
|
||
var greeting = element(by.binding('yourName')); | ||
|
||
expect(await greeting.getText()).toEqual('Hello Julie!'); | ||
}); | ||
``` | ||
As you can see, the syntax is almost the same with TypeScript async/await. | ||
1. We need wrap our asynchronous function with “async”. | ||
1. We can add “await” keyword to each operation that we want our program to | ||
wait for. | ||
**Note:** Never forget to add “await” keyword in an async function, it | ||
may bring some unexpected problem (e.g. your test might fail silently and | ||
always be reported as passed). | ||
1. Don’t forget to turn off control_flow, you cannot use a mix of `async`/`await` and the control flow: | ||
`async`/`await` causes the control flow to become unreliable (see | ||
[github issue]( https://github.com/SeleniumHQ/selenium/issues/3037)). So if you | ||
`async`/`await` anywhere in a spec, you should use the | ||
`SELENIUM_PROMISE_MANAGER: false` | ||
```javascript | ||
// An example configuration file for debugging test using async/await. | ||
exports.config = { | ||
directConnect: true, | ||
|
||
// Capabilities to be passed to the webdriver instance. | ||
capabilities: { | ||
'browserName': 'chrome' | ||
}, | ||
|
||
seleniumAddress: 'http://localhost:4444/wd/hub', | ||
|
||
// Framework to use. Jasmine is recommended. | ||
framework: 'jasmine', | ||
|
||
// Spec patterns are relative to the current working directory when | ||
// protractor is called. | ||
specs: ['async_await.js'], | ||
|
||
SELENIUM_PROMISE_MANAGER: false, | ||
|
||
// Options to be passed to Jasmine. | ||
jasmineNodeOpts: { | ||
defaultTimeoutInterval: 30000 | ||
} | ||
}; | ||
``` |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.