|
1 | 1 | /// <reference path='../_protractor/e2e.d.ts' />
|
2 | 2 | '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 | + |
3 | 33 | describe('Tutorial part 1', () => {
|
4 | 34 |
|
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' }; |
9 | 36 |
|
10 |
| - beforeEach(() => { |
11 |
| - return browser.get(''); |
12 |
| - }); |
| 37 | + beforeAll(() => browser.get('')); |
13 | 38 |
|
14 |
| - it(`should have title '${expectedTitle}'`, () => { |
| 39 | + it(`has title '${expectedTitle}'`, () => { |
15 | 40 | expect(browser.getTitle()).toEqual(expectedTitle);
|
16 | 41 | });
|
17 | 42 |
|
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); |
21 | 53 | });
|
22 | 54 |
|
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); |
26 | 62 | });
|
| 63 | + |
27 | 64 | });
|
| 65 | + |
| 66 | +function getPageElts() { |
| 67 | + return { |
| 68 | + heroDetail: element(by.css('my-app')) |
| 69 | + }; |
| 70 | +} |
0 commit comments