diff --git a/README.md b/README.md index 82f0841910e..00527e3a084 100755 --- a/README.md +++ b/README.md @@ -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.) diff --git a/examples/raw_parameter_script.py b/examples/raw_parameter_script.py index 64fcec9a68c..7a61c73cf1b 100644 --- a/examples/raw_parameter_script.py +++ b/examples/raw_parameter_script.py @@ -1,11 +1,13 @@ """ 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 @@ -13,8 +15,9 @@ 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) @@ -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" @@ -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 diff --git a/help_docs/customizing_test_runs.md b/help_docs/customizing_test_runs.md index a7918d3e79f..3caced9ae90 100644 --- a/help_docs/customizing_test_runs.md +++ b/help_docs/customizing_test_runs.md @@ -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.) diff --git a/help_docs/method_summary.md b/help_docs/method_summary.md index cc932c471c7..fdce9beeecc 100644 --- a/help_docs/method_summary.md +++ b/help_docs/method_summary.md @@ -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, @@ -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, diff --git a/mkdocs_build/requirements.txt b/mkdocs_build/requirements.txt index cbb5e00d1ab..84e6a2ac333 100644 --- a/mkdocs_build/requirements.txt +++ b/mkdocs_build/requirements.txt @@ -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 @@ -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 diff --git a/seleniumbase/__version__.py b/seleniumbase/__version__.py index 8881df4afa8..272d8c5cf86 100755 --- a/seleniumbase/__version__.py +++ b/seleniumbase/__version__.py @@ -1,2 +1,2 @@ # seleniumbase package -__version__ = "4.31.1" +__version__ = "4.31.2" diff --git a/seleniumbase/behave/behave_sb.py b/seleniumbase/behave/behave_sb.py index 8f95950a989..06668a96bbb 100644 --- a/seleniumbase/behave/behave_sb.py +++ b/seleniumbase/behave/behave_sb.py @@ -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.) @@ -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 @@ -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 @@ -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 diff --git a/seleniumbase/core/browser_launcher.py b/seleniumbase/core/browser_launcher.py index 6af8df48729..d444b42a17d 100644 --- a/seleniumbase/core/browser_launcher.py +++ b/seleniumbase/core/browser_launcher.py @@ -1544,6 +1544,7 @@ def _set_chrome_options( multi_proxy, user_agent, recorder_ext, + disable_cookies, disable_js, disable_csp, enable_ws, @@ -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: @@ -2016,6 +2019,7 @@ def _set_firefox_options( proxy_bypass_list, proxy_pac_url, user_agent, + disable_cookies, disable_js, disable_csp, firefox_arg, @@ -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: @@ -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, @@ -2348,6 +2355,7 @@ def get_driver( headless and ( proxy_auth + or disable_cookies or disable_js or ad_block_on or disable_csp @@ -2402,6 +2410,7 @@ def get_driver( cap_file, cap_string, recorder_ext, + disable_cookies, disable_js, disable_csp, enable_ws, @@ -2459,6 +2468,7 @@ def get_driver( multi_proxy, user_agent, recorder_ext, + disable_cookies, disable_js, disable_csp, enable_ws, @@ -2520,6 +2530,7 @@ def get_remote_driver( cap_file, cap_string, recorder_ext, + disable_cookies, disable_js, disable_csp, enable_ws, @@ -2656,6 +2667,7 @@ def get_remote_driver( multi_proxy, user_agent, recorder_ext, + disable_cookies, disable_js, disable_csp, enable_ws, @@ -2751,6 +2763,7 @@ def get_remote_driver( proxy_bypass_list, proxy_pac_url, user_agent, + disable_cookies, disable_js, disable_csp, firefox_arg, @@ -2829,6 +2842,7 @@ def get_remote_driver( multi_proxy, user_agent, recorder_ext, + disable_cookies, disable_js, disable_csp, enable_ws, @@ -2949,6 +2963,7 @@ def get_local_driver( multi_proxy, user_agent, recorder_ext, + disable_cookies, disable_js, disable_csp, enable_ws, @@ -3030,6 +3045,7 @@ def get_local_driver( proxy_bypass_list, proxy_pac_url, user_agent, + disable_cookies, disable_js, disable_csp, firefox_arg, @@ -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: @@ -3796,6 +3814,7 @@ def get_local_driver( multi_proxy, user_agent, recorder_ext, + disable_cookies, disable_js, disable_csp, enable_ws, @@ -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, @@ -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, diff --git a/seleniumbase/fixtures/base_case.py b/seleniumbase/fixtures/base_case.py index e3ef884946b..f74038999d3 100644 --- a/seleniumbase/fixtures/base_case.py +++ b/seleniumbase/fixtures/base_case.py @@ -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, @@ -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) @@ -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: @@ -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, @@ -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 @@ -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, diff --git a/seleniumbase/plugins/driver_manager.py b/seleniumbase/plugins/driver_manager.py index 982acfc3633..4530bf710d6 100644 --- a/seleniumbase/plugins/driver_manager.py +++ b/seleniumbase/plugins/driver_manager.py @@ -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) @@ -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) @@ -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 @@ -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, diff --git a/seleniumbase/plugins/pytest_plugin.py b/seleniumbase/plugins/pytest_plugin.py index 99e195d14ac..868e051cd4c 100644 --- a/seleniumbase/plugins/pytest_plugin.py +++ b/seleniumbase/plugins/pytest_plugin.py @@ -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.) @@ -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", @@ -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") diff --git a/seleniumbase/plugins/sb_manager.py b/seleniumbase/plugins/sb_manager.py index 547b1d1552e..ad2c3efb785 100644 --- a/seleniumbase/plugins/sb_manager.py +++ b/seleniumbase/plugins/sb_manager.py @@ -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. @@ -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. @@ -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 @@ -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 @@ -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 diff --git a/seleniumbase/plugins/selenium_plugin.py b/seleniumbase/plugins/selenium_plugin.py index ac903b98812..b9ae52ba4ef 100644 --- a/seleniumbase/plugins/selenium_plugin.py +++ b/seleniumbase/plugins/selenium_plugin.py @@ -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.) @@ -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", @@ -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