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
203 lines (174 loc) · 4.99 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
/*jslint node: true */
'use strict';
const fs = require('fs-extra');
const glob = require('glob');
const path = require('path');
const spawn = require('cross-spawn');
const selenium = require('selenium-standalone');
const protractorLauncher = require('protractor/built/launcher');
const build = require('./build');
const server = require('./utils/server');
const logger = require('../utils/logger');
// Disable this to quiet the output
const spawnOptions = { stdio: 'inherit' };
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;
}
// Catch protractor's "Kitchen Sink" error.
if (exitCode === 199) {
logger.warn('Supressing protractor\'s "kitchen sink" error 199');
exitCode = 0;
}
server.stop();
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');
protractorLauncher.init(getProtractorConfigPath(), {
params: {
localUrl: `https://localhost:${port}`,
chunks: chunks,
skyPagesConfig: skyPagesConfig
}
});
process.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', '--gecko', 'false'], spawnOptions);
if (results.error) {
reject(results.error);
return;
}
logger.info('Selenium server is ready.');
resolve();
}
});
}
/**
* Spawns the build process. Captures the config used.
*/
function spawnBuild(argv, skyPagesConfig, webpack) {
return new Promise((resolve, reject) => {
if (argv.build === false) {
logger.info('Skipping build step');
const file = 'dist/metadata.json';
if (!fs.existsSync(file)) {
logger.info(`Unable to skip build step. "${file}" not found.`);
} else {
return resolve({
metadata: fs.readJsonSync(file)
});
}
}
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);
const specsPath = path.resolve(process.cwd(), 'e2e/**/*.e2e-spec.ts');
const specsGlob = glob.sync(specsPath);
if (specsGlob.length === 0) {
logger.info('No spec files located. Stopping command from running.');
return killServers(0);
}
server.start()
.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),
port,
spawnSelenium()
]);
})
.then(values => {
spawnProtractor(
values[0],
values[1],
skyPagesConfig
);
})
.catch(err => {
logger.warn(`ERROR [skyux e2e]: ${err.message}`);
killServers(1);
});
}
module.exports = e2e;