Skip to content

Commit 061a4e5

Browse files
committed
refactor: switch e2e tests to async/await
Switches to using async/await in the e2e tests. This should make it a bit easier to deal with Protractor's async APIs.
1 parent c946631 commit 061a4e5

File tree

15 files changed

+275
-262
lines changed

15 files changed

+275
-262
lines changed

e2e/components/block-scroll-strategy/block-scroll-strategy.e2e.ts

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import {browser, Key, element, by} from 'protractor';
22
import {screenshot} from '../../screenshot';
3-
import {getScrollPosition} from '../../util/query';
3+
import {getScrollPosition, asyncSpec} from '../../util/index';
44

55

66
describe('scroll blocking', () => {
77
beforeEach(() => browser.get('/block-scroll-strategy'));
88
afterEach(() => clickOn('disable'));
99

10-
it('should not be able to scroll programmatically along the x axis', async (done) => {
10+
it('should not be able to scroll programmatically along the x axis', asyncSpec(async () => {
1111
scrollPage(0, 100);
1212
expect((await getScrollPosition()).y).toBe(100, 'Expected the page to be scrollable.');
1313

@@ -20,10 +20,9 @@ describe('scroll blocking', () => {
2020
expect((await getScrollPosition()).y).toBe(300, 'Exected page to be scrollable again.');
2121

2222
screenshot();
23-
done();
24-
});
23+
}));
2524

26-
it('should not be able to scroll programmatically along the y axis', async (done) => {
25+
it('should not be able to scroll programmatically along the y axis', asyncSpec(async () => {
2726
scrollPage(100, 0);
2827
expect((await getScrollPosition()).x).toBe(100, 'Expected the page to be scrollable.');
2928

@@ -36,10 +35,9 @@ describe('scroll blocking', () => {
3635
expect((await getScrollPosition()).x).toBe(300, 'Exected page to be scrollable again.');
3736

3837
screenshot();
39-
done();
40-
});
38+
}));
4139

42-
it('should not be able to scroll via the keyboard along the y axis', async (done) => {
40+
it('should not be able to scroll via the keyboard along the y axis', asyncSpec(async () => {
4341
const body = element(by.tagName('body'));
4442

4543
scrollPage(0, 100);
@@ -59,10 +57,9 @@ describe('scroll blocking', () => {
5957
.toBeGreaterThan(100, 'Expected the page to be scrollable again.');
6058

6159
screenshot();
62-
done();
63-
});
60+
}));
6461

65-
it('should not be able to scroll via the keyboard along the x axis', async (done) => {
62+
it('should not be able to scroll via the keyboard along the x axis', asyncSpec(async () => {
6663
const body = element(by.tagName('body'));
6764

6865
scrollPage(100, 0);
@@ -82,11 +79,10 @@ describe('scroll blocking', () => {
8279
.toBeGreaterThan(100, 'Expected the page to be scrollable again.');
8380

8481
screenshot();
85-
done();
86-
});
82+
}));
8783

8884
it('should not be able to scroll the page after reaching the end of an element along the y axis',
89-
async (done) => {
85+
asyncSpec(async () => {
9086
const scroller = element(by.id('scroller'));
9187

9288
browser.executeScript(`document.getElementById('scroller').scrollTop = 200;`);
@@ -100,11 +96,10 @@ describe('scroll blocking', () => {
10096
expect((await getScrollPosition()).y).toBe(100, 'Expected the page not to have scrolled.');
10197

10298
screenshot();
103-
done();
104-
});
99+
}));
105100

106101
it('should not be able to scroll the page after reaching the end of an element along the x axis',
107-
async (done) => {
102+
asyncSpec(async () => {
108103
const scroller = element(by.id('scroller'));
109104

110105
browser.executeScript(`document.getElementById('scroller').scrollLeft = 200;`);
@@ -118,8 +113,7 @@ describe('scroll blocking', () => {
118113
expect((await getScrollPosition()).x).toBe(100, 'Expected the page not to have scrolled.');
119114

120115
screenshot();
121-
done();
122-
});
116+
}));
123117
});
124118

125119
// Clicks on a button programmatically. Note that we can't use Protractor's `.click`, because

e2e/components/button/button.e2e.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,27 @@
11
import {browser, by, element, ExpectedConditions} from 'protractor';
22
import {screenshot} from '../../screenshot';
3+
import {asyncSpec} from '../../util/index';
34

45

56
describe('button', () => {
67
describe('disabling behavior', () => {
78
beforeEach(() => browser.get('/button'));
89

9-
it('should prevent click handlers from executing when disabled', () => {
10+
it('should prevent click handlers from executing when disabled', asyncSpec(async () => {
1011
element(by.id('test-button')).click();
1112
expect(element(by.id('click-counter')).getText()).toEqual('1');
12-
browser.wait(ExpectedConditions.not(
13-
ExpectedConditions.presenceOf(element(by.css('div.mat-ripple-element')))))
14-
.then(() => screenshot('clicked once'));
13+
14+
await browser.wait(ExpectedConditions.not(
15+
ExpectedConditions.presenceOf(element(by.css('div.mat-ripple-element')))));
16+
screenshot('clicked once');
1517

1618
element(by.id('disable-toggle')).click();
1719
element(by.id('test-button')).click();
1820
expect(element(by.id('click-counter')).getText()).toEqual('1');
19-
browser.wait(ExpectedConditions.not(
20-
ExpectedConditions.presenceOf(element(by.css('div.mat-ripple-element')))))
21-
.then(() => screenshot('click disabled'));
22-
});
21+
22+
await browser.wait(ExpectedConditions.not(
23+
ExpectedConditions.presenceOf(element(by.css('div.mat-ripple-element')))));
24+
screenshot('click disabled');
25+
}));
2326
});
2427
});
Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,47 @@
11
import {browser, by, element, Key, ExpectedConditions} from 'protractor';
22
import {screenshot} from '../../screenshot';
3+
import {asyncSpec} from '../../util/index';
34

4-
describe('checkbox', function () {
55

6-
describe('check behavior', function () {
6+
describe('checkbox', () => {
77

8-
beforeEach(function() {
9-
browser.get('/checkbox');
10-
});
8+
describe('check behavior', () => {
9+
beforeEach(() => browser.get('/checkbox'));
1110

12-
it('should be checked when clicked, and be unchecked when clicked again', () => {
11+
it('should be checked when clicked, and unchecked when clicked again', asyncSpec(async () => {
1312
let checkboxEl = element(by.id('test-checkbox'));
1413
let inputEl = element(by.css('input[id=input-test-checkbox]'));
14+
let checked: string;
1515

1616
screenshot('start');
1717
checkboxEl.click();
18-
inputEl.getAttribute('checked').then((value: string) => {
19-
expect(value).toBeTruthy('Expect checkbox "checked" property to be true');
20-
browser.wait(ExpectedConditions.not(
21-
ExpectedConditions.presenceOf(element(by.css('div.mat-ripple-element')))))
22-
.then(() => screenshot('checked'));
23-
});
18+
19+
expect(inputEl.getAttribute('checked'))
20+
.toBeTruthy('Expect checkbox "checked" property to be true');
21+
22+
await browser.wait(ExpectedConditions.not(
23+
ExpectedConditions.presenceOf(element(by.css('div.mat-ripple-element')))));
24+
screenshot('checked');
2425

2526
checkboxEl.click();
26-
inputEl.getAttribute('checked').then((value: string) => {
27-
expect(value).toBeFalsy('Expect checkbox "checked" property to be false');
28-
browser.wait(ExpectedConditions.not(
29-
ExpectedConditions.presenceOf(element(by.css('div.mat-ripple-element')))))
30-
.then(() => screenshot('unchecked'));
31-
});
32-
});
27+
28+
expect(inputEl.getAttribute('checked'))
29+
.toBeFalsy('Expect checkbox "checked" property to be false');
30+
31+
await browser.wait(ExpectedConditions.not(
32+
ExpectedConditions.presenceOf(element(by.css('div.mat-ripple-element')))));
33+
screenshot('unchecked');
34+
}));
3335

3436
it('should toggle the checkbox when pressing space', () => {
3537
let inputEl = element(by.css('input[id=input-test-checkbox]'));
3638

37-
inputEl.getAttribute('checked').then((value: string) => {
38-
expect(value).toBeFalsy('Expect checkbox "checked" property to be false');
39-
});
40-
39+
expect(inputEl.getAttribute('checked'))
40+
.toBeFalsy('Expect checkbox "checked" property to be false');
4141
inputEl.sendKeys(Key.SPACE);
4242

43-
inputEl.getAttribute('checked').then((value: string) => {
44-
expect(value).toBeTruthy('Expect checkbox "checked" property to be true');
45-
});
43+
expect(inputEl.getAttribute('checked'))
44+
.toBeTruthy('Expect checkbox "checked" property to be true');
4645
});
4746
});
4847
});

e2e/components/dialog/dialog.e2e.ts

Lines changed: 53 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
import {browser, by, element, Key} from 'protractor';
2-
import {expectToExist, expectFocusOn} from '../../util/asserts';
3-
import {pressKeys, clickElementAtPoint} from '../../util/actions';
4-
import {waitForElement} from '../../util/query';
2+
import {
3+
expectToExist,
4+
expectFocusOn,
5+
pressKeys,
6+
clickElementAtPoint,
7+
waitForElement,
8+
asyncSpec,
9+
} from '../../util/index';
10+
511

612
describe('dialog', () => {
713
beforeEach(() => browser.get('/dialog'));
@@ -17,90 +23,80 @@ describe('dialog', () => {
1723
expectToExist('.my-template-dialog');
1824
});
1925

20-
it('should close by clicking on the backdrop', () => {
26+
it('should close by clicking on the backdrop', asyncSpec(async() => {
2127
element(by.id('default')).click();
2228

23-
waitForDialog().then(() => {
24-
clickOnBackrop();
25-
expectToExist('md-dialog-container', false);
26-
});
27-
});
29+
await waitForDialog();
30+
clickOnBackrop();
31+
expectToExist('md-dialog-container', false);
32+
}));
2833

29-
it('should close by pressing escape', () => {
34+
it('should close by pressing escape', asyncSpec(async () => {
3035
element(by.id('default')).click();
3136

32-
waitForDialog().then(() => {
33-
pressKeys(Key.ESCAPE);
34-
expectToExist('md-dialog-container', false);
35-
});
36-
});
37+
await waitForDialog();
38+
pressKeys(Key.ESCAPE);
39+
expectToExist('md-dialog-container', false);
40+
}));
3741

38-
it('should close by pressing escape when the first tabbable element has lost focus', () => {
39-
element(by.id('default')).click();
42+
it('should close by pressing escape when the first tabbable element has lost focus',
43+
asyncSpec(async () => {
44+
element(by.id('default')).click();
4045

41-
waitForDialog().then(() => {
46+
await waitForDialog();
4247
clickElementAtPoint('md-dialog-container', { x: 0, y: 0 });
4348
pressKeys(Key.ESCAPE);
4449
expectToExist('md-dialog-container', false);
45-
});
46-
});
50+
}));
4751

48-
it('should close by clicking on the "close" button', () => {
52+
it('should close by clicking on the "close" button', asyncSpec(async () => {
4953
element(by.id('default')).click();
5054

51-
waitForDialog().then(() => {
52-
element(by.id('close')).click();
53-
expectToExist('md-dialog-container', false);
54-
});
55-
});
55+
await waitForDialog();
56+
element(by.id('close')).click();
57+
expectToExist('md-dialog-container', false);
58+
}));
5659

57-
it('should focus the first focusable element', () => {
60+
it('should focus the first focusable element', asyncSpec(async () => {
5861
element(by.id('default')).click();
5962

60-
waitForDialog().then(() => {
61-
expectFocusOn('md-dialog-container input');
62-
});
63-
});
63+
await waitForDialog();
64+
expectFocusOn('md-dialog-container input');
65+
}));
6466

65-
it('should restore focus to the element that opened the dialog', () => {
67+
it('should restore focus to the element that opened the dialog', asyncSpec(async () => {
6668
let openButton = element(by.id('default'));
6769

6870
openButton.click();
6971

70-
waitForDialog().then(() => {
71-
clickOnBackrop();
72-
expectFocusOn(openButton);
73-
});
74-
});
72+
await waitForDialog();
73+
clickOnBackrop();
74+
expectFocusOn(openButton);
75+
}));
7576

76-
it('should prevent tabbing out of the dialog', () => {
77+
it('should prevent tabbing out of the dialog', asyncSpec(async () => {
7778
element(by.id('default')).click();
7879

79-
waitForDialog().then(() => {
80-
let tab = Key.TAB;
80+
await waitForDialog();
81+
pressKeys(Key.TAB, Key.TAB, Key.TAB);
82+
expectFocusOn('#close');
83+
}));
8184

82-
pressKeys(tab, tab, tab);
83-
expectFocusOn('#close');
84-
});
85-
});
86-
87-
it('should be able to prevent closing by clicking on the backdrop', () => {
85+
it('should be able to prevent closing by clicking on the backdrop', asyncSpec(async () => {
8886
element(by.id('disabled')).click();
8987

90-
waitForDialog().then(() => {
91-
clickOnBackrop();
92-
expectToExist('md-dialog-container');
93-
});
94-
});
88+
await waitForDialog();
89+
clickOnBackrop();
90+
expectToExist('md-dialog-container');
91+
}));
9592

96-
it('should be able to prevent closing by pressing escape', () => {
93+
it('should be able to prevent closing by pressing escape', asyncSpec(async () => {
9794
element(by.id('disabled')).click();
9895

99-
waitForDialog().then(() => {
100-
pressKeys(Key.ESCAPE);
101-
expectToExist('md-dialog-container');
102-
});
103-
});
96+
await waitForDialog();
97+
pressKeys(Key.ESCAPE);
98+
expectToExist('md-dialog-container');
99+
}));
104100

105101
function waitForDialog() {
106102
return waitForElement('md-dialog-container');

e2e/components/grid-list/grid-list.e2e.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {browser} from 'protractor';
2-
import {expectToExist} from '../../util/asserts';
2+
import {expectToExist} from '../../util/index';
33
import {screenshot} from '../../screenshot';
44

55
describe('grid-list', () => {

0 commit comments

Comments
 (0)