Skip to content

Commit ec54309

Browse files
authored
[js] Making SeleniumManager a thin wrapper (#13853)
* [js] Making SeleniumManager a thin wrapper * [js] Fixing imports and js doc * [js] Removing unused import
1 parent e85bf8d commit ec54309

File tree

9 files changed

+77
-69
lines changed

9 files changed

+77
-69
lines changed

javascript/node/selenium-webdriver/chromium.js

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ const error = require('./lib/error')
7878
const Symbols = require('./lib/symbols')
7979
const webdriver = require('./lib/webdriver')
8080
const remote = require('./remote')
81-
const { getPath } = require('./common/driverFinder')
81+
const { getBinaryPaths } = require('./common/driverFinder')
8282

8383
/**
8484
* Custom command names supported by Chromium WebDriver.
@@ -622,14 +622,17 @@ class Driver extends webdriver.WebDriver {
622622
} else {
623623
let service = opt_serviceExecutor || this.getDefaultService()
624624
if (!service.getExecutable()) {
625-
const { driverPath, browserPath } = getPath(caps)
625+
const { driverPath, browserPath } = getBinaryPaths(caps)
626626
service.setExecutable(driverPath)
627-
const vendorOptions = caps.get(vendorCapabilityKey)
628-
if (vendorOptions) {
629-
vendorOptions['binary'] = browserPath
630-
caps.set(vendorCapabilityKey, vendorOptions)
631-
} else {
632-
caps.set(vendorCapabilityKey, { binary: browserPath })
627+
if (browserPath) {
628+
const vendorOptions = caps.get(vendorCapabilityKey)
629+
if (vendorOptions) {
630+
vendorOptions['binary'] = browserPath
631+
caps.set(vendorCapabilityKey, vendorOptions)
632+
} else {
633+
caps.set(vendorCapabilityKey, { binary: browserPath })
634+
}
635+
caps.delete(Capability.BROWSER_VERSION)
633636
}
634637
}
635638
onQuit = () => service.kill()
@@ -743,7 +746,7 @@ class Driver extends webdriver.WebDriver {
743746
* Set a permission state to the given value.
744747
*
745748
* @param {string} name A name of the permission to update.
746-
* @param {('granted'|'denied'|'prompt')} state State to set permission to.
749+
* @param {("granted"|"denied"|"prompt")} state State to set permission to.
747750
* @returns {!Promise<Object>} A promise that will be resolved when the
748751
* command has finished.
749752
* @see <https://w3c.github.io/permissions/#permission-registry> for valid

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

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,19 @@
2121
* Utility to find if a given file is present and executable.
2222
*/
2323

24-
const { driverLocation } = require('./seleniumManager')
24+
const path = require('node:path')
25+
const { binaryPaths } = require('./seleniumManager')
2526

2627
/**
2728
* Determines the path of the correct Selenium Manager binary
29+
* @param {Capabilities} capabilities browser options to fetch the driver
2830
* @returns {{browserPath: string, driverPath: string}} path of the driver
2931
* and browser location
3032
*/
31-
function getPath(capabilities) {
33+
function getBinaryPaths(capabilities) {
3234
try {
33-
return driverLocation(capabilities)
35+
const args = getArgs(capabilities)
36+
return binaryPaths(args)
3437
} catch (e) {
3538
throw Error(
3639
`Unable to obtain browser driver.
@@ -40,5 +43,34 @@ function getPath(capabilities) {
4043
}
4144
}
4245

46+
function getArgs(options) {
47+
let args = ['--browser', options.getBrowserName(), '--language-binding', 'javascript', '--output', 'json']
48+
49+
if (options.getBrowserVersion() && options.getBrowserVersion() !== '') {
50+
args.push('--browser-version', options.getBrowserVersion())
51+
}
52+
53+
const vendorOptions =
54+
options.get('goog:chromeOptions') || options.get('ms:edgeOptions') || options.get('moz:firefoxOptions')
55+
if (vendorOptions && vendorOptions.binary && vendorOptions.binary !== '') {
56+
args.push('--browser-path', path.resolve(vendorOptions.binary))
57+
}
58+
59+
const proxyOptions = options.getProxy()
60+
61+
// Check if proxyOptions exists and has properties
62+
if (proxyOptions && Object.keys(proxyOptions).length > 0) {
63+
const httpProxy = proxyOptions['httpProxy']
64+
const sslProxy = proxyOptions['sslProxy']
65+
66+
if (httpProxy !== undefined) {
67+
args.push('--proxy', httpProxy)
68+
} else if (sslProxy !== undefined) {
69+
args.push('--proxy', sslProxy)
70+
}
71+
}
72+
return args
73+
}
74+
4375
// PUBLIC API
44-
module.exports = { getPath }
76+
module.exports = { getBinaryPaths }

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

Lines changed: 3 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ const { platform } = require('node:process')
2525
const path = require('node:path')
2626
const fs = require('node:fs')
2727
const spawnSync = require('node:child_process').spawnSync
28-
const { Capability } = require('../lib/capabilities')
2928
const logging = require('../lib/logging')
3029

3130
const log_ = logging.getLogger(logging.Type.DRIVER)
@@ -63,38 +62,12 @@ function getBinary() {
6362

6463
/**
6564
* Determines the path of the correct driver
66-
* @param {Capabilities} options browser options to fetch the driver
65+
* @param {string[]} args arguments to invoke Selenium Manager
6766
* @returns {{browserPath: string, driverPath: string}} path of the driver and
6867
* browser location
6968
*/
7069

71-
function driverLocation(options) {
72-
let args = ['--browser', options.getBrowserName(), '--language-binding', 'javascript', '--output', 'json']
73-
74-
if (options.getBrowserVersion() && options.getBrowserVersion() !== '') {
75-
args.push('--browser-version', options.getBrowserVersion())
76-
}
77-
78-
const vendorOptions =
79-
options.get('goog:chromeOptions') || options.get('ms:edgeOptions') || options.get('moz:firefoxOptions')
80-
if (vendorOptions && vendorOptions.binary && vendorOptions.binary !== '') {
81-
args.push('--browser-path', path.resolve(vendorOptions.binary))
82-
}
83-
84-
const proxyOptions = options.getProxy()
85-
86-
// Check if proxyOptions exists and has properties
87-
if (proxyOptions && Object.keys(proxyOptions).length > 0) {
88-
const httpProxy = proxyOptions['httpProxy']
89-
const sslProxy = proxyOptions['sslProxy']
90-
91-
if (httpProxy !== undefined) {
92-
args.push('--proxy', httpProxy)
93-
} else if (sslProxy !== undefined) {
94-
args.push('--proxy', sslProxy)
95-
}
96-
}
97-
70+
function binaryPaths(args) {
9871
const smBinary = getBinary()
9972
const spawnResult = spawnSync(smBinary, args)
10073
let output
@@ -120,11 +93,6 @@ function driverLocation(options) {
12093
throw new Error(`Error executing command for ${smBinary} with ${args}: ${e.toString()}`)
12194
}
12295

123-
// Once driverPath is available, delete browserVersion from payload
124-
if (output.result.driver_path) {
125-
options.delete(Capability.BROWSER_VERSION)
126-
}
127-
12896
logOutput(output)
12997
return {
13098
driverPath: output.result.driver_path,
@@ -144,4 +112,4 @@ function logOutput(output) {
144112
}
145113

146114
// PUBLIC API
147-
module.exports = { driverLocation }
115+
module.exports = { binaryPaths }

javascript/node/selenium-webdriver/firefox.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,9 @@ const io = require('./io')
116116
const remote = require('./remote')
117117
const webdriver = require('./lib/webdriver')
118118
const zip = require('./io/zip')
119-
const { Browser, Capabilities } = require('./lib/capabilities')
119+
const { Browser, Capabilities, Capability } = require('./lib/capabilities')
120120
const { Zip } = require('./io/zip')
121-
const { getPath } = require('./common/driverFinder')
121+
const { getBinaryPaths } = require('./common/driverFinder')
122122
const FIREFOX_CAPABILITY_KEY = 'moz:firefoxOptions'
123123

124124
/**
@@ -541,7 +541,7 @@ class Driver extends webdriver.WebDriver {
541541
configureExecutor(executor)
542542
} else if (opt_executor instanceof remote.DriverService) {
543543
if (!opt_executor.getExecutable()) {
544-
const { driverPath, browserPath } = getPath(caps)
544+
const { driverPath, browserPath } = getBinaryPaths(caps)
545545
opt_executor.setExecutable(driverPath)
546546
firefoxBrowserPath = browserPath
547547
}
@@ -550,7 +550,7 @@ class Driver extends webdriver.WebDriver {
550550
} else {
551551
let service = new ServiceBuilder().build()
552552
if (!service.getExecutable()) {
553-
const { driverPath, browserPath } = getPath(caps)
553+
const { driverPath, browserPath } = getBinaryPaths(caps)
554554
service.setExecutable(driverPath)
555555
firefoxBrowserPath = browserPath
556556
}
@@ -566,6 +566,7 @@ class Driver extends webdriver.WebDriver {
566566
} else {
567567
caps.set(FIREFOX_CAPABILITY_KEY, { binary: firefoxBrowserPath })
568568
}
569+
caps.delete(Capability.BROWSER_VERSION)
569570
}
570571

571572
return /** @type {!Driver} */ (super.createSession(executor, caps, onQuit))

javascript/node/selenium-webdriver/ie.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ const remote = require('./remote')
3333
const webdriver = require('./lib/webdriver')
3434
const { Browser, Capabilities } = require('./lib/capabilities')
3535
const error = require('./lib/error')
36-
const { getPath } = require('./common/driverFinder')
36+
const { getBinaryPaths } = require('./common/driverFinder')
3737

3838
const OPTIONS_CAPABILITY_KEY = 'se:ieOptions'
3939
const SCROLL_BEHAVIOUR = {
@@ -223,7 +223,9 @@ class Options extends Capabilities {
223223

224224
addBrowserCommandSwitches(...args) {
225225
let current = this.options_[Key.BROWSER_COMMAND_LINE_SWITCHES] || []
226-
if (typeof current == 'string') current = current.split(' ')
226+
if (typeof current == 'string') {
227+
current = current.split(' ')
228+
}
227229
this.options_[Key.BROWSER_COMMAND_LINE_SWITCHES] = current.concat(args).join(' ')
228230
return this
229231
}
@@ -239,7 +241,9 @@ class Options extends Capabilities {
239241

240242
addArguments(...args) {
241243
let current = this.options_[Key.BROWSER_COMMAND_LINE_SWITCHES] || []
242-
if (typeof current == 'string') current = current.split(' ')
244+
if (typeof current == 'string') {
245+
current = current.split(' ')
246+
}
243247
this.options_[Key.BROWSER_COMMAND_LINE_SWITCHES] = current.concat(args).join(' ')
244248
return this
245249
}
@@ -447,7 +451,7 @@ class Driver extends webdriver.WebDriver {
447451
service = createServiceFromCapabilities(options)
448452
}
449453
if (!service.getExecutable()) {
450-
service.setExecutable(getPath(options).driverPath)
454+
service.setExecutable(getBinaryPaths(options).driverPath)
451455
}
452456

453457
let client = service.start().then((url) => new http.HttpClient(url))

javascript/node/selenium-webdriver/safari.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ const http = require('./http')
2525
const remote = require('./remote')
2626
const webdriver = require('./lib/webdriver')
2727
const { Browser, Capabilities } = require('./lib/capabilities')
28-
const { getPath } = require('./common/driverFinder')
28+
const { getBinaryPaths } = require('./common/driverFinder')
2929

3030
/**
31-
* Creates {@link selenium-webdriver/remote.DriverService} instances that manage
31+
* Creates {@link remote.DriverService} instances that manage
3232
* a [safaridriver] server in a child process.
3333
*
3434
* [safaridriver]: https://developer.apple.com/library/prerelease/content/releasenotes/General/WhatsNewInSafari/Articles/Safari_10_0.html#//apple_ref/doc/uid/TP40014305-CH11-DontLinkElementID_28
@@ -123,7 +123,7 @@ class Driver extends webdriver.WebDriver {
123123

124124
let service = new ServiceBuilder(exe).build()
125125
if (!service.getExecutable()) {
126-
service.setExecutable(getPath(caps).driverPath)
126+
service.setExecutable(getBinaryPaths(caps).driverPath)
127127
}
128128
let executor = new http.Executor(service.start().then((url) => new http.HttpClient(url)))
129129

javascript/node/selenium-webdriver/test/chrome/service_test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
const assert = require('node:assert')
2121
const chrome = require('../../chrome')
2222
const test = require('../../lib/test')
23-
const { getPath } = require('../../common/driverFinder')
23+
const { getBinaryPaths } = require('../../common/driverFinder')
2424

2525
test.suite(
2626
function (_env) {
@@ -35,7 +35,7 @@ test.suite(
3535
it('can be started on a custom path', function () {
3636
service = new chrome.ServiceBuilder().setPath('/foo/bar/baz').build()
3737
if (!service.getExecutable()) {
38-
service.setExecutable(getPath(new chrome.Options()).driverPath)
38+
service.setExecutable(getBinaryPaths(new chrome.Options()).driverPath)
3939
}
4040
return service.start().then(function (url) {
4141
assert.ok(url.endsWith('/foo/bar/baz'), 'unexpected url: ' + url)

javascript/node/selenium-webdriver/test/edge/service_test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
const assert = require('node:assert')
2121
const edge = require('../../edge')
2222
const test = require('../../lib/test')
23-
const { getPath } = require('../../common/driverFinder')
23+
const { getBinaryPaths } = require('../../common/driverFinder')
2424

2525
test.suite(
2626
function (_env) {
@@ -35,7 +35,7 @@ test.suite(
3535

3636
it('can start msedgedriver', async function () {
3737
service = new edge.ServiceBuilder().build()
38-
service.setExecutable(getPath(new edge.Options()).driverPath)
38+
service.setExecutable(getBinaryPaths(new edge.Options()).driverPath)
3939
let url = await service.start()
4040
assert(/127\.0\.0\.1/.test(url), `unexpected url: ${url}`)
4141
})

javascript/node/selenium-webdriver/testing/index.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ const remote = require('../remote')
4141
const safari = require('../safari')
4242
const { Browser } = require('../lib/capabilities')
4343
const { Builder } = require('../index')
44-
const { getPath } = require('../common/driverFinder')
44+
const { getBinaryPaths } = require('../common/driverFinder')
4545

4646
/**
4747
* Describes a browser targeted by a {@linkplain suite test suite}.
@@ -122,15 +122,15 @@ function getAvailableBrowsers() {
122122
info(`Searching for WebDriver executables installed on the current system...`)
123123

124124
let targets = [
125-
[getPath(new chrome.Options()), Browser.CHROME],
126-
[getPath(new edge.Options()), Browser.EDGE],
127-
[getPath(new firefox.Options()), Browser.FIREFOX],
125+
[getBinaryPaths(new chrome.Options()), Browser.CHROME],
126+
[getBinaryPaths(new edge.Options()), Browser.EDGE],
127+
[getBinaryPaths(new firefox.Options()), Browser.FIREFOX],
128128
]
129129
if (process.platform === 'win32') {
130-
targets.push([getPath(new ie.Options()), Browser.INTERNET_EXPLORER])
130+
targets.push([getBinaryPaths(new ie.Options()), Browser.INTERNET_EXPLORER])
131131
}
132132
if (process.platform === 'darwin') {
133-
targets.push([getPath(new safari.Options()), Browser.SAFARI])
133+
targets.push([getBinaryPaths(new safari.Options()), Browser.SAFARI])
134134
}
135135

136136
let availableBrowsers = []

0 commit comments

Comments
 (0)