-
Notifications
You must be signed in to change notification settings - Fork 116
/
Copy pathselenium_server.ts
362 lines (336 loc) · 11.6 KB
/
selenium_server.ts
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
import * as childProcess from 'child_process';
import * as fs from 'fs';
import * as loglevel from 'loglevel';
import * as os from 'os';
import * as path from 'path';
import * as request from 'request';
import {OUT_DIR, ProviderClass, ProviderConfig, ProviderInterface} from './provider';
import {convertXmlToVersionList, updateXml} from './utils/cloud_storage_xml';
import {generateConfigFile, getBinaryPathFromConfig, removeFiles,} from './utils/file_utils';
import {curlCommand, initOptions, requestBinary} from './utils/http_utils';
import {getVersion} from './utils/version_list';
const log = loglevel.getLogger('webdriver-manager');
export interface SeleniumServerProviderConfig extends ProviderConfig {
port?: number;
gridUrl?: string;
runAsNode?: boolean;
runAsGrid?: boolean;
runAsDetach?: boolean;
logLevel?: string;
}
export class SeleniumServer extends ProviderClass implements ProviderInterface {
cacheFileName = 'selenium-server.xml';
configFileName = 'selenium-server.config.json';
ignoreSSL = false;
osType = os.type();
osArch = os.arch();
outDir = OUT_DIR;
port = 4444;
gridUrl = '';
proxy: string = null;
requestUrl = 'https://selenium-release.storage.googleapis.com/';
seleniumProcess: childProcess.ChildProcess;
runAsNode = false;
runAsGrid = false;
runAsDetach = false;
logLevel: string = null;
javaOpts: {[key: string]: string} = {};
version: string = null;
maxVersion: string = null;
constructor(config?: SeleniumServerProviderConfig) {
super();
this.cacheFileName = this.setVar('cacheFileName', this.cacheFileName, config);
this.configFileName = this.setVar('configFileName', this.configFileName, config);
this.ignoreSSL = this.setVar('ignoreSSL', this.ignoreSSL, config);
this.osArch = this.setVar('osArch', this.osArch, config);
this.osType = this.setVar('osType', this.osType, config);
this.outDir = this.setVar('outDir', this.outDir, config);
this.port = this.setVar('port', this.port, config);
this.proxy = this.setVar('proxy', this.proxy, config);
this.requestUrl = this.setVar('requestUrl', this.requestUrl, config);
this.runAsNode = this.setVar('runAsNode', this.runAsNode, config);
this.gridUrl = this.setVar('gridUrl', this.gridUrl, config);
this.runAsDetach = this.setVar('runAsDetach', this.runAsDetach, config);
if (this.runAsDetach) {
this.runAsNode = true;
}
if (this.gridUrl !== '') {
this.runAsGrid = true;
}
this.version = this.setVar('version', this.version, config);
this.maxVersion = this.setVar('maxVersion', this.maxVersion, config);
this.logLevel = this.setVar('logLevel', this.logLevel, config);
if (this.logLevel) {
this.setJavaFlag('-Dselenium.LOGGER.level', this.logLevel);
}
}
/**
* Should update the cache and download, find the version to download,
* then download that binary.
* @param version Optional to provide the version number or latest.
* @param maxVersion Optional to provide the max version.
*/
async updateBinary(version?: string, maxVersion?: string): Promise<void> {
if (!version) {
version = this.version;
}
if (!maxVersion) {
maxVersion = this.maxVersion;
}
await updateXml(this.requestUrl, {
fileName: path.resolve(this.outDir, this.cacheFileName),
ignoreSSL: this.ignoreSSL,
proxy: this.proxy
});
const versionList = convertXmlToVersionList(
path.resolve(this.outDir, this.cacheFileName),
'selenium-server-standalone', versionParser, semanticVersionParser);
const versionObj = getVersion(versionList, '', version, maxVersion);
const seleniumServerUrl = this.requestUrl + versionObj.url;
const seleniumServerJar = path.resolve(this.outDir, versionObj.name);
// We should check the jar file size if it exists. The size will
// be used to either make the request, or quit the request if the file
// size matches.
let fileSize = 0;
try {
fileSize = fs.statSync(seleniumServerJar).size;
} catch (err) {
}
await requestBinary(seleniumServerUrl, {
fileName: seleniumServerJar,
fileSize,
ignoreSSL: this.ignoreSSL,
proxy: this.proxy
});
generateConfigFile(
this.outDir, path.resolve(this.outDir, this.configFileName),
matchBinaries(), seleniumServerJar);
return Promise.resolve();
}
/**
* Starts selenium standalone server and handles emitted exit events.
* @param opts The options to pass to the jar file.
* @param version The optional version of the selenium jar file.
* @returns A promise so the server can run while awaiting its completion.
*/
startServer(opts: {[key: string]: string}, version?: string):
Promise<number> {
const java = this.getJava();
return new Promise<number>(async (resolve, _) => {
if (this.runAsDetach) {
this.runAsNode = true;
const cmd = this.getCmdStartServer(opts, version);
log.info(`${java} ${cmd.join(' ')}`);
this.seleniumProcess =
childProcess.spawn(java, cmd, {detached: true, stdio: 'ignore'});
log.info(`selenium process id: ${this.seleniumProcess.pid}`);
await new Promise((resolve, _) => {
setTimeout(resolve, 2000);
});
this.seleniumProcess.unref();
await new Promise((resolve, _) => {
setTimeout(resolve, 500);
});
resolve(0);
} else {
const cmd = this.getCmdStartServer(opts, version);
log.info(`${java} ${cmd.join(' ')}`);
this.seleniumProcess =
childProcess.spawn(java, cmd, {stdio: 'inherit'});
log.info(`selenium process id: ${this.seleniumProcess.pid}`);
this.seleniumProcess.on('exit', (code: number) => {
log.info(`Selenium Standalone has exited with code: ${code}`);
resolve(code);
});
this.seleniumProcess.on('error', (err: Error) => {
log.error(`Selenium Standalone server encountered an error: ${err}`);
});
}
});
}
/**
* Get the binary file path.
* @param version Optional to provide the version number or the latest.
*/
getBinaryPath(version?: string): string|null {
try {
const configFilePath = path.resolve(this.outDir, this.configFileName);
return getBinaryPathFromConfig(configFilePath, version);
} catch (_) {
return null;
}
}
/**
* Sets a java flag option.
* @param key The java option flag.
* @param value The value of the flag.
*/
setJavaFlag(key: string, value: string) {
if (value) {
this.javaOpts[key] = value;
}
}
/**
* Get the selenium server start command (not including the java command)
* @param opts The options to pass to the jar file.
* @param version The optional version of the selenium jar file.
* @returns The spawn arguments array.
*/
getCmdStartServer(opts: {[key: string]: string}, version?: string): string[] {
const jarFile = this.getBinaryPath(version);
const options: string[] = [];
if (opts) {
for (const opt of Object.keys(opts)) {
options.push(`${opt}=${opts[opt]}`);
}
}
options.push('-jar');
options.push(jarFile);
if (this.runAsNode && !this.runAsGrid) {
options.push('-role');
options.push('node');
options.push('-servlet');
options.push('org.openqa.grid.web.servlet.LifecycleServlet');
options.push('-registerCycle');
options.push('0');
}
if (this.runAsGrid) {
options.push('-role');
options.push('node');
options.push('-hub');
options.push(this.gridUrl);
}
if (!this.runAsGrid) {
options.push('-port');
options.push(this.port.toString());
}
return options;
}
/**
* Gets the java command either by the JAVA_HOME environment variable or
* just the java command.
*/
getJava(): string {
let java = 'java';
if (process.env.JAVA_HOME) {
java = path.resolve(process.env.JAVA_HOME, 'bin', 'java');
if (this.osType === 'Windows_NT') {
java += '.exe';
}
}
return java;
}
/**
* If we are running the selenium server role = node, send
* the command to stop the server via http get request. Reference:
* https://github.com/SeleniumHQ/selenium/issues/2852#issuecomment-268324091
*
* If we are not running as the selenium server role = node, kill the
* process with pid.
*
* @param host The protocol and ip address, default http://127.0.0.1
* @param port The port number, default 4444
* @returns A promise of the http get request completing.
*/
stopServer(host?: string, port?: number): Promise<void> {
if (this.runAsNode) {
if (!host) {
host = 'http://127.0.0.1';
}
if (!port) {
port = this.port;
}
const stopUrl =
host + ':' + port + '/extra/LifecycleServlet?action=shutdown';
const options = initOptions(stopUrl, {});
log.info(curlCommand(options));
return new Promise<void>((resolve, _) => {
const req = request(options);
req.on('response', response => {
response.on('end', () => {
resolve();
});
});
});
} else if (this.seleniumProcess) {
process.kill(this.seleniumProcess.pid);
return Promise.resolve();
} else {
return Promise.reject(
'Could not stop the server, server is not running.');
}
}
/**
* Gets a comma delimited list of versions downloaded. Also has the "latest"
* downloaded noted.
*/
getStatus(): string|null {
try {
const configFilePath = path.resolve(this.outDir, this.configFileName);
const configJson = JSON.parse(fs.readFileSync(configFilePath).toString());
const versions: string[] = [];
for (const binaryPath of configJson['all']) {
let version = '';
const regex = /.*selenium-server-standalone-(\d+.\d+.\d+.*).jar/g;
try {
const exec = regex.exec(binaryPath);
if (exec && exec[1]) {
version = exec[1];
}
} catch (_) {
}
if (configJson['last'] === binaryPath) {
version += ' (latest)';
}
versions.push(version);
}
return versions.join(', ');
} catch (_) {
return null;
}
}
/**
* Get a line delimited list of files removed.
*/
cleanFiles(): string {
return removeFiles(this.outDir, [/selenium-server.*/g]);
}
}
/**
* Captures the version name which includes the semantic version and extra
* metadata. So an example for 12.34/selenium-server-standalone-12.34.56.jar,
* the version is 12.34.56. For metadata,
* 12.34/selenium-server-standalone-12.34.56-beta.jar is 12.34.56-beta.
* @param xmlKey The xml key including the partial url.
*/
export function versionParser(xmlKey: string) {
// Capture the version name 12.34.56 or 12.34.56-beta
const regex = /.*selenium-server-standalone-(\d+.\d+.\d+.*).jar/g;
try {
return regex.exec(xmlKey)[1];
} catch (_) {
return null;
}
}
/**
* Captures the version name which includes the semantic version and extra
* metadata. So an example for 12.34/selenium-server-standalone-12.34.56.jar,
* the version is 12.34.56. For metadata,
* 12.34/selenium-server-standalone-12.34.56-beta.jar is still 12.34.56.
* @param xmlKey The xml key including the partial url.
*/
export function semanticVersionParser(xmlKey: string) {
// Only capture numbers 12.34.56
const regex = /.*selenium-server-standalone-(\d+.\d+.\d+).*.jar/g;
try {
return regex.exec(xmlKey)[1];
} catch (_) {
return null;
}
}
/**
* Matches the installed binaries.
*/
export function matchBinaries(): RegExp|null {
return /selenium-server-standalone-\d+.\d+.\d+.*.jar/g;
}