|
| 1 | +/// <reference path='../_protractor/e2e.d.ts' /> |
| 2 | +'use strict'; |
| 3 | +describe('TOH Http Chapter', function () { |
| 4 | + |
| 5 | + beforeEach(function () { |
| 6 | + browser.get(''); |
| 7 | + }); |
| 8 | + |
| 9 | + function getPageStruct() { |
| 10 | + let hrefEles = element.all(by.css('my-app a')); |
| 11 | + |
| 12 | + return { |
| 13 | + hrefs: hrefEles, |
| 14 | + myDashboardHref: hrefEles.get(0), |
| 15 | + myDashboardParent: element(by.css('my-app my-dashboard')), |
| 16 | + topHeroes: element.all(by.css('my-app my-dashboard .module.hero')), |
| 17 | + |
| 18 | + myHeroesHref: hrefEles.get(1), |
| 19 | + myHeroesParent: element(by.css('my-app my-heroes')), |
| 20 | + allHeroes: element.all(by.css('my-app my-heroes li .hero-element')), |
| 21 | + |
| 22 | + firstDeleteButton: element.all(by.buttonText('Delete')).get(0), |
| 23 | + |
| 24 | + addButton: element.all(by.buttonText('Add New Hero')).get(0), |
| 25 | + |
| 26 | + heroDetail: element(by.css('my-app my-hero-detail')) |
| 27 | + }; |
| 28 | + } |
| 29 | + |
| 30 | + it('should be able to add a hero from the "Heroes" view', function(){ |
| 31 | + let page = getPageStruct(); |
| 32 | + let heroCount: webdriver.promise.Promise<number>; |
| 33 | + |
| 34 | + page.myHeroesHref.click().then(function() { |
| 35 | + browser.waitForAngular(); |
| 36 | + heroCount = page.allHeroes.count(); |
| 37 | + expect(heroCount).toBe(10, 'should show 10'); |
| 38 | + }).then(function() { |
| 39 | + return page.addButton.click(); |
| 40 | + }).then(function(){ |
| 41 | + return save(page, '', 'The New Hero'); |
| 42 | + }).then(function(){ |
| 43 | + browser.waitForAngular(); |
| 44 | + |
| 45 | + heroCount = page.allHeroes.count(); |
| 46 | + expect(heroCount).toBe(11, 'should show 11'); |
| 47 | + |
| 48 | + let newHero = element(by.xpath('//span[@class="hero-element" and contains(text(),"The New Hero")]')); |
| 49 | + expect(newHero).toBeDefined(); |
| 50 | + }); |
| 51 | + }); |
| 52 | + |
| 53 | + it('should be able to delete hero from "Heroes" view', function(){ |
| 54 | + let page = getPageStruct(); |
| 55 | + let heroCount: webdriver.promise.Promise<number>; |
| 56 | + |
| 57 | + page.myHeroesHref.click().then(function() { |
| 58 | + browser.waitForAngular(); |
| 59 | + heroCount = page.allHeroes.count(); |
| 60 | + expect(heroCount).toBe(10, 'should show 10'); |
| 61 | + }).then(function() { |
| 62 | + return page.firstDeleteButton.click(); |
| 63 | + }).then(function(){ |
| 64 | + browser.waitForAngular(); |
| 65 | + heroCount = page.allHeroes.count(); |
| 66 | + expect(heroCount).toBe(9, 'should show 9'); |
| 67 | + }); |
| 68 | + }); |
| 69 | + |
| 70 | + it('should be able to save details from "Dashboard" view', function () { |
| 71 | + let page = getPageStruct(); |
| 72 | + expect(page.myDashboardParent.isPresent()).toBe(true, 'dashboard element should be available'); |
| 73 | + let heroEle = page.topHeroes.get(2); |
| 74 | + let heroDescrEle = heroEle.element(by.css('h4')); |
| 75 | + let heroDescr: string; |
| 76 | + |
| 77 | + return heroDescrEle.getText().then(function(text) { |
| 78 | + heroDescr = text; |
| 79 | + return heroEle.click(); |
| 80 | + }).then(function() { |
| 81 | + return save(page, heroDescr, '-foo'); |
| 82 | + }) |
| 83 | + .then(function(){ |
| 84 | + return page.myDashboardHref.click(); |
| 85 | + }) |
| 86 | + .then(function() { |
| 87 | + expect(page.myDashboardParent.isPresent()).toBe(true, 'dashboard element should be back'); |
| 88 | + expect(heroDescrEle.getText()).toEqual(heroDescr + '-foo'); |
| 89 | + }); |
| 90 | + }); |
| 91 | + |
| 92 | + it('should be able to save details from "Heroes" view', function () { |
| 93 | + let page = getPageStruct(); |
| 94 | + |
| 95 | + let viewDetailsButtonEle = page.myHeroesParent.element(by.cssContainingText('button', 'View Details')); |
| 96 | + let heroEle: protractor.ElementFinder, heroDescr: string; |
| 97 | + |
| 98 | + page.myHeroesHref.click().then(function() { |
| 99 | + expect(page.myDashboardParent.isPresent()).toBe(false, 'dashboard element should NOT be present'); |
| 100 | + expect(page.myHeroesParent.isPresent()).toBe(true, 'myHeroes element should be present'); |
| 101 | + expect(viewDetailsButtonEle.isPresent()).toBe(false, 'viewDetails button should not yet be present'); |
| 102 | + heroEle = page.allHeroes.get(0); |
| 103 | + return heroEle.getText(); |
| 104 | + }).then(function(text) { |
| 105 | + // remove leading 'id' from the element |
| 106 | + heroDescr = text.substr(text.indexOf(' ') + 1); |
| 107 | + return heroEle.click(); |
| 108 | + }).then(function() { |
| 109 | + expect(viewDetailsButtonEle.isDisplayed()).toBe(true, 'viewDetails button should now be visible'); |
| 110 | + return viewDetailsButtonEle.click(); |
| 111 | + }).then(function() { |
| 112 | + return save(page, heroDescr, '-bar'); |
| 113 | + }) |
| 114 | + .then(function(){ |
| 115 | + return page.myHeroesHref.click(); |
| 116 | + }) |
| 117 | + .then(function() { |
| 118 | + expect(heroEle.getText()).toContain(heroDescr + '-bar'); |
| 119 | + }); |
| 120 | + }); |
| 121 | + |
| 122 | + function save(page: any, origValue: string, textToAdd: string) { |
| 123 | + let inputEle = page.heroDetail.element(by.css('input')); |
| 124 | + expect(inputEle.isDisplayed()).toBe(true, 'should be able to see the input box'); |
| 125 | + let saveButtonEle = page.heroDetail.element(by.buttonText('Save')); |
| 126 | + let backButtonEle = page.heroDetail.element(by.buttonText('Back')); |
| 127 | + expect(backButtonEle.isDisplayed()).toBe(true, 'should be able to see the back button'); |
| 128 | + let detailTextEle = page.heroDetail.element(by.css('div h2')); |
| 129 | + expect(detailTextEle.getText()).toContain(origValue); |
| 130 | + return sendKeys(inputEle, textToAdd).then(function () { |
| 131 | + expect(detailTextEle.getText()).toContain(origValue + textToAdd); |
| 132 | + return saveButtonEle.click(); |
| 133 | + }); |
| 134 | + } |
| 135 | + |
| 136 | + it('should be able to see the start screen', function () { |
| 137 | + let page = getPageStruct(); |
| 138 | + expect(page.hrefs.count()).toEqual(2, 'should be two dashboard choices'); |
| 139 | + expect(page.myDashboardHref.getText()).toEqual('Dashboard'); |
| 140 | + expect(page.myHeroesHref.getText()).toEqual('Heroes'); |
| 141 | + }); |
| 142 | + |
| 143 | + it('should be able to see dashboard choices', function () { |
| 144 | + let page = getPageStruct(); |
| 145 | + expect(page.topHeroes.count()).toBe(4, 'should be 4 dashboard hero choices'); |
| 146 | + }); |
| 147 | + |
| 148 | + it('should be able to toggle the views', function () { |
| 149 | + let page = getPageStruct(); |
| 150 | + |
| 151 | + expect(page.myDashboardParent.element(by.css('h3')).getText()).toEqual('Top Heroes'); |
| 152 | + page.myHeroesHref.click().then(function() { |
| 153 | + expect(page.myDashboardParent.isPresent()).toBe(false, 'should no longer see dashboard element'); |
| 154 | + expect(page.allHeroes.count()).toBeGreaterThan(4, 'should be more than 4 heroes shown'); |
| 155 | + return page.myDashboardHref.click(); |
| 156 | + }).then(function() { |
| 157 | + expect(page.myDashboardParent.isPresent()).toBe(true, 'should once again see the dashboard element'); |
| 158 | + }); |
| 159 | + |
| 160 | + }); |
| 161 | + |
| 162 | + it('should be able to edit details from "Dashboard" view', function () { |
| 163 | + let page = getPageStruct(); |
| 164 | + expect(page.myDashboardParent.isPresent()).toBe(true, 'dashboard element should be available'); |
| 165 | + let heroEle = page.topHeroes.get(3); |
| 166 | + let heroDescrEle = heroEle.element(by.css('h4')); |
| 167 | + let heroDescr: string; |
| 168 | + return heroDescrEle.getText().then(function(text) { |
| 169 | + heroDescr = text; |
| 170 | + return heroEle.click(); |
| 171 | + }).then(function() { |
| 172 | + return editDetails(page, heroDescr, '-foo'); |
| 173 | + }).then(function() { |
| 174 | + expect(page.myDashboardParent.isPresent()).toBe(true, 'dashboard element should be back'); |
| 175 | + expect(heroDescrEle.getText()).toEqual(heroDescr + '-foo'); |
| 176 | + }); |
| 177 | + }); |
| 178 | + |
| 179 | + it('should be able to edit details from "Heroes" view', function () { |
| 180 | + let page = getPageStruct(); |
| 181 | + expect(page.myDashboardParent.isPresent()).toBe(true, 'dashboard element should be present'); |
| 182 | + let viewDetailsButtonEle = page.myHeroesParent.element(by.cssContainingText('button', 'View Details')); |
| 183 | + let heroEle: protractor.ElementFinder, heroDescr: string; |
| 184 | + page.myHeroesHref.click().then(function() { |
| 185 | + expect(page.myDashboardParent.isPresent()).toBe(false, 'dashboard element should NOT be present'); |
| 186 | + expect(page.myHeroesParent.isPresent()).toBe(true, 'myHeroes element should be present'); |
| 187 | + expect(viewDetailsButtonEle.isPresent()).toBe(false, 'viewDetails button should not yet be present'); |
| 188 | + heroEle = page.allHeroes.get(2); |
| 189 | + return heroEle.getText(); |
| 190 | + }).then(function(text) { |
| 191 | + // remove leading 'id' from the element |
| 192 | + heroDescr = text.substr(text.indexOf(' ') + 1); |
| 193 | + return heroEle.click(); |
| 194 | + }).then(function() { |
| 195 | + expect(viewDetailsButtonEle.isDisplayed()).toBe(true, 'viewDetails button should now be visible'); |
| 196 | + return viewDetailsButtonEle.click(); |
| 197 | + }).then(function() { |
| 198 | + return editDetails(page, heroDescr, '-bar'); |
| 199 | + }).then(function() { |
| 200 | + expect(page.myHeroesParent.isPresent()).toBe(true, 'myHeroes element should be back'); |
| 201 | + expect(heroEle.getText()).toContain(heroDescr + '-bar'); |
| 202 | + expect(viewDetailsButtonEle.isPresent()).toBe(false, 'viewDetails button should again NOT be present'); |
| 203 | + }); |
| 204 | + }); |
| 205 | + |
| 206 | + function editDetails(page: any, origValue: string, textToAdd: string) { |
| 207 | + expect(page.myDashboardParent.isPresent()).toBe(false, 'dashboard element should NOT be present'); |
| 208 | + expect(page.myHeroesParent.isPresent()).toBe(false, 'myHeroes element should NOT be present'); |
| 209 | + expect(page.heroDetail.isDisplayed()).toBe(true, 'should be able to see hero-details'); |
| 210 | + let inputEle = page.heroDetail.element(by.css('input')); |
| 211 | + expect(inputEle.isDisplayed()).toBe(true, 'should be able to see the input box'); |
| 212 | + let buttons = page.heroDetail.all(by.css('button')); |
| 213 | + let backButtonEle = buttons.get(0); |
| 214 | + let saveButtonEle = buttons.get(1); |
| 215 | + expect(backButtonEle.isDisplayed()).toBe(true, 'should be able to see the back button'); |
| 216 | + expect(saveButtonEle.isDisplayed()).toBe(true, 'should be able to see the save button'); |
| 217 | + let detailTextEle = page.heroDetail.element(by.css('div h2')); |
| 218 | + expect(detailTextEle.getText()).toContain(origValue); |
| 219 | + return sendKeys(inputEle, textToAdd).then(function () { |
| 220 | + expect(detailTextEle.getText()).toContain(origValue + textToAdd); |
| 221 | + return saveButtonEle.click(); |
| 222 | + }); |
| 223 | + } |
| 224 | + |
| 225 | +}); |
0 commit comments