Skip to content

Commit a5b0ad9

Browse files
atrnhharsha509diemol
authored
[js] Replace execSync with spawnSync in seleniumManager.js (#11649) (#11873)
* replace execSync with spawnSync in seleniumManager.js Since execSync does not escape shell metacharacters, attempting to execute a command with a space in it will fail. This is the case when the path to selenium-manager contains a space. This commit replaces execSync with spawnSync, which does escape shell metacharacters. Fixes #11649 * [javascript] Refine usage of spawnSync --------- Co-authored-by: Sri Harsha <[email protected]> Co-authored-by: Diego Molina <[email protected]> Co-authored-by: Diego Molina <[email protected]>
1 parent f02e19d commit a5b0ad9

File tree

1 file changed

+22
-15
lines changed

1 file changed

+22
-15
lines changed

javascript/node/selenium-webdriver/common/seleniumManager.js

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
const { platform } = require('process')
2525
const path = require('path')
2626
const fs = require('fs')
27-
const execSync = require('child_process').execSync
27+
const spawnSync = require('child_process').spawnSync
2828

2929
/**
3030
* currently supported browsers for selenium-manager
@@ -53,7 +53,6 @@ function getBinary() {
5353
} else {
5454
seleniumManagerBasePath = path.join(__dirname, '..', '/bin')
5555
}
56-
console.log(seleniumManagerBasePath)
5756

5857
const filePath = path.join(seleniumManagerBasePath, directory, file)
5958

@@ -77,7 +76,7 @@ function driverLocation(options) {
7776
)
7877
}
7978

80-
let args = [getBinary(), '--browser', options.getBrowserName(), '--output', 'json']
79+
let args = ['--browser', options.getBrowserName(), '--output', 'json']
8180

8281
if (options.getBrowserVersion() && options.getBrowserVersion() !== "") {
8382
args.push("--browser-version", options.getBrowserVersion())
@@ -89,22 +88,30 @@ function driverLocation(options) {
8988
args.push("--browser-path", '"' + vendorOptions.binary + '"')
9089
}
9190

91+
const smBinary = getBinary()
92+
const spawnResult = spawnSync(smBinary, args)
9293
let output
93-
try {
94-
output = JSON.parse(execSync(args.join(' ')).toString())
95-
} catch (e) {
96-
let error
97-
try {
98-
error = JSON.parse(e.stdout.toString()).result.message
99-
} catch (e) {
100-
if (e instanceof SyntaxError) {
101-
error = e.stdout.toString()
102-
} else {
103-
error = e.toString()
94+
if (spawnResult.status) {
95+
let errorMessage
96+
if (spawnResult.stderr.toString()) {
97+
errorMessage = spawnResult.stderr.toString()
98+
}
99+
if (spawnResult.stdout.toString()) {
100+
try {
101+
output = JSON.parse(spawnResult.stdout.toString())
102+
errorMessage = output.result.message
103+
} catch (e) {
104+
errorMessage = e.toString()
104105
}
105106
}
106-
throw new Error(`Error executing command with ${args}: ${error}`)
107+
throw new Error(`Error executing command for ${smBinary} with ${args}: ${errorMessage}`)
107108
}
109+
try {
110+
output = JSON.parse(spawnResult.stdout.toString())
111+
} catch (e) {
112+
throw new Error(`Error executing command for ${smBinary} with ${args}: ${e.toString()}`)
113+
}
114+
108115

109116
for (const key in output.logs) {
110117
if (output.logs[key].level === 'WARN') {

0 commit comments

Comments
 (0)