|
| 1 | +/// <reference path='../_protractor/e2e.d.ts' /> |
| 2 | +'use strict'; |
| 3 | + |
| 4 | +const expectedH1 = 'Tour of Heroes'; |
| 5 | +const expectedTitle = `Angular 2 ${expectedH1}`; |
| 6 | +const expectedH2 = 'My Heroes'; |
| 7 | +const targetHero = { id: 16, name: 'RubberMan' }; |
| 8 | +const nameSuffix = 'X'; |
| 9 | + |
| 10 | +type WPromise<T> = webdriver.promise.Promise<T>; |
| 11 | + |
| 12 | +class Hero { |
| 13 | + id: number; |
| 14 | + name: string; |
| 15 | + |
| 16 | + // Factory methods |
| 17 | + |
| 18 | + // Get hero from s formatted as '<id> <name>'. |
| 19 | + static fromString(s: string): Hero { |
| 20 | + return { |
| 21 | + id: +s.substr(0, s.indexOf(' ')), |
| 22 | + name: s.substr(s.indexOf(' ') + 1), |
| 23 | + }; |
| 24 | + } |
| 25 | + |
| 26 | + // Get hero id and name from the given detail element. |
| 27 | + static async fromDetail(detail: protractor.ElementFinder): Promise<Hero> { |
| 28 | + // Get hero id from the first <div> |
| 29 | + let _id = await detail.all(by.css('div')).first().getText(); |
| 30 | + // Get name from the h2 |
| 31 | + let _name = await detail.element(by.css('h2')).getText(); |
| 32 | + return { |
| 33 | + id: +_id.substr(_id.indexOf(' ') + 1), |
| 34 | + name: _name.substr(0, _name.indexOf(' ')) |
| 35 | + }; |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +describe('Tutorial part 4', () => { |
| 40 | + beforeAll(() => browser.get('')); |
| 41 | + describe('Initial page', initialPageTests); |
| 42 | + describe('Select hero', selectHeroTests); |
| 43 | + describe('Update hero', updateHeroTests); |
| 44 | +}); |
| 45 | + |
| 46 | +function initialPageTests() { |
| 47 | + it(`has title '${expectedTitle}'`, () => { |
| 48 | + expect(browser.getTitle()).toEqual(expectedTitle); |
| 49 | + }); |
| 50 | + |
| 51 | + it(`has h1 '${expectedH1}'`, () => { |
| 52 | + expectHeading(1, expectedH1); |
| 53 | + }); |
| 54 | + |
| 55 | + it(`has h2 '${expectedH2}'`, () => { |
| 56 | + expectHeading(2, expectedH2); |
| 57 | + }); |
| 58 | + |
| 59 | + it('has the right number of heroes', () => { |
| 60 | + let page = getPageElts(); |
| 61 | + expect(page.heroes.count()).toEqual(10); |
| 62 | + }); |
| 63 | + |
| 64 | + it('has no selected hero and no hero details', function () { |
| 65 | + let page = getPageElts(); |
| 66 | + expect(page.selected.isPresent()).toBeFalsy('selected hero'); |
| 67 | + expect(page.heroDetail.isPresent()).toBeFalsy('no hero detail'); |
| 68 | + }); |
| 69 | +} |
| 70 | + |
| 71 | +function selectHeroTests() { |
| 72 | + it(`selects ${targetHero.name} from hero list`, function () { |
| 73 | + let hero = element(by.cssContainingText('li span.badge', targetHero.id.toString())); |
| 74 | + hero.click(); |
| 75 | + // Nothing specific to expect other than lack of exceptions. |
| 76 | + }); |
| 77 | + |
| 78 | + it(`has selected ${targetHero.name}`, function () { |
| 79 | + let page = getPageElts(); |
| 80 | + let expectedText = `${targetHero.id} ${targetHero.name}`; |
| 81 | + expect(page.selected.getText()).toBe(expectedText); |
| 82 | + }); |
| 83 | + |
| 84 | + it('shows selected hero details', async () => { |
| 85 | + let page = getPageElts(); |
| 86 | + let hero = await Hero.fromDetail(page.heroDetail); |
| 87 | + expect(hero.id).toEqual(targetHero.id); |
| 88 | + expect(hero.name).toEqual(targetHero.name); |
| 89 | + }); |
| 90 | +} |
| 91 | + |
| 92 | +function updateHeroTests() { |
| 93 | + it(`can update hero name`, () => { |
| 94 | + addToHeroName(nameSuffix); |
| 95 | + // Nothing specific to expect other than lack of exceptions. |
| 96 | + }); |
| 97 | + |
| 98 | + it(`shows updated hero name in details`, async () => { |
| 99 | + let page = getPageElts(); |
| 100 | + let hero = await Hero.fromDetail(page.heroDetail); |
| 101 | + let newName = targetHero.name + nameSuffix; |
| 102 | + expect(hero.id).toEqual(targetHero.id); |
| 103 | + expect(hero.name).toEqual(newName); |
| 104 | + }); |
| 105 | + |
| 106 | + it(`shows updated hero name in list`, async () => { |
| 107 | + let page = getPageElts(); |
| 108 | + let hero = Hero.fromString(await page.selected.getText()); |
| 109 | + let newName = targetHero.name + nameSuffix; |
| 110 | + expect(hero.id).toEqual(targetHero.id); |
| 111 | + expect(hero.name).toEqual(newName); |
| 112 | + }); |
| 113 | + |
| 114 | +} |
| 115 | + |
| 116 | +function addToHeroName(text: string): WPromise<void> { |
| 117 | + let input = element(by.css('input')); |
| 118 | + return sendKeys(input, text); |
| 119 | +} |
| 120 | + |
| 121 | +function expectHeading(hLevel: number, expectedText: string): void { |
| 122 | + let hTag = `h${hLevel}`; |
| 123 | + let hText = element(by.css(hTag)).getText(); |
| 124 | + expect(hText).toEqual(expectedText, hTag); |
| 125 | +}; |
| 126 | + |
| 127 | +function getPageElts() { |
| 128 | + return { |
| 129 | + heroes: element.all(by.css('my-app li')), |
| 130 | + selected: element(by.css('my-app li.selected')), |
| 131 | + heroDetail: element(by.css('my-app > div, my-app > my-hero-detail > div')) |
| 132 | + }; |
| 133 | +} |
0 commit comments