Add option to change Chromium's "pageLoadStrategy"

This commit is contained in:
Michael Mintz 2022-08-26 21:44:21 -04:00
parent 15dda292cc
commit 235d9bd4fe
7 changed files with 130 additions and 0 deletions

View File

@ -41,6 +41,7 @@ behave -D agent="User Agent String" -D demo
-D firefox-pref=SET (Set a Firefox preference:value set, comma-separated.)
-D extension-zip=ZIP (Load a Chrome Extension .zip|.crx, comma-separated.)
-D extension-dir=DIR (Load a Chrome Extension directory, comma-separated.)
-D pls=PLS (Set pageLoadStrategy on Chrome: "normal", "eager", or "none".)
-D headless (Run tests in headless mode. The default arg on Linux OS.)
-D headed (Run tests in headed/GUI mode on Linux OS.)
-D xvfb (Run tests using the Xvfb virtual display server on Linux OS.)
@ -153,6 +154,7 @@ def get_configured_sb(context):
sb.device_metrics = None
sb.extension_zip = None
sb.extension_dir = None
sb.page_load_strategy = None
sb.database_env = "test"
sb.log_path = "latest_logs/"
sb.archive_logs = False
@ -212,6 +214,7 @@ def get_configured_sb(context):
# Set a few sb_config vars early in case parsing args fails
sb_config.dashboard = None
sb_config._has_logs = None
sb_config._has_exception = None
sb_config.save_screenshot = None
@ -418,6 +421,24 @@ def get_configured_sb(context):
extension_dir = sb.extension_dir # revert to default
sb.extension_dir = extension_dir
continue
# Handle: -D pls=PLS / page-load-strategy=PLS / page_load_strategy=PLS
if low_key in ["pls", "page-load-strategy", "page_load_strategy"]:
page_load_strategy = userdata[key].lower()
if page_load_strategy in ["normal", "eager", "none"]:
sb.page_load_strategy = page_load_strategy
elif page_load_strategy == "true":
raise Exception(
'\nThe "pls" / "page-load-strategy" arg requires a value!'
'\nChoose from ["normal", "eager", "none"]'
'\nEg. -D pls="none"'
)
else:
raise Exception(
'\n"%s" is not a valid "pls" / "page-load-strategy" value!'
'\nChoose from ["normal", "eager", "none"]'
'\nEg. -D pls="none"' % page_load_strategy
)
continue
# Handle: -D database-env=ENVIRONMENT / database_env=ENVIRONMENT
if low_key in ["database-env", "database_env"]:
database_env = userdata[key].lower()

View File

@ -16,6 +16,18 @@ SMALL_TIMEOUT = 7
LARGE_TIMEOUT = 10
EXTREME_TIMEOUT = 30
# Default page load timeout.
# If a page takes longer than this to load, you'll get the following error:
# selenium.common.exceptions.TimeoutException:
# Message: timeout: Timed out receiving message from renderer: PLT
# In global Selenium settings, this value is set to 300 seconds by default.
PAGE_LOAD_TIMEOUT = 120
# Default page load strategy.
# ["normal", "eager", "none"]
# Selenium default = "normal"
PAGE_LOAD_STRATEGY = "normal"
# If True, existing logs from past test runs will be saved and take up space.
# If False, only the logs from the most recent test run will be saved locally.
# You can also archive existing logs on the command line with: "--archive_logs"

View File

@ -283,6 +283,7 @@ def _set_chrome_options(
user_data_dir,
extension_zip,
extension_dir,
page_load_strategy,
external_pdf,
servername,
mobile_emulator,
@ -439,6 +440,22 @@ def _set_chrome_options(
chrome_options.add_argument("--disable-hang-monitor")
chrome_options.add_argument("--disable-prompt-on-repost")
chrome_options.add_argument("--disable-3d-apis")
if (
selenium4_or_newer
and page_load_strategy
and page_load_strategy.lower() in ["eager", "none"]
):
# Only change it if not "normal", which is the default.
chrome_options.page_load_strategy = page_load_strategy.lower()
elif (
selenium4_or_newer
and not page_load_strategy
and hasattr(settings, "PAGE_LOAD_STRATEGY")
and settings.PAGE_LOAD_STRATEGY
and settings.PAGE_LOAD_STRATEGY.lower() in ["eager", "none"]
):
# Only change it if not "normal", which is the default.
chrome_options.page_load_strategy = settings.PAGE_LOAD_STRATEGY.lower()
if servername != "localhost":
use_auto_ext = True # Use Automation Extension with the Selenium Grid
if not use_auto_ext: # Disable Automation Extension / detection. (Default)
@ -768,6 +785,7 @@ def get_driver(
user_data_dir=None,
extension_zip=None,
extension_dir=None,
page_load_strategy=None,
external_pdf=None,
test_id=None,
mobile_emulator=False,
@ -873,6 +891,7 @@ def get_driver(
user_data_dir,
extension_zip,
extension_dir,
page_load_strategy,
external_pdf,
test_id,
mobile_emulator,
@ -914,6 +933,7 @@ def get_driver(
user_data_dir,
extension_zip,
extension_dir,
page_load_strategy,
external_pdf,
mobile_emulator,
device_width,
@ -959,6 +979,7 @@ def get_remote_driver(
user_data_dir,
extension_zip,
extension_dir,
page_load_strategy,
external_pdf,
test_id,
mobile_emulator,
@ -1052,6 +1073,7 @@ def get_remote_driver(
user_data_dir,
extension_zip,
extension_dir,
page_load_strategy,
external_pdf,
servername,
mobile_emulator,
@ -1275,6 +1297,7 @@ def get_remote_driver(
user_data_dir,
extension_zip,
extension_dir,
page_load_strategy,
external_pdf,
servername,
mobile_emulator,
@ -1467,6 +1490,7 @@ def get_local_driver(
user_data_dir,
extension_zip,
extension_dir,
page_load_strategy,
external_pdf,
mobile_emulator,
device_width,
@ -1808,6 +1832,24 @@ def get_local_driver(
edge_options.add_argument("--disable-hang-monitor")
edge_options.add_argument("--disable-prompt-on-repost")
edge_options.add_argument("--disable-3d-apis")
if (
selenium4_or_newer
and page_load_strategy
and page_load_strategy.lower() in ["eager", "none"]
):
# Only change it if not "normal", which is the default.
edge_options.page_load_strategy = page_load_strategy.lower()
elif (
selenium4_or_newer
and not page_load_strategy
and hasattr(settings, "PAGE_LOAD_STRATEGY")
and settings.PAGE_LOAD_STRATEGY
and settings.PAGE_LOAD_STRATEGY.lower() in ["eager", "none"]
):
# Only change it if not "normal", which is the default.
edge_options.page_load_strategy = (
settings.PAGE_LOAD_STRATEGY.lower()
)
if (settings.DISABLE_CSP_ON_CHROME or disable_csp) and not headless:
# Headless Edge doesn't support extensions, which are required
# for disabling the Content Security Policy on Edge
@ -2029,6 +2071,7 @@ def get_local_driver(
user_data_dir,
extension_zip,
extension_dir,
page_load_strategy,
external_pdf,
servername,
mobile_emulator,
@ -2086,6 +2129,7 @@ def get_local_driver(
user_data_dir,
extension_zip,
extension_dir,
page_load_strategy,
external_pdf,
servername,
mobile_emulator,
@ -2220,6 +2264,7 @@ def get_local_driver(
user_data_dir,
extension_zip,
extension_dir,
page_load_strategy,
external_pdf,
servername,
mobile_emulator,

View File

@ -3171,6 +3171,7 @@ class BaseCase(unittest.TestCase):
user_data_dir=None,
extension_zip=None,
extension_dir=None,
page_load_strategy=None,
external_pdf=None,
is_mobile=None,
d_width=None,
@ -3215,6 +3216,7 @@ class BaseCase(unittest.TestCase):
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)
page_load_strategy - the option to change pageLoadStrategy (Chrome)
external_pdf - "plugins.always_open_pdf_externally": True. (Chrome)
is_mobile - the option to use the mobile emulator (Chrome-only)
d_width - the device width of the mobile emulator (Chrome-only)
@ -3319,6 +3321,8 @@ class BaseCase(unittest.TestCase):
extension_zip = self.extension_zip
if extension_dir is None:
extension_dir = self.extension_dir
if page_load_strategy is None:
page_load_strategy = self.page_load_strategy
if external_pdf is None:
external_pdf = self.external_pdf
test_id = self.__get_test_id()
@ -3378,6 +3382,7 @@ class BaseCase(unittest.TestCase):
user_data_dir=user_data_dir,
extension_zip=extension_zip,
extension_dir=extension_dir,
page_load_strategy=page_load_strategy,
external_pdf=external_pdf,
test_id=test_id,
mobile_emulator=is_mobile,
@ -12528,6 +12533,7 @@ class BaseCase(unittest.TestCase):
self.user_data_dir = sb_config.user_data_dir
self.extension_zip = sb_config.extension_zip
self.extension_dir = sb_config.extension_dir
self.page_load_strategy = sb_config.page_load_strategy
self.external_pdf = sb_config.external_pdf
self._final_debug = sb_config.final_debug
self.window_size = sb_config.window_size
@ -12809,6 +12815,7 @@ class BaseCase(unittest.TestCase):
user_data_dir=self.user_data_dir,
extension_zip=self.extension_zip,
extension_dir=self.extension_dir,
page_load_strategy=self.page_load_strategy,
external_pdf=self.external_pdf,
is_mobile=self.mobile_emulator,
d_width=self.__device_width,

View File

@ -37,6 +37,13 @@ class ValidEnvs:
]
class PageLoadStrategy:
# Usage Example => "--pls=none"
NORMAL = "normal"
EAGER = "eager"
NONE = "none"
class Files:
DOWNLOADS_FOLDER = "downloaded_files"
ARCHIVED_DOWNLOADS_FOLDER = "archived_files"

View File

@ -56,6 +56,7 @@ def pytest_addoption(parser):
--firefox-pref=SET (Set a Firefox preference:value set, comma-separated.)
--extension-zip=ZIP (Load a Chrome Extension .zip|.crx, comma-separated.)
--extension-dir=DIR (Load a Chrome Extension directory, comma-separated.)
--pls=PLS (Set pageLoadStrategy on Chrome: "normal", "eager", or "none".)
--headless (Run tests in headless mode. The default arg on Linux OS.)
--headed (Run tests in headed/GUI mode on Linux OS.)
--xvfb (Run tests using the Xvfb virtual display server on Linux OS.)
@ -565,6 +566,22 @@ def pytest_addoption(parser):
(Can also be a comma-separated list of directories.)
Default: None.""",
)
parser.addoption(
"--pls",
"--page_load_strategy",
"--page-load-strategy",
action="store",
dest="page_load_strategy",
type=str.lower,
choices=(
constants.PageLoadStrategy.NORMAL,
constants.PageLoadStrategy.EAGER,
constants.PageLoadStrategy.NONE,
),
default=None,
help="""This option sets Chrome's pageLoadStrategy.
List of choices: "normal", "eager", "none".""",
)
parser.addoption(
"--headless",
action="store_true",
@ -1221,6 +1238,7 @@ def pytest_configure(config):
sb_config.firefox_pref = config.getoption("firefox_pref")
sb_config.extension_zip = config.getoption("extension_zip")
sb_config.extension_dir = config.getoption("extension_dir")
sb_config.page_load_strategy = config.getoption("page_load_strategy")
sb_config.with_testing_base = config.getoption("with_testing_base")
sb_config.with_db_reporting = config.getoption("with_db_reporting")
sb_config.with_s3_logging = config.getoption("with_s3_logging")

View File

@ -38,12 +38,14 @@ class SeleniumBrowser(Plugin):
--firefox-pref=SET (Set a Firefox preference:value set, comma-separated.)
--extension-zip=ZIP (Load a Chrome Extension .zip|.crx, comma-separated.)
--extension-dir=DIR (Load a Chrome Extension directory, comma-separated.)
--pls=PLS (Set pageLoadStrategy on Chrome: "normal", "eager", or "none".)
--headless (Run tests in headless mode. The default arg on Linux OS.)
--headed (Run tests in headed/GUI mode on Linux OS.)
--xvfb (Run tests using the Xvfb virtual display server on Linux OS.)
--locale=LOCALE_CODE (Set the Language Locale Code for the web browser.)
--interval=SECONDS (The autoplay interval for presentations & tour steps)
--start-page=URL (The starting URL for the web browser when tests begin.)
--skip-js-waits (Skip waiting for readyState to be complete or Angular.)
--time-limit=SECONDS (Safely fail any test that exceeds the time limit.)
--slow (Slow down the automation. Faster than using Demo Mode.)
--demo (Slow down and visually see test actions as they occur.)
@ -304,6 +306,21 @@ class SeleniumBrowser(Plugin):
(Can also be a comma-separated list of directories.)
Default: None.""",
)
parser.add_option(
"--pls",
"--page_load_strategy",
"--page-load-strategy",
action="store",
dest="page_load_strategy",
choices=(
constants.PageLoadStrategy.NORMAL,
constants.PageLoadStrategy.EAGER,
constants.PageLoadStrategy.NONE,
),
default=None,
help="""This option sets Chrome's pageLoadStrategy.
List of choices: "normal", "eager", "none".""",
)
parser.add_option(
"--headless",
action="store_true",
@ -772,12 +789,15 @@ class SeleniumBrowser(Plugin):
test.test.locale_code = self.options.locale_code
test.test.interval = self.options.interval
test.test.start_page = self.options.start_page
if self.options.skip_js_waits:
settings.SKIP_JS_WAITS = True
test.test.protocol = self.options.protocol
test.test.servername = self.options.servername
test.test.port = self.options.port
test.test.user_data_dir = self.options.user_data_dir
test.test.extension_zip = self.options.extension_zip
test.test.extension_dir = self.options.extension_dir
test.test.page_load_strategy = self.options.page_load_strategy
test.test.chromium_arg = self.options.chromium_arg
test.test.firefox_arg = self.options.firefox_arg
test.test.firefox_pref = self.options.firefox_pref