|
1 | 1 | /// <reference path='../_protractor/e2e.d.ts' />
|
2 | 2 | 'use strict';
|
3 |
| -describe('Architecture', function () { |
4 |
| - |
5 |
| - let title = 'Hero List'; |
6 |
| - |
7 |
| - beforeAll(function () { |
8 |
| - browser.get(''); |
9 |
| - }); |
10 |
| - |
11 |
| - function itReset(name: string, func: () => any) { |
12 |
| - it(name, function() { |
13 |
| - browser.get('').then(func); |
14 |
| - }); |
15 |
| - } |
16 |
| - |
17 |
| - it(`should display correct title: ${title}`, function () { |
18 |
| - expect(element(by.css('h2')).getText()).toEqual(title); |
19 |
| - }); |
20 |
| - |
21 |
| - it('should display correct detail after selection', function() { |
22 |
| - let detailView = element(by.css('hero-detail')); |
23 |
| - expect(detailView.isPresent()).toBe(false); |
24 |
| - // select the 2nd element |
25 |
| - let selectEle = element.all(by.css('hero-list > div')).get(1); |
26 |
| - selectEle.click().then(function() { |
27 |
| - return selectEle.getText(); |
28 |
| - }).then(function(selectedHeroName) { |
29 |
| - // works but too specific if we change the app |
30 |
| - // expect(selectedHeroName).toEqual('Mr. Nice'); |
31 |
| - expect(detailView.isDisplayed()).toBe(true); |
32 |
| - let detailTitleEle = element(by.css('hero-detail > h4')); |
33 |
| - expect(detailTitleEle.getText()).toContain(selectedHeroName); |
34 |
| - }); |
35 |
| - }); |
36 |
| - |
37 |
| - itReset('should display correct detail after modification', function() { |
38 |
| - let detailView = element(by.css('hero-detail')); |
39 |
| - expect(detailView.isPresent()).toBe(false); |
40 |
| - // select the 2nd element |
41 |
| - let selectEle = element.all(by.css('hero-list > div')).get(1); |
42 |
| - selectEle.click().then(function () { |
43 |
| - return selectEle.getText(); |
44 |
| - }).then(function (selectedHeroName) { |
45 |
| - let detailTitleEle = element(by.css('hero-detail > h4')); |
46 |
| - expect(detailTitleEle.getText()).toContain(selectedHeroName); |
47 |
| - let heroNameEle = element.all(by.css('hero-detail input')).get(0); |
48 |
| - |
49 |
| - // check that both the initial selected item and the detail title reflect changes |
50 |
| - // made to the input box. |
51 |
| - // heroNameEle.sendKeys('foo'); |
52 |
| - sendKeys(heroNameEle, 'foo'); |
53 |
| - expect(detailTitleEle.getText()).toContain('foo'); |
54 |
| - expect(selectEle.getText()).toContain('foo'); |
55 |
| - |
56 |
| - // getText on an input element always returns null |
57 |
| - // http://stackoverflow.com/questions/20310442/how-to-gettext-on-an-input-in-protractor |
58 |
| - // expect(heroNameEle.getText()).toEqual(selectedHeroName); |
59 |
| - expect(heroNameEle.getAttribute('value')).toEqual(selectedHeroName + 'foo'); |
60 |
| - }); |
| 3 | + |
| 4 | +const nameSuffix = 'X'; |
| 5 | +type WPromise<T> = webdriver.promise.Promise<T>; |
| 6 | + |
| 7 | +class Hero { |
| 8 | + id: number; |
| 9 | + name: string; |
| 10 | +} |
| 11 | + |
| 12 | +describe('Architecture', () => { |
| 13 | + |
| 14 | + const expectedTitle = 'Architecture of Angular 2'; |
| 15 | + const expectedH2 = ['Hero List', 'Sales Tax Calculator']; |
| 16 | + |
| 17 | + beforeAll(() => browser.get('')); |
| 18 | + |
| 19 | + it(`has title '${expectedTitle}'`, () => { |
| 20 | + expect(browser.getTitle()).toEqual(expectedTitle); |
| 21 | + }); |
| 22 | + |
| 23 | + it(`has h2 '${expectedH2}'`, () => { |
| 24 | + let h2 = element.all(by.css('h2')).map((elt) => elt.getText()); |
| 25 | + expect(h2).toEqual(expectedH2); |
61 | 26 | });
|
62 | 27 |
|
| 28 | + describe('Hero', heroTests); |
| 29 | + describe('Salex tax', salesTaxTests); |
63 | 30 | });
|
| 31 | + |
| 32 | +function heroTests() { |
| 33 | + |
| 34 | + const targetHero: Hero = { id: 2, name: 'Mr. Nice' }; |
| 35 | + |
| 36 | + it('has the right number of heroes', () => { |
| 37 | + let page = getPageElts(); |
| 38 | + expect(page.heroes.count()).toEqual(3); |
| 39 | + }); |
| 40 | + |
| 41 | + it('has no hero details initially', function () { |
| 42 | + let page = getPageElts(); |
| 43 | + expect(page.heroDetail.isPresent()).toBeFalsy('no hero detail'); |
| 44 | + }); |
| 45 | + |
| 46 | + it(`selects ${targetHero.name} from hero list`, function () { |
| 47 | + let hero = element(by.cssContainingText('li', targetHero.name)); |
| 48 | + hero.click(); |
| 49 | + // Nothing specific to expect other than lack of exceptions. |
| 50 | + }); |
| 51 | + |
| 52 | + it('shows selected hero details', async () => { |
| 53 | + let page = getPageElts(); |
| 54 | + let hero = await heroFromDetail(page.heroDetail); |
| 55 | + expect(hero.id).toEqual(targetHero.id); |
| 56 | + expect(hero.name).toEqual(targetHero.name); |
| 57 | + }); |
| 58 | + |
| 59 | + it(`can update hero name`, () => { |
| 60 | + addToHeroName(nameSuffix); |
| 61 | + // Nothing specific to expect other than lack of exceptions. |
| 62 | + }); |
| 63 | + |
| 64 | + it(`shows updated hero name in details`, async () => { |
| 65 | + let page = getPageElts(); |
| 66 | + let hero = await heroFromDetail(page.heroDetail); |
| 67 | + let newName = targetHero.name + nameSuffix; |
| 68 | + expect(hero.id).toEqual(targetHero.id); |
| 69 | + expect(hero.name).toEqual(newName); |
| 70 | + }); |
| 71 | +} |
| 72 | + |
| 73 | +function salesTaxTests() { |
| 74 | + it('has no sales tax initially', function () { |
| 75 | + let page = getPageElts(); |
| 76 | + expect(page.salesTaxDetail.isPresent()).toBeFalsy('no sales tax info'); |
| 77 | + }); |
| 78 | + |
| 79 | + it('shows sales tax', async function () { |
| 80 | + let page = getPageElts(); |
| 81 | + await sendKeys(page.salesTaxAmountInput, '10'); |
| 82 | + // Note: due to Dart bug USD is shown instead of $ |
| 83 | + let re = /The sales tax is (\$|USD)1.00/; |
| 84 | + expect(page.salesTaxDetail.getText()).toMatch(re); |
| 85 | + }); |
| 86 | +} |
| 87 | + |
| 88 | +// Helper functions |
| 89 | + |
| 90 | +function getPageElts() { |
| 91 | + return { |
| 92 | + heroes: element.all(by.css('my-app li')), |
| 93 | + heroDetail: element(by.css('my-app hero-detail')), |
| 94 | + salesTaxAmountInput: element(by.css('my-app sales-tax input')), |
| 95 | + salesTaxDetail: element(by.css('my-app sales-tax div')) |
| 96 | + }; |
| 97 | +} |
| 98 | + |
| 99 | +function addToHeroName(text: string): WPromise<void> { |
| 100 | + let input = element.all(by.css('input')).first(); |
| 101 | + return sendKeys(input, text); |
| 102 | +} |
| 103 | + |
| 104 | +async function heroFromDetail(detail: protractor.ElementFinder): Promise<Hero> { |
| 105 | + // Get hero id from the first <div> |
| 106 | + let _id = await detail.all(by.css('div')).first().getText(); |
| 107 | + // Get name from the h2 |
| 108 | + let _name = await detail.element(by.css('h4')).getText(); |
| 109 | + return { |
| 110 | + id: +_id.substr(_id.indexOf(' ') + 1), |
| 111 | + name: _name.substr(0, _name.lastIndexOf(' ')) |
| 112 | + }; |
| 113 | +} |
0 commit comments