| 
									
										
										
										
											2016-10-06 23:25:52 +01:00
										 |  |  | 'use strict'; // necessary for es6 output in node 
 | 
					
						
							| 
									
										
										
										
											2016-07-20 08:35:30 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-06 23:25:52 +01:00
										 |  |  | import { browser, element, by, ElementFinder } from 'protractor'; | 
					
						
							|  |  |  | import { promise } from 'selenium-webdriver'; | 
					
						
							| 
									
										
										
										
											2016-07-20 08:35:30 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | const expectedH1 = 'Tour of Heroes'; | 
					
						
							| 
									
										
										
										
											2016-09-20 05:24:40 +02:00
										 |  |  | const expectedTitle = `Angular ${expectedH1}`; | 
					
						
							| 
									
										
										
										
											2016-07-20 08:35:30 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | class Hero { | 
					
						
							|  |  |  |   id: number; | 
					
						
							|  |  |  |   name: string; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Factory method
 | 
					
						
							|  |  |  |   // Get hero id and name from the given detail element.
 | 
					
						
							| 
									
										
										
										
											2016-10-06 23:25:52 +01:00
										 |  |  |   static async fromDetail(detail: ElementFinder): Promise<Hero> { | 
					
						
							| 
									
										
										
										
											2016-07-20 08:35:30 -07:00
										 |  |  |     // Get hero id from the first <div>
 | 
					
						
							|  |  |  |     let _id = await detail.all(by.css('div')).first().getText(); | 
					
						
							|  |  |  |     // Get name from the h2
 | 
					
						
							|  |  |  |     let _name = await detail.element(by.css('h2')).getText(); | 
					
						
							|  |  |  |     return { | 
					
						
							|  |  |  |       id: +_id.substr(_id.indexOf(' ') + 1), | 
					
						
							| 
									
										
										
										
											2016-08-17 09:20:17 -07:00
										 |  |  |       name: _name.substr(0, _name.lastIndexOf(' ')) | 
					
						
							| 
									
										
										
										
											2016-07-20 08:35:30 -07:00
										 |  |  |     }; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const nameSuffix = 'X'; | 
					
						
							| 
									
										
										
										
											2016-10-06 23:25:52 +01:00
										 |  |  | function addToHeroName(text: string): promise.Promise<void> { | 
					
						
							| 
									
										
										
										
											2016-07-20 08:35:30 -07:00
										 |  |  |   let input = element(by.css('input')); | 
					
						
							| 
									
										
										
										
											2016-10-06 23:25:52 +01:00
										 |  |  |   return input.sendKeys(text); | 
					
						
							| 
									
										
										
										
											2016-07-20 08:35:30 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 07:40:49 -07:00
										 |  |  | describe('Tutorial part 1', () => { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-20 08:35:30 -07:00
										 |  |  |   const expectedHero = { id: 1, name: 'Windstorm' }; | 
					
						
							| 
									
										
										
										
											2016-06-08 07:40:49 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-20 08:35:30 -07:00
										 |  |  |   beforeAll(() => browser.get('')); | 
					
						
							| 
									
										
										
										
											2016-06-08 07:40:49 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-20 08:35:30 -07:00
										 |  |  |   it(`has title '${expectedTitle}'`, () => { | 
					
						
							| 
									
										
										
										
											2016-06-08 07:40:49 -07:00
										 |  |  |     expect(browser.getTitle()).toEqual(expectedTitle); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-20 08:35:30 -07:00
										 |  |  |   it(`has h1 '${expectedH1}'`, () => { | 
					
						
							|  |  |  |     let hText = element(by.css('h1')).getText(); | 
					
						
							|  |  |  |     expect(hText).toEqual(expectedH1, 'h1'); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   it(`shows initial hero details`, async () => { | 
					
						
							|  |  |  |     let page = getPageElts(); | 
					
						
							|  |  |  |     let hero = await Hero.fromDetail(page.heroDetail); | 
					
						
							|  |  |  |     expect(hero.id).toEqual(expectedHero.id); | 
					
						
							|  |  |  |     expect(hero.name).toEqual(expectedHero.name); | 
					
						
							| 
									
										
										
										
											2016-06-08 07:40:49 -07:00
										 |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-20 08:35:30 -07:00
										 |  |  |   it(`shows updated hero name`, async () => { | 
					
						
							|  |  |  |     addToHeroName(nameSuffix); | 
					
						
							|  |  |  |     let page = getPageElts(); | 
					
						
							|  |  |  |     let hero = await Hero.fromDetail(page.heroDetail); | 
					
						
							|  |  |  |     let newName = expectedHero.name + nameSuffix; | 
					
						
							|  |  |  |     expect(hero.id).toEqual(expectedHero.id); | 
					
						
							|  |  |  |     expect(hero.name).toEqual(newName); | 
					
						
							| 
									
										
										
										
											2016-06-08 07:40:49 -07:00
										 |  |  |   }); | 
					
						
							| 
									
										
										
										
											2016-07-20 08:35:30 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 07:40:49 -07:00
										 |  |  | }); | 
					
						
							| 
									
										
										
										
											2016-07-20 08:35:30 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | function getPageElts() { | 
					
						
							|  |  |  |   return { | 
					
						
							|  |  |  |     heroDetail: element(by.css('my-app')) | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | } |