Bug 1470530 - [wdspec] Refactor "session" fixture for clean-up steps. draft
authorHenrik Skupin <mail@hskupin.info>
Wed, 27 Jun 2018 16:57:51 +0200
changeset 813915 2f80f81f5ed38e31ffd0696be6f34e4f0929a66a
parent 813914 eb61eb6c1a8ca73f6da5864f298f18faaf2009b2
push id115045
push userbmo:hskupin@gmail.com
push dateWed, 04 Jul 2018 04:53:58 +0000
bugs1470530
milestone63.0a1
Bug 1470530 - [wdspec] Refactor "session" fixture for clean-up steps. By using yield for returning the session, there is no need to define all the finalizers. Instead those can be called right after the line with yield. To ease the usage of clean-up all individual clean-up methods are now called by the "cleanup_session" method in the correct order. MozReview-Commit-ID: 1jsLDjEhkQp
testing/web-platform/meta/MANIFEST.json
testing/web-platform/tests/webdriver/tests/support/fixtures.py
--- a/testing/web-platform/meta/MANIFEST.json
+++ b/testing/web-platform/meta/MANIFEST.json
@@ -620415,17 +620415,17 @@
    "5a31a3917a5157516c10951a3b3d5ffb43b992d9",
    "support"
   ],
   "webdriver/tests/support/asserts.py": [
    "b7424061b41d9bb1b87f147d5b29786695249d10",
    "support"
   ],
   "webdriver/tests/support/fixtures.py": [
-   "d24a7f39787d4d950743bafe9910ea21a54b34ec",
+   "fd53a1ed47cb430351c568227c2bc65a65374a14",
    "support"
   ],
   "webdriver/tests/support/http_request.py": [
    "cb40c781fea2280b98135522def5e6a116d7b946",
    "support"
   ],
   "webdriver/tests/support/inline.py": [
    "48399821b7abca50df824e37c41829d7a4573be1",
--- a/testing/web-platform/tests/webdriver/tests/support/fixtures.py
+++ b/testing/web-platform/tests/webdriver/tests/support/fixtures.py
@@ -29,73 +29,77 @@ def ignore_exceptions(f):
         try:
             return f(*args, **kwargs)
         except webdriver.error.WebDriverException as e:
             print("Ignored exception %s" % e, file=sys.stderr)
     inner.__name__ = f.__name__
     return inner
 
 
-@ignore_exceptions
-def _ensure_valid_window(session):
-    """If current window is not open anymore, ensure to have a valid
-    one selected.
+def cleanup_session(session):
+    """Clean-up the current session for a clean state."""
+    @ignore_exceptions
+    def _dismiss_user_prompts(session):
+        """Dismiss any open user prompts in windows."""
+        current_window = session.window_handle
 
-    """
-    try:
-        session.window_handle
-    except webdriver.NoSuchWindowException:
-        session.window_handle = session.handles[0]
-
+        for window in _windows(session):
+            session.window_handle = window
+            try:
+                session.alert.dismiss()
+            except webdriver.NoSuchAlertException:
+                pass
 
-@ignore_exceptions
-def _dismiss_user_prompts(session):
-    """Dismisses any open user prompts in windows."""
-    current_window = session.window_handle
+        session.window_handle = current_window
 
-    for window in _windows(session):
-        session.window_handle = window
+    @ignore_exceptions
+    def _ensure_valid_window(session):
+        """If current window was closed, ensure to have a valid one selected."""
         try:
-            session.alert.dismiss()
-        except webdriver.NoSuchAlertException:
-            pass
+            session.window_handle
+        except webdriver.NoSuchWindowException:
+            session.window_handle = session.handles[0]
 
-    session.window_handle = current_window
-
+    @ignore_exceptions
+    def _restore_timeouts(session):
+        """Restore modified timeouts to their default values."""
+        session.timeouts.implicit = default_implicit_wait_timeout
+        session.timeouts.page_load = default_page_load_timeout
+        session.timeouts.script = default_script_timeout
 
-@ignore_exceptions
-def _restore_timeouts(session):
-    """Restores modified timeouts to their default values"""
-    session.timeouts.implicit = default_implicit_wait_timeout
-    session.timeouts.page_load = default_page_load_timeout
-    session.timeouts.script = default_script_timeout
+    @ignore_exceptions
+    def _restore_window_state(session):
+        """Reset window to an acceptable size.
 
+        This also includes bringing it out of maximized, minimized,
+        or fullscreened state.
+        """
+        session.window.size = (800, 600)
 
-@ignore_exceptions
-def _restore_window_state(session):
-    """Reset window to an acceptable size, bringing it out of maximized,
-    minimized, or fullscreened state
+    @ignore_exceptions
+    def _restore_windows(session):
+        """Close superfluous windows opened by the test.
 
-    """
-    session.window.size = (800, 600)
+        It will not end 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()
 
-@ignore_exceptions
-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
+        session.window_handle = current_window
 
-    for window in _windows(session, exclude=[current_window]):
-        session.window_handle = window
-        if len(session.handles) > 1:
-            session.close()
-
-    session.window_handle = current_window
+    _restore_timeouts(session)
+    _ensure_valid_window(session)
+    _dismiss_user_prompts(session)
+    _restore_windows(session)
+    _restore_window_state(session)
+    _switch_to_top_level_browsing_context(session)
 
 
 @ignore_exceptions
 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.
     """
@@ -204,26 +208,19 @@ def session(capabilities, configuration,
             configuration["port"],
             capabilities=caps)
     try:
         _current_session.start()
     except webdriver.error.SessionNotCreatedException:
         if not _current_session.session_id:
             raise
 
-    # finalisers are popped off a stack,
-    # making their ordering reverse
-    request.addfinalizer(lambda: _switch_to_top_level_browsing_context(_current_session))
-    request.addfinalizer(lambda: _restore_window_state(_current_session))
-    request.addfinalizer(lambda: _restore_windows(_current_session))
-    request.addfinalizer(lambda: _dismiss_user_prompts(_current_session))
-    request.addfinalizer(lambda: _ensure_valid_window(_current_session))
-    request.addfinalizer(lambda: _restore_timeouts(_current_session))
+    yield _current_session
 
-    return _current_session
+    cleanup_session(_current_session)
 
 
 def current_session():
     return _current_session
 
 
 def url(server_config):
     def inner(path, protocol="http", query="", fragment=""):