|
| 1 | +'use strict'; // necessary for es6 output in node |
| 2 | + |
| 3 | +import { browser, element, by, ElementFinder, ElementArrayFinder } from 'protractor'; |
| 4 | +import { promise } from 'selenium-webdriver'; |
| 5 | + |
| 6 | +const expectedH1 = 'Tour of Heroes - GraphQL'; |
| 7 | +const expectedTitle = `Angular ${expectedH1}`; |
| 8 | +const targetHero = { id: 15, name: 'Magneta' }; |
| 9 | +const targetHeroDashboardIndex = 3; |
| 10 | +const nameSuffix = 'X'; |
| 11 | +const newHeroName = targetHero.name + nameSuffix; |
| 12 | + |
| 13 | +class Hero { |
| 14 | + id: number; |
| 15 | + name: string; |
| 16 | + |
| 17 | + // Factory methods |
| 18 | + |
| 19 | + // Hero from string formatted as '<id> <name>'. |
| 20 | + static fromString(s: string): Hero { |
| 21 | + return { |
| 22 | + id: +s.substr(0, s.indexOf(' ')), |
| 23 | + name: s.substr(s.indexOf(' ') + 1), |
| 24 | + }; |
| 25 | + } |
| 26 | + |
| 27 | + // Hero from hero list <li> element. |
| 28 | + static async fromLi(li: ElementFinder): Promise<Hero> { |
| 29 | + let strings = await li.all(by.xpath('span')).getText(); |
| 30 | + return { id: +strings[0], name: strings[1] }; |
| 31 | + } |
| 32 | + |
| 33 | + // Hero id and name from the given detail element. |
| 34 | + static async fromDetail(detail: ElementFinder): Promise<Hero> { |
| 35 | + // Get hero id from the first <div> |
| 36 | + let _id = await detail.all(by.css('div')).first().getText(); |
| 37 | + // Get name from the h2 |
| 38 | + let _name = await detail.element(by.css('h2')).getText(); |
| 39 | + return { |
| 40 | + id: +_id.substr(_id.indexOf(' ') + 1), |
| 41 | + name: _name.substr(0, _name.lastIndexOf(' ')) |
| 42 | + }; |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +describe('Heroes GraphQL', () => { |
| 47 | + |
| 48 | + beforeAll(() => browser.get('')); |
| 49 | + |
| 50 | + function getPageElts() { |
| 51 | + let navElts = element.all(by.css('my-app nav a')); |
| 52 | + |
| 53 | + return { |
| 54 | + navElts: navElts, |
| 55 | + |
| 56 | + myDashboardHref: navElts.get(0), |
| 57 | + myDashboard: element(by.css('my-app my-dashboard')), |
| 58 | + topHeroes: element.all(by.css('my-app my-dashboard > div h4')), |
| 59 | + |
| 60 | + myHeroesHref: navElts.get(1), |
| 61 | + myHeroes: element(by.css('my-app my-heroes')), |
| 62 | + allHeroes: element.all(by.css('my-app my-heroes li')), |
| 63 | + selectedHero: element(by.css('my-app li.selected')), |
| 64 | + selectedHeroSubview: element(by.css('my-app my-heroes > div:last-child')), |
| 65 | + |
| 66 | + heroDetail: element(by.css('my-app my-hero-detail > div')), |
| 67 | + |
| 68 | + searchBox: element(by.css('#search-box')), |
| 69 | + searchResults: element.all(by.css('.search-result')) |
| 70 | + }; |
| 71 | + } |
| 72 | + |
| 73 | + describe('Initial page', () => { |
| 74 | + |
| 75 | + it(`has title '${expectedTitle}'`, () => { |
| 76 | + expect(browser.getTitle()).toEqual(expectedTitle); |
| 77 | + }); |
| 78 | + |
| 79 | + it(`has h1 '${expectedH1}'`, () => { |
| 80 | + expectHeading(1, expectedH1); |
| 81 | + }); |
| 82 | + |
| 83 | + const expectedViewNames = ['Dashboard', 'Heroes']; |
| 84 | + it(`has views ${expectedViewNames}`, () => { |
| 85 | + let viewNames = getPageElts().navElts.map((el: ElementFinder) => el.getText()); |
| 86 | + expect(viewNames).toEqual(expectedViewNames); |
| 87 | + }); |
| 88 | + |
| 89 | + it('has dashboard as the active view', () => { |
| 90 | + let page = getPageElts(); |
| 91 | + expect(page.myDashboard.isPresent()).toBeTruthy(); |
| 92 | + }); |
| 93 | + |
| 94 | + }); |
| 95 | + |
| 96 | + describe('Dashboard tests', () => { |
| 97 | + |
| 98 | + beforeAll(() => browser.get('')); |
| 99 | + |
| 100 | + it('has top heroes', () => { |
| 101 | + let page = getPageElts(); |
| 102 | + expect(page.topHeroes.count()).toEqual(4); |
| 103 | + }); |
| 104 | + |
| 105 | + it(`selects and routes to ${targetHero.name} details`, dashboardSelectTargetHero); |
| 106 | + |
| 107 | + it(`updates hero name (${newHeroName}) in details view`, updateHeroNameInDetailView); |
| 108 | + |
| 109 | + it(`cancels and shows ${targetHero.name} in Dashboard`, () => { |
| 110 | + element(by.buttonText('Back')).click(); |
| 111 | + browser.waitForAngular(); // seems necessary to gets tests to past for toh-6 |
| 112 | + |
| 113 | + let targetHeroElt = getPageElts().topHeroes.get(targetHeroDashboardIndex); |
| 114 | + expect(targetHeroElt.getText()).toEqual(targetHero.name); |
| 115 | + }); |
| 116 | + |
| 117 | + it(`selects and routes to ${targetHero.name} details`, dashboardSelectTargetHero); |
| 118 | + |
| 119 | + it(`updates hero name (${newHeroName}) in details view`, updateHeroNameInDetailView); |
| 120 | + |
| 121 | + it(`saves and shows ${newHeroName} in Dashboard`, () => { |
| 122 | + element(by.buttonText('Save')).click(); |
| 123 | + browser.waitForAngular(); // seems necessary to gets tests to past for toh-6 |
| 124 | + |
| 125 | + let targetHeroElt = getPageElts().topHeroes.get(targetHeroDashboardIndex); |
| 126 | + expect(targetHeroElt.getText()).toEqual(newHeroName); |
| 127 | + }); |
| 128 | + |
| 129 | + }); |
| 130 | + |
| 131 | + describe('Heroes tests', () => { |
| 132 | + |
| 133 | + beforeAll(() => browser.get('')); |
| 134 | + |
| 135 | + it('can switch to Heroes view', () => { |
| 136 | + getPageElts().myHeroesHref.click(); |
| 137 | + let page = getPageElts(); |
| 138 | + expect(page.myHeroes.isPresent()).toBeTruthy(); |
| 139 | + expect(page.allHeroes.count()).toEqual(10, 'number of heroes'); |
| 140 | + }); |
| 141 | + |
| 142 | + it(`selects and shows ${targetHero.name} as selected in list`, () => { |
| 143 | + getHeroLiEltById(targetHero.id).click(); |
| 144 | + expect(Hero.fromLi(getPageElts().selectedHero)).toEqual(targetHero); |
| 145 | + }); |
| 146 | + |
| 147 | + it('shows selected hero subview', () => { |
| 148 | + let page = getPageElts(); |
| 149 | + let title = page.selectedHeroSubview.element(by.css('h2')).getText(); |
| 150 | + let expectedTitle = `${targetHero.name.toUpperCase()} is my hero`; |
| 151 | + expect(title).toEqual(expectedTitle); |
| 152 | + }); |
| 153 | + |
| 154 | + it('can route to hero details', () => { |
| 155 | + element(by.buttonText('View Details')).click(); |
| 156 | + |
| 157 | + let page = getPageElts(); |
| 158 | + expect(page.heroDetail.isPresent()).toBeTruthy('shows hero detail'); |
| 159 | + let hero = Hero.fromDetail(page.heroDetail); |
| 160 | + expect(hero).toEqual(targetHero); |
| 161 | + }); |
| 162 | + |
| 163 | + it(`updates hero name (${newHeroName}) in details view`, updateHeroNameInDetailView); |
| 164 | + |
| 165 | + it(`shows ${newHeroName} in Heroes list`, () => { |
| 166 | + element(by.buttonText('Save')).click(); |
| 167 | + browser.waitForAngular(); // seems necessary to gets tests to past for toh-6 |
| 168 | + let expectedHero = {id: targetHero.id, name: newHeroName}; |
| 169 | + expect(Hero.fromLi(getHeroLiEltById(targetHero.id))).toEqual(expectedHero); |
| 170 | + }); |
| 171 | + |
| 172 | + it(`deletes ${newHeroName} from Heroes list`, async () => { |
| 173 | + const heroesBefore = await toHeroArray(getPageElts().allHeroes); |
| 174 | + const li = getHeroLiEltById(targetHero.id); |
| 175 | + li.element(by.buttonText('x')).click(); |
| 176 | + |
| 177 | + const page = getPageElts(); |
| 178 | + expect(page.myHeroes.isPresent()).toBeTruthy(); |
| 179 | + expect(page.allHeroes.count()).toEqual(9, 'number of heroes'); |
| 180 | + const heroesAfter = await toHeroArray(page.allHeroes); |
| 181 | + const expectedHeroes = heroesBefore.filter(h => h.name !== newHeroName); |
| 182 | + expect(heroesAfter).toEqual(expectedHeroes); |
| 183 | + expect(page.selectedHeroSubview.isPresent()).toBeFalsy(); |
| 184 | + }); |
| 185 | + |
| 186 | + it(`adds back ${targetHero.name}`, async () => { |
| 187 | + const newHeroName = 'Alice'; |
| 188 | + const heroesBefore = await toHeroArray(getPageElts().allHeroes); |
| 189 | + const numHeroes = heroesBefore.length; |
| 190 | + |
| 191 | + element(by.css('input')).sendKeys(newHeroName); |
| 192 | + element(by.buttonText('Add')).click(); |
| 193 | + |
| 194 | + let page = getPageElts(); |
| 195 | + let heroesAfter = await toHeroArray(page.allHeroes); |
| 196 | + expect(heroesAfter.length).toEqual(numHeroes + 1, 'number of heroes'); |
| 197 | + |
| 198 | + expect(heroesAfter.slice(0, numHeroes)).toEqual(heroesBefore, 'Old heroes are still there'); |
| 199 | + |
| 200 | + const maxId = heroesBefore[heroesBefore.length - 1].id; |
| 201 | + expect(heroesAfter[numHeroes]).toEqual({id: maxId + 1, name: newHeroName}); |
| 202 | + }); |
| 203 | + }); |
| 204 | + |
| 205 | + describe('Progressive hero search', () => { |
| 206 | + |
| 207 | + beforeAll(() => browser.get('')); |
| 208 | + |
| 209 | + it(`searches for 'Ma'`, async () => { |
| 210 | + getPageElts().searchBox.sendKeys('Ma'); |
| 211 | + browser.sleep(1000); |
| 212 | + expect(getPageElts().searchResults.count()).toBe(4); |
| 213 | + }); |
| 214 | + |
| 215 | + it(`continues search with 'g'`, async () => { |
| 216 | + getPageElts().searchBox.sendKeys('g'); |
| 217 | + browser.sleep(1000); |
| 218 | + expect(getPageElts().searchResults.count()).toBe(2); |
| 219 | + }); |
| 220 | + |
| 221 | + it(`continues search with 'n' and gets ${targetHero.name}`, async () => { |
| 222 | + getPageElts().searchBox.sendKeys('n'); |
| 223 | + browser.sleep(1000); |
| 224 | + let page = getPageElts(); |
| 225 | + expect(page.searchResults.count()).toBe(1); |
| 226 | + let hero = page.searchResults.get(0); |
| 227 | + expect(hero.getText()).toEqual(targetHero.name); |
| 228 | + }); |
| 229 | + |
| 230 | + it(`navigates to ${targetHero.name} details view`, async () => { |
| 231 | + let hero = getPageElts().searchResults.get(0); |
| 232 | + expect(hero.getText()).toEqual(targetHero.name); |
| 233 | + hero.click(); |
| 234 | + |
| 235 | + let page = getPageElts(); |
| 236 | + expect(page.heroDetail.isPresent()).toBeTruthy('shows hero detail'); |
| 237 | + expect(Hero.fromDetail(page.heroDetail)).toEqual(targetHero); |
| 238 | + }); |
| 239 | + }); |
| 240 | + |
| 241 | + function dashboardSelectTargetHero() { |
| 242 | + let targetHeroElt = getPageElts().topHeroes.get(targetHeroDashboardIndex); |
| 243 | + expect(targetHeroElt.getText()).toEqual(targetHero.name); |
| 244 | + targetHeroElt.click(); |
| 245 | + browser.waitForAngular(); // seems necessary to gets tests to past for toh-6 |
| 246 | + |
| 247 | + let page = getPageElts(); |
| 248 | + expect(page.heroDetail.isPresent()).toBeTruthy('shows hero detail'); |
| 249 | + let hero = Hero.fromDetail(page.heroDetail); |
| 250 | + expect(hero).toEqual(targetHero); |
| 251 | + } |
| 252 | + |
| 253 | + async function updateHeroNameInDetailView() { |
| 254 | + // Assumes that the current view is the hero details view. |
| 255 | + addToHeroName(nameSuffix); |
| 256 | + |
| 257 | + let hero = await Hero.fromDetail(getPageElts().heroDetail); |
| 258 | + expect(hero).toEqual({id: targetHero.id, name: newHeroName}); |
| 259 | + } |
| 260 | + |
| 261 | +}); |
| 262 | + |
| 263 | +function addToHeroName(text: string): promise.Promise<void> { |
| 264 | + let input = element(by.css('input')); |
| 265 | + return input.sendKeys(text); |
| 266 | +} |
| 267 | + |
| 268 | +function expectHeading(hLevel: number, expectedText: string): void { |
| 269 | + let hTag = `h${hLevel}`; |
| 270 | + let hText = element(by.css(hTag)).getText(); |
| 271 | + expect(hText).toEqual(expectedText, hTag); |
| 272 | +}; |
| 273 | + |
| 274 | +function getHeroLiEltById(id: number): ElementFinder { |
| 275 | + let spanForId = element(by.cssContainingText('li span.badge', id.toString())); |
| 276 | + return spanForId.element(by.xpath('..')); |
| 277 | +} |
| 278 | + |
| 279 | +async function toHeroArray(allHeroes: ElementArrayFinder): Promise<Hero[]> { |
| 280 | + let promisedHeroes = await allHeroes.map(Hero.fromLi); |
| 281 | + // The cast is necessary to get around issuing with the signature of Promise.all() |
| 282 | + return <Promise<any>> Promise.all(promisedHeroes); |
| 283 | +} |
0 commit comments