From 07591a1bcfbfabac535c8aec6352a1bb6a91005b Mon Sep 17 00:00:00 2001 From: Michael Mintz Date: Fri, 8 Jul 2022 16:38:15 -0400 Subject: [PATCH] Add option to enable Chromium's "Do-Not-Track" feature --- README.md | 3 ++- examples/raw_parameter_script.py | 1 + help_docs/customizing_test_runs.md | 3 ++- seleniumbase/behave/behave_sb.py | 8 +++++++- seleniumbase/core/browser_launcher.py | 15 +++++++++++++++ seleniumbase/fixtures/base_case.py | 7 +++++++ seleniumbase/plugins/pytest_plugin.py | 15 ++++++++++++++- seleniumbase/plugins/selenium_plugin.py | 15 ++++++++++++++- 8 files changed, 62 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 2336de0a..10e79095 100755 --- a/README.md +++ b/README.md @@ -465,8 +465,9 @@ The code above will leave your browser window open in case there's a failure. (i --highlights=NUM # (Number of highlight animations for Demo Mode actions.) --message-duration=SECONDS # (The time length for Messenger alerts.) --check-js # (Check for JavaScript errors after page loads.) ---ad-block # (Block some types of display ads after page loads.) +--ad-block # (Block some types of display ads from loading.) --block-images # (Block images from loading during tests.) +--do-not-track # (Indicate to websites that you don't want to be tracked.) --verify-delay=SECONDS # (The delay before MasterQA verification checks.) --recorder # (Enables the Recorder for turning browser actions into code.) --rec-behave # (Same as Recorder Mode, but also generates behave-gherkin.) diff --git a/examples/raw_parameter_script.py b/examples/raw_parameter_script.py index 769e3614..070451f1 100755 --- a/examples/raw_parameter_script.py +++ b/examples/raw_parameter_script.py @@ -93,6 +93,7 @@ if pure_python: sb._dash_initialized = False sb.message_duration = None sb.block_images = False + sb.do_not_track = False sb.external_pdf = False sb.remote_debug = False sb.settings_file = None diff --git a/help_docs/customizing_test_runs.md b/help_docs/customizing_test_runs.md index 2a43eea3..0ad7c6a8 100755 --- a/help_docs/customizing_test_runs.md +++ b/help_docs/customizing_test_runs.md @@ -147,8 +147,9 @@ pytest my_first_test.py --settings-file=custom_settings.py --highlights=NUM # (Number of highlight animations for Demo Mode actions.) --message-duration=SECONDS # (The time length for Messenger alerts.) --check-js # (Check for JavaScript errors after page loads.) ---ad-block # (Block some types of display ads after page loads.) +--ad-block # (Block some types of display ads from loading.) --block-images # (Block images from loading during tests.) +--do-not-track # (Indicate to websites that you don't want to be tracked.) --verify-delay=SECONDS # (The delay before MasterQA verification checks.) --recorder # (Enables the Recorder for turning browser actions into code.) --rec-behave # (Same as Recorder Mode, but also generates behave-gherkin.) diff --git a/seleniumbase/behave/behave_sb.py b/seleniumbase/behave/behave_sb.py index a627bf80..ace3ee74 100644 --- a/seleniumbase/behave/behave_sb.py +++ b/seleniumbase/behave/behave_sb.py @@ -57,8 +57,9 @@ behave -D agent="User Agent String" -D demo -D highlights=NUM (Number of highlight animations for Demo Mode actions.) -D message-duration=SECONDS (The time length for Messenger alerts.) -D check-js (Check for JavaScript errors after page loads.) --D ad-block (Block some types of display ads after page loads.) +-D ad-block (Block some types of display ads from loading.) -D block-images (Block images from loading during tests.) +-D do-not-track (Indicate to websites that you don't want to be tracked.) -D verify-delay=SECONDS (The delay before MasterQA verification checks.) -D recorder (Enables the Recorder for turning browser actions into code.) -D rec-behave (Same as Recorder Mode, but also generates behave-gherkin.) @@ -188,6 +189,7 @@ def get_configured_sb(context): sb._dash_initialized = False sb.message_duration = None sb.block_images = False + sb.do_not_track = False sb.external_pdf = False sb.remote_debug = False sb.settings_file = None @@ -578,6 +580,10 @@ def get_configured_sb(context): if low_key in ["block-images", "block_images"]: sb.block_images = True continue + # Handle: -D do-not-track / do_not_track + if low_key in ["do-not-track", "do_not_track"]: + sb.do_not_track = True + continue # Handle: -D external-pdf / external_pdf if low_key in ["external-pdf", "external_pdf"]: sb.external_pdf = True diff --git a/seleniumbase/core/browser_launcher.py b/seleniumbase/core/browser_launcher.py index c618cf41..6f0fa7c0 100755 --- a/seleniumbase/core/browser_launcher.py +++ b/seleniumbase/core/browser_launcher.py @@ -277,6 +277,7 @@ def _set_chrome_options( swiftshader, ad_block_on, block_images, + do_not_track, chromium_arg, user_data_dir, extension_zip, @@ -313,6 +314,8 @@ def _set_chrome_options( prefs["intl.accept_languages"] = locale_code if block_images: prefs["profile.managed_default_content_settings.images"] = 2 + if do_not_track: + prefs["enable_do_not_track"] = True if external_pdf: prefs["plugins.always_open_pdf_externally"] = True chrome_options.add_experimental_option("prefs", prefs) @@ -752,6 +755,7 @@ def get_driver( swiftshader=None, ad_block_on=None, block_images=None, + do_not_track=None, chromium_arg=None, firefox_arg=None, firefox_pref=None, @@ -856,6 +860,7 @@ def get_driver( swiftshader, ad_block_on, block_images, + do_not_track, chromium_arg, firefox_arg, firefox_pref, @@ -896,6 +901,7 @@ def get_driver( swiftshader, ad_block_on, block_images, + do_not_track, chromium_arg, firefox_arg, firefox_pref, @@ -940,6 +946,7 @@ def get_remote_driver( swiftshader, ad_block_on, block_images, + do_not_track, chromium_arg, firefox_arg, firefox_pref, @@ -1034,6 +1041,7 @@ def get_remote_driver( swiftshader, ad_block_on, block_images, + do_not_track, chromium_arg, user_data_dir, extension_zip, @@ -1242,6 +1250,7 @@ def get_remote_driver( swiftshader, ad_block_on, block_images, + do_not_track, chromium_arg, user_data_dir, extension_zip, @@ -1424,6 +1433,7 @@ def get_local_driver( swiftshader, ad_block_on, block_images, + do_not_track, chromium_arg, firefox_arg, firefox_pref, @@ -1663,6 +1673,8 @@ def get_local_driver( prefs["intl.accept_languages"] = locale_code if block_images: prefs["profile.managed_default_content_settings.images"] = 2 + if do_not_track: + prefs["enable_do_not_track"] = True if external_pdf: prefs["plugins.always_open_pdf_externally"] = True edge_options.add_experimental_option("prefs", prefs) @@ -1924,6 +1936,7 @@ def get_local_driver( swiftshader, ad_block_on, block_images, + do_not_track, chromium_arg, user_data_dir, extension_zip, @@ -1980,6 +1993,7 @@ def get_local_driver( swiftshader, ad_block_on, block_images, + do_not_track, chromium_arg, user_data_dir, extension_zip, @@ -2100,6 +2114,7 @@ def get_local_driver( swiftshader, ad_block_on, block_images, + do_not_track, chromium_arg, user_data_dir, extension_zip, diff --git a/seleniumbase/fixtures/base_case.py b/seleniumbase/fixtures/base_case.py index 01bb22f7..6946f83b 100755 --- a/seleniumbase/fixtures/base_case.py +++ b/seleniumbase/fixtures/base_case.py @@ -2981,6 +2981,7 @@ class BaseCase(unittest.TestCase): swiftshader=None, ad_block_on=None, block_images=None, + do_not_track=None, chromium_arg=None, firefox_arg=None, firefox_pref=None, @@ -3024,6 +3025,7 @@ class BaseCase(unittest.TestCase): swiftshader - the option to use Chrome's swiftshader (Chrome-only) ad_block_on - the option to block ads from loading (Chromium-only) block_images - the option to block images from loading (Chrome) + do_not_track - indicate that websites should not track you (Chrome) chromium_arg - the option to add a Chromium arg to Chrome/Edge firefox_arg - the option to add a Firefox arg to Firefox runs firefox_pref - the option to add a Firefox pref:value set (Firefox) @@ -3120,6 +3122,8 @@ class BaseCase(unittest.TestCase): ad_block_on = self.ad_block_on if block_images is None: block_images = self.block_images + if do_not_track is None: + do_not_track = self.do_not_track if chromium_arg is None: chromium_arg = self.chromium_arg if firefox_arg is None: @@ -3184,6 +3188,7 @@ class BaseCase(unittest.TestCase): swiftshader=swiftshader, ad_block_on=ad_block_on, block_images=block_images, + do_not_track=do_not_track, chromium_arg=chromium_arg, firefox_arg=firefox_arg, firefox_pref=firefox_pref, @@ -12175,6 +12180,7 @@ class BaseCase(unittest.TestCase): self.js_checking_on = sb_config.js_checking_on self.ad_block_on = sb_config.ad_block_on self.block_images = sb_config.block_images + self.do_not_track = sb_config.do_not_track self.chromium_arg = sb_config.chromium_arg self.firefox_arg = sb_config.firefox_arg self.firefox_pref = sb_config.firefox_pref @@ -12495,6 +12501,7 @@ class BaseCase(unittest.TestCase): swiftshader=self.swiftshader, ad_block_on=self.ad_block_on, block_images=self.block_images, + do_not_track=self.do_not_track, chromium_arg=self.chromium_arg, firefox_arg=self.firefox_arg, firefox_pref=self.firefox_pref, diff --git a/seleniumbase/plugins/pytest_plugin.py b/seleniumbase/plugins/pytest_plugin.py index b40a07a3..528d0ce6 100644 --- a/seleniumbase/plugins/pytest_plugin.py +++ b/seleniumbase/plugins/pytest_plugin.py @@ -68,8 +68,9 @@ def pytest_addoption(parser): --highlights=NUM (Number of highlight animations for Demo Mode actions.) --message-duration=SECONDS (The time length for Messenger alerts.) --check-js (Check for JavaScript errors after page loads.) - --ad-block (Block some types of display ads after page loads.) + --ad-block (Block some types of display ads from loading.) --block-images (Block images from loading during tests.) + --do-not-track (Indicate to websites that you don't want to be tracked.) --verify-delay=SECONDS (The delay before MasterQA verification checks.) --recorder (Enables the Recorder for turning browser actions into code.) --rec-behave (Same as Recorder Mode, but also generates behave-gherkin.) @@ -721,6 +722,17 @@ def pytest_addoption(parser): help="""Using this makes WebDriver block images from loading on web pages during tests.""", ) + parser.addoption( + "--do_not_track", + "--do-not-track", + action="store_true", + dest="do_not_track", + default=False, + help="""Indicate to websites that you don't want to be + tracked. The browser will send an extra HTTP + header each time it requests a web page. + https://support.google.com/chrome/answer/2790761""", + ) parser.addoption( "--verify_delay", "--verify-delay", @@ -1220,6 +1232,7 @@ def pytest_configure(config): sb_config.js_checking_on = config.getoption("js_checking_on") sb_config.ad_block_on = config.getoption("ad_block_on") sb_config.block_images = config.getoption("block_images") + sb_config.do_not_track = config.getoption("do_not_track") sb_config.verify_delay = config.getoption("verify_delay") sb_config.recorder_mode = config.getoption("recorder_mode") sb_config.recorder_ext = config.getoption("recorder_mode") # Again diff --git a/seleniumbase/plugins/selenium_plugin.py b/seleniumbase/plugins/selenium_plugin.py index 1898d09e..16814299 100755 --- a/seleniumbase/plugins/selenium_plugin.py +++ b/seleniumbase/plugins/selenium_plugin.py @@ -49,8 +49,9 @@ class SeleniumBrowser(Plugin): --highlights=NUM (Number of highlight animations for Demo Mode actions.) --message-duration=SECONDS (The time length for Messenger alerts.) --check-js (Check for JavaScript errors after page loads.) - --ad-block (Block some types of display ads after page loads.) + --ad-block (Block some types of display ads from loading.) --block-images (Block images from loading during tests.) + --do-not-track (Indicate to websites that you don't want to be tracked.) --verify-delay=SECONDS (The delay before MasterQA verification checks.) --recorder (Enables the Recorder for turning browser actions into code.) --rec-behave (Same as Recorder Mode, but also generates behave-gherkin.) @@ -453,6 +454,17 @@ class SeleniumBrowser(Plugin): help="""Using this makes WebDriver block images from loading on web pages during tests.""", ) + parser.add_option( + "--do_not_track", + "--do-not-track", + action="store_true", + dest="do_not_track", + default=False, + help="""Indicate to websites that you don't want to be + tracked. The browser will send an extra HTTP + header each time it requests a web page. + https://support.google.com/chrome/answer/2790761""", + ) parser.add_option( "--verify_delay", "--verify-delay", @@ -771,6 +783,7 @@ class SeleniumBrowser(Plugin): test.test.js_checking_on = self.options.js_checking_on test.test.ad_block_on = self.options.ad_block_on test.test.block_images = self.options.block_images + test.test.do_not_track = self.options.do_not_track test.test.verify_delay = self.options.verify_delay # MasterQA test.test.recorder_mode = self.options.recorder_mode test.test.recorder_ext = self.options.recorder_mode # Again