From 9e052e82599bb76f37315331ad6f771382f99d85 Mon Sep 17 00:00:00 2001 From: Michael Mintz Date: Thu, 9 Jul 2020 16:08:32 -0400 Subject: [PATCH] Add a command-line option to block images in tests --- README.md | 1 + examples/raw_parameter_script.py | 5 +++-- help_docs/customizing_test_runs.md | 1 + seleniumbase/core/browser_launcher.py | 24 ++++++++++++++---------- seleniumbase/fixtures/base_case.py | 10 ++++++++-- seleniumbase/plugins/pytest_plugin.py | 8 ++++++++ seleniumbase/plugins/selenium_plugin.py | 9 +++++++++ 7 files changed, 44 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 1466374f..a510c668 100755 --- a/README.md +++ b/README.md @@ -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".) diff --git a/examples/raw_parameter_script.py b/examples/raw_parameter_script.py index 3053afc2..7db419c8 100755 --- a/examples/raw_parameter_script.py +++ b/examples/raw_parameter_script.py @@ -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 diff --git a/help_docs/customizing_test_runs.md b/help_docs/customizing_test_runs.md index 46ac165b..d760b52a 100755 --- a/help_docs/customizing_test_runs.md +++ b/help_docs/customizing_test_runs.md @@ -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".) diff --git a/seleniumbase/core/browser_launcher.py b/seleniumbase/core/browser_launcher.py index f4c6f87b..77012b67 100755 --- a/seleniumbase/core/browser_launcher.py +++ b/seleniumbase/core/browser_launcher.py @@ -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: diff --git a/seleniumbase/fixtures/base_case.py b/seleniumbase/fixtures/base_case.py index 459a4a5e..37b55b82 100755 --- a/seleniumbase/fixtures/base_case.py +++ b/seleniumbase/fixtures/base_case.py @@ -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, diff --git a/seleniumbase/plugins/pytest_plugin.py b/seleniumbase/plugins/pytest_plugin.py index d7112f91..4ebc7c7c 100644 --- a/seleniumbase/plugins/pytest_plugin.py +++ b/seleniumbase/plugins/pytest_plugin.py @@ -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') diff --git a/seleniumbase/plugins/selenium_plugin.py b/seleniumbase/plugins/selenium_plugin.py index da282ac2..de4fbb30 100755 --- a/seleniumbase/plugins/selenium_plugin.py +++ b/seleniumbase/plugins/selenium_plugin.py @@ -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