Add ways to disable "beforeunload"

This commit is contained in:
Michael Mintz 2022-08-23 21:14:00 -04:00
parent 13e689bade
commit aeb36bf9a8
8 changed files with 66 additions and 1 deletions

View File

@ -504,6 +504,7 @@ The code above will leave your browser window open in case there's a failure. (i
--devtools # (Open Chrome's DevTools when the browser opens.)
--reuse-session | --rs # (Reuse the browser session between tests.)
--crumbs # (Delete all cookies between tests reusing a session.)
--disable-beforeunload # (Disable the "beforeunload" event on Chrome.)
--window-size=WIDTH,HEIGHT # (Set the browser's starting window size.)
--maximize # (Start tests with the browser window maximized.)
--screenshot # (Save a screenshot at the end of each test.)

View File

@ -72,6 +72,7 @@ if pure_python:
sb.visual_baseline = False
sb.window_size = None
sb.maximize_option = False
sb._disable_beforeunload = False
sb.save_screenshot_after_test = False
sb.timeout_multiplier = None
sb.pytest_html_report = None

View File

@ -170,6 +170,7 @@ pytest my_first_test.py --settings-file=custom_settings.py
--devtools # (Open Chrome's DevTools when the browser opens.)
--reuse-session | --rs # (Reuse the browser session between tests.)
--crumbs # (Delete all cookies between tests reusing a session.)
--disable-beforeunload # (Disable the "beforeunload" event on Chrome.)
--window-size=WIDTH,HEIGHT # (Set the browser's starting window size.)
--maximize # (Start tests with the browser window maximized.)
--screenshot # (Save a screenshot at the end of each test.)

View File

@ -375,6 +375,8 @@ self.ad_block()
self.show_file_choosers()
self.disable_beforeunload()
self.get_domain_url(url)
self.get_beautiful_soup(source=None)

View File

@ -79,7 +79,8 @@ behave -D agent="User Agent String" -D demo
-D devtools (Open Chrome's DevTools when the browser opens.)
-D reuse-session | -D rs (Reuse browser session between tests.)
-D crumbs (Delete all cookies between tests reusing a session.)
-D window-size (Set the browser window size: "Width,Height".)
-D disable-beforeunload (Disable the "beforeunload" event on Chrome.)
-D window-size=WIDTH,HEIGHT (Set the browser's starting window size.)
-D maximize (Start tests with the browser window maximized.)
-D screenshot (Save a screenshot at the end of each test.)
-D visual-baseline (Set the visual baseline for Visual/Layout tests.)
@ -165,6 +166,7 @@ def get_configured_sb(context):
sb._multithreaded = False
sb._reuse_session = False
sb._crumbs = False
sb._disable_beforeunload = False
sb.visual_baseline = False
sb.window_size = None
sb.maximize_option = False
@ -475,6 +477,10 @@ def get_configured_sb(context):
if low_key == "crumbs":
sb._crumbs = True
continue
# Handle: -D disable-beforeunload / disable_beforeunload
if low_key in ["disable-beforeunload", "disable_beforeunload"]:
sb._disable_beforeunload = True
continue
# Handle: -D visual-baseline / visual_baseline
if low_key in ["visual-baseline", "visual_baseline"]:
sb.visual_baseline = True

View File

@ -445,6 +445,7 @@ class BaseCase(unittest.TestCase):
try:
if self.driver.current_url != pre_action_url:
self.__ad_block_as_needed()
self.__disable_beforeunload_as_needed()
except Exception:
try:
self.wait_for_ready_state_complete()
@ -544,6 +545,7 @@ class BaseCase(unittest.TestCase):
self.wait_for_angularjs(timeout=settings.MINI_TIMEOUT)
if self.driver.current_url != pre_action_url:
self.__ad_block_as_needed()
self.__disable_beforeunload_as_needed()
if self.demo_mode:
if self.driver.current_url != pre_action_url:
self.__demo_mode_pause_if_active()
@ -1915,6 +1917,7 @@ class BaseCase(unittest.TestCase):
self.wait_for_angularjs(timeout=settings.MINI_TIMEOUT)
if self.driver.current_url != pre_action_url:
self.__ad_block_as_needed()
self.__disable_beforeunload_as_needed()
if self.demo_mode:
if self.driver.current_url != pre_action_url:
self.__demo_mode_pause_if_active()
@ -2505,6 +2508,9 @@ class BaseCase(unittest.TestCase):
else:
# A smaller subset of self.wait_for_ready_state_complete()
self.wait_for_angularjs(timeout=settings.MINI_TIMEOUT)
if self.driver.current_url != pre_action_url:
self.__ad_block_as_needed()
self.__disable_beforeunload_as_needed()
if self.demo_mode:
if self.driver.current_url != pre_action_url:
self.__demo_mode_pause_if_active()
@ -3646,6 +3652,7 @@ class BaseCase(unittest.TestCase):
if self.js_checking_on:
self.assert_no_js_errors()
self.__ad_block_as_needed()
self.__disable_beforeunload_as_needed()
return True
def wait_for_angularjs(self, timeout=None, **kwargs):
@ -5434,6 +5441,28 @@ class BaseCase(unittest.TestCase):
action = ["sh_fc", "", origin, time_stamp]
self.__extra_actions.append(action)
def disable_beforeunload(self):
"""This prevents: "Leave Site? Changes you made may not be saved."
on Chromium browsers (Chrome or Edge).
SB already sets "dom.disable_beforeunload" for Firefox options."""
self.__check_scope()
self.__check_browser()
if (
self.is_chromium()
and self.driver.current_url.startswith("http")
):
try:
self.driver.execute_script("window.onbeforeunload=null;")
except Exception:
pass
def __disable_beforeunload_as_needed(self):
if (
hasattr(self, "_disable_beforeunload")
and self._disable_beforeunload
):
self.disable_beforeunload()
def get_domain_url(self, url):
self.__check_scope()
return page_utils.get_domain_url(url)
@ -12454,6 +12483,7 @@ class BaseCase(unittest.TestCase):
self._multithreaded = sb_config._multithreaded
self._reuse_session = sb_config.reuse_session
self._crumbs = sb_config.crumbs
self._disable_beforeunload = sb_config._disable_beforeunload
self.dashboard = sb_config.dashboard
self._dash_initialized = sb_config._dashboard_initialized
if self.dashboard and self._multithreaded:

View File

@ -92,6 +92,7 @@ def pytest_addoption(parser):
--devtools (Open Chrome's DevTools when the browser opens.)
--reuse-session | --rs (Reuse browser session between tests.)
--crumbs (Delete all cookies between tests reusing a session.)
--disable-beforeunload (Disable the "beforeunload" event on Chrome.)
--window-size=WIDTH,HEIGHT (Set the browser's starting window size.)
--maximize (Start tests with the browser window maximized.)
--screenshot (Save a screenshot at the end of each test.)
@ -952,6 +953,16 @@ def pytest_addoption(parser):
that reuse the same browser session. This option
is only needed when using "--reuse-session".""",
)
parser.addoption(
"--disable-beforeunload",
"--disable_beforeunload",
action="store_true",
dest="_disable_beforeunload",
default=False,
help="""The option to disable the "beforeunload" event
on Chromium browsers (Chrome or Edge).
This is already the default Firefox option.""",
)
parser.addoption(
"--window-size",
"--window_size",
@ -1279,6 +1290,7 @@ def pytest_configure(config):
sb_config.devtools = config.getoption("devtools")
sb_config.reuse_session = config.getoption("reuse_session")
sb_config.crumbs = config.getoption("crumbs")
sb_config._disable_beforeunload = config.getoption("_disable_beforeunload")
sb_config.shared_driver = None # The default driver for session reuse
sb_config.window_size = config.getoption("window_size")
sb_config.maximize_option = config.getoption("maximize_option")

View File

@ -68,6 +68,7 @@ class SeleniumBrowser(Plugin):
--incognito (Enable Chrome's Incognito mode.)
--guest (Enable Chrome's Guest mode.)
--devtools (Open Chrome's DevTools when the browser opens.)
--disable-beforeunload (Disable the "beforeunload" event on Chrome.)
--window-size=WIDTH,HEIGHT (Set the browser's starting window size.)
--maximize (Start tests with the browser window maximized.)
--screenshot (Save a screenshot at the end of each test.)
@ -644,6 +645,16 @@ class SeleniumBrowser(Plugin):
default=False,
help="""Using this opens Chrome's DevTools.""",
)
parser.add_option(
"--disable-beforeunload",
"--disable_beforeunload",
action="store_true",
dest="_disable_beforeunload",
default=False,
help="""The option to disable the "beforeunload" event
on Chromium browsers (Chrome or Edge).
This is already the default Firefox option.""",
)
parser.add_option(
"--window-size",
"--window_size",
@ -814,6 +825,7 @@ class SeleniumBrowser(Plugin):
test.test.incognito = self.options.incognito
test.test.guest_mode = self.options.guest_mode
test.test.devtools = self.options.devtools
test.test._disable_beforeunload = self.options._disable_beforeunload
test.test.window_size = self.options.window_size
test.test.maximize_option = self.options.maximize_option
test.test.save_screenshot_after_test = self.options.save_screenshot