|
| 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