Skip to content

Commit cc922c9

Browse files
committed
test: add class with e2e test utilities
* Adds a class that simplifies some common and hard to remember Protractor expressions. * Refactors the existing tests to use the new utilities. Fixes #2025.
1 parent 3cf25a0 commit cc922c9

File tree

12 files changed

+222
-196
lines changed

12 files changed

+222
-196
lines changed

e2e/components/button/button.e2e.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import {browser, by, element} from 'protractor';
22

3-
describe('button', function () {
4-
describe('disabling behavior', function () {
5-
beforeEach(function() {
6-
browser.get('/button');
7-
});
8-
it('should prevent click handlers from executing when disabled', function () {
3+
describe('button', () => {
4+
describe('disabling behavior', () => {
5+
beforeEach(() => browser.get('/button'));
6+
7+
it('should prevent click handlers from executing when disabled', () => {
98
element(by.id('test-button')).click();
109
expect(element(by.id('click-counter')).getText()).toEqual('1');
1110

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
import {browser, by, element} from 'protractor';
22

3-
describe('checkbox', function () {
4-
describe('check behavior', function () {
5-
beforeEach(function() {
6-
browser.get('/checkbox');
7-
});
8-
it('should be checked when clicked, and be unchecked when clicked again', function () {
3+
describe('checkbox', () => {
4+
describe('check behavior', () => {
5+
beforeEach(() => browser.get('/checkbox'));
6+
7+
it('should be checked when clicked, and be unchecked when clicked again', () => {
98
element(by.id('test-checkbox')).click();
10-
element(by.css('input[id=input-test-checkbox]')).getAttribute('checked').then((value: string) => {
11-
expect(value).toBeTruthy('Expect checkbox "checked" property to be true');
12-
});
9+
element(by.css('input[id=input-test-checkbox]')).getAttribute('checked')
10+
.then((value: string) => {
11+
expect(value).toBeTruthy('Expect checkbox "checked" property to be true');
12+
});
1313

1414
element(by.id('test-checkbox')).click();
15-
element(by.css('input[id=input-test-checkbox]')).getAttribute('checked').then((value: string) => {
16-
expect(value).toBeFalsy('Expect checkbox "checked" property to be false');
17-
});
15+
element(by.css('input[id=input-test-checkbox]')).getAttribute('checked')
16+
.then((value: string) => {
17+
expect(value).toBeFalsy('Expect checkbox "checked" property to be false');
18+
});
1819
});
1920
});
2021
});
Lines changed: 18 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,31 @@
1-
import {browser, by, element, Key, ProtractorBy} from 'protractor';
1+
import {browser, by, element, Key} from 'protractor';
2+
import {E2EUtils} from '../../utils.e2e';
23

34
describe('dialog', () => {
5+
const utils = new E2EUtils();
6+
47
beforeEach(() => browser.get('/dialog'));
58

69
it('should open a dialog', () => {
710
element(by.id('default')).click();
8-
waitForDialog().then((isPresent: boolean) => expect(isPresent).toBe(true));
11+
utils.expectToExist('md-dialog-container');
912
});
1013

1114
it('should close by clicking on the backdrop', () => {
1215
element(by.id('default')).click();
1316

1417
waitForDialog().then(() => {
1518
clickOnBackrop();
16-
waitForDialog().then((isPresent: boolean) => expect(isPresent).toBe(false));
19+
utils.expectToExist('md-dialog-container', false);
1720
});
1821
});
1922

2023
it('should close by pressing escape', () => {
2124
element(by.id('default')).click();
2225

2326
waitForDialog().then(() => {
24-
pressEscape();
25-
waitForDialog().then((isPresent: boolean) => expect(isPresent).toBe(false));
27+
utils.pressKeys(Key.ESCAPE);
28+
utils.expectToExist('md-dialog-container', false);
2629
});
2730
});
2831

@@ -31,15 +34,15 @@ describe('dialog', () => {
3134

3235
waitForDialog().then(() => {
3336
element(by.id('close')).click();
34-
waitForDialog().then((isPresent: boolean) => expect(isPresent).toBe(false));
37+
utils.expectToExist('md-dialog-container', false);
3538
});
3639
});
3740

3841
it('should focus the first focusable element', () => {
3942
element(by.id('default')).click();
4043

4144
waitForDialog().then(() => {
42-
expectFocusOn(element(by.css('md-dialog-container input')));
45+
utils.expectFocusOn('md-dialog-container input');
4346
});
4447
});
4548

@@ -50,7 +53,7 @@ describe('dialog', () => {
5053

5154
waitForDialog().then(() => {
5255
clickOnBackrop();
53-
expectFocusOn(openButton);
56+
utils.expectFocusOn(openButton);
5457
});
5558
});
5659

@@ -60,8 +63,8 @@ describe('dialog', () => {
6063
waitForDialog().then(() => {
6164
let tab = Key.TAB;
6265

63-
browser.actions().sendKeys(tab, tab, tab).perform();
64-
expectFocusOn(element(by.id('close')));
66+
utils.pressKeys(tab, tab, tab);
67+
utils.expectFocusOn('#close');
6568
});
6669
});
6770

@@ -70,38 +73,24 @@ describe('dialog', () => {
7073

7174
waitForDialog().then(() => {
7275
clickOnBackrop();
73-
waitForDialog().then((isPresent: boolean) => expect(isPresent).toBe(true));
76+
utils.expectToExist('md-dialog-container');
7477
});
7578
});
7679

7780
it('should be able to prevent closing by pressing escape', () => {
7881
element(by.id('disabled')).click();
7982

8083
waitForDialog().then(() => {
81-
pressEscape();
82-
waitForDialog().then((isPresent: boolean) => expect(isPresent).toBe(true));
84+
utils.pressKeys(Key.ESCAPE);
85+
utils.expectToExist('md-dialog-container');
8386
});
8487
});
8588

8689
function waitForDialog() {
87-
return browser.isElementPresent(by.css('md-dialog-container') as ProtractorBy);
90+
return utils.waitForElement('md-dialog-container');
8891
}
8992

9093
function clickOnBackrop() {
91-
browser.actions()
92-
// We need to move the cursor to the top left so
93-
// the dialog doesn't receive the click accidentally.
94-
.mouseMove(element(by.css('.cdk-overlay-backdrop')).getWebElement(), { x: 0, y: 0 })
95-
.click()
96-
.perform();
97-
}
98-
99-
function pressEscape() {
100-
browser.actions().sendKeys(Key.ESCAPE).perform();
101-
}
102-
103-
// TODO(crisbeto): should be moved to a common util. copied from the menu e2e setup.
104-
function expectFocusOn(el: any): void {
105-
expect(browser.driver.switchTo().activeElement().getInnerHtml()).toBe(el.getInnerHtml());
94+
utils.clickElementAtPoint('.md-overlay-backdrop', { x: 0, y: 0 });
10695
}
10796
});
Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1-
import {browser, by, element} from 'protractor';
1+
import {browser} from 'protractor';
2+
import {E2EUtils} from '../../utils.e2e';
23

34
describe('grid-list', () => {
5+
const utils = new E2EUtils();
6+
47
beforeEach(() => browser.get('/grid-list'));
58

69
it('should render a grid list container', () => {
7-
expect(element(by.css('md-grid-list')).isPresent()).toBe(true);
10+
utils.expectToExist('md-grid-list');
811
});
912

1013
it('should render list items inside the grid list container', () => {
11-
let container = element(by.css('md-grid-list'));
12-
expect(container.isElementPresent(by.css('md-grid-tile'))).toBe(true);
14+
utils.expectToExist('md-grid-list md-grid-tile');
1315
});
1416
});

e2e/components/list/list.e2e.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1-
import {browser, by, element} from 'protractor';
1+
import {browser} from 'protractor';
2+
import {E2EUtils} from '../../utils.e2e';
23

34
describe('list', () => {
5+
const utils = new E2EUtils();
6+
47
beforeEach(() => browser.get('/list'));
58

69
it('should render a list container', () => {
7-
expect(element(by.css('md-list')).isPresent()).toBe(true);
10+
utils.expectToExist('md-list');
811
});
912

1013
it('should render list items inside the list container', () => {
11-
let container = element(by.css('md-list'));
12-
expect(container.isElementPresent(by.css('md-list-item'))).toBe(true);
14+
utils.expectToExist('md-list md-list-item');
1315
});
1416
});

e2e/components/menu/menu-page.ts

Lines changed: 16 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,33 @@
1-
import {browser, by, element, ElementFinder, ProtractorBy} from 'protractor';
1+
import {browser, by, element, ElementFinder} from 'protractor';
22

33
export class MenuPage {
4+
constructor() { browser.get('/menu'); }
45

5-
constructor() {
6-
browser.get('/menu');
7-
}
6+
menu(): ElementFinder { return element(by.css('.md-menu-panel')); }
87

9-
menu() { return element(by.css('.md-menu-panel')); }
8+
start(): ElementFinder { return element(by.id('start')); }
109

11-
start() { return element(by.id('start')); }
10+
trigger(): ElementFinder { return element(by.id('trigger')); }
1211

13-
trigger() { return element(by.id('trigger')); }
12+
triggerTwo(): ElementFinder { return element(by.id('trigger-two')); }
1413

15-
triggerTwo() { return element(by.id('trigger-two')); }
14+
backdrop(): ElementFinder { return element(by.css('.cdk-overlay-backdrop')); }
1615

17-
backdrop() { return element(by.css('.cdk-overlay-backdrop')); }
16+
items(index: number): ElementFinder { return element.all(by.css('[md-menu-item]')).get(index); }
1817

19-
items(index: number) { return element.all(by.css('[md-menu-item]')).get(index); }
18+
textArea(): ElementFinder { return element(by.id('text')); }
2019

21-
textArea() { return element(by.id('text')); }
20+
beforeTrigger(): ElementFinder { return element(by.id('before-t')); }
2221

23-
beforeTrigger() { return element(by.id('before-t')); }
22+
aboveTrigger(): ElementFinder { return element(by.id('above-t')); }
2423

25-
aboveTrigger() { return element(by.id('above-t')); }
24+
combinedTrigger(): ElementFinder { return element(by.id('combined-t')); }
2625

27-
combinedTrigger() { return element(by.id('combined-t')); }
26+
beforeMenu(): ElementFinder { return element(by.css('.md-menu-panel.before')); }
2827

29-
beforeMenu() { return element(by.css('.md-menu-panel.before')); }
28+
aboveMenu(): ElementFinder { return element(by.css('.md-menu-panel.above')); }
3029

31-
aboveMenu() { return element(by.css('.md-menu-panel.above')); }
30+
combinedMenu(): ElementFinder { return element(by.css('.md-menu-panel.combined')); }
3231

33-
combinedMenu() { return element(by.css('.md-menu-panel.combined')); }
34-
35-
// TODO(kara): move to common testing utility
36-
pressKey(key: string): void {
37-
browser.actions().sendKeys(key).perform();
38-
}
39-
40-
// TODO(kara): move to common testing utility
41-
expectFocusOn(el: any): void {
42-
expect(browser.driver.switchTo().activeElement().getInnerHtml())
43-
.toBe(el.getInnerHtml());
44-
}
45-
46-
expectMenuPresent(expected: boolean) {
47-
return browser.isElementPresent(by.css('.md-menu-panel') as ProtractorBy)
48-
.then((isPresent: boolean) => {
49-
expect(isPresent).toBe(expected);
50-
});
51-
}
52-
53-
expectMenuLocation(el: ElementFinder, {x, y}: {x: number, y: number}) {
54-
el.getLocation().then(loc => {
55-
expect(loc.x).toEqual(x, 'Expect the x-position to be equal');
56-
expect(loc.y).toEqual(y, 'Expect the y-position to be equal');
57-
});
58-
}
59-
60-
expectMenuAlignedWith(el: ElementFinder, id: string) {
61-
element(by.id(id)).getLocation().then(loc => {
62-
this.expectMenuLocation(el, {x: loc.x, y: loc.y});
63-
});
64-
}
65-
66-
getResultText() {
67-
return this.textArea().getText();
68-
}
32+
getResultText() { return this.textArea().getText(); }
6933
}

0 commit comments

Comments
 (0)