Skip to content
This repository was archived by the owner on Dec 4, 2017. It is now read-only.

Commit 248eeac

Browse files
committed
tests(toh-1/e2e): cleanup and updates incl. use of async/await
1 parent eafd7db commit 248eeac

File tree

1 file changed

+57
-14
lines changed

1 file changed

+57
-14
lines changed
+57-14
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,70 @@
11
/// <reference path='../_protractor/e2e.d.ts' />
22
'use strict';
3+
4+
type WPromise<T> = webdriver.promise.Promise<T>;
5+
6+
const expectedH1 = 'Tour of Heroes';
7+
const expectedTitle = `Angular 2 ${expectedH1}`;
8+
9+
class Hero {
10+
id: number;
11+
name: string;
12+
13+
// Factory method
14+
// Get hero id and name from the given detail element.
15+
static async fromDetail(detail: protractor.ElementFinder): Promise<Hero> {
16+
// Get hero id from the first <div>
17+
let _id = await detail.all(by.css('div')).first().getText();
18+
// Get name from the h2
19+
let _name = await detail.element(by.css('h2')).getText();
20+
return {
21+
id: +_id.substr(_id.indexOf(' ') + 1),
22+
name: _name.substr(0, _name.indexOf(' '))
23+
};
24+
}
25+
}
26+
27+
const nameSuffix = 'X';
28+
function addToHeroName(text: string): WPromise<void> {
29+
let input = element(by.css('input'));
30+
return sendKeys(input, text);
31+
}
32+
333
describe('Tutorial part 1', () => {
434

5-
let expectedH1 = 'Tour of Heroes';
6-
let expectedTitle = `Angular 2 ${expectedH1}`;
7-
let hero = { id: 1, name: 'Windstorm' };
8-
let expectedH2 = `${hero.name} details!`;
35+
const expectedHero = { id: 1, name: 'Windstorm' };
936

10-
beforeEach(() => {
11-
return browser.get('');
12-
});
37+
beforeAll(() => browser.get(''));
1338

14-
it(`should have title '${expectedTitle}'`, () => {
39+
it(`has title '${expectedTitle}'`, () => {
1540
expect(browser.getTitle()).toEqual(expectedTitle);
1641
});
1742

18-
it(`should have '${expectedH2}'`, () => {
19-
let text = element(by.css('h2')).getText();
20-
expect(text).toEqual(expectedH2);
43+
it(`has h1 '${expectedH1}'`, () => {
44+
let hText = element(by.css('h1')).getText();
45+
expect(hText).toEqual(expectedH1, 'h1');
46+
});
47+
48+
it(`shows initial hero details`, async () => {
49+
let page = getPageElts();
50+
let hero = await Hero.fromDetail(page.heroDetail);
51+
expect(hero.id).toEqual(expectedHero.id);
52+
expect(hero.name).toEqual(expectedHero.name);
2153
});
2254

23-
it(`should have input name '${hero.name}'`, () => {
24-
let name = element(by.css('input')).getAttribute('value');
25-
expect(name).toEqual(hero.name);
55+
it(`shows updated hero name`, async () => {
56+
addToHeroName(nameSuffix);
57+
let page = getPageElts();
58+
let hero = await Hero.fromDetail(page.heroDetail);
59+
let newName = expectedHero.name + nameSuffix;
60+
expect(hero.id).toEqual(expectedHero.id);
61+
expect(hero.name).toEqual(newName);
2662
});
63+
2764
});
65+
66+
function getPageElts() {
67+
return {
68+
heroDetail: element(by.css('my-app'))
69+
};
70+
}

0 commit comments

Comments
 (0)