|
| 1 | +/// <reference path="../_protractor/e2e.d.ts" /> |
| 2 | +'use strict'; // necessary for node! |
| 3 | + |
| 4 | +// THESE TESTS ARE INCOMPLETE |
| 5 | +describeIf(browser.appIsTs || browser.appIsJs, 'Form Validation Tests', function () { |
| 6 | + |
| 7 | + beforeAll(function () { |
| 8 | + browser.get(''); |
| 9 | + }); |
| 10 | + |
| 11 | + describe('Hero Form 1', () => { |
| 12 | + beforeAll(() => { |
| 13 | + getPage('hero-form-template1'); |
| 14 | + }); |
| 15 | + |
| 16 | + tests(); |
| 17 | + }); |
| 18 | + |
| 19 | + describe('Hero Form 2', () => { |
| 20 | + beforeAll(() => { |
| 21 | + getPage('hero-form-template2'); |
| 22 | + }); |
| 23 | + |
| 24 | + tests(); |
| 25 | + bobTests(); |
| 26 | + }); |
| 27 | + |
| 28 | + describe('Hero Form 3 (Reactive)', () => { |
| 29 | + beforeAll(() => { |
| 30 | + getPage('hero-form-reactive3'); |
| 31 | + makeNameTooLong(); |
| 32 | + }); |
| 33 | + |
| 34 | + tests(); |
| 35 | + bobTests(); |
| 36 | + }); |
| 37 | +}); |
| 38 | + |
| 39 | +////////// |
| 40 | + |
| 41 | +const testName = 'Test Name'; |
| 42 | + |
| 43 | +let page: { |
| 44 | + section: protractor.ElementFinder, |
| 45 | + form: protractor.ElementFinder, |
| 46 | + title: protractor.ElementFinder, |
| 47 | + nameInput: protractor.ElementFinder, |
| 48 | + alterEgoInput: protractor.ElementFinder, |
| 49 | + powerSelect: protractor.ElementFinder, |
| 50 | + errorMessages: protractor.ElementArrayFinder, |
| 51 | + heroFormButtons: protractor.ElementArrayFinder, |
| 52 | + heroSubmitted: protractor.ElementFinder |
| 53 | +}; |
| 54 | + |
| 55 | +function getPage(sectionTag: string) { |
| 56 | + let section = element(by.css(sectionTag)); |
| 57 | + let buttons = section.all(by.css('button')); |
| 58 | + |
| 59 | + page = { |
| 60 | + section: section, |
| 61 | + form: section.element(by.css('form')), |
| 62 | + title: section.element(by.css('h1')), |
| 63 | + nameInput: section.element(by.css('#name')), |
| 64 | + alterEgoInput: section.element(by.css('#alterEgo')), |
| 65 | + powerSelect: section.element(by.css('#power')), |
| 66 | + errorMessages: section.all(by.css('div.alert')), |
| 67 | + heroFormButtons: buttons, |
| 68 | + heroSubmitted: section.element(by.css('hero-submitted > div')) |
| 69 | + }; |
| 70 | +} |
| 71 | + |
| 72 | +function tests() { |
| 73 | + it('should display correct title', function () { |
| 74 | + expect(page.title.getText()).toContain('Hero Form'); |
| 75 | + }); |
| 76 | + |
| 77 | + it('should not display submitted message before submit', function () { |
| 78 | + expect(page.heroSubmitted.isElementPresent(by.css('h2'))).toBe(false); |
| 79 | + }); |
| 80 | + |
| 81 | + it('should have form buttons', function () { |
| 82 | + expect(page.heroFormButtons.count()).toEqual(2); |
| 83 | + }); |
| 84 | + |
| 85 | + it('should have error at start', function () { |
| 86 | + expectFormIsInvalid(); |
| 87 | + }); |
| 88 | + |
| 89 | + // it('showForm', function () { |
| 90 | + // page.form.getInnerHtml().then(html => console.log(html)); |
| 91 | + // }); |
| 92 | + |
| 93 | + it('should have disabled submit button', function () { |
| 94 | + expect(page.heroFormButtons.get(0).isEnabled()).toBe(false); |
| 95 | + }); |
| 96 | + |
| 97 | + it('resetting name to valid name should clear errors', function () { |
| 98 | + const ele = page.nameInput; |
| 99 | + expect(ele.isPresent()).toBe(true, 'nameInput should exist'); |
| 100 | + ele.clear(); |
| 101 | + ele.sendKeys(testName); |
| 102 | + expectFormIsValid(); |
| 103 | + }); |
| 104 | + |
| 105 | + it('should produce "required" error after clearing name', function () { |
| 106 | + page.nameInput.clear(); |
| 107 | + // page.alterEgoInput.click(); // to blur ... didn't work |
| 108 | + page.nameInput.sendKeys('x', protractor.Key.BACK_SPACE); // ugh! |
| 109 | + expect(page.form.getAttribute('class')).toMatch('ng-invalid'); |
| 110 | + expect(page.errorMessages.get(0).getText()).toContain('required'); |
| 111 | + }); |
| 112 | + |
| 113 | + it('should produce "at least 4 characters" error when name="x"', function () { |
| 114 | + page.nameInput.clear(); |
| 115 | + page.nameInput.sendKeys('x'); // too short |
| 116 | + expectFormIsInvalid(); |
| 117 | + expect(page.errorMessages.get(0).getText()).toContain('at least 4 characters'); |
| 118 | + }); |
| 119 | + |
| 120 | + it('resetting name to valid name again should clear errors', function () { |
| 121 | + page.nameInput.sendKeys(testName); |
| 122 | + expectFormIsValid(); |
| 123 | + }); |
| 124 | + |
| 125 | + it('should have enabled submit button', function () { |
| 126 | + const submitBtn = page.heroFormButtons.get(0); |
| 127 | + expect(submitBtn.isEnabled()).toBe(true); |
| 128 | + }); |
| 129 | + |
| 130 | + it('should hide form after submit', function () { |
| 131 | + page.heroFormButtons.get(0).click(); |
| 132 | + expect(page.title.isDisplayed()).toBe(false); |
| 133 | + }); |
| 134 | + |
| 135 | + it('submitted form should be displayed', function () { |
| 136 | + expect(page.heroSubmitted.isElementPresent(by.css('h2'))).toBe(true); |
| 137 | + }); |
| 138 | + |
| 139 | + it('submitted form should have new hero name', function () { |
| 140 | + expect(page.heroSubmitted.getText()).toContain(testName); |
| 141 | + }); |
| 142 | + |
| 143 | + it('clicking edit button should reveal form again', function () { |
| 144 | + const editBtn = page.heroSubmitted.element(by.css('button')); |
| 145 | + editBtn.click(); |
| 146 | + expect(page.heroSubmitted.isElementPresent(by.css('h2'))) |
| 147 | + .toBe(false, 'submitted hidden again'); |
| 148 | + expect(page.title.isDisplayed()).toBe(true, 'can see form title'); |
| 149 | + }); |
| 150 | +} |
| 151 | + |
| 152 | +function expectFormIsValid() { |
| 153 | + expect(page.form.getAttribute('class')).toMatch('ng-valid'); |
| 154 | +} |
| 155 | + |
| 156 | +function expectFormIsInvalid() { |
| 157 | + expect(page.form.getAttribute('class')).toMatch('ng-invalid'); |
| 158 | +} |
| 159 | + |
| 160 | +function bobTests() { |
| 161 | + const emsg = 'Someone named "Bob" cannot be a hero.'; |
| 162 | + |
| 163 | + it('should produce "no bob" error after setting name to "Bobby"', function () { |
| 164 | + page.nameInput.clear(); |
| 165 | + page.nameInput.sendKeys('Bobby'); |
| 166 | + expectFormIsInvalid(); |
| 167 | + expect(page.errorMessages.get(0).getText()).toBe(emsg); |
| 168 | + }); |
| 169 | + |
| 170 | + it('should be ok again with valid name', function () { |
| 171 | + page.nameInput.clear(); |
| 172 | + page.nameInput.sendKeys(testName); |
| 173 | + expectFormIsValid(); |
| 174 | + }); |
| 175 | +} |
| 176 | + |
| 177 | +function makeNameTooLong() { |
| 178 | + // make the first name invalid |
| 179 | + page.nameInput.sendKeys('ThisHeroNameHasWayWayTooManyLetters'); |
| 180 | +} |
0 commit comments