16
16
from seleniumbase .fixtures import constants
17
17
from seleniumbase .fixtures import page_utils
18
18
from seleniumbase import drivers # webdriver storage folder for SeleniumBase
19
+ from seleniumbase import extensions # browser extensions storage folder
19
20
DRIVER_DIR = os .path .dirname (os .path .realpath (drivers .__file__ ))
21
+ EXTENSIONS_DIR = os .path .dirname (os .path .realpath (extensions .__file__ ))
22
+ DISABLE_CSP_ZIP_PATH = "%s/%s" % (EXTENSIONS_DIR , "disable_csp.zip" )
20
23
PROXY_ZIP_PATH = proxy_helper .PROXY_ZIP_PATH
21
24
PROXY_ZIP_PATH_2 = proxy_helper .PROXY_ZIP_PATH_2
22
25
PLATFORM = sys .platform
@@ -82,8 +85,16 @@ def _add_chrome_proxy_extension(
82
85
return chrome_options
83
86
84
87
88
+ def _add_chrome_disable_csp_extension (chrome_options ):
89
+ """ Disable Chrome's Content-Security-Policy with a browser extension.
90
+ See https://github.com/PhilGrayson/chrome-csp-disable for details. """
91
+ disable_csp_zip = DISABLE_CSP_ZIP_PATH
92
+ chrome_options .add_extension (disable_csp_zip )
93
+ return chrome_options
94
+
95
+
85
96
def _set_chrome_options (
86
- downloads_path , proxy_string , proxy_auth ,
97
+ downloads_path , headless , proxy_string , proxy_auth ,
87
98
proxy_user , proxy_pass , user_agent ):
88
99
chrome_options = webdriver .ChromeOptions ()
89
100
prefs = {
@@ -108,6 +119,10 @@ def _set_chrome_options(
108
119
chrome_options .add_argument ("--disable-single-click-autofill" )
109
120
chrome_options .add_argument ("--disable-translate" )
110
121
chrome_options .add_argument ("--disable-web-security" )
122
+ if settings .DISABLE_CONTENT_SECURITY_POLICY and not headless :
123
+ # Headless Chrome doesn't support extensions, which are required
124
+ # for disabling the Content Security Policy on Chrome
125
+ chrome_options = _add_chrome_disable_csp_extension (chrome_options )
111
126
if proxy_string :
112
127
if proxy_auth :
113
128
chrome_options = _add_chrome_proxy_extension (
@@ -135,7 +150,8 @@ def _create_firefox_profile(downloads_path, proxy_string, user_agent):
135
150
profile .set_preference ("general.useragent.override" , user_agent )
136
151
profile .set_preference (
137
152
"security.mixed_content.block_active_content" , False )
138
- profile .set_preference ("security.csp.enable" , False )
153
+ if settings .DISABLE_CONTENT_SECURITY_POLICY :
154
+ profile .set_preference ("security.csp.enable" , False )
139
155
profile .set_preference (
140
156
"browser.download.manager.showAlertOnComplete" , False )
141
157
profile .set_preference ("browser.privatebrowsing.autostart" , True )
@@ -247,7 +263,7 @@ def get_remote_driver(
247
263
desired_caps = capabilities_parser .get_desired_capabilities (cap_file )
248
264
if browser_name == constants .Browser .GOOGLE_CHROME :
249
265
chrome_options = _set_chrome_options (
250
- downloads_path , proxy_string , proxy_auth ,
266
+ downloads_path , headless , proxy_string , proxy_auth ,
251
267
proxy_user , proxy_pass , user_agent )
252
268
if headless :
253
269
if not proxy_auth :
@@ -458,7 +474,7 @@ def get_local_driver(
458
474
elif browser_name == constants .Browser .GOOGLE_CHROME :
459
475
try :
460
476
chrome_options = _set_chrome_options (
461
- downloads_path , proxy_string , proxy_auth ,
477
+ downloads_path , headless , proxy_string , proxy_auth ,
462
478
proxy_user , proxy_pass , user_agent )
463
479
if headless :
464
480
# Headless Chrome doesn't support extensions, which are
0 commit comments