|
1 |
| -describe('Browser', function() { |
| 1 | +class Person { |
2 | 2 |
|
3 |
| - var newBrowser; |
| 3 | + constructor(name, browser) { |
| 4 | + this.name = name; |
| 5 | + this.browser = browser; |
| 6 | + this.$ = browser.$; |
| 7 | + this.element = browser.element; |
| 8 | + } |
4 | 9 |
|
5 |
| - afterEach(function(done) { |
| 10 | + async openApp() { |
| 11 | + await this.browser.get('index.html#/interaction'); |
| 12 | + }; |
| 13 | + |
| 14 | + async login() { |
| 15 | + await this.element(by.model('userInput')).sendKeys(this.name); |
| 16 | + await this.$('#sendUser').click(); |
| 17 | + }; |
| 18 | + |
| 19 | + async clearMessages() { |
| 20 | + await this.$('#clearMessages').click(); |
| 21 | + }; |
| 22 | + |
| 23 | + async sendMessage(msg) { |
| 24 | + await this.element(by.model('message')).sendKeys(msg); |
| 25 | + await this.$('#sendMessage').click(); |
| 26 | + }; |
| 27 | + |
| 28 | + getMessages() { |
| 29 | + return this.element.all(by.repeater('msg in messages track by $index')); |
| 30 | + }; |
| 31 | +}; |
| 32 | + |
| 33 | +describe('Browser', () => { |
| 34 | + |
| 35 | + let newBrowser; |
| 36 | + |
| 37 | + afterEach(async() => { |
6 | 38 | // Calling quit will remove the browser.
|
7 | 39 | // You can choose to not quit the browser, and protractor will quit all of
|
8 | 40 | // them for you when it exits (i.e. if you need a static number of browsers
|
9 | 41 | // throughout all of your tests). However, I'm forking browsers in my tests
|
10 | 42 | // and don't want to pile up my browser count.
|
11 | 43 | if (newBrowser) {
|
12 |
| - newBrowser.quit().then(() => { |
13 |
| - done(); |
14 |
| - }); |
15 |
| - } else { |
16 |
| - done(); |
| 44 | + await newBrowser.quit(); |
17 | 45 | }
|
18 | 46 | });
|
19 | 47 |
|
20 |
| - it('should be able to fork', function() { |
21 |
| - browser.get('index.html'); |
22 |
| - newBrowser = browser.forkNewDriverInstance(); |
| 48 | + it('should be able to fork', async() => { |
| 49 | + await browser.get('index.html'); |
| 50 | + newBrowser = await browser.forkNewDriverInstance().ready; |
23 | 51 | expect(newBrowser).not.toEqual(browser);
|
24 | 52 | expect(newBrowser.driver).not.toEqual(browser.driver);
|
25 |
| - expect(newBrowser.driver.getCurrentUrl()).toEqual('data:,'); |
| 53 | + expect(await newBrowser.driver.getCurrentUrl()).toEqual('data:,'); |
26 | 54 | });
|
27 | 55 |
|
28 |
| - it('should be able to navigate to same url on fork', function() { |
29 |
| - browser.get('index.html'); |
30 |
| - newBrowser = browser.forkNewDriverInstance(true); |
31 |
| - expect(newBrowser.driver.getCurrentUrl()). |
32 |
| - toMatch('index.html#/form'); |
| 56 | + it('should be able to navigate to same url on fork', async() => { |
| 57 | + await browser.get('index.html'); |
| 58 | + newBrowser = await browser.forkNewDriverInstance(true).ready; |
| 59 | + expect(await newBrowser.driver.getCurrentUrl()).toMatch('index.html#/form'); |
33 | 60 | });
|
34 | 61 |
|
35 |
| - it('should be able to copy mock modules on fork', function() { |
36 |
| - var mockModule = function() { |
37 |
| - var newModule = angular.module('mockModule', []); |
| 62 | + it('should be able to copy mock modules on fork', async() => { |
| 63 | + const mockModule = () => { |
| 64 | + const newModule = angular.module('mockModule', []); |
38 | 65 | newModule.value('version', '2');
|
39 | 66 | };
|
40 | 67 |
|
41 | 68 | browser.addMockModule('mockModule', mockModule);
|
42 |
| - browser.get('index.html'); |
| 69 | + await browser.get('index.html'); |
43 | 70 |
|
44 |
| - newBrowser = browser.forkNewDriverInstance(true, true); |
45 |
| - expect(newBrowser.element(by.css('[app-version]')).getText()).toEqual('2'); |
| 71 | + newBrowser = await browser.forkNewDriverInstance(true, true).ready; |
| 72 | + expect(await newBrowser.element(by.css('[app-version]')).getText()) |
| 73 | + .toEqual('2'); |
46 | 74 | });
|
47 | 75 |
|
48 | 76 |
|
49 |
| - describe('Multiple browsers', function() { |
| 77 | + describe('Multiple browsers', () => { |
50 | 78 |
|
51 |
| - var Person = function(name, browser) { |
52 |
| - var $ = browser.$; |
53 |
| - var element = browser.element; |
54 |
| - |
55 |
| - this.openApp = function() { |
56 |
| - browser.get('index.html#/interaction'); |
57 |
| - }; |
58 |
| - |
59 |
| - this.login = function() { |
60 |
| - element(by.model('userInput')).sendKeys(name); |
61 |
| - $('#sendUser').click(); |
62 |
| - }; |
63 |
| - |
64 |
| - this.clearMessages = function() { |
65 |
| - $('#clearMessages').click(); |
66 |
| - }; |
67 |
| - |
68 |
| - this.sendMessage = function(msg) { |
69 |
| - element(by.model('message')).sendKeys(msg); |
70 |
| - $('#sendMessage').click(); |
71 |
| - }; |
72 |
| - |
73 |
| - this.getMessages = function() { |
74 |
| - return element.all(by.repeater('msg in messages track by $index')); |
75 |
| - }; |
76 |
| - }; |
| 79 | + |
77 | 80 |
|
78 |
| - var p0, p1; |
| 81 | + let p0, p1; |
79 | 82 |
|
80 |
| - beforeEach(function() { |
| 83 | + beforeEach(async() => { |
81 | 84 | // default browser.
|
82 | 85 | p0 = new Person('p0', browser);
|
83 |
| - p0.openApp(); |
84 |
| - p0.login(); |
85 |
| - p0.clearMessages(); |
| 86 | + await p0.openApp(); |
| 87 | + await p0.login(); |
| 88 | + await p0.clearMessages(); |
86 | 89 |
|
87 | 90 | // Any additional browsers can be instantiated via browser.forkNewDriverInstance().
|
88 |
| - newBrowser = browser.forkNewDriverInstance(true); |
| 91 | + newBrowser = await browser.forkNewDriverInstance(true).ready; |
89 | 92 | p1 = new Person('p1', newBrowser);
|
90 |
| - p1.openApp(); |
91 |
| - p1.login(); |
| 93 | + await p1.openApp(); |
| 94 | + await p1.login(); |
92 | 95 | });
|
93 | 96 |
|
94 |
| - it('should be able to interact', function() { |
95 |
| - expect(p0.getMessages().count()).toEqual(0); |
| 97 | + it('should be able to interact', async() => { |
| 98 | + expect(await p0.getMessages().count()).toEqual(0); |
96 | 99 |
|
97 |
| - p0.sendMessage('p0'); |
98 |
| - browser.sleep(100); // The app polls every 100ms for updates. |
99 |
| - expect(p0.getMessages().count()).toEqual(1); |
100 |
| - expect(p1.getMessages().count()).toEqual(1); |
| 100 | + await p0.sendMessage('p0'); |
| 101 | + await browser.sleep(100); // The app polls every 100ms for updates. |
| 102 | + expect(await p0.getMessages().count()).toEqual(1); |
| 103 | + expect(await p1.getMessages().count()).toEqual(1); |
101 | 104 |
|
102 |
| - p1.sendMessage('p1'); |
103 |
| - browser.sleep(100); // The app polls every 100ms for updates. |
104 |
| - expect(p0.getMessages().count()).toEqual(2); |
105 |
| - expect(p1.getMessages().count()).toEqual(2); |
| 105 | + await p1.sendMessage('p1'); |
| 106 | + await browser.sleep(100); // The app polls every 100ms for updates. |
| 107 | + expect(await p0.getMessages().count()).toEqual(2); |
| 108 | + expect(await p1.getMessages().count()).toEqual(2); |
106 | 109 | });
|
107 | 110 |
|
108 |
| - it('should perform actions in sync', function() { |
109 |
| - var ACTIONS = 10; |
110 |
| - expect(p0.getMessages().count()).toEqual(0); |
| 111 | + it('should perform actions in sync', async() => { |
| 112 | + const ACTIONS = 10; |
| 113 | + expect(await p0.getMessages().count()).toEqual(0); |
111 | 114 |
|
112 |
| - var expectedMessages = []; |
113 |
| - var i; |
| 115 | + let expectedMessages = []; |
| 116 | + let i; |
114 | 117 | for (i = 0; i < ACTIONS; ++i) {
|
115 |
| - p0.sendMessage(i); |
| 118 | + await p0.sendMessage(i); |
116 | 119 | expectedMessages.push('p0: ' + i);
|
117 | 120 | }
|
118 | 121 | for (i = 0; i < ACTIONS; ++i) {
|
119 |
| - p1.sendMessage(i); |
| 122 | + await p1.sendMessage(i); |
120 | 123 | expectedMessages.push('p1: ' + i);
|
121 | 124 | }
|
122 | 125 | for (i = 0; i < ACTIONS; ++i) {
|
123 |
| - p0.sendMessage(i); |
124 |
| - p1.sendMessage(i); |
| 126 | + await p0.sendMessage(i); |
| 127 | + await p1.sendMessage(i); |
125 | 128 | expectedMessages.push('p0: ' + i);
|
126 | 129 | expectedMessages.push('p1: ' + i);
|
127 | 130 | }
|
128 | 131 |
|
129 |
| - browser.sleep(100); // The app polls every 100ms for updates. |
130 |
| - expect(p0.getMessages().getText()).toEqual(expectedMessages); |
131 |
| - expect(p1.getMessages().getText()).toEqual(expectedMessages); |
| 132 | + await browser.sleep(100); // The app polls every 100ms for updates. |
| 133 | + expect(await p0.getMessages().getText()).toEqual(expectedMessages); |
| 134 | + expect(await p1.getMessages().getText()).toEqual(expectedMessages); |
132 | 135 | });
|
133 | 136 | });
|
134 | 137 | });
|
0 commit comments