Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
seleniumhq
GitHub Repository: seleniumhq/selenium
Path: blob/trunk/javascript/selenium-webdriver/common/seleniumManager.js
2884 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
/**
19
* This implementation is still in beta, and may change.
20
*
21
* Wrapper for getting information from the Selenium Manager binaries
22
*/
23
24
const { platform } = require('node:process')
25
const path = require('node:path')
26
const fs = require('node:fs')
27
const spawnSync = require('node:child_process').spawnSync
28
const logging = require('../lib/logging')
29
30
const log_ = logging.getLogger(logging.Type.DRIVER)
31
let debugMessagePrinted = false
32
33
/**
34
* Determines the path of the correct Selenium Manager binary
35
* @returns {string}
36
*/
37
function getBinary() {
38
const directory = {
39
darwin: 'macos',
40
win32: 'windows',
41
cygwin: 'windows',
42
linux: 'linux',
43
}[platform]
44
45
const file = directory === 'windows' ? 'selenium-manager.exe' : 'selenium-manager'
46
47
let seleniumManagerBasePath = path.join(__dirname, '..', '/bin')
48
49
const filePath = process.env.SE_MANAGER_PATH || path.join(seleniumManagerBasePath, directory, file)
50
51
if (!fs.existsSync(filePath)) {
52
throw new Error(`Unable to obtain Selenium Manager at ${filePath}`)
53
}
54
55
if (!debugMessagePrinted) {
56
log_.debug(`Selenium Manager binary found at ${filePath}`)
57
debugMessagePrinted = true // Set the flag to true after printing the debug message
58
}
59
60
return filePath
61
}
62
63
/**
64
* Determines the path of the correct driver
65
* @param {string[]} args arguments to invoke Selenium Manager
66
* @returns {{browserPath: string, driverPath: string}} path of the driver and
67
* browser location
68
*/
69
70
function binaryPaths(args) {
71
const smBinary = getBinary()
72
const spawnResult = spawnSync(smBinary, args)
73
let output
74
if (spawnResult.status) {
75
let errorMessage
76
if (spawnResult.stderr.toString()) {
77
errorMessage = spawnResult.stderr.toString()
78
}
79
if (spawnResult.stdout.toString()) {
80
try {
81
output = JSON.parse(spawnResult.stdout.toString())
82
logOutput(output)
83
errorMessage = output.result.message
84
} catch (e) {
85
errorMessage = e.toString()
86
}
87
}
88
throw new Error(`Error executing command for ${smBinary} with ${args}: ${errorMessage}`)
89
}
90
try {
91
output = JSON.parse(spawnResult.stdout.toString())
92
} catch (e) {
93
throw new Error(`Error executing command for ${smBinary} with ${args}: ${e.toString()}`)
94
}
95
96
logOutput(output)
97
return {
98
driverPath: output.result.driver_path,
99
browserPath: output.result.browser_path,
100
}
101
}
102
103
function logOutput(output) {
104
for (const key in output.logs) {
105
if (output.logs[key].level === 'WARN') {
106
log_.warning(`${output.logs[key].message}`)
107
}
108
if (['DEBUG', 'INFO'].includes(output.logs[key].level)) {
109
log_.debug(`${output.logs[key].message}`)
110
}
111
}
112
}
113
114
// PUBLIC API
115
module.exports = { binaryPaths }
116
117