Bug 1322383 - Ensure that finalizers for webdriver tests always work on a valid window. r=jgraham a=test-only
authorHenrik Skupin <mail@hskupin.info>
Tue, 21 Mar 2017 12:02:47 +0100
changeset 395346 1d5124bd2bcb9d7fb5012c45b13312bc9f20a211
parent 395345 1156fbdde40cd633b6a8fb4311de963d42af396e
child 395347 c64597242ffdedf65b2ad4545f2c2b343cbfe04a
push id1468
push userasasaki@mozilla.com
push dateMon, 05 Jun 2017 19:31:07 +0000
treeherdermozilla-release@0641fc6ee9d1 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjgraham, test-only
bugs1322383
milestone54.0a2
Bug 1322383 - Ensure that finalizers for webdriver tests always work on a valid window. r=jgraham a=test-only 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
@@ -28,14 +28,15 @@ 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.window_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)