Skip to content

Commit

Permalink
Add option for disabling cookies
Browse files Browse the repository at this point in the history
  • Loading branch information
mdmintz committed Sep 26, 2024
1 parent 5c31bbf commit 0821783
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 5 deletions.
9 changes: 8 additions & 1 deletion seleniumbase/behave/behave_sb.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@
-D rec-behave (Same as Recorder Mode, but also generates behave-gherkin.)
-D rec-sleep (If the Recorder is enabled, also records self.sleep calls.)
-D rec-print (If the Recorder is enabled, prints output after tests end.)
-D disable-js (Disable JavaScript on Chromium. May break websites!)
-D disable-cookies (Disable Cookies on websites. Pages might break!)
-D disable-js (Disable JavaScript on websites. Pages might break!)
-D disable-csp (Disable the Content Security Policy of websites.)
-D disable-ws (Disable Web Security on Chromium-based browsers.)
-D enable-ws (Enable Web Security on Chromium-based browsers.)
Expand Down Expand Up @@ -180,6 +181,7 @@ def get_configured_sb(context):
sb.database_env = "test"
sb.log_path = constants.Logs.LATEST + os.sep
sb.archive_logs = False
sb.disable_cookies = False
sb.disable_js = False
sb.disable_csp = False
sb.disable_ws = False
Expand Down Expand Up @@ -535,6 +537,10 @@ def get_configured_sb(context):
if low_key in ["archive-logs", "archive_logs"]:
sb.archive_logs = True
continue
# Handle: -D disable-cookies / disable_cookies
if low_key in ["disable-cookies", "disable_cookies"]:
sb.disable_cookies = True
continue
# Handle: -D disable-js / disable_js
if low_key in ["disable-js", "disable_js"]:
sb.disable_js = True
Expand Down Expand Up @@ -1005,6 +1011,7 @@ def get_configured_sb(context):
sb_config.pdb_option = sb.pdb_option
sb_config.rec_behave = sb.rec_behave
sb_config.rec_print = sb.rec_print
sb_config.disable_cookies = sb.disable_cookies
sb_config.disable_js = sb.disable_js
sb_config.disable_csp = sb.disable_csp
sb_config.record_sleep = sb.record_sleep
Expand Down
21 changes: 21 additions & 0 deletions seleniumbase/core/browser_launcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -1544,6 +1544,7 @@ def _set_chrome_options(
multi_proxy,
user_agent,
recorder_ext,
disable_cookies,
disable_js,
disable_csp,
enable_ws,
Expand Down Expand Up @@ -1621,6 +1622,8 @@ def _set_chrome_options(
prefs["intl.accept_languages"] = locale_code
if block_images:
prefs["profile.managed_default_content_settings.images"] = 2
if disable_cookies:
prefs["profile.default_content_setting_values.cookies"] = 2
if disable_js:
prefs["profile.managed_default_content_settings.javascript"] = 2
if do_not_track:
Expand Down Expand Up @@ -2016,6 +2019,7 @@ def _set_firefox_options(
proxy_bypass_list,
proxy_pac_url,
user_agent,
disable_cookies,
disable_js,
disable_csp,
firefox_arg,
Expand Down Expand Up @@ -2089,6 +2093,8 @@ def _set_firefox_options(
"security.mixed_content.block_active_content", False
)
options.set_preference("security.warn_submit_insecure", False)
if disable_cookies:
options.set_preference("network.cookie.cookieBehavior", 2)
if disable_js:
options.set_preference("javascript.enabled", False)
if settings.DISABLE_CSP_ON_FIREFOX or disable_csp:
Expand Down Expand Up @@ -2188,6 +2194,7 @@ def get_driver(
cap_file=None,
cap_string=None,
recorder_ext=False,
disable_cookies=False,
disable_js=False,
disable_csp=False,
enable_ws=False,
Expand Down Expand Up @@ -2348,6 +2355,7 @@ def get_driver(
headless
and (
proxy_auth
or disable_cookies
or disable_js
or ad_block_on
or disable_csp
Expand Down Expand Up @@ -2402,6 +2410,7 @@ def get_driver(
cap_file,
cap_string,
recorder_ext,
disable_cookies,
disable_js,
disable_csp,
enable_ws,
Expand Down Expand Up @@ -2459,6 +2468,7 @@ def get_driver(
multi_proxy,
user_agent,
recorder_ext,
disable_cookies,
disable_js,
disable_csp,
enable_ws,
Expand Down Expand Up @@ -2520,6 +2530,7 @@ def get_remote_driver(
cap_file,
cap_string,
recorder_ext,
disable_cookies,
disable_js,
disable_csp,
enable_ws,
Expand Down Expand Up @@ -2656,6 +2667,7 @@ def get_remote_driver(
multi_proxy,
user_agent,
recorder_ext,
disable_cookies,
disable_js,
disable_csp,
enable_ws,
Expand Down Expand Up @@ -2751,6 +2763,7 @@ def get_remote_driver(
proxy_bypass_list,
proxy_pac_url,
user_agent,
disable_cookies,
disable_js,
disable_csp,
firefox_arg,
Expand Down Expand Up @@ -2829,6 +2842,7 @@ def get_remote_driver(
multi_proxy,
user_agent,
recorder_ext,
disable_cookies,
disable_js,
disable_csp,
enable_ws,
Expand Down Expand Up @@ -2949,6 +2963,7 @@ def get_local_driver(
multi_proxy,
user_agent,
recorder_ext,
disable_cookies,
disable_js,
disable_csp,
enable_ws,
Expand Down Expand Up @@ -3030,6 +3045,7 @@ def get_local_driver(
proxy_bypass_list,
proxy_pac_url,
user_agent,
disable_cookies,
disable_js,
disable_csp,
firefox_arg,
Expand Down Expand Up @@ -3386,6 +3402,8 @@ def get_local_driver(
prefs["intl.accept_languages"] = locale_code
if block_images:
prefs["profile.managed_default_content_settings.images"] = 2
if disable_cookies:
prefs["profile.default_content_setting_values.cookies"] = 2
if disable_js:
prefs["profile.managed_default_content_settings.javascript"] = 2
if do_not_track:
Expand Down Expand Up @@ -3796,6 +3814,7 @@ def get_local_driver(
multi_proxy,
user_agent,
recorder_ext,
disable_cookies,
disable_js,
disable_csp,
enable_ws,
Expand Down Expand Up @@ -4321,6 +4340,7 @@ def get_local_driver(
None, # multi_proxy
None, # user_agent
None, # recorder_ext
disable_cookies,
disable_js,
disable_csp,
enable_ws,
Expand Down Expand Up @@ -4563,6 +4583,7 @@ def get_local_driver(
None, # multi_proxy
None, # user_agent
None, # recorder_ext
disable_cookies,
disable_js,
disable_csp,
enable_ws,
Expand Down
7 changes: 7 additions & 0 deletions seleniumbase/fixtures/base_case.py
Original file line number Diff line number Diff line change
Expand Up @@ -3760,6 +3760,7 @@ def get_new_driver(
cap_file=None,
cap_string=None,
recorder_ext=None,
disable_cookies=None,
disable_js=None,
disable_csp=None,
enable_ws=None,
Expand Down Expand Up @@ -3820,6 +3821,7 @@ def get_new_driver(
cap_file - the file containing desired capabilities for the browser
cap_string - the string with desired capabilities for the browser
recorder_ext - the option to enable the SBase Recorder extension
disable_cookies - the option to disable Cookies (May break things!)
disable_js - the option to disable JavaScript (May break websites!)
disable_csp - an option to disable Chrome's Content Security Policy
enable_ws - the option to enable the Web Security feature (Chrome)
Expand Down Expand Up @@ -3918,6 +3920,8 @@ def get_new_driver(
user_agent = self.user_agent
if recorder_ext is None:
recorder_ext = self.recorder_ext
if disable_cookies is None:
disable_cookies = self.disable_cookies
if disable_js is None:
disable_js = self.disable_js
if disable_csp is None:
Expand Down Expand Up @@ -4029,6 +4033,7 @@ def get_new_driver(
cap_file=cap_file,
cap_string=cap_string,
recorder_ext=recorder_ext,
disable_cookies=disable_cookies,
disable_js=disable_js,
disable_csp=disable_csp,
enable_ws=enable_ws,
Expand Down Expand Up @@ -14394,6 +14399,7 @@ def setUp(self, masterqa_mode=False):
elif self.record_sleep and not self.recorder_mode:
self.recorder_mode = True
self.recorder_ext = True
self.disable_cookies = sb_config.disable_cookies
self.disable_js = sb_config.disable_js
self.disable_csp = sb_config.disable_csp
self.disable_ws = sb_config.disable_ws
Expand Down Expand Up @@ -14772,6 +14778,7 @@ def setUp(self, masterqa_mode=False):
cap_file=self.cap_file,
cap_string=self.cap_string,
recorder_ext=self.recorder_ext,
disable_cookies=self.disable_cookies,
disable_js=self.disable_js,
disable_csp=self.disable_csp,
enable_ws=self.enable_ws,
Expand Down
12 changes: 10 additions & 2 deletions seleniumbase/plugins/driver_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ def Driver(
cap_file=None, # The desired capabilities to use with a Selenium Grid.
cap_string=None, # The desired capabilities to use with a Selenium Grid.
recorder_ext=None, # Enables the SeleniumBase Recorder Chromium extension.
disable_js=None, # Disable JavaScript on websites. Pages might break!
disable_cookies=None, # Disable Cookies on websites. (Pages might break!)
disable_js=None, # Disable JavaScript on websites. (Pages might break!)
disable_csp=None, # Disable the Content Security Policy of websites.
enable_ws=None, # Enable Web Security on Chromium-based browsers.
disable_ws=None, # Reverse of "enable_ws". (None and False are different)
Expand Down Expand Up @@ -172,7 +173,8 @@ def Driver(
cap_file (str): The desired capabilities to use with a Selenium Grid.
cap_string (str): The desired capabilities to use with a Selenium Grid.
recorder_ext (bool): Enables the SeleniumBase Recorder Chromium extension.
disable_js (bool): Disable JavaScript on websites. Pages might break!
disable_cookies (bool): Disable Cookies on websites. (Pages might break!)
disable_js (bool): Disable JavaScript on websites. (Pages might break!)
disable_csp (bool): Disable the Content Security Policy of websites.
enable_ws (bool): Enable Web Security on Chromium-based browsers.
disable_ws (bool): Reverse of "enable_ws". (None and False are different)
Expand Down Expand Up @@ -646,6 +648,11 @@ def Driver(
use_auto_ext = True
else:
use_auto_ext = False
if disable_cookies is None:
if "--disable-cookies" in sys_argv:
disable_cookies = True
else:
disable_cookies = False
if disable_js is None:
if "--disable-js" in sys_argv:
disable_js = True
Expand Down Expand Up @@ -772,6 +779,7 @@ def Driver(
cap_file=cap_file,
cap_string=cap_string,
recorder_ext=recorder_ext,
disable_cookies=disable_cookies,
disable_js=disable_js,
disable_csp=disable_csp,
enable_ws=enable_ws,
Expand Down
11 changes: 11 additions & 0 deletions seleniumbase/plugins/pytest_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ def pytest_addoption(parser):
--rec-behave (Same as Recorder Mode, but also generates behave-gherkin.)
--rec-sleep (If the Recorder is enabled, also records self.sleep calls.)
--rec-print (If the Recorder is enabled, prints output after tests end.)
--disable-cookies (Disable Cookies on websites. Pages might break!)
--disable-js (Disable JavaScript on websites. Pages might break!)
--disable-csp (Disable the Content Security Policy of websites.)
--disable-ws (Disable Web Security on Chromium-based browsers.)
Expand Down Expand Up @@ -992,6 +993,15 @@ def pytest_addoption(parser):
help="""The option to disable JavaScript on web pages.
Warning: Most web pages will stop working!""",
)
parser.addoption(
"--disable_cookies",
"--disable-cookies",
action="store_true",
dest="disable_cookies",
default=False,
help="""The option to disable Cookies on web pages.
Warning: Several pages may stop working!""",
)
parser.addoption(
"--disable_csp",
"--disable-csp",
Expand Down Expand Up @@ -1628,6 +1638,7 @@ def pytest_configure(config):
elif sb_config.record_sleep and not sb_config.recorder_mode:
sb_config.recorder_mode = True
sb_config.recorder_ext = True
sb_config.disable_cookies = config.getoption("disable_cookies")
sb_config.disable_js = config.getoption("disable_js")
sb_config.disable_csp = config.getoption("disable_csp")
sb_config.disable_ws = config.getoption("disable_ws")
Expand Down
13 changes: 11 additions & 2 deletions seleniumbase/plugins/sb_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ def SB(
cap_file=None, # The desired capabilities to use with a Selenium Grid.
cap_string=None, # The desired capabilities to use with a Selenium Grid.
recorder_ext=None, # Enables the SeleniumBase Recorder Chromium extension.
disable_js=None, # Disable JavaScript on websites. Pages might break!
disable_cookies=None, # Disable Cookies on websites. (Pages might break!)
disable_js=None, # Disable JavaScript on websites. (Pages might break!)
disable_csp=None, # Disable the Content Security Policy of websites.
enable_ws=None, # Enable Web Security on Chromium-based browsers.
enable_sync=None, # Enable "Chrome Sync" on websites.
Expand Down Expand Up @@ -166,7 +167,8 @@ def SB(
cap_file (str): The desired capabilities to use with a Selenium Grid.
cap_string (str): The desired capabilities to use with a Selenium Grid.
recorder_ext (bool): Enables the SeleniumBase Recorder Chromium extension.
disable_js (bool): Disable JavaScript on websites. Pages might break!
disable_cookies (bool): Disable Cookies on websites. (Pages might break!)
disable_js (bool): Disable JavaScript on websites. (Pages might break!)
disable_csp (bool): Disable the Content Security Policy of websites.
enable_ws (bool): Enable Web Security on Chromium-based browsers.
enable_sync (bool): Enable "Chrome Sync" on websites.
Expand Down Expand Up @@ -739,6 +741,11 @@ def SB(
use_auto_ext = True
else:
use_auto_ext = False
if disable_cookies is None:
if "--disable-cookies" in sys_argv:
disable_cookies = True
else:
disable_cookies = False
if disable_js is None:
if "--disable-js" in sys_argv:
disable_js = True
Expand Down Expand Up @@ -973,6 +980,7 @@ def SB(
sb_config.log_cdp_events = log_cdp_events
sb_config.no_sandbox = None
sb_config.disable_gpu = None
sb_config.disable_cookies = disable_cookies
sb_config.disable_js = disable_js
sb_config._multithreaded = False
sb_config.reuse_session = False
Expand Down Expand Up @@ -1081,6 +1089,7 @@ def SB(
sb.log_cdp_events = sb_config.log_cdp_events
sb.no_sandbox = sb_config.no_sandbox
sb.disable_gpu = sb_config.disable_gpu
sb.disable_cookies = sb_config.disable_cookies
sb.disable_js = sb_config.disable_js
sb._multithreaded = sb_config._multithreaded
sb._reuse_session = sb_config.reuse_session
Expand Down
11 changes: 11 additions & 0 deletions seleniumbase/plugins/selenium_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ class SeleniumBrowser(Plugin):
--rec-behave (Same as Recorder Mode, but also generates behave-gherkin.)
--rec-sleep (If the Recorder is enabled, also records self.sleep calls.)
--rec-print (If the Recorder is enabled, prints output after tests end.)
--disable-cookies (Disable Cookies on websites. Pages might break!)
--disable-js (Disable JavaScript on websites. Pages might break!)
--disable-csp (Disable the Content Security Policy of websites.)
--disable-ws (Disable Web Security on Chromium-based browsers.)
Expand Down Expand Up @@ -710,6 +711,15 @@ def options(self, parser, env):
help="""The option to disable JavaScript on web pages.
Warning: Most web pages will stop working!""",
)
parser.addoption(
"--disable_cookies",
"--disable-cookies",
action="store_true",
dest="disable_cookies",
default=False,
help="""The option to disable Cookies on web pages.
Warning: Several pages may stop working!""",
)
parser.addoption(
"--disable_csp",
"--disable-csp",
Expand Down Expand Up @@ -1230,6 +1240,7 @@ def beforeTest(self, test):
elif self.options.record_sleep:
test.test.recorder_mode = True
test.test.recorder_ext = True
test.test.disable_cookies = self.options.disable_cookies
test.test.disable_js = self.options.disable_js
test.test.disable_csp = self.options.disable_csp
test.test.disable_ws = self.options.disable_ws
Expand Down

0 comments on commit 0821783

Please sign in to comment.