|
1 | 1 | import { AppPage } from './app.po';
|
2 |
| -import { browser, logging } from 'protractor'; |
| 2 | +import { browser, by, element, ElementArrayFinder, ElementFinder } from 'protractor'; |
3 | 3 |
|
4 |
| -describe('workspace-project App', () => { |
5 |
| - let page: AppPage; |
| 4 | +describe('Cross-field validation App', () => { |
| 5 | + |
| 6 | + let appPage: AppPage; |
6 | 7 |
|
7 | 8 | beforeEach(() => {
|
8 |
| - page = new AppPage(); |
| 9 | + appPage = new AppPage(); |
9 | 10 | });
|
10 | 11 |
|
11 |
| - it('should display welcome message', () => { |
12 |
| - page.navigateTo(); |
13 |
| - expect(page.getTitleText()).toEqual('angular-form-input-cross-validator app is running!'); |
| 12 | + it('Should render page title', () => { |
| 13 | + appPage.navigateTo(); |
| 14 | + expect(appPage.getTitleText()).toEqual('Angular - Reactive form input value cross-validation'); |
14 | 15 | });
|
| 16 | +}); |
| 17 | + |
| 18 | +describe('Reactive and template-driven form validation tests: ', () => { |
| 19 | + |
| 20 | + beforeAll(() => browser.get('')); |
| 21 | + |
| 22 | + describe('Reactive form', () => { |
| 23 | + beforeAll(() => { |
| 24 | + getElements('app-reactive-form'); |
| 25 | + }); |
15 | 26 |
|
16 |
| - afterEach(async () => { |
17 |
| - // Assert that there are no errors emitted from the browser |
18 |
| - const logs = await browser.manage().logs().get(logging.Type.BROWSER); |
19 |
| - expect(logs).not.toContain(jasmine.objectContaining({ |
20 |
| - level: logging.Level.SEVERE, |
21 |
| - } as logging.Entry)); |
| 27 | + initialStateTest(); |
| 28 | + emailTests(); |
| 29 | + compareEmails(); |
| 30 | + passwordTests(); |
| 31 | + comparePasswords(); |
| 32 | + }); |
| 33 | + |
| 34 | + describe('Template-driven form', () => { |
| 35 | + beforeAll(() => { |
| 36 | + navigateToPage('templateForm'); |
| 37 | + getElements('app-template-driven-form'); |
| 38 | + }); |
| 39 | + |
| 40 | + initialStateTest(); |
| 41 | + emailTests(); |
| 42 | + compareEmails(); |
| 43 | + passwordTests(); |
| 44 | + comparePasswords(); |
22 | 45 | });
|
23 | 46 | });
|
| 47 | + |
| 48 | +let page: { |
| 49 | + form: ElementFinder, |
| 50 | + nameInput: ElementFinder, |
| 51 | + emailInput: ElementFinder, |
| 52 | + confirmEmailInput: ElementFinder, |
| 53 | + passwordInput: ElementFinder, |
| 54 | + confirmPasswordInput: ElementFinder, |
| 55 | + errorMessages: ElementArrayFinder, |
| 56 | +}; |
| 57 | + |
| 58 | +function getElements(selector: string) { |
| 59 | + const template = element(by.css(selector)); |
| 60 | + page = { |
| 61 | + form: template.element(by.css('form')), |
| 62 | + nameInput: template.element(by.css('#name')), |
| 63 | + emailInput: template.element(by.css('#email')), |
| 64 | + confirmEmailInput: template.element(by.css('#confirmEmail')), |
| 65 | + passwordInput: template.element(by.css('#password')), |
| 66 | + confirmPasswordInput: template.element(by.css('#confirmPassword')), |
| 67 | + errorMessages: template.all(by.css('div.alert')), |
| 68 | + }; |
| 69 | +} |
| 70 | + |
| 71 | +function navigateToPage(linkId: string) { |
| 72 | + element(by.id(linkId)).click().then(() => { |
| 73 | + browser.sleep(2000).then(() => { |
| 74 | + browser.getCurrentUrl(); |
| 75 | + // .then((actualUrl) => { console.log("actualUrl: ", actualUrl); }); |
| 76 | + }); |
| 77 | + }); |
| 78 | +} |
| 79 | + |
| 80 | +function initialStateTest() { |
| 81 | + it('Should have error on load', async () => { |
| 82 | + expect(await page.form.getAttribute('class')).toMatch('ng-invalid'); |
| 83 | + }); |
| 84 | +} |
| 85 | + |
| 86 | +function emailTests() { |
| 87 | + |
| 88 | + it('Should render "email required" error when Email has no value', async () => { |
| 89 | + await browser.actions().click(page.emailInput).perform(); |
| 90 | + await browser.actions().click(page.confirmEmailInput).perform(); |
| 91 | + expect(await page.form.getAttribute('class')).toMatch('ng-invalid'); |
| 92 | + expect(await page.errorMessages.get(0).getText()).toContain('Email is required'); |
| 93 | + }); |
| 94 | + |
| 95 | + it("Should render 'Invalid email' error when Email value is 'abc'", async () => { |
| 96 | + await page.emailInput.sendKeys('abc'); |
| 97 | + await browser.actions().click(page.confirmEmailInput).perform(); |
| 98 | + expect(await page.form.getAttribute('class')).toMatch('ng-invalid'); |
| 99 | + expect(await page.errorMessages.get(0).getText()).toContain('Please enter a valid email.'); |
| 100 | + }); |
| 101 | + |
| 102 | + it("Should clear 'Invalid email' error when Email value is '[email protected]'", async () => { |
| 103 | + await page.emailInput.clear(); |
| 104 | + await page.emailInput.sendKeys('[email protected]'); |
| 105 | + await browser.actions().click(page.confirmEmailInput).perform(); |
| 106 | + expect(await page.errorMessages.get(0).getText()).not.toContain('Please enter a valid email.'); |
| 107 | + }); |
| 108 | +}; |
| 109 | + |
| 110 | +function compareEmails() { |
| 111 | + |
| 112 | + it("Should render 'Email entries do not match' error if 'confirmEmail' value is '[email protected]'", async () => { |
| 113 | + await page.confirmEmailInput.sendKeys('[email protected]'); |
| 114 | + await browser.actions().click(page.passwordInput).perform(); |
| 115 | + expect(await page.form.getAttribute('class')).toMatch('ng-invalid'); |
| 116 | + expect(await page.errorMessages.get(0).getText()).toBe('Email entries do not match.'); |
| 117 | + }); |
| 118 | + |
| 119 | + it("Should clear 'Email do not match' error if 'email' and 'confirmEmail' values match", async () => { |
| 120 | + await page.confirmEmailInput.clear(); |
| 121 | + await page.confirmEmailInput.sendKeys('[email protected]'); |
| 122 | + await browser.actions().click(page.passwordInput).perform(); |
| 123 | + expect(await page.errorMessages.get(0).getText()).not.toContain('Email entries do not match.'); |
| 124 | + }); |
| 125 | +}; |
| 126 | + |
| 127 | +function passwordTests() { |
| 128 | + |
| 129 | + it('Should render "password required" error when password has no value', async () => { |
| 130 | + await browser.actions().click(page.passwordInput).perform(); |
| 131 | + await browser.actions().click(page.confirmPasswordInput).perform(); |
| 132 | + expect(await page.form.getAttribute('class')).toMatch('ng-invalid'); |
| 133 | + expect(await page.errorMessages.get(0).getText()).toContain('Password is required'); |
| 134 | + }); |
| 135 | +}; |
| 136 | + |
| 137 | +function comparePasswords() { |
| 138 | + |
| 139 | + it("should render 'password do not match' error if 'password' and 'confirmPassword' values do not match", async () => { |
| 140 | + await page.passwordInput.sendKeys('abcdef'); |
| 141 | + await page.confirmPasswordInput.sendKeys('uvxyz'); |
| 142 | + await browser.actions().mouseDown(page.passwordInput).perform(); |
| 143 | + expect(await page.form.getAttribute('class')).toMatch('ng-invalid'); |
| 144 | + expect(await page.errorMessages.get(0).getText()).toBe('Password entries do not match'); |
| 145 | + }); |
| 146 | + |
| 147 | + it("Should clear 'Password do not match' error if 'password' and 'confirmPassword' values match", async () => { |
| 148 | + await page.confirmPasswordInput.clear(); |
| 149 | + await page.confirmPasswordInput.sendKeys('abcdef'); |
| 150 | + await element(by.css('app-root')).click(); |
| 151 | + expect(await page.errorMessages.get(0).isPresent()).toBe(false); |
| 152 | + }); |
| 153 | +}; |
0 commit comments