|
1 | 1 | /// <reference path="../_protractor/e2e.d.ts" />
|
2 | 2 | 'use strict'; // necessary for node!
|
3 |
| -describeIf(browser.appIsTs || browser.appIsJs, 'Forms Tests', function () { |
4 | 3 |
|
5 |
| - beforeEach(function () { |
| 4 | +// THESE TESTS ARE INCOMPLETE |
| 5 | +describeIf(browser.appIsTs || browser.appIsJs, 'Form Validation Tests', function () { |
| 6 | + |
| 7 | + beforeAll(function () { |
6 | 8 | browser.get('');
|
7 | 9 | });
|
8 | 10 |
|
9 |
| - it('should display correct title', function () { |
10 |
| - expect(element.all(by.css('h1')).get(0).getText()).toEqual('Hero Form'); |
| 11 | + describe('Hero Form 1', () => { |
| 12 | + beforeAll(() => { |
| 13 | + getPage('hero-form-template1'); |
| 14 | + }); |
| 15 | + |
| 16 | + tests(); |
11 | 17 | });
|
12 | 18 |
|
| 19 | + describe('Hero Form 2', () => { |
| 20 | + beforeAll(() => { |
| 21 | + getPage('hero-form-template2'); |
| 22 | + }); |
13 | 23 |
|
14 |
| - it('should not display message before submit', function () { |
15 |
| - let ele = element(by.css('h2')); |
16 |
| - expect(ele.isDisplayed()).toBe(false); |
| 24 | + tests(); |
| 25 | + bobTests(); |
17 | 26 | });
|
18 | 27 |
|
19 |
| - it('should hide form after submit', function () { |
20 |
| - let ele = element.all(by.css('h1')).get(0); |
21 |
| - expect(ele.isDisplayed()).toBe(true); |
22 |
| - let b = element.all(by.css('button[type=submit]')).get(0); |
23 |
| - b.click().then(function() { |
24 |
| - expect(ele.isDisplayed()).toBe(false); |
| 28 | + describe('Hero Form 3 (Reactive)', () => { |
| 29 | + beforeAll(() => { |
| 30 | + getPage('hero-form-reactive3'); |
| 31 | + makeNameTooLong(); |
25 | 32 | });
|
| 33 | + |
| 34 | + tests(); |
| 35 | + bobTests(); |
26 | 36 | });
|
| 37 | +}); |
27 | 38 |
|
28 |
| - it('should display message after submit', function () { |
29 |
| - let b = element.all(by.css('button[type=submit]')).get(0); |
30 |
| - b.click().then(function() { |
31 |
| - expect(element(by.css('h2')).getText()).toContain('You submitted the following'); |
32 |
| - }); |
| 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); |
33 | 128 | });
|
34 | 129 |
|
35 | 130 | it('should hide form after submit', function () {
|
36 |
| - let alterEgoEle = element.all(by.css('input[ngcontrol=alterEgo]')).get(0); |
37 |
| - expect(alterEgoEle.isDisplayed()).toBe(true); |
38 |
| - let submitButtonEle = element.all(by.css('button[type=submit]')).get(0); |
39 |
| - submitButtonEle.click().then(function() { |
40 |
| - expect(alterEgoEle.isDisplayed()).toBe(false); |
41 |
| - }); |
| 131 | + page.heroFormButtons.get(0).click(); |
| 132 | + expect(page.title.isDisplayed()).toBe(false); |
42 | 133 | });
|
43 | 134 |
|
44 |
| - it('should reflect submitted data after submit', function () { |
45 |
| - let test = 'testing 1 2 3'; |
46 |
| - let newValue: string; |
47 |
| - let alterEgoEle = element.all(by.css('input[ngcontrol=alterEgo]')).get(0); |
48 |
| - alterEgoEle.getAttribute('value').then(function(value) { |
49 |
| - // alterEgoEle.sendKeys(test); |
50 |
| - sendKeys(alterEgoEle, test); |
51 |
| - newValue = value + test; |
52 |
| - expect(alterEgoEle.getAttribute('value')).toEqual(newValue); |
53 |
| - }).then(function() { |
54 |
| - let b = element.all(by.css('button[type=submit]')).get(0); |
55 |
| - return b.click(); |
56 |
| - }).then(function() { |
57 |
| - let alterEgoTextEle = element(by.cssContainingText('div', 'Alter Ego')); |
58 |
| - expect(alterEgoTextEle.isPresent()).toBe(true, 'cannot locate "Alter Ego" label'); |
59 |
| - let divEle = element(by.cssContainingText('div', newValue)); |
60 |
| - expect(divEle.isPresent()).toBe(true, 'cannot locate div with this text: ' + newValue); |
61 |
| - }); |
| 135 | + it('submitted form should be displayed', function () { |
| 136 | + expect(page.heroSubmitted.isElementPresent(by.css('h2'))).toBe(true); |
62 | 137 | });
|
63 |
| -}); |
64 | 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