Skip to content

Commit 07591a1

Browse files
committed
Add option to enable Chromium's "Do-Not-Track" feature
1 parent 536aa79 commit 07591a1

File tree

8 files changed

+62
-5
lines changed

8 files changed

+62
-5
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -465,8 +465,9 @@ The code above will leave your browser window open in case there's a failure. (i
465465
--highlights=NUM # (Number of highlight animations for Demo Mode actions.)
466466
--message-duration=SECONDS # (The time length for Messenger alerts.)
467467
--check-js # (Check for JavaScript errors after page loads.)
468-
--ad-block # (Block some types of display ads after page loads.)
468+
--ad-block # (Block some types of display ads from loading.)
469469
--block-images # (Block images from loading during tests.)
470+
--do-not-track # (Indicate to websites that you don't want to be tracked.)
470471
--verify-delay=SECONDS # (The delay before MasterQA verification checks.)
471472
--recorder # (Enables the Recorder for turning browser actions into code.)
472473
--rec-behave # (Same as Recorder Mode, but also generates behave-gherkin.)

examples/raw_parameter_script.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@
9393
sb._dash_initialized = False
9494
sb.message_duration = None
9595
sb.block_images = False
96+
sb.do_not_track = False
9697
sb.external_pdf = False
9798
sb.remote_debug = False
9899
sb.settings_file = None

help_docs/customizing_test_runs.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,9 @@ pytest my_first_test.py --settings-file=custom_settings.py
147147
--highlights=NUM # (Number of highlight animations for Demo Mode actions.)
148148
--message-duration=SECONDS # (The time length for Messenger alerts.)
149149
--check-js # (Check for JavaScript errors after page loads.)
150-
--ad-block # (Block some types of display ads after page loads.)
150+
--ad-block # (Block some types of display ads from loading.)
151151
--block-images # (Block images from loading during tests.)
152+
--do-not-track # (Indicate to websites that you don't want to be tracked.)
152153
--verify-delay=SECONDS # (The delay before MasterQA verification checks.)
153154
--recorder # (Enables the Recorder for turning browser actions into code.)
154155
--rec-behave # (Same as Recorder Mode, but also generates behave-gherkin.)

seleniumbase/behave/behave_sb.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,9 @@
5757
-D highlights=NUM (Number of highlight animations for Demo Mode actions.)
5858
-D message-duration=SECONDS (The time length for Messenger alerts.)
5959
-D check-js (Check for JavaScript errors after page loads.)
60-
-D ad-block (Block some types of display ads after page loads.)
60+
-D ad-block (Block some types of display ads from loading.)
6161
-D block-images (Block images from loading during tests.)
62+
-D do-not-track (Indicate to websites that you don't want to be tracked.)
6263
-D verify-delay=SECONDS (The delay before MasterQA verification checks.)
6364
-D recorder (Enables the Recorder for turning browser actions into code.)
6465
-D rec-behave (Same as Recorder Mode, but also generates behave-gherkin.)
@@ -188,6 +189,7 @@ def get_configured_sb(context):
188189
sb._dash_initialized = False
189190
sb.message_duration = None
190191
sb.block_images = False
192+
sb.do_not_track = False
191193
sb.external_pdf = False
192194
sb.remote_debug = False
193195
sb.settings_file = None
@@ -578,6 +580,10 @@ def get_configured_sb(context):
578580
if low_key in ["block-images", "block_images"]:
579581
sb.block_images = True
580582
continue
583+
# Handle: -D do-not-track / do_not_track
584+
if low_key in ["do-not-track", "do_not_track"]:
585+
sb.do_not_track = True
586+
continue
581587
# Handle: -D external-pdf / external_pdf
582588
if low_key in ["external-pdf", "external_pdf"]:
583589
sb.external_pdf = True

seleniumbase/core/browser_launcher.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,7 @@ def _set_chrome_options(
277277
swiftshader,
278278
ad_block_on,
279279
block_images,
280+
do_not_track,
280281
chromium_arg,
281282
user_data_dir,
282283
extension_zip,
@@ -313,6 +314,8 @@ def _set_chrome_options(
313314
prefs["intl.accept_languages"] = locale_code
314315
if block_images:
315316
prefs["profile.managed_default_content_settings.images"] = 2
317+
if do_not_track:
318+
prefs["enable_do_not_track"] = True
316319
if external_pdf:
317320
prefs["plugins.always_open_pdf_externally"] = True
318321
chrome_options.add_experimental_option("prefs", prefs)
@@ -752,6 +755,7 @@ def get_driver(
752755
swiftshader=None,
753756
ad_block_on=None,
754757
block_images=None,
758+
do_not_track=None,
755759
chromium_arg=None,
756760
firefox_arg=None,
757761
firefox_pref=None,
@@ -856,6 +860,7 @@ def get_driver(
856860
swiftshader,
857861
ad_block_on,
858862
block_images,
863+
do_not_track,
859864
chromium_arg,
860865
firefox_arg,
861866
firefox_pref,
@@ -896,6 +901,7 @@ def get_driver(
896901
swiftshader,
897902
ad_block_on,
898903
block_images,
904+
do_not_track,
899905
chromium_arg,
900906
firefox_arg,
901907
firefox_pref,
@@ -940,6 +946,7 @@ def get_remote_driver(
940946
swiftshader,
941947
ad_block_on,
942948
block_images,
949+
do_not_track,
943950
chromium_arg,
944951
firefox_arg,
945952
firefox_pref,
@@ -1034,6 +1041,7 @@ def get_remote_driver(
10341041
swiftshader,
10351042
ad_block_on,
10361043
block_images,
1044+
do_not_track,
10371045
chromium_arg,
10381046
user_data_dir,
10391047
extension_zip,
@@ -1242,6 +1250,7 @@ def get_remote_driver(
12421250
swiftshader,
12431251
ad_block_on,
12441252
block_images,
1253+
do_not_track,
12451254
chromium_arg,
12461255
user_data_dir,
12471256
extension_zip,
@@ -1424,6 +1433,7 @@ def get_local_driver(
14241433
swiftshader,
14251434
ad_block_on,
14261435
block_images,
1436+
do_not_track,
14271437
chromium_arg,
14281438
firefox_arg,
14291439
firefox_pref,
@@ -1663,6 +1673,8 @@ def get_local_driver(
16631673
prefs["intl.accept_languages"] = locale_code
16641674
if block_images:
16651675
prefs["profile.managed_default_content_settings.images"] = 2
1676+
if do_not_track:
1677+
prefs["enable_do_not_track"] = True
16661678
if external_pdf:
16671679
prefs["plugins.always_open_pdf_externally"] = True
16681680
edge_options.add_experimental_option("prefs", prefs)
@@ -1924,6 +1936,7 @@ def get_local_driver(
19241936
swiftshader,
19251937
ad_block_on,
19261938
block_images,
1939+
do_not_track,
19271940
chromium_arg,
19281941
user_data_dir,
19291942
extension_zip,
@@ -1980,6 +1993,7 @@ def get_local_driver(
19801993
swiftshader,
19811994
ad_block_on,
19821995
block_images,
1996+
do_not_track,
19831997
chromium_arg,
19841998
user_data_dir,
19851999
extension_zip,
@@ -2100,6 +2114,7 @@ def get_local_driver(
21002114
swiftshader,
21012115
ad_block_on,
21022116
block_images,
2117+
do_not_track,
21032118
chromium_arg,
21042119
user_data_dir,
21052120
extension_zip,

seleniumbase/fixtures/base_case.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2981,6 +2981,7 @@ def get_new_driver(
29812981
swiftshader=None,
29822982
ad_block_on=None,
29832983
block_images=None,
2984+
do_not_track=None,
29842985
chromium_arg=None,
29852986
firefox_arg=None,
29862987
firefox_pref=None,
@@ -3024,6 +3025,7 @@ def get_new_driver(
30243025
swiftshader - the option to use Chrome's swiftshader (Chrome-only)
30253026
ad_block_on - the option to block ads from loading (Chromium-only)
30263027
block_images - the option to block images from loading (Chrome)
3028+
do_not_track - indicate that websites should not track you (Chrome)
30273029
chromium_arg - the option to add a Chromium arg to Chrome/Edge
30283030
firefox_arg - the option to add a Firefox arg to Firefox runs
30293031
firefox_pref - the option to add a Firefox pref:value set (Firefox)
@@ -3120,6 +3122,8 @@ def get_new_driver(
31203122
ad_block_on = self.ad_block_on
31213123
if block_images is None:
31223124
block_images = self.block_images
3125+
if do_not_track is None:
3126+
do_not_track = self.do_not_track
31233127
if chromium_arg is None:
31243128
chromium_arg = self.chromium_arg
31253129
if firefox_arg is None:
@@ -3184,6 +3188,7 @@ def get_new_driver(
31843188
swiftshader=swiftshader,
31853189
ad_block_on=ad_block_on,
31863190
block_images=block_images,
3191+
do_not_track=do_not_track,
31873192
chromium_arg=chromium_arg,
31883193
firefox_arg=firefox_arg,
31893194
firefox_pref=firefox_pref,
@@ -12175,6 +12180,7 @@ def setUp(self, masterqa_mode=False):
1217512180
self.js_checking_on = sb_config.js_checking_on
1217612181
self.ad_block_on = sb_config.ad_block_on
1217712182
self.block_images = sb_config.block_images
12183+
self.do_not_track = sb_config.do_not_track
1217812184
self.chromium_arg = sb_config.chromium_arg
1217912185
self.firefox_arg = sb_config.firefox_arg
1218012186
self.firefox_pref = sb_config.firefox_pref
@@ -12495,6 +12501,7 @@ def setUp(self, masterqa_mode=False):
1249512501
swiftshader=self.swiftshader,
1249612502
ad_block_on=self.ad_block_on,
1249712503
block_images=self.block_images,
12504+
do_not_track=self.do_not_track,
1249812505
chromium_arg=self.chromium_arg,
1249912506
firefox_arg=self.firefox_arg,
1250012507
firefox_pref=self.firefox_pref,

seleniumbase/plugins/pytest_plugin.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,9 @@ def pytest_addoption(parser):
6868
--highlights=NUM (Number of highlight animations for Demo Mode actions.)
6969
--message-duration=SECONDS (The time length for Messenger alerts.)
7070
--check-js (Check for JavaScript errors after page loads.)
71-
--ad-block (Block some types of display ads after page loads.)
71+
--ad-block (Block some types of display ads from loading.)
7272
--block-images (Block images from loading during tests.)
73+
--do-not-track (Indicate to websites that you don't want to be tracked.)
7374
--verify-delay=SECONDS (The delay before MasterQA verification checks.)
7475
--recorder (Enables the Recorder for turning browser actions into code.)
7576
--rec-behave (Same as Recorder Mode, but also generates behave-gherkin.)
@@ -721,6 +722,17 @@ def pytest_addoption(parser):
721722
help="""Using this makes WebDriver block images from
722723
loading on web pages during tests.""",
723724
)
725+
parser.addoption(
726+
"--do_not_track",
727+
"--do-not-track",
728+
action="store_true",
729+
dest="do_not_track",
730+
default=False,
731+
help="""Indicate to websites that you don't want to be
732+
tracked. The browser will send an extra HTTP
733+
header each time it requests a web page.
734+
https://support.google.com/chrome/answer/2790761""",
735+
)
724736
parser.addoption(
725737
"--verify_delay",
726738
"--verify-delay",
@@ -1220,6 +1232,7 @@ def pytest_configure(config):
12201232
sb_config.js_checking_on = config.getoption("js_checking_on")
12211233
sb_config.ad_block_on = config.getoption("ad_block_on")
12221234
sb_config.block_images = config.getoption("block_images")
1235+
sb_config.do_not_track = config.getoption("do_not_track")
12231236
sb_config.verify_delay = config.getoption("verify_delay")
12241237
sb_config.recorder_mode = config.getoption("recorder_mode")
12251238
sb_config.recorder_ext = config.getoption("recorder_mode") # Again

seleniumbase/plugins/selenium_plugin.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,9 @@ class SeleniumBrowser(Plugin):
4949
--highlights=NUM (Number of highlight animations for Demo Mode actions.)
5050
--message-duration=SECONDS (The time length for Messenger alerts.)
5151
--check-js (Check for JavaScript errors after page loads.)
52-
--ad-block (Block some types of display ads after page loads.)
52+
--ad-block (Block some types of display ads from loading.)
5353
--block-images (Block images from loading during tests.)
54+
--do-not-track (Indicate to websites that you don't want to be tracked.)
5455
--verify-delay=SECONDS (The delay before MasterQA verification checks.)
5556
--recorder (Enables the Recorder for turning browser actions into code.)
5657
--rec-behave (Same as Recorder Mode, but also generates behave-gherkin.)
@@ -453,6 +454,17 @@ def options(self, parser, env):
453454
help="""Using this makes WebDriver block images from
454455
loading on web pages during tests.""",
455456
)
457+
parser.add_option(
458+
"--do_not_track",
459+
"--do-not-track",
460+
action="store_true",
461+
dest="do_not_track",
462+
default=False,
463+
help="""Indicate to websites that you don't want to be
464+
tracked. The browser will send an extra HTTP
465+
header each time it requests a web page.
466+
https://support.google.com/chrome/answer/2790761""",
467+
)
456468
parser.add_option(
457469
"--verify_delay",
458470
"--verify-delay",
@@ -771,6 +783,7 @@ def beforeTest(self, test):
771783
test.test.js_checking_on = self.options.js_checking_on
772784
test.test.ad_block_on = self.options.ad_block_on
773785
test.test.block_images = self.options.block_images
786+
test.test.do_not_track = self.options.do_not_track
774787
test.test.verify_delay = self.options.verify_delay # MasterQA
775788
test.test.recorder_mode = self.options.recorder_mode
776789
test.test.recorder_ext = self.options.recorder_mode # Again

0 commit comments

Comments
 (0)