Skip to content
This repository was archived by the owner on Jul 29, 2024. It is now read-only.

Commit a7411b6

Browse files
antgelqiyigg
authored andcommittedJan 25, 2018
docs(page_objects): Add async / await example (#4675)
1 parent d116f5d commit a7411b6

File tree

1 file changed

+48
-4
lines changed

1 file changed

+48
-4
lines changed
 

‎docs/page-objects.md

+48-4
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,42 @@ var AngularHomepage = function() {
3636
this.setName = function(name) {
3737
nameInput.sendKeys(name);
3838
};
39-
40-
this.getGreeting = function() {
39+
40+
this.getGreetingText = function() {
4141
return greeting.getText();
4242
};
4343
};
4444
module.exports = new AngularHomepage();
4545
```
46+
47+
Or, if using `async / await`, something like this: (Note that functions
48+
that don't use `await` shouldn't have the `async` prefix.)
49+
50+
```js
51+
var AngularHomepage = function() {
52+
var nameInput = element(by.model('yourName'));
53+
var greeting = element(by.binding('yourName'));
54+
55+
this.get = async function() {
56+
await browser.get('http://www.angularjs.org');
57+
};
58+
59+
this.setName = async function(name) {
60+
await nameInput.sendKeys(name);
61+
};
62+
63+
this.getGreetingText = async function() {
64+
return await greeting.getText();
65+
};
66+
67+
// Not async, returns the element
68+
this.getGreeting = function() {
69+
return greeting;
70+
};
71+
};
72+
module.exports = new AngularHomepage();
73+
```
74+
4675
The next thing you need to do is modify the test script to use the Page Object and its properties. Note that the _functionality_ of the test script itself does not change (nothing is added or deleted).
4776

4877
In the test script, you'll `require` the Page Object as presented above. The path to the Page Object _will be relative_ to your spec, so adjust accordingly.
@@ -55,15 +84,30 @@ describe('angularjs homepage', function() {
5584

5685
angularHomepage.setName('Julie');
5786

58-
expect(angularHomepage.getGreeting()).toEqual('Hello Julie!');
87+
expect(angularHomepage.getGreetingText()).toEqual('Hello Julie!');
88+
});
89+
});
90+
```
91+
92+
If using `async / await`, that would turn into something like:
93+
94+
```js
95+
var angularHomepage = require('./AngularHomepage');
96+
describe('angularjs homepage', function() {
97+
it('should greet the named user', async function() {
98+
await angularHomepage.get();
99+
100+
await angularHomepage.setName('Julie');
101+
102+
expect(await angularHomepage.getGreetingText()).toEqual('Hello Julie!');
59103
});
60104
});
61105
```
62106

63107
Configuring Test Suites
64108
-----------------------
65109

66-
It is possible to separate your tests into various test suites. In your config file, you could setup the suites option as shown below.
110+
It is possible to separate your tests into various test suites. In your config file, you could setup the suites option as shown below:
67111

68112
```js
69113
exports.config = {

0 commit comments

Comments
 (0)
This repository has been archived.