Skip to content

Commit 338f28d

Browse files
committed
Add "--external-pdf" option to open PDFs externally
1 parent a6c6068 commit 338f28d

File tree

5 files changed

+53
-6
lines changed

5 files changed

+53
-6
lines changed

examples/raw_parameter_script.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@
8282
sb._dash_initialized = False
8383
sb.message_duration = 2
8484
sb.block_images = False
85+
sb.external_pdf = False
8586
sb.remote_debug = False
8687
sb.settings_file = None
8788
sb.user_data_dir = None

seleniumbase/core/browser_launcher.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,7 @@ def _set_chrome_options(
273273
user_data_dir,
274274
extension_zip,
275275
extension_dir,
276+
external_pdf,
276277
servername,
277278
mobile_emulator,
278279
device_width,
@@ -302,6 +303,8 @@ def _set_chrome_options(
302303
prefs["intl.accept_languages"] = locale_code
303304
if block_images:
304305
prefs["profile.managed_default_content_settings.images"] = 2
306+
if external_pdf:
307+
prefs["plugins.always_open_pdf_externally"] = True
305308
chrome_options.add_experimental_option("prefs", prefs)
306309
chrome_options.add_experimental_option("w3c", True)
307310
if enable_sync:
@@ -728,6 +731,7 @@ def get_driver(
728731
user_data_dir=None,
729732
extension_zip=None,
730733
extension_dir=None,
734+
external_pdf=None,
731735
test_id=None,
732736
mobile_emulator=False,
733737
device_width=None,
@@ -803,6 +807,7 @@ def get_driver(
803807
user_data_dir,
804808
extension_zip,
805809
extension_dir,
810+
external_pdf,
806811
test_id,
807812
mobile_emulator,
808813
device_width,
@@ -841,6 +846,7 @@ def get_driver(
841846
user_data_dir,
842847
extension_zip,
843848
extension_dir,
849+
external_pdf,
844850
mobile_emulator,
845851
device_width,
846852
device_height,
@@ -883,6 +889,7 @@ def get_remote_driver(
883889
user_data_dir,
884890
extension_zip,
885891
extension_dir,
892+
external_pdf,
886893
test_id,
887894
mobile_emulator,
888895
device_width,
@@ -971,6 +978,7 @@ def get_remote_driver(
971978
user_data_dir,
972979
extension_zip,
973980
extension_dir,
981+
external_pdf,
974982
servername,
975983
mobile_emulator,
976984
device_width,
@@ -1298,6 +1306,7 @@ def get_local_driver(
12981306
user_data_dir,
12991307
extension_zip,
13001308
extension_dir,
1309+
external_pdf,
13011310
mobile_emulator,
13021311
device_width,
13031312
device_height,
@@ -1490,6 +1499,8 @@ def get_local_driver(
14901499
prefs["intl.accept_languages"] = locale_code
14911500
if block_images:
14921501
prefs["profile.managed_default_content_settings.images"] = 2
1502+
if external_pdf:
1503+
prefs["plugins.always_open_pdf_externally"] = True
14931504
edge_options.add_experimental_option("prefs", prefs)
14941505
edge_options.add_experimental_option("w3c", True)
14951506
edge_options.add_argument(
@@ -1738,6 +1749,7 @@ def get_local_driver(
17381749
user_data_dir,
17391750
extension_zip,
17401751
extension_dir,
1752+
external_pdf,
17411753
servername,
17421754
mobile_emulator,
17431755
device_width,
@@ -1791,6 +1803,7 @@ def get_local_driver(
17911803
user_data_dir,
17921804
extension_zip,
17931805
extension_dir,
1806+
external_pdf,
17941807
servername,
17951808
mobile_emulator,
17961809
device_width,
@@ -1888,6 +1901,7 @@ def get_local_driver(
18881901
user_data_dir,
18891902
extension_zip,
18901903
extension_dir,
1904+
external_pdf,
18911905
servername,
18921906
mobile_emulator,
18931907
device_width,

seleniumbase/fixtures/base_case.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2762,6 +2762,7 @@ def get_new_driver(
27622762
user_data_dir=None,
27632763
extension_zip=None,
27642764
extension_dir=None,
2765+
external_pdf=None,
27652766
is_mobile=None,
27662767
d_width=None,
27672768
d_height=None,
@@ -2803,6 +2804,7 @@ def get_new_driver(
28032804
user_data_dir - Chrome's User Data Directory to use (Chrome-only)
28042805
extension_zip - A Chrome Extension ZIP file to use (Chrome-only)
28052806
extension_dir - A Chrome Extension folder to use (Chrome-only)
2807+
external_pdf - "plugins.always_open_pdf_externally": True. (Chrome)
28062808
is_mobile - the option to use the mobile emulator (Chrome-only)
28072809
d_width - the device width of the mobile emulator (Chrome-only)
28082810
d_height - the device height of the mobile emulator (Chrome-only)
@@ -2899,6 +2901,8 @@ def get_new_driver(
28992901
extension_zip = self.extension_zip
29002902
if extension_dir is None:
29012903
extension_dir = self.extension_dir
2904+
if external_pdf is None:
2905+
external_pdf = self.external_pdf
29022906
test_id = self.__get_test_id()
29032907
if cap_file is None:
29042908
cap_file = self.cap_file
@@ -2954,6 +2958,7 @@ def get_new_driver(
29542958
user_data_dir=user_data_dir,
29552959
extension_zip=extension_zip,
29562960
extension_dir=extension_dir,
2961+
external_pdf=external_pdf,
29572962
test_id=test_id,
29582963
mobile_emulator=is_mobile,
29592964
device_width=d_width,
@@ -10899,6 +10904,7 @@ def setUp(self, masterqa_mode=False):
1089910904
self.user_data_dir = sb_config.user_data_dir
1090010905
self.extension_zip = sb_config.extension_zip
1090110906
self.extension_dir = sb_config.extension_dir
10907+
self.external_pdf = sb_config.external_pdf
1090210908
self.maximize_option = sb_config.maximize_option
1090310909
self.save_screenshot_after_test = sb_config.save_screenshot
1090410910
self.visual_baseline = sb_config.visual_baseline
@@ -11159,6 +11165,7 @@ def setUp(self, masterqa_mode=False):
1115911165
user_data_dir=self.user_data_dir,
1116011166
extension_zip=self.extension_zip,
1116111167
extension_dir=self.extension_dir,
11168+
external_pdf=self.external_pdf,
1116211169
is_mobile=self.mobile_emulator,
1116311170
d_width=self.__device_width,
1116411171
d_height=self.__device_height,

seleniumbase/plugins/pytest_plugin.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ def pytest_addoption(parser):
8181
--maximize (Start tests with the web browser window maximized.)
8282
--save-screenshot (Save a screenshot at the end of each test.)
8383
--visual-baseline (Set the visual baseline for Visual/Layout tests.)
84+
--external-pdf (Set Chromium "plugins.always_open_pdf_externally": True.)
8485
--timeout-multiplier=MULTIPLIER (Multiplies the default timeout values.)
8586
"""
8687
c1 = ""
@@ -831,8 +832,8 @@ def pytest_addoption(parser):
831832
dest="crumbs",
832833
default=False,
833834
help="""The option to delete all cookies between tests
834-
that reuse the same browser session. This option
835-
is only needed when using "--reuse-session".""",
835+
that reuse the same browser session. This option
836+
is only needed when using "--reuse-session".""",
836837
)
837838
parser.addoption(
838839
"--maximize_window",
@@ -853,8 +854,8 @@ def pytest_addoption(parser):
853854
action="store_true",
854855
dest="save_screenshot",
855856
default=False,
856-
help="""Take a screenshot on last page after the last step
857-
of the test. (Added to the "latest_logs" folder.)""",
857+
help="""Save a screenshot at the end of the test.
858+
(Added to the "latest_logs/" folder.)""",
858859
)
859860
parser.addoption(
860861
"--visual_baseline",
@@ -867,6 +868,17 @@ def pytest_addoption(parser):
867868
When a test calls self.check_window(), it will
868869
rebuild its files in the visual_baseline folder.""",
869870
)
871+
parser.addoption(
872+
"--external_pdf",
873+
"--external-pdf",
874+
action="store_true",
875+
dest="external_pdf",
876+
default=False,
877+
help="""This option sets the following on Chromium:
878+
"plugins.always_open_pdf_externally": True,
879+
which causes opened PDF URLs to download immediately,
880+
instead of being displayed in the browser window.""",
881+
)
870882
parser.addoption(
871883
"--timeout_multiplier",
872884
"--timeout-multiplier",
@@ -1114,6 +1126,7 @@ def pytest_configure(config):
11141126
sb_config.maximize_option = config.getoption("maximize_option")
11151127
sb_config.save_screenshot = config.getoption("save_screenshot")
11161128
sb_config.visual_baseline = config.getoption("visual_baseline")
1129+
sb_config.external_pdf = config.getoption("external_pdf")
11171130
sb_config.timeout_multiplier = config.getoption("timeout_multiplier")
11181131
sb_config._is_timeout_changed = False
11191132
sb_config._SMALL_TIMEOUT = settings.SMALL_TIMEOUT

seleniumbase/plugins/selenium_plugin.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ class SeleniumBrowser(Plugin):
6060
--maximize (Start tests with the web browser window maximized.)
6161
--save-screenshot (Save a screenshot at the end of each test.)
6262
--visual-baseline (Set the visual baseline for Visual/Layout tests.)
63+
--external-pdf (Set Chromium "plugins.always_open_pdf_externally": True.)
6364
--timeout-multiplier=MULTIPLIER (Multiplies the default timeout values.)
6465
"""
6566

@@ -585,8 +586,7 @@ def options(self, parser, env):
585586
action="store_true",
586587
dest="save_screenshot",
587588
default=False,
588-
help="""(DEPRECATED) - Screenshots are enabled by default now.
589-
This option saves screenshots during test failures.
589+
help="""Save a screenshot at the end of the test.
590590
(Added to the "latest_logs/" folder.)""",
591591
)
592592
parser.add_option(
@@ -600,6 +600,17 @@ def options(self, parser, env):
600600
When a test calls self.check_window(), it will
601601
rebuild its files in the visual_baseline folder.""",
602602
)
603+
parser.add_option(
604+
"--external_pdf",
605+
"--external-pdf",
606+
action="store_true",
607+
dest="external_pdf",
608+
default=False,
609+
help="""This option sets the following on Chromium:
610+
"plugins.always_open_pdf_externally": True,
611+
which causes opened PDF URLs to download immediately,
612+
instead of being displayed in the browser window.""",
613+
)
603614
parser.add_option(
604615
"--timeout_multiplier",
605616
"--timeout-multiplier",
@@ -677,6 +688,7 @@ def beforeTest(self, test):
677688
test.test.maximize_option = self.options.maximize_option
678689
test.test.save_screenshot_after_test = self.options.save_screenshot
679690
test.test.visual_baseline = self.options.visual_baseline
691+
test.test.external_pdf = self.options.external_pdf
680692
test.test.timeout_multiplier = self.options.timeout_multiplier
681693
test.test.dashboard = False
682694
test.test._multithreaded = False

0 commit comments

Comments
 (0)