Bug 1322383 - Ensure that finalizers always work on a valid window. r=jgraham

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

--HG--
extra : rebase_source : aafbaa7a3ca1b9b478626f234d33fda3f0168925
This commit is contained in:
Henrik Skupin 2017-03-21 12:02:47 +01:00
parent ba50caf91b
commit d258b98f2c
2 changed files with 13 additions and 0 deletions

View File

@ -36,6 +36,7 @@ def session(_session, request):
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

View File

@ -1,5 +1,14 @@
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
@ -13,6 +22,7 @@ def dismiss_user_prompts(session):
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.
@ -26,6 +36,7 @@ def restore_windows(session):
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
@ -33,6 +44,7 @@ def switch_to_top_level_browsing_context(session):
"""
session.switch_frame(None)
def _windows(session, exclude=None):
"""Set of window handles, filtered by an `exclude` list if
provided.