Skip to content

Commit

Permalink
Merge pull request #3165 from seleniumbase/options-for-disabling-cookies
Browse files Browse the repository at this point in the history
Add option for disabling cookies
  • Loading branch information
mdmintz authored Sep 26, 2024
2 parents 5c31bbf + 94b0358 commit 36f229b
Show file tree
Hide file tree
Showing 13 changed files with 100 additions and 15 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -681,6 +681,7 @@ pytest test_coffee_cart.py --trace
--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
21 changes: 14 additions & 7 deletions examples/raw_parameter_script.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
""" The main purpose of this file is to demonstrate running SeleniumBase
scripts without the use of Pytest by calling the script directly
with Python or from a Python interactive interpreter. Based on
whether relative imports work or don't, the script can autodetect
how this file was run. With pure Python, it will initialize
all the variables that would've been automatically initialized
by the Pytest plugin. The setUp() and tearDown() methods are also
now called from the script itself.
with Python or from a Python interactive interpreter.
Based on whether relative imports work or don't work, this script
can autodetect how this file was run. With pure Python, it
initializes all the variables that would've been automatically
initialized by the Pytest plugin. The setUp() and tearDown() methods
are also now called from the script itself.
(Note: The SB() and Driver() formats make this example obsolete.)
One big advantage to running tests with Pytest is that most of this
is done for you automatically, with the option to update any of the
parameters through command-line parsing. Pytest also provides you
with other plugins, such as ones for generating test reports,
handling multithreading, and parametrized tests. Depending on your
specific needs, you may need to call SeleniumBase commands without
using Pytest, and this example shows you how.
using Pytest, and this example shows you one way of doing that.
"""

pure_python = False
try:
# Running with Pytest / (Finds test methods to run using autodiscovery)
Expand All @@ -33,9 +36,11 @@
sb.browser = "chrome"
sb.is_behave = False
sb.headless = False
sb.headless1 = False
sb.headless2 = False
sb.headed = False
sb.xvfb = False
sb.xvfb_metrics = None
sb.start_page = None
sb.locale_code = None
sb.protocol = "http"
Expand Down Expand Up @@ -79,9 +84,11 @@
sb.esc_end = False
sb.use_wire = False
sb.enable_3d_apis = False
sb.window_position = None
sb.window_size = None
sb.maximize_option = False
sb.visual_baseline = False
sb.disable_cookies = False
sb.disable_features = None
sb._disable_beforeunload = False
sb.save_screenshot_after_test = False
Expand Down
1 change: 1 addition & 0 deletions help_docs/customizing_test_runs.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ pytest my_first_test.py --settings-file=custom_settings.py
--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
2 changes: 2 additions & 0 deletions help_docs/method_summary.md
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,7 @@ self.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 All @@ -297,6 +298,7 @@ self.get_new_driver(
log_cdp_events=None,
no_sandbox=None,
disable_gpu=None,
headless1=None,
headless2=None,
incognito=None,
guest_mode=None,
Expand Down
4 changes: 2 additions & 2 deletions mkdocs_build/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# Minimum Python version: 3.8 (for generating docs only)

regex>=2024.9.11
pymdown-extensions>=10.10.1
pymdown-extensions>=10.10.2
pipdeptree>=2.23.4
python-dateutil>=2.8.2
Markdown==3.7
Expand All @@ -20,7 +20,7 @@ lxml==5.3.0
pyquery==2.0.1
readtime==3.0.0
mkdocs==1.6.1
mkdocs-material==9.5.37
mkdocs-material==9.5.38
mkdocs-exclude-search==0.6.6
mkdocs-simple-hooks==0.1.5
mkdocs-material-extensions==1.3.1
2 changes: 1 addition & 1 deletion seleniumbase/__version__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# seleniumbase package
__version__ = "4.31.1"
__version__ = "4.31.2"
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
Loading

0 comments on commit 36f229b

Please sign in to comment.