Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
seleniumhq
GitHub Repository: seleniumhq/selenium
Path: blob/trunk/javascript/selenium-webdriver/remote/util.js
3220 views
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('node:path')
21
const cp = require('node:child_process')
22
const logging = require('../lib/logging')
23
24
/**
25
* returns path to java or 'java' string if JAVA_HOME does not exist in env obj
26
* @returns {string}
27
*/
28
function getJavaPath() {
29
return process.env['JAVA_HOME'] ? path.join(process.env['JAVA_HOME'], 'bin/java') : 'java'
30
}
31
32
/**
33
* @param {string} seleniumStandalonePath path to standalone server
34
* @returns {boolean}
35
*/
36
function isSelenium3x(seleniumStandalonePath) {
37
const javaPath = getJavaPath()
38
39
const execRes = cp.execFileSync(javaPath, ['-jar', seleniumStandalonePath, '--version'])
40
41
return execRes.toString().trim().startsWith('Selenium server version: 3')
42
}
43
44
/**
45
* @param {string} seleniumStandalonePath path to standalone server
46
* @param {Array.<string>} args spawn arguments array
47
* returns formatted args based on selenium standalone server version
48
* @returns {Array.<string>}
49
*/
50
function formatSpawnArgs(seleniumStandalonePath, args) {
51
if (isSelenium3x(seleniumStandalonePath)) {
52
logging
53
.getLogger(logging.Type.SERVER)
54
.warning('Deprecation: Support for Standalone Server 3.x will be removed soon. Please update to version 4.x')
55
return args
56
}
57
58
const standaloneArg = 'standalone'
59
const port3xArgFormat = '-port'
60
const port4xArgFormat = '--port'
61
62
let formattedArgs = Array.from(args)
63
64
const standaloneArgIndex = formattedArgs.findIndex((arg) => arg === seleniumStandalonePath)
65
const v3portArgFormat = formattedArgs.findIndex((arg) => arg === port3xArgFormat)
66
67
// old v3x port arg format was -port, new v4x port arg format is --port
68
if (v3portArgFormat !== -1) {
69
formattedArgs[v3portArgFormat] = port4xArgFormat
70
}
71
72
// 'standalone' arg should be right after jar file path
73
// in case if it is already in place - returns args
74
if (formattedArgs[standaloneArgIndex + 1] === standaloneArg) return formattedArgs
75
76
// insert 'standalone' right after jar file path
77
formattedArgs.splice(standaloneArgIndex + 1, 0, standaloneArg)
78
79
return formattedArgs
80
}
81
82
// PUBLIC API
83
module.exports = {
84
getJavaPath,
85
isSelenium3x,
86
formatSpawnArgs,
87
}
88
89