Add a command-line option to block images in tests

This commit is contained in:
Michael Mintz 2020-07-09 16:08:32 -04:00
parent bb2c2f32b3
commit 9e052e8259
7 changed files with 44 additions and 14 deletions

View File

@ -321,6 +321,7 @@ SeleniumBase provides additional Pytest command-line options for tests:
--message-duration=SECONDS # (The time length for Messenger alerts.)
--check-js # (The option to check for JavaScript errors after page loads.)
--ad-block # (The option to block some display ads after page loads.)
--block-images # (The option to block images from loading during tests.)
--verify-delay=SECONDS # (The delay before MasterQA verification checks.)
--disable-csp # (This disables the Content Security Policy of websites.)
--enable-sync # (The option to enable "Chrome Sync".)

View File

@ -57,16 +57,17 @@ except (ImportError, ValueError):
sb.save_screenshot_after_test = False
sb.timeout_multiplier = None
sb.pytest_html_report = None
sb.report_on = False
sb.with_db_reporting = False
sb.with_s3_logging = False
sb.js_checking_on = False
sb.report_on = False
sb.is_pytest = False
sb.time_limit = None
sb.slow_mode = False
sb.demo_mode = False
sb.time_limit = None
sb.demo_sleep = 1
sb.message_duration = 2
sb.block_images = False
sb.settings_file = None
sb.user_data_dir = None
sb.proxy_string = None

View File

@ -118,6 +118,7 @@ SeleniumBase provides additional Pytest command-line options for tests:
--message-duration=SECONDS # (The time length for Messenger alerts.)
--check-js # (The option to check for JavaScript errors after page loads.)
--ad-block # (The option to block some display ads after page loads.)
--block-images # (The option to block images from loading during tests.)
--verify-delay=SECONDS # (The delay before MasterQA verification checks.)
--disable-csp # (This disables the Content Security Policy of websites.)
--enable-sync # (The option to enable "Chrome Sync".)

View File

@ -132,7 +132,7 @@ def _set_chrome_options(
downloads_path, headless,
proxy_string, proxy_auth, proxy_user, proxy_pass,
user_agent, disable_csp, enable_sync, use_auto_ext,
no_sandbox, disable_gpu, incognito, guest_mode, devtools,
no_sandbox, disable_gpu, incognito, guest_mode, devtools, block_images,
user_data_dir, extension_zip, extension_dir, servername,
mobile_emulator, device_width, device_height, device_pixel_ratio):
chrome_options = webdriver.ChromeOptions()
@ -144,6 +144,8 @@ def _set_chrome_options(
"password_manager_enabled": False
}
}
if block_images:
prefs["profile.managed_default_content_settings.images"] = 2
chrome_options.add_experimental_option("prefs", prefs)
chrome_options.add_experimental_option("w3c", True)
if enable_sync:
@ -357,7 +359,8 @@ def get_driver(browser_name, headless=False, use_grid=False,
disable_csp=None, enable_sync=None, use_auto_ext=None,
no_sandbox=None, disable_gpu=None,
incognito=None, guest_mode=None, devtools=None,
user_data_dir=None, extension_zip=None, extension_dir=None,
block_images=None, user_data_dir=None,
extension_zip=None, extension_dir=None,
test_id=None, mobile_emulator=False, device_width=None,
device_height=None, device_pixel_ratio=None):
proxy_auth = False
@ -395,14 +398,14 @@ def get_driver(browser_name, headless=False, use_grid=False,
proxy_string, proxy_auth, proxy_user, proxy_pass, user_agent,
cap_file, cap_string, disable_csp, enable_sync, use_auto_ext,
no_sandbox, disable_gpu, incognito, guest_mode, devtools,
user_data_dir, extension_zip, extension_dir, test_id,
block_images, user_data_dir, extension_zip, extension_dir, test_id,
mobile_emulator, device_width, device_height, device_pixel_ratio)
else:
return get_local_driver(
browser_name, headless, servername,
proxy_string, proxy_auth, proxy_user, proxy_pass, user_agent,
disable_csp, enable_sync, use_auto_ext, no_sandbox, disable_gpu,
incognito, guest_mode, devtools,
incognito, guest_mode, devtools, block_images,
user_data_dir, extension_zip, extension_dir,
mobile_emulator, device_width, device_height, device_pixel_ratio)
@ -411,7 +414,7 @@ def get_remote_driver(
browser_name, headless, servername, port, proxy_string, proxy_auth,
proxy_user, proxy_pass, user_agent, cap_file, cap_string,
disable_csp, enable_sync, use_auto_ext, no_sandbox, disable_gpu,
incognito, guest_mode, devtools,
incognito, guest_mode, devtools, block_images,
user_data_dir, extension_zip, extension_dir, test_id,
mobile_emulator, device_width, device_height, device_pixel_ratio):
downloads_path = download_helper.get_downloads_folder()
@ -443,7 +446,7 @@ def get_remote_driver(
downloads_path, headless,
proxy_string, proxy_auth, proxy_user, proxy_pass, user_agent,
disable_csp, enable_sync, use_auto_ext, no_sandbox, disable_gpu,
incognito, guest_mode, devtools,
incognito, guest_mode, devtools, block_images,
user_data_dir, extension_zip, extension_dir, servername,
mobile_emulator, device_width, device_height, device_pixel_ratio)
capabilities = chrome_options.to_capabilities()
@ -567,7 +570,7 @@ def get_local_driver(
browser_name, headless, servername,
proxy_string, proxy_auth, proxy_user, proxy_pass, user_agent,
disable_csp, enable_sync, use_auto_ext, no_sandbox, disable_gpu,
incognito, guest_mode, devtools,
incognito, guest_mode, devtools, block_images,
user_data_dir, extension_zip, extension_dir,
mobile_emulator, device_width, device_height, device_pixel_ratio):
'''
@ -658,7 +661,8 @@ def get_local_driver(
proxy_string, proxy_auth, proxy_user, proxy_pass, user_agent,
disable_csp, enable_sync, use_auto_ext,
no_sandbox, disable_gpu, incognito, guest_mode, devtools,
user_data_dir, extension_zip, extension_dir, servername,
block_images, user_data_dir,
extension_zip, extension_dir, servername,
mobile_emulator, device_width, device_height,
device_pixel_ratio)
if LOCAL_EDGEDRIVER and os.path.exists(LOCAL_EDGEDRIVER):
@ -715,8 +719,8 @@ def get_local_driver(
proxy_string, proxy_auth, proxy_user, proxy_pass, user_agent,
disable_csp, enable_sync, use_auto_ext,
no_sandbox, disable_gpu, incognito, guest_mode, devtools,
user_data_dir, extension_zip, extension_dir, servername,
mobile_emulator, device_width, device_height,
block_images, user_data_dir, extension_zip, extension_dir,
servername, mobile_emulator, device_width, device_height,
device_pixel_ratio)
if LOCAL_CHROMEDRIVER and os.path.exists(LOCAL_CHROMEDRIVER):
try:

View File

@ -1592,8 +1592,8 @@ class BaseCase(unittest.TestCase):
disable_csp=None, enable_sync=None, use_auto_ext=None,
no_sandbox=None, disable_gpu=None,
incognito=None, guest_mode=None, devtools=None,
user_data_dir=None, extension_zip=None,
extension_dir=None, is_mobile=False,
block_images=None, user_data_dir=None,
extension_zip=None, extension_dir=None, is_mobile=False,
d_width=None, d_height=None, d_p_r=None):
""" This method spins up an extra browser for tests that require
more than one. The first browser is already provided by tests
@ -1616,6 +1616,7 @@ class BaseCase(unittest.TestCase):
incognito - the option to enable Chrome's Incognito mode (Chrome)
guest - the option to enable Chrome's Guest mode (Chrome)
devtools - the option to open Chrome's DevTools on start (Chrome)
block_images - the option to block images from loading (Chrome)
user_data_dir - Chrome's User Data Directory to use (Chrome-only)
extension_zip - A Chrome Extension ZIP file to use (Chrome-only)
extension_dir - A Chrome Extension folder to use (Chrome-only)
@ -1680,6 +1681,8 @@ class BaseCase(unittest.TestCase):
guest_mode = self.guest_mode
if devtools is None:
devtools = self.devtools
if block_images is None:
block_images = self.block_images
if user_data_dir is None:
user_data_dir = self.user_data_dir
if extension_zip is None:
@ -1725,6 +1728,7 @@ class BaseCase(unittest.TestCase):
incognito=incognito,
guest_mode=guest_mode,
devtools=devtools,
block_images=block_images,
user_data_dir=user_data_dir,
extension_zip=extension_zip,
extension_dir=extension_dir,
@ -5269,6 +5273,7 @@ class BaseCase(unittest.TestCase):
self.message_duration = sb_config.message_duration
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.verify_delay = sb_config.verify_delay
self.disable_csp = sb_config.disable_csp
self.enable_sync = sb_config.enable_sync
@ -5440,6 +5445,7 @@ class BaseCase(unittest.TestCase):
incognito=self.incognito,
guest_mode=self.guest_mode,
devtools=self.devtools,
block_images=self.block_images,
user_data_dir=self.user_data_dir,
extension_zip=self.extension_zip,
extension_dir=self.extension_dir,

View File

@ -42,6 +42,7 @@ def pytest_addoption(parser):
--message-duration=SECONDS (The time length for Messenger alerts.)
--check-js (The option to check for JavaScript errors after page loads.)
--ad-block (The option to block some display ads after page loads.)
--block-images (The option to block images from loading during tests.)
--verify-delay=SECONDS (The delay before MasterQA verification checks.)
--disable-csp (This disables the Content Security Policy of websites.)
--enable-sync (The option to enable "Chrome Sync".)
@ -330,6 +331,12 @@ def pytest_addoption(parser):
default=False,
help="""Using this makes WebDriver block display ads
that are defined in ad_block_list.AD_BLOCK_LIST.""")
parser.addoption('--block_images', '--block-images',
action="store_true",
dest='block_images',
default=False,
help="""Using this makes WebDriver block images from
loading on web pages during tests.""")
parser.addoption('--verify_delay', '--verify-delay',
action='store',
dest='verify_delay',
@ -477,6 +484,7 @@ def pytest_configure(config):
sb_config.message_duration = config.getoption('message_duration')
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.verify_delay = config.getoption('verify_delay')
sb_config.disable_csp = config.getoption('disable_csp')
sb_config.enable_sync = config.getoption('enable_sync')

View File

@ -33,6 +33,7 @@ class SeleniumBrowser(Plugin):
--message-duration=SECONDS (The time length for Messenger alerts.)
--check-js (The option to check for JavaScript errors after page loads.)
--ad-block (The option to block some display ads after page loads.)
--block-images (The option to block images from loading during tests.)
--verify-delay=SECONDS (The delay before MasterQA verification checks.)
--disable-csp (This disables the Content Security Policy of websites.)
--enable-sync (The option to enable "Chrome Sync".)
@ -240,6 +241,13 @@ class SeleniumBrowser(Plugin):
default=False,
help="""Using this makes WebDriver block display ads
that are defined in ad_block_list.AD_BLOCK_LIST.""")
parser.add_option(
'--block_images', '--block-images',
action="store_true",
dest='block_images',
default=False,
help="""Using this makes WebDriver block images from
loading on web pages during tests.""")
parser.add_option(
'--verify_delay', '--verify-delay',
action='store',
@ -367,6 +375,7 @@ class SeleniumBrowser(Plugin):
test.test.message_duration = self.options.message_duration
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.verify_delay = self.options.verify_delay # MasterQA
test.test.disable_csp = self.options.disable_csp
test.test.enable_sync = self.options.enable_sync