Bug 1322383 - Update Puppeteer and firefox-ui tests for valid window checks.
We have to ensure to switch to the current browser window after closing all the windows
in tearDown.
MozReview-Commit-ID: CK1mI6ImBxW
--- a/testing/firefox-ui/tests/functional/private_browsing/test_about_private_browsing.py
+++ b/testing/firefox-ui/tests/functional/private_browsing/test_about_private_browsing.py
@@ -16,16 +16,19 @@ class TestAboutPrivateBrowsing(Puppeteer
# Use a fake local support URL
support_url = 'about:blank?'
self.marionette.set_pref('app.support.baseURL', support_url)
self.pb_url = support_url + 'private-browsing'
def tearDown(self):
try:
+ self.puppeteer.windows.close_all([self.browser])
+ self.browser.switch_to()
+
self.marionette.clear_pref('app.support.baseURL')
finally:
super(TestAboutPrivateBrowsing, self).tearDown()
def testCheckAboutPrivateBrowsing(self):
self.assertFalse(self.browser.is_private)
with self.marionette.using_context('content'):
--- a/testing/firefox-ui/tests/puppeteer/test_windows.py
+++ b/testing/firefox-ui/tests/puppeteer/test_windows.py
@@ -38,16 +38,17 @@ class BaseWindowTestCase(PuppeteerMixin,
super(BaseWindowTestCase, self).tearDown()
class TestWindows(BaseWindowTestCase):
def tearDown(self):
try:
self.puppeteer.windows.close_all([self.browser])
+ self.browser.switch_to()
finally:
super(TestWindows, self).tearDown()
def test_switch_to(self):
url = self.marionette.absolute_url('layout/mozilla.html')
# Open two more windows
for index in range(0, 2):
@@ -102,18 +103,19 @@ class TestWindows(BaseWindowTestCase):
self.browser.switch_to()
class TestBaseWindow(BaseWindowTestCase):
def tearDown(self):
try:
self.puppeteer.windows.close_all([self.browser])
+ self.browser.switch_to()
finally:
- BaseWindowTestCase.tearDown(self)
+ super(TestBaseWindow, self).tearDown()
def test_basics(self):
# force BaseWindow instance
win1 = BaseWindow(self.marionette, self.browser.handle)
self.assertEquals(win1.handle, self.marionette.current_chrome_window_handle)
self.assertEquals(win1.window_element,
self.marionette.find_element(By.CSS_SELECTOR, ':root'))
@@ -142,17 +144,18 @@ class TestBaseWindow(BaseWindowTestCase)
self.assertEquals(len(self.marionette.chrome_window_handles), 2)
self.assertNotEquals(win1.handle, win2.handle)
self.assertEquals(win2.handle, self.marionette.current_chrome_window_handle)
win2.close()
self.assertTrue(win2.closed)
self.assertEquals(len(self.marionette.chrome_window_handles), 1)
- self.assertEquals(win2.handle, self.marionette.current_chrome_window_handle)
+ with self.assertRaises(NoSuchWindowException):
+ self.marionette.current_chrome_window_handle
Wait(self.marionette).until(lambda _: win1.focused) # catch the no focused window
win1.focus()
# Open and close a new window by a custom callback
def opener(window):
window.marionette.execute_script(""" window.open(); """)
@@ -210,18 +213,19 @@ class TestBaseWindow(BaseWindowTestCase)
win1.switch_to()
class TestBrowserWindow(BaseWindowTestCase):
def tearDown(self):
try:
self.puppeteer.windows.close_all([self.browser])
+ self.browser.switch_to()
finally:
- BaseWindowTestCase.tearDown(self)
+ super(TestBrowserWindow, self).tearDown()
def test_basic(self):
self.assertNotEqual(self.browser.dtds, [])
self.assertNotEqual(self.browser.properties, [])
self.assertFalse(self.browser.is_private)
self.assertIsNotNone(self.browser.menubar)
--- a/testing/marionette/driver.js
+++ b/testing/marionette/driver.js
@@ -304,43 +304,57 @@ GeckoDriver.prototype.sendTargettedAsync
default:
throw new WebDriverError(e);
}
}
};
/**
- * Gets the current active window.
+ * Get the current active window.
*
* @param {Context=} forcedContext
- * Optional name of the context to use for the checks.
- * Defaults to the current context.
+ * Optional name of the context to use for the checks. It will be required
+ * if a command always needs a specific context, whether which context is
+ * currently set. Defaults to the current context.
*
* @return {nsIDOMWindow}
+ * The currently active window.
*/
GeckoDriver.prototype.getCurrentWindow = function (forcedContext = undefined) {
let context = typeof forcedContext == "undefined" ? this.context : forcedContext;
let win = null;
- if (this.curFrame === null) {
- if (this.curBrowser === null) {
- let typ = (context === Context.CONTENT) ? "navigator:browser" : null;
- win = Services.wm.getMostRecentWindow(typ);
- } else {
- if (context === Context.CHROME) {
- win = this.curBrowser.window;
- } else {
- if (this.curBrowser.tab && browser.getBrowserForTab(this.curBrowser.tab)) {
+ switch (context) {
+ case Context.CHROME:
+ if (this.curFrame === null) {
+ if (this.curBrowser === null) {
+ win = Services.wm.getMostRecentWindow(null);
+ } else {
win = this.curBrowser.window;
}
+ } else {
+ win = this.curFrame;
}
- }
- } else {
- win = this.curFrame;
+ break;
+
+ case Context.CONTENT:
+ if (this.curFrame === null) {
+ if (this.curBrowser === null) {
+ // For content context we are only interested in browser windows
+ win = Services.wm.getMostRecentWindow("navigator:browser");
+ } else {
+ if (this.curBrowser.tab && browser.getBrowserForTab(this.curBrowser.tab)) {
+ win = this.curBrowser.window;
+ }
+ }
+ } else {
+ win = this.curFrame;
+ }
+ break;
}
return win;
};
GeckoDriver.prototype.addFrameCloseListener = function (action) {
let win = this.getCurrentWindow();
this.mozBrowserClose = e => {
--- a/testing/marionette/puppeteer/firefox/firefox_puppeteer/ui/windows.py
+++ b/testing/marionette/puppeteer/firefox/firefox_puppeteer/ui/windows.py
@@ -183,19 +183,17 @@ class Windows(BaseLib):
# if no handle has been found switch back to original window
if not target_handle:
self.marionette.switch_to_window(current_handle)
if target_handle is None:
raise NoSuchWindowException("No window found for '{}'"
.format(target))
- # only switch if necessary
- if target_handle != self.marionette.current_chrome_window_handle:
- self.marionette.switch_to_window(target_handle)
+ self.marionette.switch_to_window(target_handle)
return self.create_window_instance(target_handle)
@classmethod
def register_window(cls, window_type, window_class):
"""Registers a chrome window with this class so that this class may in
turn create the appropriate window instance later on.
@@ -410,17 +408,19 @@ class BaseWindow(BaseLib):
if kwargs[modifier] is True:
keys.append(keymap[modifier])
# Bug 1125209 - Only lower-case command keys should be sent
keys.append(command_key.lower())
self.switch_to()
- self.window_element.send_keys(*keys)
+
+ with self.marionette.using_context("chrome"):
+ self.window_element.send_keys(*keys)
def switch_to(self, focus=False):
"""Switches the context to this chrome window.
By default it will not focus the window. If that behavior is wanted, the
`focus` parameter can be used.
:param focus: If `True`, the chrome window will be focused.