Bug 1328676 - Focus web content on starting new session and navigating; r?automatedtester draft
authorAndreas Tolfsen <ato@mozilla.com>
Wed, 04 Jan 2017 19:38:34 +0000
changeset 456041 17eb6ca856dcbca80209929472d77f06f8a712fa
parent 456040 a63a7f16ef675f97127ec8a712e7b194b0c1884d
child 541108 b2ed6e5ba2632a39cb693fa118dc782b3d0409bf
push id40365
push userbmo:ato@mozilla.com
push dateWed, 04 Jan 2017 19:39:41 +0000
reviewersautomatedtester
bugs1328676
milestone53.0a1
Bug 1328676 - Focus web content on starting new session and navigating; r?automatedtester When a new Marionette session is started, the web content frame (currently selected <xul:browser> element) is not in focus, causing issues such as https://github.com/mozilla/geckodriver/issues/394. This changes the current web content to be selected upon top-level browsing context navigation and the creation of new sessions. MozReview-Commit-ID: EgG9gRHtwOA
testing/marionette/driver.js
testing/marionette/harness/marionette_harness/tests/unit/test_navigation.py
--- a/testing/marionette/driver.js
+++ b/testing/marionette/driver.js
@@ -593,16 +593,18 @@ GeckoDriver.prototype.newSession = funct
     }, BROWSER_STARTUP_FINISHED, false);
   } else {
     runSessionStart.call(this);
   }
 
   yield registerBrowsers;
   yield browserListening;
 
+  this.curBrowser.browserForTab.focus();
+
   return {
     sessionId: this.sessionId,
     capabilities: this.sessionCapabilities,
   };
 };
 
 /**
  * Send the current session's capabilities to the client.
@@ -1030,16 +1032,17 @@ GeckoDriver.prototype.get = function*(cm
     cmd.parameters.command_id = id;
     cmd.parameters.pageTimeout = this.pageTimeout;
     this.mm.broadcastAsyncMessage(
         "Marionette:pollForReadyState" + this.curBrowser.curFrameId,
         cmd.parameters);
   });
 
   yield get;
+  this.curBrowser.browserForTab.focus();
 };
 
 /**
  * Get a string representing the current URL.
  *
  * On Desktop this returns a string representation of the URL of the
  * current top level browsing context.  This is equivalent to
  * document.location.href.
@@ -2339,18 +2342,17 @@ GeckoDriver.prototype.sessionTearDown = 
 
   this.switchToGlobalMessageManager();
 
   // reset frame to the top-most frame
   this.curFrame = null;
   if (this.mainFrame) {
     try {
       this.mainFrame.focus();
-    }
-    catch (e) {
+    } catch (e) {
       this.mainFrame = null;
     }
   }
 
   this.sessionId = null;
 
   if (this.observing !== null) {
     for (let topic in this.observing) {
--- a/testing/marionette/harness/marionette_harness/tests/unit/test_navigation.py
+++ b/testing/marionette/harness/marionette_harness/tests/unit/test_navigation.py
@@ -178,16 +178,25 @@ class TestNavigate(WindowManagerMixin, M
     def test_image_document_to_image_document(self):
         self.marionette.navigate(self.fixtures.where_is("test.html"))
 
         self.marionette.navigate(self.fixtures.where_is("white.png"))
         self.assertIn("white.png", self.marionette.title)
         self.marionette.navigate(self.fixtures.where_is("black.png"))
         self.assertIn("black.png", self.marionette.title)
 
+    def test_focus_after_navigation(self):
+        self.marionette.quit()
+        self.marionette.start_session()
+
+        self.marionette.navigate(inline("<input autofocus>"))
+        active_el = self.marionette.execute_script("return document.activeElement")
+        focus_el = self.marionette.find_element(By.CSS_SELECTOR, ":focus")
+        self.assertEqual(active_el, focus_el)
+
 
 class TestTLSNavigation(MarionetteTestCase):
     insecure_tls = {"acceptInsecureCerts": True}
     secure_tls = {"acceptInsecureCerts": False}
 
     def setUp(self):
         MarionetteTestCase.setUp(self)
         self.marionette.delete_session()