|
| 1 | +/// <reference path='../_protractor/e2e.d.ts' /> |
| 2 | +'use strict'; |
| 3 | +describe('NgModule', function () { |
| 4 | + |
| 5 | + // helpers |
| 6 | + const gold = 'rgba(255, 215, 0, 1)'; |
| 7 | + const powderblue = 'rgba(176, 224, 230, 1)'; |
| 8 | + const lightgray = 'rgba(211, 211, 211, 1)'; |
| 9 | + |
| 10 | + function getCommonsSectionStruct() { |
| 11 | + const buttons = element.all(by.css('nav a')); |
| 12 | + |
| 13 | + return { |
| 14 | + title: element.all(by.tagName('h1')).get(0), |
| 15 | + subtitle: element.all(by.css('app-title p i')).get(0), |
| 16 | + contactButton: buttons.get(0), |
| 17 | + crisisButton: buttons.get(1), |
| 18 | + heroesButton: buttons.get(2) |
| 19 | + }; |
| 20 | + } |
| 21 | + |
| 22 | + function getContactSectionStruct() { |
| 23 | + const buttons = element.all(by.css('app-contact form button')); |
| 24 | + |
| 25 | + return { |
| 26 | + header: element.all(by.css('app-contact h2')).get(0), |
| 27 | + popupMessage: element.all(by.css('app-contact div')).get(0), |
| 28 | + contactNameHeader: element.all(by.css('app-contact form h3')).get(0), |
| 29 | + input: element.all(by.css('app-contact form input')).get(0), |
| 30 | + validationError: element.all(by.css('app-contact form .alert')).get(0), |
| 31 | + saveButton: buttons.get(0), // can't be tested |
| 32 | + nextContactButton: buttons.get(1), |
| 33 | + newContactButton: buttons.get(2) |
| 34 | + }; |
| 35 | + } |
| 36 | + |
| 37 | + function getCrisisSectionStruct() { |
| 38 | + return { |
| 39 | + title: element.all(by.css('ng-component h3')).get(0), |
| 40 | + items: element.all(by.css('ng-component a')), |
| 41 | + itemId: element.all(by.css('ng-component div')).get(0), |
| 42 | + listLink: element.all(by.css('ng-component a')).get(0), |
| 43 | + }; |
| 44 | + } |
| 45 | + |
| 46 | + function getHeroesSectionStruct() { |
| 47 | + return { |
| 48 | + header: element.all(by.css('ng-component h2')).get(0), |
| 49 | + title: element.all(by.css('ng-component h3')).get(0), |
| 50 | + items: element.all(by.css('ng-component a')), |
| 51 | + itemId: element.all(by.css('ng-component ng-component div div')).get(0), |
| 52 | + itemInput: element.all(by.css('ng-component ng-component input')).get(0), |
| 53 | + listLink: element.all(by.css('ng-component ng-component a')).get(0), |
| 54 | + }; |
| 55 | + } |
| 56 | + |
| 57 | + // tests |
| 58 | + function appTitleTests(color: string) { |
| 59 | + return function() { |
| 60 | + it('should have a gray header', function() { |
| 61 | + const commons = getCommonsSectionStruct(); |
| 62 | + expect(commons.title.getCssValue('backgroundColor')).toBe(color); |
| 63 | + }); |
| 64 | + |
| 65 | + it('should welcome us', function () { |
| 66 | + const commons = getCommonsSectionStruct(); |
| 67 | + expect(commons.subtitle.getText()).toBe('Welcome, Sam Spade'); |
| 68 | + }); |
| 69 | + }; |
| 70 | + } |
| 71 | + |
| 72 | + function contactTests(color: string) { |
| 73 | + return function() { |
| 74 | + it('shows the contact\'s owner', function() { |
| 75 | + const contacts = getContactSectionStruct(); |
| 76 | + expect(contacts.header.getText()).toBe('Contact of Sam Spade'); |
| 77 | + }); |
| 78 | + |
| 79 | + it('can cycle between contacts', function () { |
| 80 | + const contacts = getContactSectionStruct(); |
| 81 | + const nextButton = contacts.nextContactButton; |
| 82 | + expect(contacts.contactNameHeader.getText()).toBe('Awesome Sam Spade'); |
| 83 | + expect(contacts.contactNameHeader.getCssValue('backgroundColor')).toBe(color); |
| 84 | + nextButton.click().then(function () { |
| 85 | + expect(contacts.contactNameHeader.getText()).toBe('Awesome Nick Danger'); |
| 86 | + return nextButton.click(); |
| 87 | + }).then(function () { |
| 88 | + expect(contacts.contactNameHeader.getText()).toBe('Awesome Nancy Drew'); |
| 89 | + }); |
| 90 | + }); |
| 91 | + |
| 92 | + it('can change an existing contact', function () { |
| 93 | + const contacts = getContactSectionStruct(); |
| 94 | + contacts.input.sendKeys('a'); |
| 95 | + expect(contacts.input.getCssValue('backgroundColor')).toBe(color); |
| 96 | + expect(contacts.contactNameHeader.getText()).toBe('Awesome Sam Spadea'); |
| 97 | + }); |
| 98 | + |
| 99 | + it('can create a new contact', function () { |
| 100 | + const contacts = getContactSectionStruct(); |
| 101 | + const newContactButton = contacts.newContactButton; |
| 102 | + newContactButton.click().then(function () { |
| 103 | + expect(contacts.validationError.getText()).toBe('Name is required'); |
| 104 | + contacts.input.sendKeys('John Doe'); |
| 105 | + expect(contacts.contactNameHeader.getText()).toBe('Awesome John Doe'); |
| 106 | + expect(contacts.validationError.getText()).toBe(''); |
| 107 | + }); |
| 108 | + }); |
| 109 | + }; |
| 110 | + } |
| 111 | + |
| 112 | + describe('index.html', function () { |
| 113 | + beforeEach(function () { |
| 114 | + browser.get(''); |
| 115 | + }); |
| 116 | + |
| 117 | + describe('app-title', appTitleTests(lightgray)); |
| 118 | + |
| 119 | + describe('contact', contactTests(lightgray)); |
| 120 | + |
| 121 | + describe('crisis center', function () { |
| 122 | + beforeEach(function () { |
| 123 | + getCommonsSectionStruct().crisisButton.click(); |
| 124 | + }); |
| 125 | + |
| 126 | + it('shows a list of crisis', function () { |
| 127 | + const crisis = getCrisisSectionStruct(); |
| 128 | + expect(crisis.title.getText()).toBe('Crisis List'); |
| 129 | + expect(crisis.items.count()).toBe(4); |
| 130 | + expect(crisis.items.get(0).getText()).toBe('1 - Dragon Burning Cities'); |
| 131 | + }); |
| 132 | + |
| 133 | + it('can navigate to one crisis details', function () { |
| 134 | + const crisis = getCrisisSectionStruct(); |
| 135 | + crisis.items.get(0).click().then(function() { |
| 136 | + expect(crisis.itemId.getText()).toBe('Crisis id: 1'); |
| 137 | + return crisis.listLink.click(); |
| 138 | + }).then(function () { |
| 139 | + // We are back to the list |
| 140 | + expect(crisis.items.count()).toBe(4); |
| 141 | + }); |
| 142 | + }); |
| 143 | + }); |
| 144 | + |
| 145 | + describe('heroes', function () { |
| 146 | + beforeEach(function () { |
| 147 | + getCommonsSectionStruct().heroesButton.click(); |
| 148 | + }); |
| 149 | + |
| 150 | + it('shows a list of heroes', function() { |
| 151 | + const heroes = getHeroesSectionStruct(); |
| 152 | + expect(heroes.header.getText()).toBe('Heroes of Sam Spade'); |
| 153 | + expect(heroes.title.getText()).toBe('Hero List'); |
| 154 | + expect(heroes.items.count()).toBe(6); |
| 155 | + expect(heroes.items.get(0).getText()).toBe('11 - Mr. Nice'); |
| 156 | + }); |
| 157 | + |
| 158 | + it('can navigate and edit one hero details', function () { |
| 159 | + const heroes = getHeroesSectionStruct(); |
| 160 | + heroes.items.get(0).click().then(function () { |
| 161 | + expect(heroes.itemId.getText()).toBe('Id: 11'); |
| 162 | + heroes.itemInput.sendKeys(' try'); |
| 163 | + return heroes.listLink.click(); |
| 164 | + }).then(function () { |
| 165 | + // We are back to the list |
| 166 | + expect(heroes.items.count()).toBe(6); |
| 167 | + expect(heroes.items.get(0).getText()).toBe('11 - Mr. Nice try'); |
| 168 | + }); |
| 169 | + }); |
| 170 | + }); |
| 171 | + }); |
| 172 | + |
| 173 | + describe('index.0.html', function() { |
| 174 | + beforeEach(function () { |
| 175 | + browser.get('index.0.html'); |
| 176 | + }); |
| 177 | + |
| 178 | + it('has a title', function () { |
| 179 | + const title = element.all(by.tagName('h1')).get(0); |
| 180 | + expect(title.getText()).toBe('Minimal NgModule'); |
| 181 | + }); |
| 182 | + }); |
| 183 | + |
| 184 | + describe('index.1.html', function () { |
| 185 | + beforeEach(function () { |
| 186 | + browser.get('index.1.html'); |
| 187 | + }); |
| 188 | + |
| 189 | + describe('app-title', appTitleTests(powderblue)); |
| 190 | + }); |
| 191 | + |
| 192 | + describe('index.1b.html', function () { |
| 193 | + beforeEach(function () { |
| 194 | + browser.get('index.1b.html'); |
| 195 | + }); |
| 196 | + |
| 197 | + describe('app-title', appTitleTests(powderblue)); |
| 198 | + |
| 199 | + describe('contact', contactTests(powderblue)); |
| 200 | + }); |
| 201 | + |
| 202 | + describe('index.2.html', function () { |
| 203 | + beforeEach(function () { |
| 204 | + browser.get('index.2.html'); |
| 205 | + }); |
| 206 | + |
| 207 | + describe('app-title', appTitleTests(gold)); |
| 208 | + |
| 209 | + describe('contact', contactTests(powderblue)); |
| 210 | + }); |
| 211 | + |
| 212 | + describe('index.3.html', function () { |
| 213 | + beforeEach(function () { |
| 214 | + browser.get('index.3.html'); |
| 215 | + }); |
| 216 | + |
| 217 | + describe('app-title', appTitleTests(gold)); |
| 218 | + }); |
| 219 | + |
| 220 | +}); |
0 commit comments