Skip to content

Commit 34743ce

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 a1acf99 commit 34743ce

File tree

12 files changed

+210
-181
lines changed

12 files changed

+210
-181
lines changed

e2e/components/button/button.e2e.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
describe('button', function () {
2-
describe('disabling behavior', function () {
3-
beforeEach(function() {
4-
browser.get('/button');
5-
});
6-
it('should prevent click handlers from executing when disabled', function () {
1+
describe('button', () => {
2+
describe('disabling behavior', () => {
3+
beforeEach(() => browser.get('/button'));
4+
5+
it('should prevent click handlers from executing when disabled', () => {
76
element(by.id('test-button')).click();
87
expect(element(by.id('click-counter')).getText()).toEqual('1');
98

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
1-
describe('checkbox', function () {
2-
describe('check behavior', function () {
3-
beforeEach(function() {
4-
browser.get('/checkbox');
5-
});
6-
it('should be checked when clicked, and be unchecked when clicked again', function () {
1+
describe('checkbox', () => {
2+
describe('check behavior', () => {
3+
beforeEach(() => browser.get('/checkbox'));
4+
5+
it('should be checked when clicked, and be unchecked when clicked again', () => {
76
element(by.id('test-checkbox')).click();
8-
element(by.css('input[id=input-test-checkbox]')).getAttribute('checked').then((value: string) => {
9-
expect(value).toBeTruthy('Expect checkbox "checked" property to be true');
10-
});
7+
element(by.css('input[id=input-test-checkbox]')).getAttribute('checked')
8+
.then((value: string) => {
9+
expect(value).toBeTruthy('Expect checkbox "checked" property to be true');
10+
});
1111

1212
element(by.id('test-checkbox')).click();
13-
element(by.css('input[id=input-test-checkbox]')).getAttribute('checked').then((value: string) => {
14-
expect(value).toBeFalsy('Expect checkbox "checked" property to be false');
15-
});
13+
element(by.css('input[id=input-test-checkbox]')).getAttribute('checked')
14+
.then((value: string) => {
15+
expect(value).toBeFalsy('Expect checkbox "checked" property to be false');
16+
});
1617
});
1718
});
1819
});

e2e/components/dialog/dialog.e2e.ts

Lines changed: 18 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,30 @@
1+
import {E2EUtils} from '../../utils.e2e';
2+
13
describe('dialog', () => {
4+
const utils = new E2EUtils();
5+
26
beforeEach(() => browser.get('/dialog'));
37

48
it('should open a dialog', () => {
59
element(by.id('default')).click();
6-
waitForDialog().then(isPresent => expect(isPresent).toBe(true));
10+
utils.expectToExist('md-dialog-container');
711
});
812

913
it('should close by clicking on the backdrop', () => {
1014
element(by.id('default')).click();
1115

1216
waitForDialog().then(() => {
1317
clickOnBackrop();
14-
waitForDialog().then(isPresent => expect(isPresent).toBe(false));
18+
utils.expectToExist('md-dialog-container', false);
1519
});
1620
});
1721

1822
it('should close by pressing escape', () => {
1923
element(by.id('default')).click();
2024

2125
waitForDialog().then(() => {
22-
pressEscape();
23-
waitForDialog().then(isPresent => expect(isPresent).toBe(false));
26+
utils.pressKeys(protractor.Key.ESCAPE);
27+
utils.expectToExist('md-dialog-container', false);
2428
});
2529
});
2630

@@ -29,15 +33,15 @@ describe('dialog', () => {
2933

3034
waitForDialog().then(() => {
3135
element(by.id('close')).click();
32-
waitForDialog().then(isPresent => expect(isPresent).toBe(false));
36+
utils.expectToExist('md-dialog-container', false);
3337
});
3438
});
3539

3640
it('should focus the first focusable element', () => {
3741
element(by.id('default')).click();
3842

3943
waitForDialog().then(() => {
40-
expectFocusOn(element(by.css('md-dialog-container input')));
44+
utils.expectFocusOn('md-dialog-container input');
4145
});
4246
});
4347

@@ -48,7 +52,7 @@ describe('dialog', () => {
4852

4953
waitForDialog().then(() => {
5054
clickOnBackrop();
51-
expectFocusOn(openButton);
55+
utils.expectFocusOn(openButton);
5256
});
5357
});
5458

@@ -58,8 +62,8 @@ describe('dialog', () => {
5862
waitForDialog().then(() => {
5963
let tab = protractor.Key.TAB;
6064

61-
browser.actions().sendKeys(tab, tab, tab).perform();
62-
expectFocusOn(element(by.id('close')));
65+
utils.pressKeys(tab, tab, tab);
66+
utils.expectFocusOn('#close');
6367
});
6468
});
6569

@@ -68,38 +72,24 @@ describe('dialog', () => {
6872

6973
waitForDialog().then(() => {
7074
clickOnBackrop();
71-
waitForDialog().then(isPresent => expect(isPresent).toBe(true));
75+
utils.expectToExist('md-dialog-container');
7276
});
7377
});
7478

7579
it('should be able to prevent closing by pressing escape', () => {
7680
element(by.id('disabled')).click();
7781

7882
waitForDialog().then(() => {
79-
pressEscape();
80-
waitForDialog().then(isPresent => expect(isPresent).toBe(true));
83+
utils.pressKeys(protractor.Key.ESCAPE);
84+
utils.expectToExist('md-dialog-container');
8185
});
8286
});
8387

8488
function waitForDialog() {
85-
return browser.isElementPresent(by.css('md-dialog-container'));
89+
return utils.waitForElement('md-dialog-container');
8690
}
8791

8892
function clickOnBackrop() {
89-
browser.actions()
90-
// We need to move the cursor to the top left so
91-
// the dialog doesn't receive the click accidentally.
92-
.mouseMove(element(by.css('.md-overlay-backdrop')).getWebElement(), { x: 0, y: 0 })
93-
.click()
94-
.perform();
95-
}
96-
97-
function pressEscape() {
98-
browser.actions().sendKeys(protractor.Key.ESCAPE).perform();
99-
}
100-
101-
// TODO(crisbeto): should be moved to a common util. copied from the menu e2e setup.
102-
function expectFocusOn(el: any): void {
103-
expect(browser.driver.switchTo().activeElement().getInnerHtml()).toBe(el.getInnerHtml());
93+
utils.clickElementAtPoint('.md-overlay-backdrop', { x: 0, y: 0 });
10494
}
10595
});
Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1+
import {E2EUtils} from '../../utils.e2e';
2+
13
describe('grid-list', () => {
4+
const utils = new E2EUtils();
5+
26
beforeEach(() => browser.get('/grid-list'));
37

48
it('should render a grid list container', () => {
5-
expect(element(by.css('md-grid-list')).isPresent()).toBe(true);
9+
utils.expectToExist('md-grid-list');
610
});
711

812
it('should render list items inside the grid list container', () => {
9-
let container = element(by.css('md-grid-list'));
10-
expect(container.isElementPresent(by.css('md-grid-tile'))).toBe(true);
13+
utils.expectToExist('md-grid-list md-grid-tile');
1114
});
1215
});

e2e/components/list/list.e2e.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1+
import {E2EUtils} from '../../utils.e2e';
2+
13
describe('list', () => {
4+
const utils = new E2EUtils();
5+
26
beforeEach(() => browser.get('/list'));
37

48
it('should render a list container', () => {
5-
expect(element(by.css('md-list')).isPresent()).toBe(true);
9+
utils.expectToExist('md-list');
610
});
711

812
it('should render list items inside the list container', () => {
9-
let container = element(by.css('md-list'));
10-
expect(container.isElementPresent(by.css('md-list-item'))).toBe(true);
13+
utils.expectToExist('md-list md-list-item');
1114
});
1215
});

e2e/components/menu/menu-page.ts

Lines changed: 3 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
1-
import ElementFinder = protractor.ElementFinder;
2-
31
export class MenuPage {
4-
5-
constructor() {
6-
browser.get('/menu');
7-
}
2+
constructor() { browser.get('/menu'); }
83

94
menu() { return element(by.css('.md-menu-panel')); }
105

@@ -16,9 +11,7 @@ export class MenuPage {
1611

1712
backdrop() { return element(by.css('.md-overlay-backdrop')); }
1813

19-
items(index: number) {
20-
return element.all(by.css('[md-menu-item]')).get(index);
21-
}
14+
items(index: number) { return element.all(by.css('[md-menu-item]')).get(index); }
2215

2316
textArea() { return element(by.id('text')); }
2417

@@ -34,37 +27,5 @@ export class MenuPage {
3427

3528
combinedMenu() { return element(by.css('.md-menu-panel.combined')); }
3629

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

0 commit comments

Comments
 (0)