Transform the "ad-block" feature into a Chromium extension
This commit is contained in:
parent
1af6a98f50
commit
bd2754a4d3
|
@ -28,6 +28,7 @@ if not os.environ["PATH"].startswith(DRIVER_DIR):
|
|||
os.environ["PATH"] = DRIVER_DIR + os.pathsep + os.environ["PATH"]
|
||||
EXTENSIONS_DIR = os.path.dirname(os.path.realpath(extensions.__file__))
|
||||
DISABLE_CSP_ZIP_PATH = "%s/%s" % (EXTENSIONS_DIR, "disable_csp.zip")
|
||||
AD_BLOCK_ZIP_PATH = "%s/%s" % (EXTENSIONS_DIR, "ad_block.zip")
|
||||
RECORDER_ZIP_PATH = "%s/%s" % (EXTENSIONS_DIR, "recorder.zip")
|
||||
PROXY_ZIP_PATH = proxy_helper.PROXY_ZIP_PATH
|
||||
PROXY_ZIP_PATH_2 = proxy_helper.PROXY_ZIP_PATH_2
|
||||
|
@ -199,8 +200,17 @@ def _add_chrome_disable_csp_extension(chrome_options):
|
|||
return chrome_options
|
||||
|
||||
|
||||
def _add_chrome_ad_block_extension(chrome_options):
|
||||
"""Block Ads on Chromium Browsers with a browser extension.
|
||||
See https://github.com/slingamn/simpleblock for details."""
|
||||
ad_block_zip = AD_BLOCK_ZIP_PATH
|
||||
chrome_options.add_extension(ad_block_zip)
|
||||
return chrome_options
|
||||
|
||||
|
||||
def _add_chrome_recorder_extension(chrome_options):
|
||||
"""The SeleniumBase Recorder Chromium extension."""
|
||||
"""The SeleniumBase Recorder Chrome/Edge extension.
|
||||
https://seleniumbase.io/help_docs/recorder_mode/"""
|
||||
recorder_zip = RECORDER_ZIP_PATH
|
||||
chrome_options.add_extension(recorder_zip)
|
||||
return chrome_options
|
||||
|
@ -228,6 +238,7 @@ def _set_chrome_options(
|
|||
devtools,
|
||||
remote_debug,
|
||||
swiftshader,
|
||||
ad_block_on,
|
||||
block_images,
|
||||
chromium_arg,
|
||||
user_data_dir,
|
||||
|
@ -322,10 +333,10 @@ def _set_chrome_options(
|
|||
chrome_options.add_experimental_option(
|
||||
"mobileEmulation", emulator_settings
|
||||
)
|
||||
chrome_options.add_argument("--enable-sync")
|
||||
if (
|
||||
not proxy_auth
|
||||
and not disable_csp
|
||||
and not ad_block_on
|
||||
and not recorder_ext
|
||||
and (not extension_zip and not extension_dir)
|
||||
):
|
||||
|
@ -391,7 +402,8 @@ def _set_chrome_options(
|
|||
# Headless Chrome doesn't support extensions, which are required
|
||||
# for disabling the Content Security Policy on Chrome
|
||||
chrome_options = _add_chrome_disable_csp_extension(chrome_options)
|
||||
chrome_options.add_argument("--enable-sync")
|
||||
if ad_block_on and not headless:
|
||||
chrome_options = _add_chrome_ad_block_extension(chrome_options)
|
||||
if recorder_ext and not headless:
|
||||
chrome_options = _add_chrome_recorder_extension(chrome_options)
|
||||
if proxy_string:
|
||||
|
@ -687,6 +699,7 @@ def get_driver(
|
|||
devtools=None,
|
||||
remote_debug=None,
|
||||
swiftshader=None,
|
||||
ad_block_on=None,
|
||||
block_images=None,
|
||||
chromium_arg=None,
|
||||
firefox_arg=None,
|
||||
|
@ -760,6 +773,7 @@ def get_driver(
|
|||
devtools,
|
||||
remote_debug,
|
||||
swiftshader,
|
||||
ad_block_on,
|
||||
block_images,
|
||||
chromium_arg,
|
||||
firefox_arg,
|
||||
|
@ -796,6 +810,7 @@ def get_driver(
|
|||
devtools,
|
||||
remote_debug,
|
||||
swiftshader,
|
||||
ad_block_on,
|
||||
block_images,
|
||||
chromium_arg,
|
||||
firefox_arg,
|
||||
|
@ -836,6 +851,7 @@ def get_remote_driver(
|
|||
devtools,
|
||||
remote_debug,
|
||||
swiftshader,
|
||||
ad_block_on,
|
||||
block_images,
|
||||
chromium_arg,
|
||||
firefox_arg,
|
||||
|
@ -900,6 +916,7 @@ def get_remote_driver(
|
|||
devtools,
|
||||
remote_debug,
|
||||
swiftshader,
|
||||
ad_block_on,
|
||||
block_images,
|
||||
chromium_arg,
|
||||
user_data_dir,
|
||||
|
@ -1053,6 +1070,7 @@ def get_local_driver(
|
|||
devtools,
|
||||
remote_debug,
|
||||
swiftshader,
|
||||
ad_block_on,
|
||||
block_images,
|
||||
chromium_arg,
|
||||
firefox_arg,
|
||||
|
@ -1220,6 +1238,7 @@ def get_local_driver(
|
|||
devtools,
|
||||
remote_debug,
|
||||
swiftshader,
|
||||
ad_block_on,
|
||||
block_images,
|
||||
chromium_arg,
|
||||
user_data_dir,
|
||||
|
@ -1307,7 +1326,6 @@ def get_local_driver(
|
|||
edge_options.add_experimental_option(
|
||||
"mobileEmulation", emulator_settings
|
||||
)
|
||||
edge_options.add_argument("--enable-sync")
|
||||
if user_data_dir:
|
||||
abs_path = os.path.abspath(user_data_dir)
|
||||
edge_options.add_argument("user-data-dir=%s" % abs_path)
|
||||
|
@ -1342,10 +1360,10 @@ def get_local_driver(
|
|||
# Headless Edge doesn't support extensions, which are required
|
||||
# for disabling the Content Security Policy on Edge
|
||||
edge_options = _add_chrome_disable_csp_extension(edge_options)
|
||||
edge_options.add_argument("--enable-sync")
|
||||
if ad_block_on and not headless:
|
||||
edge_options = _add_chrome_ad_block_extension(edge_options)
|
||||
if recorder_ext and not headless:
|
||||
edge_options = _add_chrome_recorder_extension(edge_options)
|
||||
edge_options.add_argument("--enable-sync")
|
||||
if proxy_string:
|
||||
if proxy_auth:
|
||||
edge_options = _add_chrome_proxy_extension(
|
||||
|
@ -1431,6 +1449,7 @@ def get_local_driver(
|
|||
devtools,
|
||||
remote_debug,
|
||||
swiftshader,
|
||||
ad_block_on,
|
||||
block_images,
|
||||
chromium_arg,
|
||||
user_data_dir,
|
||||
|
@ -1475,6 +1494,7 @@ def get_local_driver(
|
|||
devtools,
|
||||
remote_debug,
|
||||
swiftshader,
|
||||
ad_block_on,
|
||||
block_images,
|
||||
chromium_arg,
|
||||
user_data_dir,
|
||||
|
@ -1556,6 +1576,7 @@ def get_local_driver(
|
|||
devtools,
|
||||
remote_debug,
|
||||
swiftshader,
|
||||
ad_block_on,
|
||||
block_images,
|
||||
chromium_arg,
|
||||
user_data_dir,
|
||||
|
|
Binary file not shown.
|
@ -2480,6 +2480,7 @@ class BaseCase(unittest.TestCase):
|
|||
devtools=None,
|
||||
remote_debug=None,
|
||||
swiftshader=None,
|
||||
ad_block_on=None,
|
||||
block_images=None,
|
||||
chromium_arg=None,
|
||||
firefox_arg=None,
|
||||
|
@ -2519,6 +2520,7 @@ class BaseCase(unittest.TestCase):
|
|||
devtools - the option to open Chrome's DevTools on start (Chrome)
|
||||
remote_debug - the option to enable Chrome's Remote Debugger
|
||||
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)
|
||||
chromium_arg - the option to add a Chromium arg to Chrome/Edge
|
||||
firefox_arg - the option to add a Firefox arg to Firefox runs
|
||||
|
@ -2604,6 +2606,8 @@ class BaseCase(unittest.TestCase):
|
|||
remote_debug = self.remote_debug
|
||||
if swiftshader is None:
|
||||
swiftshader = self.swiftshader
|
||||
if ad_block_on is None:
|
||||
ad_block_on = self.ad_block_on
|
||||
if block_images is None:
|
||||
block_images = self.block_images
|
||||
if chromium_arg is None:
|
||||
|
@ -2664,6 +2668,7 @@ class BaseCase(unittest.TestCase):
|
|||
devtools=devtools,
|
||||
remote_debug=remote_debug,
|
||||
swiftshader=swiftshader,
|
||||
ad_block_on=ad_block_on,
|
||||
block_images=block_images,
|
||||
chromium_arg=chromium_arg,
|
||||
firefox_arg=firefox_arg,
|
||||
|
@ -2891,13 +2896,11 @@ class BaseCase(unittest.TestCase):
|
|||
self.wait_for_angularjs(timeout=settings.MINI_TIMEOUT)
|
||||
if self.js_checking_on:
|
||||
self.assert_no_js_errors()
|
||||
if self.ad_block_on:
|
||||
# If the ad_block feature is enabled, then block ads for new URLs
|
||||
if self.ad_block_on and (self.headless or not self.is_chromium()):
|
||||
# For Chromium browsers in headed mode, the extension is used
|
||||
current_url = self.get_current_url()
|
||||
if not current_url == self.__last_page_load_url:
|
||||
time.sleep(0.02)
|
||||
self.ad_block()
|
||||
time.sleep(0.02)
|
||||
if self.is_element_present("iframe"):
|
||||
time.sleep(0.1) # iframe ads take slightly longer to load
|
||||
self.ad_block() # Do ad_block on slower-loading iframes
|
||||
|
@ -9944,6 +9947,7 @@ class BaseCase(unittest.TestCase):
|
|||
devtools=self.devtools,
|
||||
remote_debug=self.remote_debug,
|
||||
swiftshader=self.swiftshader,
|
||||
ad_block_on=self.ad_block_on,
|
||||
block_images=self.block_images,
|
||||
chromium_arg=self.chromium_arg,
|
||||
firefox_arg=self.firefox_arg,
|
||||
|
|
Loading…
Reference in New Issue