diff --git a/dotnet/src/webdriver/Firefox/FirefoxDriverService.cs b/dotnet/src/webdriver/Firefox/FirefoxDriverService.cs index 7d558a176a1c0..75456f0ee9e14 100644 --- a/dotnet/src/webdriver/Firefox/FirefoxDriverService.cs +++ b/dotnet/src/webdriver/Firefox/FirefoxDriverService.cs @@ -173,7 +173,7 @@ protected override string CommandLineArguments } else { - argsBuilder.Append(string.Format(CultureInfo.InvariantCulture, " --websocket-port {0}", PortUtilities.FindFreePort())); + argsBuilder.Append(" --websocket-port 0"); } if (this.BrowserCommunicationPort > 0) diff --git a/javascript/selenium-webdriver/BUILD.bazel b/javascript/selenium-webdriver/BUILD.bazel index 29f840faa9092..fd9398b76b99f 100644 --- a/javascript/selenium-webdriver/BUILD.bazel +++ b/javascript/selenium-webdriver/BUILD.bazel @@ -216,6 +216,7 @@ SMALL_TESTS = [ "test/bidi/serialization/union_test.js", "test/bidi/serialization/wire_contract_test.js", "test/bidi/vendor_test.js", + "test/firefox/service_test.js", "test/http/http_test.js", "test/http/util_test.js", "test/io/io_test.js", diff --git a/javascript/selenium-webdriver/firefox.js b/javascript/selenium-webdriver/firefox.js index 683a30115eee5..401593fb70a57 100644 --- a/javascript/selenium-webdriver/firefox.js +++ b/javascript/selenium-webdriver/firefox.js @@ -414,22 +414,21 @@ class ServiceBuilder extends remote.DriverService.Builder { } /** - * Overrides the parent build() method to add the websocket port argument - * for Firefox when not connecting to an existing instance. + * Overrides the parent build() method to let Firefox pick the websocket port + * when not connecting to an existing instance and no port was supplied. * * @return {!DriverService} A new driver service instance. */ build() { let port = this.options_.port || findFreePort() - let argsPromise = Promise.resolve(port).then((port) => { + let argsPromise = Promise.all([port, Promise.all(this.options_.args)]).then(([port, userArgs]) => { // 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 - }) + let args = userArgs.concat(`--port=${port}`) + const skipWebSocketPort = userArgs.some( + (arg) => arg === '--connect-existing' || (typeof arg === 'string' && arg.startsWith('--websocket-port')), + ) + if (!skipWebSocketPort) { + args.push('--websocket-port=0') } return args }) diff --git a/javascript/selenium-webdriver/test/firefox/service_test.js b/javascript/selenium-webdriver/test/firefox/service_test.js new file mode 100644 index 0000000000000..0c75357c92042 --- /dev/null +++ b/javascript/selenium-webdriver/test/firefox/service_test.js @@ -0,0 +1,59 @@ +// Licensed to the Software Freedom Conservancy (SFC) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The SFC licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +'use strict' + +const assert = require('node:assert') +const firefox = require('selenium-webdriver/firefox') + +describe('firefox.ServiceBuilder', function () { + function argsFor(builder) { + return builder.setPort(4444).build().args_ + } + + it('lets Firefox choose the websocket port', async function () { + const args = await argsFor(new firefox.ServiceBuilder('geckodriver')) + + assert.deepStrictEqual(args, ['--port=4444', '--websocket-port=0']) + }) + + it('keeps a user-supplied websocket port', async function () { + const args = await argsFor(new firefox.ServiceBuilder('geckodriver').addArguments('--websocket-port=1234')) + + assert.deepStrictEqual(args, ['--websocket-port=1234', '--port=4444']) + }) + + it('keeps a user-supplied websocket port given as a separate value', async function () { + const args = await argsFor(new firefox.ServiceBuilder('geckodriver').addArguments('--websocket-port', '1234')) + + assert.deepStrictEqual(args, ['--websocket-port', '1234', '--port=4444']) + }) + + it('keeps a user-supplied websocket port given as a promise', async function () { + const args = await argsFor( + new firefox.ServiceBuilder('geckodriver').addArguments(Promise.resolve('--websocket-port=1234')), + ) + + assert.deepStrictEqual(args, ['--websocket-port=1234', '--port=4444']) + }) + + it('omits the websocket port when connecting to an existing instance', async function () { + const args = await argsFor(new firefox.ServiceBuilder('geckodriver').addArguments('--connect-existing')) + + assert.deepStrictEqual(args, ['--connect-existing', '--port=4444']) + }) +}) diff --git a/py/selenium/webdriver/firefox/service.py b/py/selenium/webdriver/firefox/service.py index b5fb253b5002d..254ef56bcf982 100644 --- a/py/selenium/webdriver/firefox/service.py +++ b/py/selenium/webdriver/firefox/service.py @@ -21,7 +21,7 @@ from collections.abc import Mapping, Sequence from typing import IO, Any -from selenium.webdriver.common import service, utils +from selenium.webdriver.common import service class Service(service.Service): @@ -76,10 +76,10 @@ def __init__( **kwargs, ) - # Set a port for CDP - if "--connect-existing" not in self._service_args: + has_websocket_port = any(arg.startswith("--websocket-port") for arg in self._service_args) + if "--connect-existing" not in self._service_args and not has_websocket_port: self._service_args.append("--websocket-port") - self._service_args.append(f"{utils.free_port()}") + self._service_args.append("0") def command_line_args(self) -> list[str]: return ["--port", f"{self.port}"] + self._service_args diff --git a/py/test/unit/selenium/webdriver/firefox/firefox_service_tests.py b/py/test/unit/selenium/webdriver/firefox/firefox_service_tests.py index 95b0f80a7c9bf..e9d3b4185de36 100644 --- a/py/test/unit/selenium/webdriver/firefox/firefox_service_tests.py +++ b/py/test/unit/selenium/webdriver/firefox/firefox_service_tests.py @@ -30,3 +30,27 @@ def test_command_line_args(): break assert found, "Provided arguments do not exist in array" + + +def test_lets_firefox_choose_the_websocket_port(): + args = Service().command_line_args() + + assert args[args.index("--websocket-port") + 1] == "0" + + +def test_keeps_user_supplied_websocket_port(): + service = Service(service_args=["--websocket-port", "1234"]) + + assert service.service_args == ["--websocket-port", "1234"] + + +def test_keeps_user_supplied_websocket_port_with_equals(): + service = Service(service_args=["--websocket-port=1234"]) + + assert service.service_args == ["--websocket-port=1234"] + + +def test_omits_websocket_port_when_connecting_to_existing_firefox(): + service = Service(service_args=["--connect-existing"]) + + assert service.service_args == ["--connect-existing"]