Skip to content

Commit fce3214

Browse files
feat/fix spawn format for SeleniumServer, issue 11405 (#11412)
Co-authored-by: Sri Harsha <[email protected]>
1 parent 4d4020c commit fce3214

File tree

2 files changed

+100
-7
lines changed

2 files changed

+100
-7
lines changed

javascript/node/selenium-webdriver/remote/index.js

+10-7
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
'use strict'
1919

2020
const fs = require('fs')
21-
const path = require('path')
2221
const url = require('url')
2322

2423
const httpUtil = require('../http/util')
@@ -30,6 +29,8 @@ const input = require('../lib/input')
3029
const net = require('../net')
3130
const portprober = require('../net/portprober')
3231

32+
const { getJavaPath, formatSpawnArgs } = require('./util')
33+
3334
/**
3435
* @typedef {(string|!Array<string|number|!stream.Stream|null|undefined>)}
3536
*/
@@ -236,7 +237,7 @@ class DriverService {
236237
net.getLoopbackAddress()
237238
}
238239

239-
var serverUrl = url.format({
240+
const serverUrl = url.format({
240241
protocol: 'http',
241242
hostname: hostname,
242243
port: port + '',
@@ -479,13 +480,15 @@ class SeleniumServer extends DriverService {
479480
let port = resolved[0]
480481
let jvmArgs = resolved[1]
481482
let args = resolved[2]
482-
return jvmArgs.concat('-jar', jar, '-port', port).concat(args)
483+
484+
const fullArgsList = jvmArgs
485+
.concat('-jar', jar, '-port', port)
486+
.concat(args)
487+
488+
return formatSpawnArgs(jar, fullArgsList)
483489
})
484490

485-
let java = 'java'
486-
if (process.env['JAVA_HOME']) {
487-
java = path.join(process.env['JAVA_HOME'], 'bin/java')
488-
}
491+
const java = getJavaPath()
489492

490493
super(java, {
491494
loopback: options.loopback,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
// Licensed to the Software Freedom Conservancy (SFC) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The SFC licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
'use strict'
19+
20+
const path = require('path')
21+
const cp = require('child_process')
22+
23+
/**
24+
* returns path to java or 'java' string if JAVA_HOME does not exist in env obj
25+
* @returns {string}
26+
*/
27+
function getJavaPath() {
28+
return process.env['JAVA_HOME']
29+
? path.join(process.env['JAVA_HOME'], 'bin/java')
30+
: 'java'
31+
}
32+
33+
/**
34+
* @param {string} seleniumStandalonePath path to standalone server
35+
* @returns {boolean}
36+
*/
37+
function isSelenium3x(seleniumStandalonePath) {
38+
const javaPath = getJavaPath()
39+
40+
const execRes = cp.execSync(
41+
`${javaPath} -jar ${seleniumStandalonePath} --version`
42+
)
43+
44+
return execRes.toString().trim().startsWith('Selenium server version: 3')
45+
}
46+
47+
/**
48+
* @param {string} seleniumStandalonePath path to standalone server
49+
* @param {Array.<string>} args spawn arguments array
50+
* returns formatted args based on selenium standalone server version
51+
* @returns {Array.<string>}
52+
*/
53+
function formatSpawnArgs(seleniumStandalonePath, args) {
54+
if (isSelenium3x(seleniumStandalonePath)) return args
55+
56+
const standaloneArg = 'standalone'
57+
const port3xArgFormat = '-port'
58+
const port4xArgFormat = '--port'
59+
60+
let formattedArgs = Array.from(args)
61+
62+
const standaloneArgIndex = formattedArgs.findIndex(
63+
(arg) => arg === seleniumStandalonePath
64+
)
65+
const v3portArgFormat = formattedArgs.findIndex(
66+
(arg) => arg === port3xArgFormat
67+
)
68+
69+
// old v3x port arg format was -port, new v4x port arg format is --port
70+
if (v3portArgFormat !== -1) {
71+
formattedArgs[v3portArgFormat] = port4xArgFormat
72+
}
73+
74+
// 'standalone' arg should be right after jar file path
75+
// in case if it is already in place - returns args
76+
if (formattedArgs[standaloneArgIndex + 1] === standaloneArg)
77+
return formattedArgs
78+
79+
// insert 'standalone' right after jar file path
80+
formattedArgs.splice(standaloneArgIndex + 1, 0, standaloneArg)
81+
82+
return formattedArgs
83+
}
84+
85+
// PUBLIC API
86+
module.exports = {
87+
getJavaPath,
88+
isSelenium3x,
89+
formatSpawnArgs,
90+
}

0 commit comments

Comments
 (0)