Bug 1328676 - Focus web content on starting new session and navigating. r=automatedtester, a=test-only
authorAndreas Tolfsen <ato@mozilla.com>
Wed, 04 Jan 2017 19:38:34 +0000
changeset 353405 274babf768bc76f13e091ba7e82368e7c8c196da
parent 353404 ab4717c0d745d87dddc0518cf0ae7d1ab6b9623a
child 353406 9a9493b6820804ccb4d2089f337179341b151133
push id6795
push userjlund@mozilla.com
push dateMon, 23 Jan 2017 14:19:46 +0000
treeherdermozilla-esr52@76101b503191 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersautomatedtester, test-only
bugs1328676
milestone52.0a2
Bug 1328676 - Focus web content on starting new session and navigating. r=automatedtester, a=test-only 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
@@ -585,16 +585,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.
@@ -1022,16 +1024,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.
@@ -2331,18 +2334,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()