Bug 1322383 - Ensure that finalizers for webdriver tests always work on a valid window.
In case of tests are closing the current window, and do not switch back to
a valid window, the finalizers will fail because the window to operate on
doesn't exist anymore.
MozReview-Commit-ID: 8tX6oK45530
--- a/testing/web-platform/tests/webdriver/conftest.py
+++ b/testing/web-platform/tests/webdriver/conftest.py
@@ -31,16 +31,17 @@ def _session(request):
@pytest.fixture(scope="function")
def session(_session, request):
# finalisers are popped off a stack,
# making their ordering reverse
request.addfinalizer(lambda: cleanup.switch_to_top_level_browsing_context(_session))
request.addfinalizer(lambda: cleanup.restore_windows(_session))
request.addfinalizer(lambda: cleanup.dismiss_user_prompts(_session))
+ request.addfinalizer(lambda: cleanup.ensure_valid_window(_session))
return _session
@pytest.fixture(scope="function")
def http(session):
return HTTPRequest(session.transport.host, session.transport.port)
--- a/testing/web-platform/tests/webdriver/util/cleanup.py
+++ b/testing/web-platform/tests/webdriver/util/cleanup.py
@@ -1,43 +1,55 @@
import webdriver
+
+def ensure_valid_window(session):
+ """If current window is not open anymore, ensure to have a valid one selected."""
+ try:
+ session.window_handle
+ except webdriver.NoSuchWindowException:
+ session.window_handle = session.handles[0]
+
+
def dismiss_user_prompts(session):
"""Dismisses any open user prompts in windows."""
current_window = session.window_handle
for window in _windows(session):
session.window_handle = window
try:
session.alert.dismiss()
except webdriver.NoSuchAlertException:
pass
session.window_handle = current_window
+
def restore_windows(session):
"""Closes superfluous windows opened by the test without ending
the session implicitly by closing the last window.
"""
current_window = session.window_handle
for window in _windows(session, exclude=[current_window]):
session.window_handle = window
if len(session.handles) > 1:
session.close()
session.window_handle = current_window
+
def switch_to_top_level_browsing_context(session):
"""If the current browsing context selected by WebDriver is a
`<frame>` or an `<iframe>`, switch it back to the top-level
browsing context.
"""
session.switch_frame(None)
+
def _windows(session, exclude=None):
"""Set of window handles, filtered by an `exclude` list if
provided.
"""
if exclude is None:
exclude = []
wins = [w for w in session.handles if w not in exclude]
return set(wins)