Bug 1433145 -Handle unexpected alerts when switching tests, r=ato draft
authorJames Graham <james@hoppipolla.co.uk>
Wed, 24 Jan 2018 21:00:27 +0000
changeset 738009 e3516704044b6901563323f3c799924d1dc07693
parent 738008 5a87f932e12778745e779b6e92f21e6cb90388b4
push id96825
push userbmo:james@hoppipolla.co.uk
push dateThu, 25 Jan 2018 16:16:36 +0000
reviewersato
bugs1433145
milestone60.0a1
Bug 1433145 -Handle unexpected alerts when switching tests, r=ato MozReview-Commit-ID: 55HzXggbw6N
testing/web-platform/tests/tools/wptrunner/wptrunner/executors/executormarionette.py
--- a/testing/web-platform/tests/tools/wptrunner/wptrunner/executors/executormarionette.py
+++ b/testing/web-platform/tests/tools/wptrunner/wptrunner/executors/executormarionette.py
@@ -126,23 +126,24 @@ class MarionetteProtocol(Protocol):
 
     def load_runner(self, protocol):
         # Check if we previously had a test window open, and if we did make sure it's closed
         self.marionette.execute_script("if (window.wrappedJSObject.win) {window.wrappedJSObject.win.close()}")
         url = urlparse.urljoin(self.executor.server_url(protocol), "/testharness_runner.html")
         self.logger.debug("Loading %s" % url)
         self.runner_handle = self.marionette.current_window_handle
         try:
-            self.marionette.navigate(url)
+            self.dismiss_alert(lambda: self.marionette.navigate(url))
         except Exception as e:
             self.logger.critical(
                 "Loading initial page %s failed. Ensure that the "
                 "there are no other programs bound to this port and "
                 "that your firewall rules or network setup does not "
                 "prevent access.\e%s" % (url, traceback.format_exc(e)))
+            raise
         self.marionette.execute_script(
             "document.title = '%s'" % threading.current_thread().name.replace("'", '"'))
 
     def close_old_windows(self, protocol):
         handles = self.marionette.window_handles
         runner_handle = None
         try:
             handles.remove(self.runner_handle)
@@ -152,27 +153,41 @@ class MarionetteProtocol(Protocol):
             # This isn't supposed to happen, but marionette ids are not yet stable
             # We assume that the first handle returned corresponds to the runner,
             # but it hopefully doesn't matter too much if that assumption is
             # wrong since we reload the runner in that tab anyway.
             runner_handle = handles.pop(0)
 
         for handle in handles:
             try:
+                self.dismiss_alert(lambda:self.marionette.switch_to_window(handle))
                 self.marionette.switch_to_window(handle)
                 self.marionette.close()
             except errors.NoSuchWindowException:
                 # We might have raced with the previous test to close this
                 # window, skip it.
                 pass
 
         self.marionette.switch_to_window(runner_handle)
         if runner_handle != self.runner_handle:
             self.load_runner(protocol)
 
+    def dismiss_alert(self, f):
+        while True:
+            try:
+                f()
+            except errors.UnexpectedAlertOpen:
+                alert = self.marionette.switch_to_alert()
+                try:
+                    alert.dismiss()
+                except errors.NoAlertPresentException:
+                    pass
+            else:
+                break
+
     def wait(self):
         try:
             socket_timeout = self.marionette.client.socket_timeout
         except AttributeError:
             # This can happen if there was a crash
             return
         if socket_timeout:
             try: