Path: blob/trunk/javascript/selenium-webdriver/remote/util.js
1864 views
// Licensed to the Software Freedom Conservancy (SFC) under one1// or more contributor license agreements. See the NOTICE file2// distributed with this work for additional information3// regarding copyright ownership. The SFC licenses this file4// to you under the Apache License, Version 2.0 (the5// "License"); you may not use this file except in compliance6// with the License. You may obtain a copy of the License at7//8// http://www.apache.org/licenses/LICENSE-2.09//10// Unless required by applicable law or agreed to in writing,11// software distributed under the License is distributed on an12// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY13// KIND, either express or implied. See the License for the14// specific language governing permissions and limitations15// under the License.1617'use strict'1819const path = require('node:path')20const cp = require('node:child_process')21const logging = require('../lib/logging')2223/**24* returns path to java or 'java' string if JAVA_HOME does not exist in env obj25* @returns {string}26*/27function getJavaPath() {28return process.env['JAVA_HOME'] ? path.join(process.env['JAVA_HOME'], 'bin/java') : 'java'29}3031/**32* @param {string} seleniumStandalonePath path to standalone server33* @returns {boolean}34*/35function isSelenium3x(seleniumStandalonePath) {36const javaPath = getJavaPath()3738const execRes = cp.execFileSync(javaPath, ['-jar', seleniumStandalonePath, '--version'])3940return execRes.toString().trim().startsWith('Selenium server version: 3')41}4243/**44* @param {string} seleniumStandalonePath path to standalone server45* @param {Array.<string>} args spawn arguments array46* returns formatted args based on selenium standalone server version47* @returns {Array.<string>}48*/49function formatSpawnArgs(seleniumStandalonePath, args) {50if (isSelenium3x(seleniumStandalonePath)) {51logging52.getLogger(logging.Type.SERVER)53.warning('Deprecation: Support for Standalone Server 3.x will be removed soon. Please update to version 4.x')54return args55}5657const standaloneArg = 'standalone'58const port3xArgFormat = '-port'59const port4xArgFormat = '--port'6061let formattedArgs = Array.from(args)6263const standaloneArgIndex = formattedArgs.findIndex((arg) => arg === seleniumStandalonePath)64const v3portArgFormat = formattedArgs.findIndex((arg) => arg === port3xArgFormat)6566// old v3x port arg format was -port, new v4x port arg format is --port67if (v3portArgFormat !== -1) {68formattedArgs[v3portArgFormat] = port4xArgFormat69}7071// 'standalone' arg should be right after jar file path72// in case if it is already in place - returns args73if (formattedArgs[standaloneArgIndex + 1] === standaloneArg) return formattedArgs7475// insert 'standalone' right after jar file path76formattedArgs.splice(standaloneArgIndex + 1, 0, standaloneArg)7778return formattedArgs79}8081// PUBLIC API82module.exports = {83getJavaPath,84isSelenium3x,85formatSpawnArgs,86}878889