diff --git a/javascript/selenium-webdriver/firefox.js b/javascript/selenium-webdriver/firefox.js index f6cd1cf1e9d7e..1633646666078 100644 --- a/javascript/selenium-webdriver/firefox.js +++ b/javascript/selenium-webdriver/firefox.js @@ -121,6 +121,7 @@ const zip = require('./io/zip') const { Browser, Capabilities, Capability } = require('./lib/capabilities') const { Zip } = require('./io/zip') const { getBinaryPaths } = require('./common/driverFinder') +const { findFreePort } = require('./net/portprober') const FIREFOX_CAPABILITY_KEY = 'moz:firefoxOptions' /** @@ -505,6 +506,31 @@ class ServiceBuilder extends remote.DriverService.Builder { enableVerboseLogging(opt_trace) { return this.addArguments(opt_trace ? '-vv' : '-v') } + + /** + * Overrides the parent build() method to add the websocket port argument + * for Firefox when not connecting to an existing instance. + * + * @return {!DriverService} A new driver service instance. + */ + build() { + let port = this.options_.port || findFreePort() + let argsPromise = Promise.resolve(port).then((port) => { + // Start with the default --port argument. + let args = this.options_.args.concat(`--port=${port}`) + // If the "--connect-existing" flag is not set, add the websocket port. + if (!this.options_.args.some((arg) => arg === '--connect-existing')) { + return findFreePort().then((wsPort) => { + args.push(`--websocket-port=${wsPort}`) + return args + }) + } + return args + }) + + let options = Object.assign({}, this.options_, { args: argsPromise, port }) + return new remote.DriverService(this.exe_, options) + } } /**