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

Commit

Permalink
docs(page_objects): Add async / await example (#4675)
Browse files Browse the repository at this point in the history
  • Loading branch information
antgel authored and qiyigg committed Jan 25, 2018
1 parent d116f5d commit a7411b6
Showing 1 changed file with 48 additions and 4 deletions.
52 changes: 48 additions & 4 deletions docs/page-objects.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,42 @@ var AngularHomepage = function() {
this.setName = function(name) {
nameInput.sendKeys(name);
};
this.getGreeting = function() {

this.getGreetingText = function() {
return greeting.getText();
};
};
module.exports = new AngularHomepage();
```

Or, if using `async / await`, something like this: (Note that functions
that don't use `await` shouldn't have the `async` prefix.)

```js
var AngularHomepage = function() {
var nameInput = element(by.model('yourName'));
var greeting = element(by.binding('yourName'));

this.get = async function() {
await browser.get('http://www.angularjs.org');
};

this.setName = async function(name) {
await nameInput.sendKeys(name);
};

this.getGreetingText = async function() {
return await greeting.getText();
};

// Not async, returns the element
this.getGreeting = function() {
return greeting;
};
};
module.exports = new AngularHomepage();
```

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).

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.
Expand All @@ -55,15 +84,30 @@ describe('angularjs homepage', function() {

angularHomepage.setName('Julie');

expect(angularHomepage.getGreeting()).toEqual('Hello Julie!');
expect(angularHomepage.getGreetingText()).toEqual('Hello Julie!');
});
});
```

If using `async / await`, that would turn into something like:

```js
var angularHomepage = require('./AngularHomepage');
describe('angularjs homepage', function() {
it('should greet the named user', async function() {
await angularHomepage.get();

await angularHomepage.setName('Julie');

expect(await angularHomepage.getGreetingText()).toEqual('Hello Julie!');
});
});
```

Configuring Test Suites
-----------------------

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

```js
exports.config = {
Expand Down

0 comments on commit a7411b6

Please sign in to comment.