Skip to content

Commit 46d3468

Browse files
committed
Accept a custom geckodriver port
By default geckodriver uses a random port. This change allows passing a custom port number in `geckodriver_port`.
2 parents f1ddbbb + c436104 commit 46d3468

2 files changed

Lines changed: 20 additions & 3 deletions

File tree

tbselenium/tbdriver.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ def __init__(self,
3939
default_bridge_type="",
4040
headless=False,
4141
options=None,
42-
use_custom_profile=False
42+
use_custom_profile=False,
43+
geckodriver_port=0 # by default a random port will be used
4344
):
4445

4546
# use_custom_profile: whether to launch from and *write to* the given
@@ -79,12 +80,14 @@ def __init__(self,
7980
tbb_service = Service(
8081
executable_path=executable_path,
8182
log_path=tbb_logfile_path,
82-
service_args=["--marionette-port", "2828"]
83+
service_args=["--marionette-port", "2828"],
84+
port=geckodriver_port
8385
)
8486
else:
8587
tbb_service = Service(
8688
executable_path=executable_path,
87-
log_path=tbb_logfile_path
89+
log_path=tbb_logfile_path,
90+
port=geckodriver_port
8891
)
8992
self.options.binary = self.tbb_fx_binary_path
9093
self.options.add_argument('--class')

tbselenium/test/test_tbdriver.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
from tbselenium import common as cm
1111
from tbselenium.test import TBB_PATH
1212
from tbselenium.test.fixtures import TBDriverFixture
13+
from selenium.webdriver.common.utils import free_port
14+
from tbselenium.utils import is_busy
1315

1416

1517
class TBDriverTest(unittest.TestCase):
@@ -126,5 +128,17 @@ def test_custom_profile_and_binary(self):
126128
self.assertEqual(mod_time_before, mod_time_after)
127129

128130

131+
class TBDriverCustomGeckoDriverPort(unittest.TestCase):
132+
133+
def test_should_accept_custom_geckodriver_port(self):
134+
"""Make sure we accept a custom port number to run geckodriver on."""
135+
random_port = free_port()
136+
with TBDriverFixture(TBB_PATH, geckodriver_port=random_port) as driver:
137+
driver.load_url_ensure(cm.ABOUT_TOR_URL)
138+
self.assertTrue(is_busy(random_port)) # check if the port is used
139+
# check if the port is closed after we quit
140+
self.assertFalse(is_busy(random_port))
141+
142+
129143
if __name__ == "__main__":
130144
unittest.main()

0 commit comments

Comments
 (0)