Skip to content

Commit 6bce984

Browse files
committed
Add options to set custom Firefox args and preferences
1 parent 91ca4a2 commit 6bce984

File tree

4 files changed

+143
-2
lines changed

4 files changed

+143
-2
lines changed

seleniumbase/core/browser_launcher.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,8 @@ def _set_firefox_options(
375375
proxy_string,
376376
user_agent,
377377
disable_csp,
378+
firefox_arg,
379+
firefox_pref,
378380
):
379381
options = webdriver.FirefoxOptions()
380382
options.accept_untrusted_certs = True
@@ -466,6 +468,51 @@ def _set_firefox_options(
466468
"vnd.openxmlformats-officedocument.spreadsheetml.sheet"
467469
),
468470
)
471+
if firefox_arg:
472+
# Can be a comma-separated list of Firefox args
473+
firefox_arg_list = firefox_arg.split(",")
474+
for firefox_arg_item in firefox_arg_list:
475+
firefox_arg_item = firefox_arg_item.strip()
476+
if not firefox_arg_item.startswith("--"):
477+
if firefox_arg_item.startswith("-"):
478+
firefox_arg_item = "-" + firefox_arg_item
479+
else:
480+
firefox_arg_item = "--" + firefox_arg_item
481+
if len(firefox_arg_item) >= 3:
482+
options.add_argument(firefox_arg_item)
483+
if firefox_pref:
484+
# Can be a comma-separated list of Firefox preference:value pairs
485+
firefox_pref_list = firefox_pref.split(",")
486+
for firefox_pref_item in firefox_pref_list:
487+
f_pref = None
488+
f_pref_value = None
489+
needs_conversion = False
490+
if firefox_pref_item.count(":") == 0:
491+
f_pref = firefox_pref_item
492+
f_pref_value = True
493+
elif firefox_pref_item.count(":") == 1:
494+
f_pref = firefox_pref_item.split(":")[0]
495+
f_pref_value = firefox_pref_item.split(":")[1]
496+
needs_conversion = True
497+
else: # More than one ":" in the set. (Too many!)
498+
raise Exception(
499+
'Incorrect formatting for Firefox "pref:value" set!'
500+
)
501+
f_pref = firefox_pref_item.strip()
502+
if needs_conversion:
503+
f_pref_value = firefox_pref_item.strip()
504+
if f_pref_value.lower == "true" or len(f_pref_value) == 0:
505+
f_pref_value = True
506+
elif f_pref_value.lower == "false":
507+
f_pref_value = False
508+
elif f_pref_value.isdigit():
509+
f_pref_value = int(f_pref_value)
510+
elif f_pref_value.isdecimal():
511+
f_pref_value = float(f_pref_value)
512+
else:
513+
pass # keep as string
514+
if len(f_pref) >= 1:
515+
options.set_preference(f_pref, f_pref_value)
469516
return options
470517

471518

@@ -556,6 +603,8 @@ def get_driver(
556603
swiftshader=None,
557604
block_images=None,
558605
chromium_arg=None,
606+
firefox_arg=None,
607+
firefox_pref=None,
559608
user_data_dir=None,
560609
extension_zip=None,
561610
extension_dir=None,
@@ -626,6 +675,8 @@ def get_driver(
626675
swiftshader,
627676
block_images,
628677
chromium_arg,
678+
firefox_arg,
679+
firefox_pref,
629680
user_data_dir,
630681
extension_zip,
631682
extension_dir,
@@ -659,6 +710,8 @@ def get_driver(
659710
swiftshader,
660711
block_images,
661712
chromium_arg,
713+
firefox_arg,
714+
firefox_pref,
662715
user_data_dir,
663716
extension_zip,
664717
extension_dir,
@@ -696,6 +749,8 @@ def get_remote_driver(
696749
swiftshader,
697750
block_images,
698751
chromium_arg,
752+
firefox_arg,
753+
firefox_pref,
699754
user_data_dir,
700755
extension_zip,
701756
extension_dir,
@@ -783,6 +838,8 @@ def get_remote_driver(
783838
proxy_string,
784839
user_agent,
785840
disable_csp,
841+
firefox_arg,
842+
firefox_pref,
786843
)
787844
capabilities = firefox_options.to_capabilities()
788845
capabilities["marionette"] = True
@@ -907,6 +964,8 @@ def get_local_driver(
907964
swiftshader,
908965
block_images,
909966
chromium_arg,
967+
firefox_arg,
968+
firefox_pref,
910969
user_data_dir,
911970
extension_zip,
912971
extension_dir,
@@ -929,6 +988,8 @@ def get_local_driver(
929988
proxy_string,
930989
user_agent,
931990
disable_csp,
991+
firefox_arg,
992+
firefox_pref,
932993
)
933994
if LOCAL_GECKODRIVER and os.path.exists(LOCAL_GECKODRIVER):
934995
try:

seleniumbase/fixtures/base_case.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2227,10 +2227,12 @@ def get_new_driver(
22272227
swiftshader=None,
22282228
block_images=None,
22292229
chromium_arg=None,
2230+
firefox_arg=None,
2231+
firefox_pref=None,
22302232
user_data_dir=None,
22312233
extension_zip=None,
22322234
extension_dir=None,
2233-
is_mobile=False,
2235+
is_mobile=None,
22342236
d_width=None,
22352237
d_height=None,
22362238
d_p_r=None,
@@ -2263,6 +2265,8 @@ def get_new_driver(
22632265
swiftshader - the option to use Chrome's swiftshader (Chrome-only)
22642266
block_images - the option to block images from loading (Chrome)
22652267
chromium_arg - the option to add a Chromium arg to Chrome/Edge
2268+
firefox_arg - the option to add a Firefox arg to Firefox runs
2269+
firefox_pref - the option to add a Firefox pref:value set (Firefox)
22662270
user_data_dir - Chrome's User Data Directory to use (Chrome-only)
22672271
extension_zip - A Chrome Extension ZIP file to use (Chrome-only)
22682272
extension_dir - A Chrome Extension folder to use (Chrome-only)
@@ -2346,6 +2350,10 @@ def get_new_driver(
23462350
block_images = self.block_images
23472351
if chromium_arg is None:
23482352
chromium_arg = self.chromium_arg
2353+
if firefox_arg is None:
2354+
firefox_arg = self.firefox_arg
2355+
if firefox_pref is None:
2356+
firefox_pref = self.firefox_pref
23492357
if user_data_dir is None:
23502358
user_data_dir = self.user_data_dir
23512359
if extension_zip is None:
@@ -2358,7 +2366,7 @@ def get_new_driver(
23582366
if cap_string is None:
23592367
cap_string = self.cap_string
23602368
if is_mobile is None:
2361-
is_mobile = False
2369+
is_mobile = self.mobile_emulator
23622370
if d_width is None:
23632371
d_width = self.__device_width
23642372
if d_height is None:
@@ -2399,6 +2407,8 @@ def get_new_driver(
23992407
swiftshader=swiftshader,
24002408
block_images=block_images,
24012409
chromium_arg=chromium_arg,
2410+
firefox_arg=firefox_arg,
2411+
firefox_pref=firefox_pref,
24022412
user_data_dir=user_data_dir,
24032413
extension_zip=extension_zip,
24042414
extension_dir=extension_dir,
@@ -8388,6 +8398,8 @@ def setUp(self, masterqa_mode=False):
83888398
self.ad_block_on = sb_config.ad_block_on
83898399
self.block_images = sb_config.block_images
83908400
self.chromium_arg = sb_config.chromium_arg
8401+
self.firefox_arg = sb_config.firefox_arg
8402+
self.firefox_pref = sb_config.firefox_pref
83918403
self.verify_delay = sb_config.verify_delay
83928404
self.disable_csp = sb_config.disable_csp
83938405
self.disable_ws = sb_config.disable_ws
@@ -8631,6 +8643,8 @@ def setUp(self, masterqa_mode=False):
86318643
swiftshader=self.swiftshader,
86328644
block_images=self.block_images,
86338645
chromium_arg=self.chromium_arg,
8646+
firefox_arg=self.firefox_arg,
8647+
firefox_pref=self.firefox_pref,
86348648
user_data_dir=self.user_data_dir,
86358649
extension_zip=self.extension_zip,
86368650
extension_dir=self.extension_dir,

seleniumbase/plugins/pytest_plugin.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ def pytest_addoption(parser):
4444
--mobile (Use the mobile device emulator while running tests.)
4545
--metrics=STRING (Set mobile metrics: "CSSWidth,CSSHeight,PixelRatio".)
4646
--chromium-arg=ARG (Add a Chromium arg for Chrome/Edge, comma-separated.)
47+
--firefox-arg=ARG (Add a Firefox arg for Firefox, comma-separated.)
48+
--firefox-pref=SET (Set a Firefox preference:value set, comma-separated.)
4749
--extension-zip=ZIP (Load a Chrome Extension .zip|.crx, comma-separated.)
4850
--extension-dir=DIR (Load a Chrome Extension directory, comma-separated.)
4951
--headless (Run tests headlessly. Default mode on Linux OS.)
@@ -422,6 +424,35 @@ def pytest_addoption(parser):
422424
added to the beginning of the arg automatically.
423425
Default: None.""",
424426
)
427+
parser.addoption(
428+
"--firefox_arg",
429+
"--firefox-arg",
430+
action="store",
431+
dest="firefox_arg",
432+
default=None,
433+
help="""Add a Firefox argument for Firefox browser runs.
434+
Format: A comma-separated list of Firefox args.
435+
If an arg doesn't start with "--", that will be
436+
added to the beginning of the arg automatically.
437+
Default: None.""",
438+
)
439+
parser.addoption(
440+
"--firefox_pref",
441+
"--firefox-pref",
442+
action="store",
443+
dest="firefox_pref",
444+
default=None,
445+
help="""Set a Firefox preference:value combination.
446+
Format: A comma-separated list of pref:value items.
447+
Example usage:
448+
--firefox-pref="browser.formfill.enable:True"
449+
--firefox-pref="pdfjs.disabled:False"
450+
--firefox-pref="abc.def.xyz:42,hello.world:text"
451+
Boolean and integer values to the right of the ":"
452+
will be automatically converted into proper format.
453+
If there's no ":" in the string, then True is used.
454+
Default: None.""",
455+
)
425456
parser.addoption(
426457
"--extension_zip",
427458
"--extension-zip",
@@ -916,6 +947,8 @@ def pytest_configure(config):
916947
sb_config.interval = config.getoption("interval")
917948
sb_config.start_page = config.getoption("start_page")
918949
sb_config.chromium_arg = config.getoption("chromium_arg")
950+
sb_config.firefox_arg = config.getoption("firefox_arg")
951+
sb_config.firefox_pref = config.getoption("firefox_pref")
919952
sb_config.extension_zip = config.getoption("extension_zip")
920953
sb_config.extension_dir = config.getoption("extension_dir")
921954
sb_config.with_testing_base = config.getoption("with_testing_base")

seleniumbase/plugins/selenium_plugin.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ class SeleniumBrowser(Plugin):
2525
--mobile (Use the mobile device emulator while running tests.)
2626
--metrics=STRING (Set mobile metrics: "CSSWidth,CSSHeight,PixelRatio".)
2727
--chromium-arg=ARG (Add a Chromium arg for Chrome/Edge, comma-separated.)
28+
--firefox-arg=ARG (Add a Firefox arg for Firefox, comma-separated.)
29+
--firefox-pref=SET (Set a Firefox preference:value set, comma-separated.)
2830
--extension-zip=ZIP (Load a Chrome Extension .zip|.crx, comma-separated.)
2931
--extension-dir=DIR (Load a Chrome Extension directory, comma-separated.)
3032
--headless (Run tests headlessly. Default mode on Linux OS.)
@@ -199,6 +201,35 @@ def options(self, parser, env):
199201
added to the beginning of the arg automatically.
200202
Default: None.""",
201203
)
204+
parser.add_option(
205+
"--firefox_arg",
206+
"--firefox-arg",
207+
action="store",
208+
dest="firefox_arg",
209+
default=None,
210+
help="""Add a Firefox argument for Firefox browser runs.
211+
Format: A comma-separated list of Firefox args.
212+
If an arg doesn't start with "--", that will be
213+
added to the beginning of the arg automatically.
214+
Default: None.""",
215+
)
216+
parser.add_option(
217+
"--firefox_pref",
218+
"--firefox-pref",
219+
action="store",
220+
dest="firefox_pref",
221+
default=None,
222+
help="""Set a Firefox preference:value combination.
223+
Format: A comma-separated list of pref:value items.
224+
Example usage:
225+
--firefox-pref="browser.formfill.enable:True"
226+
--firefox-pref="pdfjs.disabled:False"
227+
--firefox-pref="abc.def.xyz:42,hello.world:text"
228+
Boolean and integer values to the right of the ":"
229+
will be automatically converted into proper format.
230+
If there's no ":" in the string, then True is used.
231+
Default: None.""",
232+
)
202233
parser.add_option(
203234
"--extension_zip",
204235
"--extension-zip",
@@ -552,6 +583,8 @@ def beforeTest(self, test):
552583
test.test.extension_zip = self.options.extension_zip
553584
test.test.extension_dir = self.options.extension_dir
554585
test.test.chromium_arg = self.options.chromium_arg
586+
test.test.firefox_arg = self.options.firefox_arg
587+
test.test.firefox_pref = self.options.firefox_pref
555588
test.test.proxy_string = self.options.proxy_string
556589
test.test.user_agent = self.options.user_agent
557590
test.test.mobile_emulator = self.options.mobile_emulator

0 commit comments

Comments
 (0)