2017-02-22 13:13:21 -05:00
|
|
|
'use strict'; // necessary for node!
|
|
|
|
|
|
|
|
import { browser, element, by, protractor, ElementFinder, ElementArrayFinder } from 'protractor';
|
|
|
|
|
|
|
|
// THESE TESTS ARE INCOMPLETE
|
2017-04-13 17:35:13 -04:00
|
|
|
describe('Form Validation Tests', function () {
|
2017-02-22 13:13:21 -05:00
|
|
|
|
|
|
|
beforeAll(function () {
|
|
|
|
browser.get('');
|
|
|
|
});
|
|
|
|
|
2017-08-01 19:09:57 -04:00
|
|
|
describe('Template-driven form', () => {
|
2017-02-22 13:13:21 -05:00
|
|
|
beforeAll(() => {
|
2017-08-01 19:09:57 -04:00
|
|
|
getPage('hero-form-template');
|
2017-02-22 13:13:21 -05:00
|
|
|
});
|
|
|
|
|
2017-08-01 19:09:57 -04:00
|
|
|
tests('Template-Driven Form');
|
2017-02-22 13:13:21 -05:00
|
|
|
});
|
|
|
|
|
2017-08-01 19:09:57 -04:00
|
|
|
describe('Reactive form', () => {
|
2017-02-22 13:13:21 -05:00
|
|
|
beforeAll(() => {
|
2017-08-01 19:09:57 -04:00
|
|
|
getPage('hero-form-reactive');
|
2017-02-22 13:13:21 -05:00
|
|
|
});
|
|
|
|
|
2017-08-01 19:09:57 -04:00
|
|
|
tests('Reactive Form');
|
2017-02-22 13:13:21 -05:00
|
|
|
bobTests();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
//////////
|
|
|
|
|
|
|
|
const testName = 'Test Name';
|
|
|
|
|
|
|
|
let page: {
|
|
|
|
section: ElementFinder,
|
|
|
|
form: ElementFinder,
|
|
|
|
title: ElementFinder,
|
|
|
|
nameInput: ElementFinder,
|
|
|
|
alterEgoInput: ElementFinder,
|
|
|
|
powerSelect: ElementFinder,
|
2017-08-01 19:09:57 -04:00
|
|
|
powerOption: ElementFinder,
|
2017-02-22 13:13:21 -05:00
|
|
|
errorMessages: ElementArrayFinder,
|
|
|
|
heroFormButtons: ElementArrayFinder,
|
|
|
|
heroSubmitted: ElementFinder
|
|
|
|
};
|
|
|
|
|
|
|
|
function getPage(sectionTag: string) {
|
|
|
|
let section = element(by.css(sectionTag));
|
|
|
|
let buttons = section.all(by.css('button'));
|
|
|
|
|
|
|
|
page = {
|
|
|
|
section: section,
|
|
|
|
form: section.element(by.css('form')),
|
|
|
|
title: section.element(by.css('h1')),
|
|
|
|
nameInput: section.element(by.css('#name')),
|
|
|
|
alterEgoInput: section.element(by.css('#alterEgo')),
|
|
|
|
powerSelect: section.element(by.css('#power')),
|
2017-08-01 19:09:57 -04:00
|
|
|
powerOption: section.element(by.css('#power option')),
|
2017-02-22 13:13:21 -05:00
|
|
|
errorMessages: section.all(by.css('div.alert')),
|
|
|
|
heroFormButtons: buttons,
|
2017-08-01 19:09:57 -04:00
|
|
|
heroSubmitted: section.element(by.css('.submitted-message'))
|
2017-02-22 13:13:21 -05:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-08-01 19:09:57 -04:00
|
|
|
function tests(title: string) {
|
|
|
|
|
2017-02-22 13:13:21 -05:00
|
|
|
it('should display correct title', function () {
|
2017-08-01 19:09:57 -04:00
|
|
|
expect(page.title.getText()).toContain(title);
|
2017-02-22 13:13:21 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should not display submitted message before submit', function () {
|
2017-08-01 19:09:57 -04:00
|
|
|
expect(page.heroSubmitted.isElementPresent(by.css('p'))).toBe(false);
|
2017-02-22 13:13:21 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should have form buttons', function () {
|
|
|
|
expect(page.heroFormButtons.count()).toEqual(2);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should have error at start', function () {
|
|
|
|
expectFormIsInvalid();
|
|
|
|
});
|
|
|
|
|
|
|
|
// it('showForm', function () {
|
|
|
|
// page.form.getInnerHtml().then(html => console.log(html));
|
|
|
|
// });
|
|
|
|
|
|
|
|
it('should have disabled submit button', function () {
|
|
|
|
expect(page.heroFormButtons.get(0).isEnabled()).toBe(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('resetting name to valid name should clear errors', function () {
|
|
|
|
const ele = page.nameInput;
|
|
|
|
expect(ele.isPresent()).toBe(true, 'nameInput should exist');
|
|
|
|
ele.clear();
|
|
|
|
ele.sendKeys(testName);
|
|
|
|
expectFormIsValid();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should produce "required" error after clearing name', function () {
|
|
|
|
page.nameInput.clear();
|
|
|
|
// page.alterEgoInput.click(); // to blur ... didn't work
|
|
|
|
page.nameInput.sendKeys('x', protractor.Key.BACK_SPACE); // ugh!
|
|
|
|
expect(page.form.getAttribute('class')).toMatch('ng-invalid');
|
|
|
|
expect(page.errorMessages.get(0).getText()).toContain('required');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should produce "at least 4 characters" error when name="x"', function () {
|
|
|
|
page.nameInput.clear();
|
|
|
|
page.nameInput.sendKeys('x'); // too short
|
|
|
|
expectFormIsInvalid();
|
|
|
|
expect(page.errorMessages.get(0).getText()).toContain('at least 4 characters');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('resetting name to valid name again should clear errors', function () {
|
|
|
|
page.nameInput.sendKeys(testName);
|
|
|
|
expectFormIsValid();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should have enabled submit button', function () {
|
|
|
|
const submitBtn = page.heroFormButtons.get(0);
|
|
|
|
expect(submitBtn.isEnabled()).toBe(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should hide form after submit', function () {
|
|
|
|
page.heroFormButtons.get(0).click();
|
2017-08-01 19:09:57 -04:00
|
|
|
expect(page.heroFormButtons.get(0).isDisplayed()).toBe(false);
|
2017-02-22 13:13:21 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('submitted form should be displayed', function () {
|
2017-08-01 19:09:57 -04:00
|
|
|
expect(page.heroSubmitted.isElementPresent(by.css('p'))).toBe(true);
|
2017-02-22 13:13:21 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('submitted form should have new hero name', function () {
|
|
|
|
expect(page.heroSubmitted.getText()).toContain(testName);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('clicking edit button should reveal form again', function () {
|
2017-08-01 19:09:57 -04:00
|
|
|
const newFormBtn = page.heroSubmitted.element(by.css('button'));
|
|
|
|
newFormBtn.click();
|
|
|
|
expect(page.heroSubmitted.isElementPresent(by.css('p')))
|
2017-02-22 13:13:21 -05:00
|
|
|
.toBe(false, 'submitted hidden again');
|
|
|
|
expect(page.title.isDisplayed()).toBe(true, 'can see form title');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function expectFormIsValid() {
|
|
|
|
expect(page.form.getAttribute('class')).toMatch('ng-valid');
|
|
|
|
}
|
|
|
|
|
|
|
|
function expectFormIsInvalid() {
|
|
|
|
expect(page.form.getAttribute('class')).toMatch('ng-invalid');
|
|
|
|
}
|
|
|
|
|
|
|
|
function bobTests() {
|
2017-08-01 19:09:57 -04:00
|
|
|
const emsg = 'Name cannot be Bob.';
|
2017-02-22 13:13:21 -05:00
|
|
|
|
|
|
|
it('should produce "no bob" error after setting name to "Bobby"', function () {
|
2017-08-01 19:09:57 -04:00
|
|
|
// Re-populate select element
|
|
|
|
page.powerSelect.click();
|
|
|
|
page.powerOption.click();
|
|
|
|
|
2017-02-22 13:13:21 -05:00
|
|
|
page.nameInput.clear();
|
|
|
|
page.nameInput.sendKeys('Bobby');
|
|
|
|
expectFormIsInvalid();
|
|
|
|
expect(page.errorMessages.get(0).getText()).toBe(emsg);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should be ok again with valid name', function () {
|
|
|
|
page.nameInput.clear();
|
|
|
|
page.nameInput.sendKeys(testName);
|
|
|
|
expectFormIsValid();
|
|
|
|
});
|
|
|
|
}
|