This repository was archived by the owner on Dec 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathe2e.js
242 lines (211 loc) · 5.85 KB
/
e2e.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/*jslint node: true */
'use strict';
const path = require('path');
const spawn = require('cross-spawn');
const logger = require('../utils/logger');
const portfinder = require('portfinder');
const HttpServer = require('http-server');
const selenium = require('selenium-standalone');
const build = require('./build');
// Disable this to quiet the output
const spawnOptions = { stdio: 'inherit' };
let httpServer;
let seleniumServer;
let start;
/**
* Function to get the protractorConfigPath
* @name getProtractorConfigPath
* @returns {string} protractorConfigPath
*/
function getProtractorConfigPath() {
return path.resolve(
__dirname,
'..',
'config',
'protractor',
'protractor.conf.js'
);
}
/**
* Handles killing off the selenium and webpack servers.
* @name killServers
*/
function killServers(exitCode) {
logger.info('Cleaning up running servers');
if (seleniumServer) {
logger.info('Closing selenium server');
seleniumServer.kill();
seleniumServer = null;
}
if (httpServer) {
logger.info('Closing http server');
httpServer.close();
httpServer = null;
}
// Catch protractor's "Kitchen Sink" error.
if (exitCode === 199) {
logger.warn('Supressing protractor\'s "kitchen sink" error 199');
exitCode = 0;
}
logger.info(`Execution Time: ${(new Date().getTime() - start) / 1000} seconds`);
logger.info(`Exiting process with ${exitCode}`);
process.exit(exitCode || 0);
}
/**
* Spawns the protractor command.
* Perhaps this should be API driven?
* @name spawnProtractor
*/
function spawnProtractor(chunks, port, skyPagesConfig) {
logger.info('Running Protractor');
const protractorPath = path.resolve(
'node_modules',
'.bin',
'protractor'
);
// Generate a trimmed-down version of skyPagesConfig to pass to host-utils.js
let trimmedConfig = {
skyux: {
host: {},
app: {}
}
};
trimmedConfig.skyux.name = skyPagesConfig.skyux.name;
trimmedConfig.skyux.host.url = skyPagesConfig.skyux.host.url;
trimmedConfig.skyux.app.externals = skyPagesConfig.skyux.app.externals;
const protractor = spawn.spawn(
protractorPath,
[
getProtractorConfigPath(),
`--baseUrl ${skyPagesConfig.skyux.host.url}`,
`--params.localUrl=https://localhost:${port}`,
`--params.chunks=${JSON.stringify(chunks)}`,
`--params.skyPagesConfig=${JSON.stringify(trimmedConfig)}`
],
spawnOptions
);
protractor.on('exit', killServers);
}
/**
* Spawns the selenium server if directConnect is not enabled.
* @name spawnSelenium
*/
function spawnSelenium() {
const config = require(getProtractorConfigPath()).config;
return new Promise((resolve, reject) => {
logger.info('Spawning selenium...');
// Assumes we're running selenium ourselves, so we should prep it
if (config.seleniumAddress) {
logger.info('Installing Selenium...');
selenium.install({ logger: logger.info }, () => {
logger.info('Selenium installed. Starting...');
selenium.start((err, child) => {
if (err) {
reject(err);
return;
}
seleniumServer = child;
logger.info('Selenium server is ready.');
resolve();
});
});
// Otherwise we need to prep protractor's selenium
} else {
const webdriverManagerPath = path.resolve(
'node_modules',
'.bin',
'webdriver-manager'
);
let results = spawn.sync(webdriverManagerPath, ['update'], spawnOptions);
if (results.error) {
reject(results.error);
return;
}
logger.info('Selenium server is ready.');
resolve();
}
});
}
/**
* Spawns the httpServer
*/
function spawnServer() {
return new Promise((resolve, reject) => {
logger.info('Requesting open port...');
httpServer = HttpServer.createServer({
root: 'dist/',
cors: true,
https: {
cert: path.resolve(__dirname, '../', 'ssl', 'server.crt'),
key: path.resolve(__dirname, '../', 'ssl', 'server.key')
},
logFn: (req, res, err) => {
if (err) {
reject(err);
return;
}
}
});
portfinder
.getPortPromise()
.then(port => {
logger.info(`Open port found: ${port}`);
logger.info('Starting web server...');
httpServer.listen(port, 'localhost', () => {
logger.info('Web server running.');
resolve(port);
});
})
.catch(reject);
});
}
/**
* Spawns the build process. Captures the config used.
*/
function spawnBuild(argv, skyPagesConfig, webpack) {
return new Promise((resolve, reject) => {
logger.info('Running build...');
build(argv, skyPagesConfig, webpack)
.then(stats => {
logger.info('Build complete.');
resolve(stats.toJson().chunks);
})
.catch(reject);
});
}
/**
* Spawns the necessary commands for e2e.
* Assumes build was ran.
* @name e2e
*/
function e2e(argv, skyPagesConfig, webpack) {
start = new Date().getTime();
process.on('SIGINT', killServers);
spawnServer()
.then((port) => {
argv.assets = 'https://localhost:' + port;
// The assets URL is built by combining the assets URL above with
// the app's root directory, but in e2e tests the assets files
// are served directly from the root. This will back up a directory
// so that asset URLs are built relative to the root rather than
// the app's root directory.
argv.assetsrel = '../';
return Promise
.all([
spawnBuild(argv, skyPagesConfig, webpack),
spawnSelenium()
])
.then(values => {
spawnProtractor(
values[0],
port,
skyPagesConfig
);
});
})
.catch(err => {
logger.warn(`ERROR [skyux e2e]: ${err.message}`);
killServers(1);
});
}
module.exports = e2e;