Skip to content

Commit a07d1fd

Browse files
authored
[javascript] Add browser output from Selenium Manager to options (#12411)
* [javascript] Add browser output from Selenium Manager to options * [javascript] Seems Chrome PDF tests do not need to be using headless anymore
1 parent 0fa6c3f commit a07d1fd

File tree

12 files changed

+66
-64
lines changed

12 files changed

+66
-64
lines changed

javascript/node/selenium-webdriver/chrome.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@
130130
const io = require('./io')
131131
const { Browser } = require('./lib/capabilities')
132132
const chromium = require('./chromium')
133+
const CHROME_CAPABILITY_KEY = 'goog:chromeOptions'
133134

134135
/** @type {remote.DriverService} */
135136

@@ -220,7 +221,7 @@ class Driver extends chromium.Driver {
220221
static createSession(opt_config, opt_serviceExecutor) {
221222
let caps = opt_config || new Options()
222223
return /** @type {!Driver} */ (
223-
super.createSession(caps, opt_serviceExecutor)
224+
super.createSession(caps, opt_serviceExecutor, 'goog', CHROME_CAPABILITY_KEY)
224225
)
225226
}
226227

@@ -233,13 +234,12 @@ class Driver extends chromium.Driver {
233234
}
234235
}
235236

236-
Options.prototype.CAPABILITY_KEY = 'goog:chromeOptions'
237+
Options.prototype.CAPABILITY_KEY = CHROME_CAPABILITY_KEY
237238
Options.prototype.BROWSER_NAME_VALUE = Browser.CHROME
238-
Driver.prototype.VENDOR_COMMAND_PREFIX = 'goog'
239239

240240
// PUBLIC API
241241
module.exports = {
242-
Driver: Driver,
242+
Driver,
243243
Options,
244244
ServiceBuilder,
245245
}

javascript/node/selenium-webdriver/chromium.js

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -669,27 +669,38 @@ class Driver extends webdriver.WebDriver {
669669
/**
670670
* Creates a new session with the WebDriver server.
671671
*
672-
* @param {(Capabilities|Options)=} opt_config The configuration options.
672+
* @param {(Capabilities|Options)=} caps The configuration options.
673673
* @param {(remote.DriverService|http.Executor)=} opt_serviceExecutor Either
674674
* a DriverService to use for the remote end, or a preconfigured executor
675675
* for an externally managed endpoint. If neither is provided, the
676676
* {@linkplain ##getDefaultService default service} will be used by
677677
* default.
678+
* @param vendorPrefix Either 'goog' or 'ms'
679+
* @param vendorCapabilityKey Either 'goog:chromeOptions' or 'ms:edgeOptions'
678680
* @return {!Driver} A new driver instance.
679681
*/
680-
static createSession(caps, opt_serviceExecutor) {
682+
static createSession(caps, opt_serviceExecutor,
683+
vendorPrefix = '', vendorCapabilityKey = '') {
681684
let executor
682685
let onQuit
683686
if (opt_serviceExecutor instanceof http.Executor) {
684687
executor = opt_serviceExecutor
685-
configureExecutor(executor, this.VENDOR_COMMAND_PREFIX)
688+
configureExecutor(executor, vendorPrefix)
686689
} else {
687690
let service = opt_serviceExecutor || this.getDefaultService()
688691
if (!service.getExecutable()) {
689-
service.setExecutable(getPath(caps))
692+
const {driverPath, browserPath} = getPath(caps)
693+
service.setExecutable(driverPath)
694+
const vendorOptions = caps.get(vendorCapabilityKey)
695+
if (vendorOptions) {
696+
vendorOptions['binary'] = browserPath
697+
caps.set(vendorCapabilityKey, vendorOptions)
698+
} else {
699+
caps.set(vendorCapabilityKey, {binary: browserPath})
700+
}
690701
}
691702
onQuit = () => service.kill()
692-
executor = createExecutor(service.start(), this.VENDOR_COMMAND_PREFIX)
703+
executor = createExecutor(service.start(), vendorPrefix)
693704
}
694705

695706
// W3C spec requires noProxy value to be an array of strings, but Chromium

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ const { driverLocation } = require('./seleniumManager')
2525

2626
/**
2727
* Determines the path of the correct Selenium Manager binary
28-
* @returns {string}
28+
* @returns {{browserPath: string, driverPath: string}} path of the driver
29+
* and browser location
2930
*/
3031
function getPath(capabilities) {
3132
try {

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ function getBinary() {
6262
/**
6363
* Determines the path of the correct driver
6464
* @param {Capabilities} options browser options to fetch the driver
65-
* @returns {string} path of the driver location
65+
* @returns {{browserPath: string, driverPath: string}} path of the driver and
66+
* browser location
6667
*/
6768

6869
function driverLocation(options) {
@@ -126,8 +127,10 @@ function driverLocation(options) {
126127
}
127128

128129
logOutput(output)
129-
130-
return output.result.message
130+
return {
131+
driverPath: output.result.driver_path,
132+
browserPath: output.result.browser_path,
133+
}
131134
}
132135

133136
function logOutput (output) {

javascript/node/selenium-webdriver/edge.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@
7979

8080
const { Browser } = require('./lib/capabilities')
8181
const chromium = require('./chromium')
82+
const EDGE_CAPABILITY_KEY = 'ms:edgeOptions'
8283

8384
/** @type {remote.DriverService} */
8485

@@ -147,7 +148,7 @@ class Driver extends chromium.Driver {
147148
static createSession(opt_config, opt_serviceExecutor) {
148149
let caps = opt_config || new Options()
149150
return /** @type {!Driver} */ (
150-
super.createSession(caps, opt_serviceExecutor)
151+
super.createSession(caps, opt_serviceExecutor, 'ms', EDGE_CAPABILITY_KEY)
151152
)
152153
}
153154

@@ -168,8 +169,7 @@ class Driver extends chromium.Driver {
168169
}
169170

170171
Options.prototype.BROWSER_NAME_VALUE = Browser.EDGE
171-
Options.prototype.CAPABILITY_KEY = 'ms:edgeOptions'
172-
Driver.prototype.VENDOR_CAPABILITY_PREFIX = 'ms'
172+
Options.prototype.CAPABILITY_KEY = EDGE_CAPABILITY_KEY
173173

174174
// PUBLIC API
175175

javascript/node/selenium-webdriver/firefox.js

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ const zip = require('./io/zip')
128128
const { Browser, Capabilities } = require('./lib/capabilities')
129129
const { Zip } = require('./io/zip')
130130
const { getPath } = require('./common/driverFinder')
131+
const FIREFOX_CAPABILITY_KEY = 'moz:firefoxOptions'
131132

132133
/**
133134
* Thrown when there an add-on is malformed.
@@ -263,10 +264,10 @@ class Options extends Capabilities {
263264
* @private
264265
*/
265266
firefoxOptions_() {
266-
let options = this.get('moz:firefoxOptions')
267+
let options = this.get(FIREFOX_CAPABILITY_KEY)
267268
if (!options) {
268269
options = {}
269-
this.set('moz:firefoxOptions', options)
270+
this.set(FIREFOX_CAPABILITY_KEY, options)
270271
}
271272
return options
272273
}
@@ -580,6 +581,8 @@ class Driver extends webdriver.WebDriver {
580581
let caps =
581582
opt_config instanceof Capabilities ? opt_config : new Options(opt_config)
582583

584+
let firefoxBrowserPath = null
585+
583586
let executor
584587
let onQuit
585588

@@ -588,19 +591,33 @@ class Driver extends webdriver.WebDriver {
588591
configureExecutor(executor)
589592
} else if (opt_executor instanceof remote.DriverService) {
590593
if (!opt_executor.getExecutable()) {
591-
opt_executor.setExecutable(getPath(opt_config))
594+
const {driverPath, browserPath} = getPath(caps)
595+
opt_executor.setExecutable(driverPath)
596+
firefoxBrowserPath = browserPath
592597
}
593598
executor = createExecutor(opt_executor.start())
594599
onQuit = () => opt_executor.kill()
595600
} else {
596601
let service = new ServiceBuilder().build()
597602
if (!service.getExecutable()) {
598-
service.setExecutable(getPath(opt_config))
603+
const {driverPath, browserPath} = getPath(caps)
604+
service.setExecutable(driverPath)
605+
firefoxBrowserPath = browserPath
599606
}
600607
executor = createExecutor(service.start())
601608
onQuit = () => service.kill()
602609
}
603610

611+
if (firefoxBrowserPath) {
612+
const vendorOptions = caps.get(FIREFOX_CAPABILITY_KEY)
613+
if (vendorOptions) {
614+
vendorOptions['binary'] = firefoxBrowserPath
615+
caps.set(FIREFOX_CAPABILITY_KEY, vendorOptions)
616+
} else {
617+
caps.set(FIREFOX_CAPABILITY_KEY, {binary: firefoxBrowserPath})
618+
}
619+
}
620+
604621
return /** @type {!Driver} */ (super.createSession(executor, caps, onQuit))
605622
}
606623

javascript/node/selenium-webdriver/ie.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,7 @@ class Driver extends webdriver.WebDriver {
455455
service = createServiceFromCapabilities(options)
456456
}
457457
if (!service.getExecutable()) {
458-
service.setExecutable(getPath(options))
458+
service.setExecutable(getPath(options).driverPath)
459459
}
460460

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

javascript/node/selenium-webdriver/safari.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ 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')
2829

2930
/**
3031
* Creates {@link selenium-webdriver/remote.DriverService} instances that manage
@@ -43,7 +44,7 @@ class ServiceBuilder extends remote.DriverService.Builder {
4344
}
4445
}
4546

46-
const OPTIONS_CAPABILITY_KEY = 'safari.options'
47+
const OPTIONS_CAPABILITY_KEY = 'safari:options'
4748
const TECHNOLOGY_PREVIEW_OPTIONS_KEY = 'technologyPreview'
4849

4950
/**
@@ -122,6 +123,9 @@ class Driver extends webdriver.WebDriver {
122123
}
123124

124125
let service = new ServiceBuilder(exe).build()
126+
if (!service.getExecutable()) {
127+
service.setExecutable(getPath(caps).driverPath)
128+
}
125129
let executor = new http.Executor(
126130
service.start().then((url) => new http.HttpClient(url))
127131
)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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()))
38+
service.setExecutable(getPath(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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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()))
38+
service.setExecutable(getPath(new edge.Options()).driverPath)
3939
let url = await service.start()
4040
assert(/127\.0\.0\.1/.test(url), `unexpected url: ${url}`)
4141
})

0 commit comments

Comments
 (0)