Bug 1322383 - Ensure that finalizers for webdriver tests always work on a valid window. draft
authorHenrik Skupin <mail@hskupin.info>
Tue, 21 Mar 2017 12:02:47 +0100
changeset 502991 b06f54ca5a3cf372b7ab86636bdb8b3978418a4b
parent 502990 270e688f21a0439329766cc1f71b574854af21ec
child 502992 8aa93c5827ee321b03e94778c9d605be332a01cb
push id50441
push userbmo:hskupin@gmail.com
push dateWed, 22 Mar 2017 15:51:36 +0000
bugs1322383
milestone55.0a1
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
testing/web-platform/tests/webdriver/conftest.py
testing/web-platform/tests/webdriver/util/cleanup.py
--- 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)