michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, you can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: var EXPORTED_SYMBOLS = ["MozMillController", "globalEventRegistry", michael@0: "sleep", "windowMap"]; michael@0: michael@0: const Cc = Components.classes; michael@0: const Ci = Components.interfaces; michael@0: const Cu = Components.utils; michael@0: michael@0: var EventUtils = {}; Cu.import('resource://mozmill/stdlib/EventUtils.js', EventUtils); michael@0: michael@0: var assertions = {}; Cu.import('resource://mozmill/modules/assertions.js', assertions); michael@0: var broker = {}; Cu.import('resource://mozmill/driver/msgbroker.js', broker); michael@0: var elementslib = {}; Cu.import('resource://mozmill/driver/elementslib.js', elementslib); michael@0: var errors = {}; Cu.import('resource://mozmill/modules/errors.js', errors); michael@0: var mozelement = {}; Cu.import('resource://mozmill/driver/mozelement.js', mozelement); michael@0: var utils = {}; Cu.import('resource://mozmill/stdlib/utils.js', utils); michael@0: var windows = {}; Cu.import('resource://mozmill/modules/windows.js', windows); michael@0: michael@0: // Declare most used utils functions in the controller namespace michael@0: var assert = new assertions.Assert(); michael@0: var waitFor = assert.waitFor; michael@0: michael@0: var sleep = utils.sleep; michael@0: michael@0: // For Mozmill 1.5 backward compatibility michael@0: var windowMap = windows.map; michael@0: michael@0: waitForEvents = function () { michael@0: } michael@0: michael@0: waitForEvents.prototype = { michael@0: /** michael@0: * Initialize list of events for given node michael@0: */ michael@0: init: function waitForEvents_init(node, events) { michael@0: if (node.getNode != undefined) michael@0: node = node.getNode(); michael@0: michael@0: this.events = events; michael@0: this.node = node; michael@0: node.firedEvents = {}; michael@0: this.registry = {}; michael@0: michael@0: for each (var e in events) { michael@0: var listener = function (event) { michael@0: this.firedEvents[event.type] = true; michael@0: } michael@0: michael@0: this.registry[e] = listener; michael@0: this.registry[e].result = false; michael@0: this.node.addEventListener(e, this.registry[e], true); michael@0: } michael@0: }, michael@0: michael@0: /** michael@0: * Wait until all assigned events have been fired michael@0: */ michael@0: wait: function waitForEvents_wait(timeout, interval) { michael@0: for (var e in this.registry) { michael@0: assert.waitFor(function () { michael@0: return this.node.firedEvents[e] == true; michael@0: }, "waitForEvents.wait(): Event '" + ex + "' has been fired.", timeout, interval); michael@0: michael@0: this.node.removeEventListener(e, this.registry[e], true); michael@0: } michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * Class to handle menus and context menus michael@0: * michael@0: * @constructor michael@0: * @param {MozMillController} controller michael@0: * Mozmill controller of the window under test michael@0: * @param {string} menuSelector michael@0: * jQuery like selector string of the element michael@0: * @param {object} document michael@0: * Document to use for finding the menu michael@0: * [optional - default: aController.window.document] michael@0: */ michael@0: var Menu = function (controller, menuSelector, document) { michael@0: this._controller = controller; michael@0: this._menu = null; michael@0: michael@0: document = document || controller.window.document; michael@0: var node = document.querySelector(menuSelector); michael@0: if (node) { michael@0: // We don't unwrap nodes automatically yet (Bug 573185) michael@0: node = node.wrappedJSObject || node; michael@0: this._menu = new mozelement.Elem(node); michael@0: } else { michael@0: throw new Error("Menu element '" + menuSelector + "' not found."); michael@0: } michael@0: } michael@0: michael@0: Menu.prototype = { michael@0: michael@0: /** michael@0: * Open and populate the menu michael@0: * michael@0: * @param {ElemBase} contextElement michael@0: * Element whose context menu has to be opened michael@0: * @returns {Menu} The Menu instance michael@0: */ michael@0: open: function Menu_open(contextElement) { michael@0: // We have to open the context menu michael@0: var menu = this._menu.getNode(); michael@0: if ((menu.localName == "popup" || menu.localName == "menupopup") && michael@0: contextElement && contextElement.exists()) { michael@0: this._controller.rightClick(contextElement); michael@0: assert.waitFor(function () { michael@0: return menu.state == "open"; michael@0: }, "Context menu has been opened."); michael@0: } michael@0: michael@0: // Run through the entire menu and populate with dynamic entries michael@0: this._buildMenu(menu); michael@0: michael@0: return this; michael@0: }, michael@0: michael@0: /** michael@0: * Close the menu michael@0: * michael@0: * @returns {Menu} The Menu instance michael@0: */ michael@0: close: function Menu_close() { michael@0: var menu = this._menu.getNode(); michael@0: michael@0: this._controller.keypress(this._menu, "VK_ESCAPE", {}); michael@0: assert.waitFor(function () { michael@0: return menu.state == "closed"; michael@0: }, "Context menu has been closed."); michael@0: michael@0: return this; michael@0: }, michael@0: michael@0: /** michael@0: * Retrieve the specified menu entry michael@0: * michael@0: * @param {string} itemSelector michael@0: * jQuery like selector string of the menu item michael@0: * @returns {ElemBase} Menu element michael@0: * @throws Error If menu element has not been found michael@0: */ michael@0: getItem: function Menu_getItem(itemSelector) { michael@0: // Run through the entire menu and populate with dynamic entries michael@0: this._buildMenu(this._menu.getNode()); michael@0: michael@0: var node = this._menu.getNode().querySelector(itemSelector); michael@0: michael@0: if (!node) { michael@0: throw new Error("Menu entry '" + itemSelector + "' not found."); michael@0: } michael@0: michael@0: return new mozelement.Elem(node); michael@0: }, michael@0: michael@0: /** michael@0: * Click the specified menu entry michael@0: * michael@0: * @param {string} itemSelector michael@0: * jQuery like selector string of the menu item michael@0: * michael@0: * @returns {Menu} The Menu instance michael@0: */ michael@0: click: function Menu_click(itemSelector) { michael@0: this._controller.click(this.getItem(itemSelector)); michael@0: michael@0: return this; michael@0: }, michael@0: michael@0: /** michael@0: * Synthesize a keypress against the menu michael@0: * michael@0: * @param {string} key michael@0: * Key to press michael@0: * @param {object} modifier michael@0: * Key modifiers michael@0: * @see MozMillController#keypress michael@0: * michael@0: * @returns {Menu} The Menu instance michael@0: */ michael@0: keypress: function Menu_keypress(key, modifier) { michael@0: this._controller.keypress(this._menu, key, modifier); michael@0: michael@0: return this; michael@0: }, michael@0: michael@0: /** michael@0: * Opens the context menu, click the specified entry and michael@0: * make sure that the menu has been closed. michael@0: * michael@0: * @param {string} itemSelector michael@0: * jQuery like selector string of the element michael@0: * @param {ElemBase} contextElement michael@0: * Element whose context menu has to be opened michael@0: * michael@0: * @returns {Menu} The Menu instance michael@0: */ michael@0: select: function Menu_select(itemSelector, contextElement) { michael@0: this.open(contextElement); michael@0: this.click(itemSelector); michael@0: this.close(); michael@0: }, michael@0: michael@0: /** michael@0: * Recursive function which iterates through all menu elements and michael@0: * populates the menus with dynamic menu entries. michael@0: * michael@0: * @param {node} menu michael@0: * Top menu node whose elements have to be populated michael@0: */ michael@0: _buildMenu: function Menu__buildMenu(menu) { michael@0: var items = menu ? menu.childNodes : null; michael@0: michael@0: Array.forEach(items, function (item) { michael@0: // When we have a menu node, fake a click onto it to populate michael@0: // the sub menu with dynamic entries michael@0: if (item.tagName == "menu") { michael@0: var popup = item.querySelector("menupopup"); michael@0: michael@0: if (popup) { michael@0: var popupEvent = this._controller.window.document.createEvent("MouseEvent"); michael@0: popupEvent.initMouseEvent("popupshowing", true, true, michael@0: this._controller.window, 0, 0, 0, 0, 0, michael@0: false, false, false, false, 0, null); michael@0: popup.dispatchEvent(popupEvent); michael@0: michael@0: this._buildMenu(popup); michael@0: } michael@0: } michael@0: }, this); michael@0: } michael@0: }; michael@0: michael@0: var MozMillController = function (window) { michael@0: this.window = window; michael@0: michael@0: this.mozmillModule = {}; michael@0: Cu.import('resource://mozmill/driver/mozmill.js', this.mozmillModule); michael@0: michael@0: var self = this; michael@0: assert.waitFor(function () { michael@0: return window != null && self.isLoaded(); michael@0: }, "controller(): Window has been initialized."); michael@0: michael@0: // Ensure to focus the window which will move it virtually into the foreground michael@0: // when focusmanager.testmode is set enabled. michael@0: this.window.focus(); michael@0: michael@0: var windowType = window.document.documentElement.getAttribute('windowtype'); michael@0: if (controllerAdditions[windowType] != undefined ) { michael@0: this.prototype = new utils.Copy(this.prototype); michael@0: controllerAdditions[windowType](this); michael@0: this.windowtype = windowType; michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * Returns the global browser object of the window michael@0: * michael@0: * @returns {Object} The browser object michael@0: */ michael@0: MozMillController.prototype.__defineGetter__("browserObject", function () { michael@0: return utils.getBrowserObject(this.window); michael@0: }); michael@0: michael@0: // constructs a MozMillElement from the controller's window michael@0: MozMillController.prototype.__defineGetter__("rootElement", function () { michael@0: if (this._rootElement == undefined) { michael@0: let docElement = this.window.document.documentElement; michael@0: this._rootElement = new mozelement.MozMillElement("Elem", docElement); michael@0: } michael@0: michael@0: return this._rootElement; michael@0: }); michael@0: michael@0: MozMillController.prototype.sleep = utils.sleep; michael@0: MozMillController.prototype.waitFor = assert.waitFor; michael@0: michael@0: // Open the specified url in the current tab michael@0: MozMillController.prototype.open = function (url) { michael@0: switch (this.mozmillModule.Application) { michael@0: case "Firefox": michael@0: case "MetroFirefox": michael@0: // Stop a running page load to not overlap requests michael@0: if (this.browserObject.selectedBrowser) { michael@0: this.browserObject.selectedBrowser.stop(); michael@0: } michael@0: michael@0: this.browserObject.loadURI(url); michael@0: break; michael@0: michael@0: default: michael@0: throw new Error("MozMillController.open not supported."); michael@0: } michael@0: michael@0: broker.pass({'function':'Controller.open()'}); michael@0: } michael@0: michael@0: /** michael@0: * Take a screenshot of specified node michael@0: * michael@0: * @param {Element} node michael@0: * The window or DOM element to capture michael@0: * @param {String} name michael@0: * The name of the screenshot used in reporting and as filename michael@0: * @param {Boolean} save michael@0: * If true saves the screenshot as 'name.jpg' in tempdir, michael@0: * otherwise returns a dataURL michael@0: * @param {Element[]} highlights michael@0: * A list of DOM elements to highlight by drawing a red rectangle around them michael@0: * michael@0: * @returns {Object} Object which contains properties like filename, dataURL, michael@0: * name and timestamp of the screenshot michael@0: */ michael@0: MozMillController.prototype.screenshot = function (node, name, save, highlights) { michael@0: if (!node) { michael@0: throw new Error("node is undefined"); michael@0: } michael@0: michael@0: // Unwrap the node and highlights michael@0: if ("getNode" in node) { michael@0: node = node.getNode(); michael@0: } michael@0: michael@0: if (highlights) { michael@0: for (var i = 0; i < highlights.length; ++i) { michael@0: if ("getNode" in highlights[i]) { michael@0: highlights[i] = highlights[i].getNode(); michael@0: } michael@0: } michael@0: } michael@0: michael@0: // If save is false, a dataURL is used michael@0: // Include both in the report anyway to avoid confusion and make the report easier to parse michael@0: var screenshot = {"filename": undefined, michael@0: "dataURL": utils.takeScreenshot(node, highlights), michael@0: "name": name, michael@0: "timestamp": new Date().toLocaleString()}; michael@0: michael@0: if (!save) { michael@0: return screenshot; michael@0: } michael@0: michael@0: // Save the screenshot to disk michael@0: michael@0: let {filename, failure} = utils.saveDataURL(screenshot.dataURL, name); michael@0: screenshot.filename = filename; michael@0: screenshot.failure = failure; michael@0: michael@0: if (failure) { michael@0: broker.log({'function': 'controller.screenshot()', michael@0: 'message': 'Error writing to file: ' + screenshot.filename}); michael@0: } else { michael@0: // Send the screenshot object to python over jsbridge michael@0: broker.sendMessage("screenshot", screenshot); michael@0: broker.pass({'function': 'controller.screenshot()'}); michael@0: } michael@0: michael@0: return screenshot; michael@0: } michael@0: michael@0: /** michael@0: * Checks if the specified window has been loaded michael@0: * michael@0: * @param {DOMWindow} [aWindow=this.window] Window object to check for loaded state michael@0: */ michael@0: MozMillController.prototype.isLoaded = function (aWindow) { michael@0: var win = aWindow || this.window; michael@0: michael@0: return windows.map.getValue(utils.getWindowId(win), "loaded") || false; michael@0: }; michael@0: michael@0: MozMillController.prototype.__defineGetter__("waitForEvents", function () { michael@0: if (this._waitForEvents == undefined) { michael@0: this._waitForEvents = new waitForEvents(); michael@0: } michael@0: michael@0: return this._waitForEvents; michael@0: }); michael@0: michael@0: /** michael@0: * Wrapper function to create a new instance of a menu michael@0: * @see Menu michael@0: */ michael@0: MozMillController.prototype.getMenu = function (menuSelector, document) { michael@0: return new Menu(this, menuSelector, document); michael@0: }; michael@0: michael@0: MozMillController.prototype.__defineGetter__("mainMenu", function () { michael@0: return this.getMenu("menubar"); michael@0: }); michael@0: michael@0: MozMillController.prototype.__defineGetter__("menus", function () { michael@0: logDeprecated('controller.menus', 'Use controller.mainMenu instead'); michael@0: }); michael@0: michael@0: MozMillController.prototype.waitForImage = function (aElement, timeout, interval) { michael@0: this.waitFor(function () { michael@0: return aElement.getNode().complete == true; michael@0: }, "timeout exceeded for waitForImage " + aElement.getInfo(), timeout, interval); michael@0: michael@0: broker.pass({'function':'Controller.waitForImage()'}); michael@0: } michael@0: michael@0: MozMillController.prototype.startUserShutdown = function (timeout, restart, next, resetProfile) { michael@0: if (restart && resetProfile) { michael@0: throw new Error("You can't have a user-restart and reset the profile; there is a race condition"); michael@0: } michael@0: michael@0: let shutdownObj = { michael@0: 'user': true, michael@0: 'restart': Boolean(restart), michael@0: 'next': next, michael@0: 'resetProfile': Boolean(resetProfile), michael@0: 'timeout': timeout michael@0: }; michael@0: michael@0: broker.sendMessage('shutdown', shutdownObj); michael@0: } michael@0: michael@0: /** michael@0: * Restart the application michael@0: * michael@0: * @param {string} aNext michael@0: * Name of the next test function to run after restart michael@0: * @param {boolean} [aFlags=undefined] michael@0: * Additional flags how to handle the shutdown or restart. The attributes michael@0: * eRestarti386 (0x20) and eRestartx86_64 (0x30) have not been documented yet. michael@0: * @see https://developer.mozilla.org/nsIAppStartup#Attributes michael@0: */ michael@0: MozMillController.prototype.restartApplication = function (aNext, aFlags) { michael@0: var flags = Ci.nsIAppStartup.eAttemptQuit | Ci.nsIAppStartup.eRestart; michael@0: michael@0: if (aFlags) { michael@0: flags |= aFlags; michael@0: } michael@0: michael@0: broker.sendMessage('shutdown', {'user': false, michael@0: 'restart': true, michael@0: 'flags': flags, michael@0: 'next': aNext, michael@0: 'timeout': 0 }); michael@0: michael@0: // We have to ensure to stop the test from continuing until the application is michael@0: // shutting down. The only way to do that is by throwing an exception. michael@0: throw new errors.ApplicationQuitError(); michael@0: } michael@0: michael@0: /** michael@0: * Stop the application michael@0: * michael@0: * @param {boolean} [aResetProfile=false] michael@0: * Whether to reset the profile during restart michael@0: * @param {boolean} [aFlags=undefined] michael@0: * Additional flags how to handle the shutdown or restart. The attributes michael@0: * eRestarti386 and eRestartx86_64 have not been documented yet. michael@0: * @see https://developer.mozilla.org/nsIAppStartup#Attributes michael@0: */ michael@0: MozMillController.prototype.stopApplication = function (aResetProfile, aFlags) { michael@0: var flags = Ci.nsIAppStartup.eAttemptQuit; michael@0: michael@0: if (aFlags) { michael@0: flags |= aFlags; michael@0: } michael@0: michael@0: broker.sendMessage('shutdown', {'user': false, michael@0: 'restart': false, michael@0: 'flags': flags, michael@0: 'resetProfile': aResetProfile, michael@0: 'timeout': 0 }); michael@0: michael@0: // We have to ensure to stop the test from continuing until the application is michael@0: // shutting down. The only way to do that is by throwing an exception. michael@0: throw new errors.ApplicationQuitError(); michael@0: } michael@0: michael@0: //Browser navigation functions michael@0: MozMillController.prototype.goBack = function () { michael@0: this.window.content.history.back(); michael@0: broker.pass({'function':'Controller.goBack()'}); michael@0: michael@0: return true; michael@0: } michael@0: michael@0: MozMillController.prototype.goForward = function () { michael@0: this.window.content.history.forward(); michael@0: broker.pass({'function':'Controller.goForward()'}); michael@0: michael@0: return true; michael@0: } michael@0: michael@0: MozMillController.prototype.refresh = function () { michael@0: this.window.content.location.reload(true); michael@0: broker.pass({'function':'Controller.refresh()'}); michael@0: michael@0: return true; michael@0: } michael@0: michael@0: function logDeprecated(funcName, message) { michael@0: broker.log({'function': funcName + '() - DEPRECATED', michael@0: 'message': funcName + '() is deprecated. ' + message}); michael@0: } michael@0: michael@0: function logDeprecatedAssert(funcName) { michael@0: logDeprecated('controller.' + funcName, michael@0: '. Use the generic `assertion` module instead.'); michael@0: } michael@0: michael@0: MozMillController.prototype.assertText = function (el, text) { michael@0: logDeprecatedAssert("assertText"); michael@0: michael@0: var n = el.getNode(); michael@0: michael@0: if (n && n.innerHTML == text) { michael@0: broker.pass({'function': 'Controller.assertText()'}); michael@0: } else { michael@0: throw new Error("could not validate element " + el.getInfo() + michael@0: " with text "+ text); michael@0: } michael@0: michael@0: return true; michael@0: }; michael@0: michael@0: /** michael@0: * Assert that a specified node exists michael@0: */ michael@0: MozMillController.prototype.assertNode = function (el) { michael@0: logDeprecatedAssert("assertNode"); michael@0: michael@0: //this.window.focus(); michael@0: var element = el.getNode(); michael@0: if (!element) { michael@0: throw new Error("could not find element " + el.getInfo()); michael@0: } michael@0: michael@0: broker.pass({'function': 'Controller.assertNode()'}); michael@0: return true; michael@0: }; michael@0: michael@0: /** michael@0: * Assert that a specified node doesn't exist michael@0: */ michael@0: MozMillController.prototype.assertNodeNotExist = function (el) { michael@0: logDeprecatedAssert("assertNodeNotExist"); michael@0: michael@0: try { michael@0: var element = el.getNode(); michael@0: } catch (e) { michael@0: broker.pass({'function': 'Controller.assertNodeNotExist()'}); michael@0: } michael@0: michael@0: if (element) { michael@0: throw new Error("Unexpectedly found element " + el.getInfo()); michael@0: } else { michael@0: broker.pass({'function':'Controller.assertNodeNotExist()'}); michael@0: } michael@0: michael@0: return true; michael@0: }; michael@0: michael@0: /** michael@0: * Assert that a form element contains the expected value michael@0: */ michael@0: MozMillController.prototype.assertValue = function (el, value) { michael@0: logDeprecatedAssert("assertValue"); michael@0: michael@0: var n = el.getNode(); michael@0: michael@0: if (n && n.value == value) { michael@0: broker.pass({'function': 'Controller.assertValue()'}); michael@0: } else { michael@0: throw new Error("could not validate element " + el.getInfo() + michael@0: " with value " + value); michael@0: } michael@0: michael@0: return false; michael@0: }; michael@0: michael@0: /** michael@0: * Check if the callback function evaluates to true michael@0: */ michael@0: MozMillController.prototype.assert = function (callback, message, thisObject) { michael@0: logDeprecatedAssert("assert"); michael@0: michael@0: utils.assert(callback, message, thisObject); michael@0: broker.pass({'function': ": controller.assert('" + callback + "')"}); michael@0: michael@0: return true; michael@0: } michael@0: michael@0: /** michael@0: * Assert that a provided value is selected in a select element michael@0: */ michael@0: MozMillController.prototype.assertSelected = function (el, value) { michael@0: logDeprecatedAssert("assertSelected"); michael@0: michael@0: var n = el.getNode(); michael@0: var validator = value; michael@0: michael@0: if (n && n.options[n.selectedIndex].value == validator) { michael@0: broker.pass({'function':'Controller.assertSelected()'}); michael@0: } else { michael@0: throw new Error("could not assert value for element " + el.getInfo() + michael@0: " with value " + value); michael@0: } michael@0: michael@0: return true; michael@0: }; michael@0: michael@0: /** michael@0: * Assert that a provided checkbox is checked michael@0: */ michael@0: MozMillController.prototype.assertChecked = function (el) { michael@0: logDeprecatedAssert("assertChecked"); michael@0: michael@0: var element = el.getNode(); michael@0: michael@0: if (element && element.checked == true) { michael@0: broker.pass({'function':'Controller.assertChecked()'}); michael@0: } else { michael@0: throw new Error("assert failed for checked element " + el.getInfo()); michael@0: } michael@0: michael@0: return true; michael@0: }; michael@0: michael@0: /** michael@0: * Assert that a provided checkbox is not checked michael@0: */ michael@0: MozMillController.prototype.assertNotChecked = function (el) { michael@0: logDeprecatedAssert("assertNotChecked"); michael@0: michael@0: var element = el.getNode(); michael@0: michael@0: if (!element) { michael@0: throw new Error("Could not find element" + el.getInfo()); michael@0: } michael@0: michael@0: if (!element.hasAttribute("checked") || element.checked != true) { michael@0: broker.pass({'function': 'Controller.assertNotChecked()'}); michael@0: } else { michael@0: throw new Error("assert failed for not checked element " + el.getInfo()); michael@0: } michael@0: michael@0: return true; michael@0: }; michael@0: michael@0: /** michael@0: * Assert that an element's javascript property exists or has a particular value michael@0: * michael@0: * if val is undefined, will return true if the property exists. michael@0: * if val is specified, will return true if the property exists and has the correct value michael@0: */ michael@0: MozMillController.prototype.assertJSProperty = function (el, attrib, val) { michael@0: logDeprecatedAssert("assertJSProperty"); michael@0: michael@0: var element = el.getNode(); michael@0: michael@0: if (!element){ michael@0: throw new Error("could not find element " + el.getInfo()); michael@0: } michael@0: michael@0: var value = element[attrib]; michael@0: var res = (value !== undefined && (val === undefined ? true : michael@0: String(value) == String(val))); michael@0: if (res) { michael@0: broker.pass({'function':'Controller.assertJSProperty("' + el.getInfo() + '") : ' + val}); michael@0: } else { michael@0: throw new Error("Controller.assertJSProperty(" + el.getInfo() + ") : " + michael@0: (val === undefined ? "property '" + attrib + michael@0: "' doesn't exist" : val + " == " + value)); michael@0: } michael@0: michael@0: return true; michael@0: }; michael@0: michael@0: /** michael@0: * Assert that an element's javascript property doesn't exist or doesn't have a particular value michael@0: * michael@0: * if val is undefined, will return true if the property doesn't exist. michael@0: * if val is specified, will return true if the property doesn't exist or doesn't have the specified value michael@0: */ michael@0: MozMillController.prototype.assertNotJSProperty = function (el, attrib, val) { michael@0: logDeprecatedAssert("assertNotJSProperty"); michael@0: michael@0: var element = el.getNode(); michael@0: michael@0: if (!element){ michael@0: throw new Error("could not find element " + el.getInfo()); michael@0: } michael@0: michael@0: var value = element[attrib]; michael@0: var res = (val === undefined ? value === undefined : String(value) != String(val)); michael@0: if (res) { michael@0: broker.pass({'function':'Controller.assertNotProperty("' + el.getInfo() + '") : ' + val}); michael@0: } else { michael@0: throw new Error("Controller.assertNotJSProperty(" + el.getInfo() + ") : " + michael@0: (val === undefined ? "property '" + attrib + michael@0: "' exists" : val + " != " + value)); michael@0: } michael@0: michael@0: return true; michael@0: }; michael@0: michael@0: /** michael@0: * Assert that an element's dom property exists or has a particular value michael@0: * michael@0: * if val is undefined, will return true if the property exists. michael@0: * if val is specified, will return true if the property exists and has the correct value michael@0: */ michael@0: MozMillController.prototype.assertDOMProperty = function (el, attrib, val) { michael@0: logDeprecatedAssert("assertDOMProperty"); michael@0: michael@0: var element = el.getNode(); michael@0: michael@0: if (!element){ michael@0: throw new Error("could not find element " + el.getInfo()); michael@0: } michael@0: michael@0: var value, res = element.hasAttribute(attrib); michael@0: if (res && val !== undefined) { michael@0: value = element.getAttribute(attrib); michael@0: res = (String(value) == String(val)); michael@0: } michael@0: michael@0: if (res) { michael@0: broker.pass({'function':'Controller.assertDOMProperty("' + el.getInfo() + '") : ' + val}); michael@0: } else { michael@0: throw new Error("Controller.assertDOMProperty(" + el.getInfo() + ") : " + michael@0: (val === undefined ? "property '" + attrib + michael@0: "' doesn't exist" : val + " == " + value)); michael@0: } michael@0: michael@0: return true; michael@0: }; michael@0: michael@0: /** michael@0: * Assert that an element's dom property doesn't exist or doesn't have a particular value michael@0: * michael@0: * if val is undefined, will return true if the property doesn't exist. michael@0: * if val is specified, will return true if the property doesn't exist or doesn't have the specified value michael@0: */ michael@0: MozMillController.prototype.assertNotDOMProperty = function (el, attrib, val) { michael@0: logDeprecatedAssert("assertNotDOMProperty"); michael@0: michael@0: var element = el.getNode(); michael@0: michael@0: if (!element) { michael@0: throw new Error("could not find element " + el.getInfo()); michael@0: } michael@0: michael@0: var value, res = element.hasAttribute(attrib); michael@0: if (res && val !== undefined) { michael@0: value = element.getAttribute(attrib); michael@0: res = (String(value) == String(val)); michael@0: } michael@0: michael@0: if (!res) { michael@0: broker.pass({'function':'Controller.assertNotDOMProperty("' + el.getInfo() + '") : ' + val}); michael@0: } else { michael@0: throw new Error("Controller.assertNotDOMProperty(" + el.getInfo() + ") : " + michael@0: (val == undefined ? "property '" + attrib + michael@0: "' exists" : val + " == " + value)); michael@0: } michael@0: michael@0: return true; michael@0: }; michael@0: michael@0: /** michael@0: * Assert that a specified image has actually loaded. The Safari workaround results michael@0: * in additional requests for broken images (in Safari only) but works reliably michael@0: */ michael@0: MozMillController.prototype.assertImageLoaded = function (el) { michael@0: logDeprecatedAssert("assertImageLoaded"); michael@0: michael@0: var img = el.getNode(); michael@0: michael@0: if (!img || img.tagName != 'IMG') { michael@0: throw new Error('Controller.assertImageLoaded() failed.') michael@0: return false; michael@0: } michael@0: michael@0: var comp = img.complete; michael@0: var ret = null; // Return value michael@0: michael@0: // Workaround for Safari -- it only supports the michael@0: // complete attrib on script-created images michael@0: if (typeof comp == 'undefined') { michael@0: test = new Image(); michael@0: // If the original image was successfully loaded, michael@0: // src for new one should be pulled from cache michael@0: test.src = img.src; michael@0: comp = test.complete; michael@0: } michael@0: michael@0: // Check the complete attrib. Note the strict michael@0: // equality check -- we don't want undefined, null, etc. michael@0: // -------------------------- michael@0: if (comp === false) { michael@0: // False -- Img failed to load in IE/Safari, or is michael@0: // still trying to load in FF michael@0: ret = false; michael@0: } else if (comp === true && img.naturalWidth == 0) { michael@0: // True, but image has no size -- image failed to michael@0: // load in FF michael@0: ret = false; michael@0: } else { michael@0: // Otherwise all we can do is assume everything's michael@0: // hunky-dory michael@0: ret = true; michael@0: } michael@0: michael@0: if (ret) { michael@0: broker.pass({'function':'Controller.assertImageLoaded'}); michael@0: } else { michael@0: throw new Error('Controller.assertImageLoaded() failed.') michael@0: } michael@0: michael@0: return true; michael@0: }; michael@0: michael@0: /** michael@0: * Drag one element to the top x,y coords of another specified element michael@0: */ michael@0: MozMillController.prototype.mouseMove = function (doc, start, dest) { michael@0: // if one of these elements couldn't be looked up michael@0: if (typeof start != 'object'){ michael@0: throw new Error("received bad coordinates"); michael@0: } michael@0: michael@0: if (typeof dest != 'object'){ michael@0: throw new Error("received bad coordinates"); michael@0: } michael@0: michael@0: var triggerMouseEvent = function (element, clientX, clientY) { michael@0: clientX = clientX ? clientX: 0; michael@0: clientY = clientY ? clientY: 0; michael@0: michael@0: // make the mouse understand where it is on the screen michael@0: var screenX = element.boxObject.screenX ? element.boxObject.screenX : 0; michael@0: var screenY = element.boxObject.screenY ? element.boxObject.screenY : 0; michael@0: michael@0: var evt = element.ownerDocument.createEvent('MouseEvents'); michael@0: if (evt.initMouseEvent) { michael@0: evt.initMouseEvent('mousemove', true, true, element.ownerDocument.defaultView, michael@0: 1, screenX, screenY, clientX, clientY); michael@0: } else { michael@0: evt.initEvent('mousemove', true, true); michael@0: } michael@0: michael@0: element.dispatchEvent(evt); michael@0: }; michael@0: michael@0: // Do the initial move to the drag element position michael@0: triggerMouseEvent(doc.body, start[0], start[1]); michael@0: triggerMouseEvent(doc.body, dest[0], dest[1]); michael@0: michael@0: broker.pass({'function':'Controller.mouseMove()'}); michael@0: return true; michael@0: } michael@0: michael@0: /** michael@0: * Drag an element to the specified offset on another element, firing mouse and michael@0: * drag events. Adapted from ChromeUtils.js synthesizeDrop() michael@0: * michael@0: * @deprecated Use the MozMillElement object michael@0: * michael@0: * @param {MozElement} aSrc michael@0: * Source element to be dragged michael@0: * @param {MozElement} aDest michael@0: * Destination element over which the drop occurs michael@0: * @param {Number} [aOffsetX=element.width/2] michael@0: * Relative x offset for dropping on the aDest element michael@0: * @param {Number} [aOffsetY=element.height/2] michael@0: * Relative y offset for dropping on the aDest element michael@0: * @param {DOMWindow} [aSourceWindow=this.element.ownerDocument.defaultView] michael@0: * Custom source Window to be used. michael@0: * @param {String} [aDropEffect="move"] michael@0: * Effect used for the drop event michael@0: * @param {Object[]} [aDragData] michael@0: * An array holding custom drag data to be used during the drag event michael@0: * Format: [{ type: "text/plain", "Text to drag"}, ...] michael@0: * michael@0: * @returns {String} the captured dropEffect michael@0: */ michael@0: MozMillController.prototype.dragToElement = function (aSrc, aDest, aOffsetX, michael@0: aOffsetY, aSourceWindow, michael@0: aDropEffect, aDragData) { michael@0: logDeprecated("controller.dragToElement", "Use the MozMillElement object."); michael@0: return aSrc.dragToElement(aDest, aOffsetX, aOffsetY, aSourceWindow, null, michael@0: aDropEffect, aDragData); michael@0: }; michael@0: michael@0: function Tabs(controller) { michael@0: this.controller = controller; michael@0: } michael@0: michael@0: Tabs.prototype.getTab = function (index) { michael@0: return this.controller.browserObject.browsers[index].contentDocument; michael@0: } michael@0: michael@0: Tabs.prototype.__defineGetter__("activeTab", function () { michael@0: return this.controller.browserObject.selectedBrowser.contentDocument; michael@0: }); michael@0: michael@0: Tabs.prototype.selectTab = function (index) { michael@0: // GO in to tab manager and grab the tab by index and call focus. michael@0: } michael@0: michael@0: Tabs.prototype.findWindow = function (doc) { michael@0: for (var i = 0; i <= (this.controller.window.frames.length - 1); i++) { michael@0: if (this.controller.window.frames[i].document == doc) { michael@0: return this.controller.window.frames[i]; michael@0: } michael@0: } michael@0: michael@0: throw new Error("Cannot find window for document. Doc title == " + doc.title); michael@0: } michael@0: michael@0: Tabs.prototype.getTabWindow = function (index) { michael@0: return this.findWindow(this.getTab(index)); michael@0: } michael@0: michael@0: Tabs.prototype.__defineGetter__("activeTabWindow", function () { michael@0: return this.findWindow(this.activeTab); michael@0: }); michael@0: michael@0: Tabs.prototype.__defineGetter__("length", function () { michael@0: return this.controller.browserObject.browsers.length; michael@0: }); michael@0: michael@0: Tabs.prototype.__defineGetter__("activeTabIndex", function () { michael@0: var browser = this.controller.browserObject; michael@0: michael@0: switch(this.controller.mozmillModule.Application) { michael@0: case "MetroFirefox": michael@0: return browser.tabs.indexOf(browser.selectedTab); michael@0: case "Firefox": michael@0: default: michael@0: return browser.tabContainer.selectedIndex; michael@0: } michael@0: }); michael@0: michael@0: Tabs.prototype.selectTabIndex = function (aIndex) { michael@0: var browser = this.controller.browserObject; michael@0: michael@0: switch(this.controller.mozmillModule.Application) { michael@0: case "MetroFirefox": michael@0: browser.selectedTab = browser.tabs[aIndex]; michael@0: break; michael@0: case "Firefox": michael@0: default: michael@0: browser.selectTabAtIndex(aIndex); michael@0: } michael@0: } michael@0: michael@0: function browserAdditions (controller) { michael@0: controller.tabs = new Tabs(controller); michael@0: michael@0: controller.waitForPageLoad = function (aDocument, aTimeout, aInterval) { michael@0: var timeout = aTimeout || 30000; michael@0: var win = null; michael@0: var timed_out = false; michael@0: michael@0: // If a user tries to do waitForPageLoad(2000), this will assign the michael@0: // interval the first arg which is most likely what they were expecting michael@0: if (typeof(aDocument) == "number"){ michael@0: timeout = aDocument; michael@0: } michael@0: michael@0: // If we have a real document use its default view michael@0: if (aDocument && (typeof(aDocument) === "object") && michael@0: "defaultView" in aDocument) michael@0: win = aDocument.defaultView; michael@0: michael@0: // If no document has been specified, fallback to the default view of the michael@0: // currently selected tab browser michael@0: win = win || this.browserObject.selectedBrowser.contentWindow; michael@0: michael@0: // Wait until the content in the tab has been loaded michael@0: try { michael@0: this.waitFor(function () { michael@0: return windows.map.hasPageLoaded(utils.getWindowId(win)); michael@0: }, "Timeout", timeout, aInterval); michael@0: } michael@0: catch (ex if ex instanceof errors.TimeoutError) { michael@0: timed_out = true; michael@0: } michael@0: finally { michael@0: state = 'URI=' + win.document.location.href + michael@0: ', readyState=' + win.document.readyState; michael@0: message = "controller.waitForPageLoad(" + state + ")"; michael@0: michael@0: if (timed_out) { michael@0: throw new errors.AssertionError(message); michael@0: } michael@0: michael@0: broker.pass({'function': message}); michael@0: } michael@0: } michael@0: } michael@0: michael@0: var controllerAdditions = { michael@0: 'navigator:browser' :browserAdditions michael@0: }; michael@0: michael@0: /** michael@0: * DEPRECATION WARNING michael@0: * michael@0: * The following methods have all been DEPRECATED as of Mozmill 2.0 michael@0: */ michael@0: MozMillController.prototype.assertProperty = function (el, attrib, val) { michael@0: logDeprecatedAssert("assertProperty"); michael@0: michael@0: return this.assertJSProperty(el, attrib, val); michael@0: }; michael@0: michael@0: MozMillController.prototype.assertPropertyNotExist = function (el, attrib) { michael@0: logDeprecatedAssert("assertPropertyNotExist"); michael@0: return this.assertNotJSProperty(el, attrib); michael@0: }; michael@0: michael@0: /** michael@0: * DEPRECATION WARNING michael@0: * michael@0: * The following methods have all been DEPRECATED as of Mozmill 2.0 michael@0: * Use the MozMillElement object instead (https://developer.mozilla.org/en/Mozmill/Mozmill_Element_Object) michael@0: */ michael@0: MozMillController.prototype.select = function (aElement, index, option, value) { michael@0: logDeprecated("controller.select", "Use the MozMillElement object."); michael@0: michael@0: return aElement.select(index, option, value); michael@0: }; michael@0: michael@0: MozMillController.prototype.keypress = function (aElement, aKey, aModifiers, aExpectedEvent) { michael@0: logDeprecated("controller.keypress", "Use the MozMillElement object."); michael@0: michael@0: if (!aElement) { michael@0: aElement = new mozelement.MozMillElement("Elem", this.window); michael@0: } michael@0: michael@0: return aElement.keypress(aKey, aModifiers, aExpectedEvent); michael@0: } michael@0: michael@0: MozMillController.prototype.type = function (aElement, aText, aExpectedEvent) { michael@0: logDeprecated("controller.type", "Use the MozMillElement object."); michael@0: michael@0: if (!aElement) { michael@0: aElement = new mozelement.MozMillElement("Elem", this.window); michael@0: } michael@0: michael@0: var that = this; michael@0: var retval = true; michael@0: Array.forEach(aText, function (letter) { michael@0: if (!that.keypress(aElement, letter, {}, aExpectedEvent)) { michael@0: retval = false; } michael@0: }); michael@0: michael@0: return retval; michael@0: } michael@0: michael@0: MozMillController.prototype.mouseEvent = function (aElement, aOffsetX, aOffsetY, aEvent, aExpectedEvent) { michael@0: logDeprecated("controller.mouseEvent", "Use the MozMillElement object."); michael@0: michael@0: return aElement.mouseEvent(aOffsetX, aOffsetY, aEvent, aExpectedEvent); michael@0: } michael@0: michael@0: MozMillController.prototype.click = function (aElement, left, top, expectedEvent) { michael@0: logDeprecated("controller.click", "Use the MozMillElement object."); michael@0: michael@0: return aElement.click(left, top, expectedEvent); michael@0: } michael@0: michael@0: MozMillController.prototype.doubleClick = function (aElement, left, top, expectedEvent) { michael@0: logDeprecated("controller.doubleClick", "Use the MozMillElement object."); michael@0: michael@0: return aElement.doubleClick(left, top, expectedEvent); michael@0: } michael@0: michael@0: MozMillController.prototype.mouseDown = function (aElement, button, left, top, expectedEvent) { michael@0: logDeprecated("controller.mouseDown", "Use the MozMillElement object."); michael@0: michael@0: return aElement.mouseDown(button, left, top, expectedEvent); michael@0: }; michael@0: michael@0: MozMillController.prototype.mouseOut = function (aElement, button, left, top, expectedEvent) { michael@0: logDeprecated("controller.mouseOut", "Use the MozMillElement object."); michael@0: michael@0: return aElement.mouseOut(button, left, top, expectedEvent); michael@0: }; michael@0: michael@0: MozMillController.prototype.mouseOver = function (aElement, button, left, top, expectedEvent) { michael@0: logDeprecated("controller.mouseOver", "Use the MozMillElement object."); michael@0: michael@0: return aElement.mouseOver(button, left, top, expectedEvent); michael@0: }; michael@0: michael@0: MozMillController.prototype.mouseUp = function (aElement, button, left, top, expectedEvent) { michael@0: logDeprecated("controller.mouseUp", "Use the MozMillElement object."); michael@0: michael@0: return aElement.mouseUp(button, left, top, expectedEvent); michael@0: }; michael@0: michael@0: MozMillController.prototype.middleClick = function (aElement, left, top, expectedEvent) { michael@0: logDeprecated("controller.middleClick", "Use the MozMillElement object."); michael@0: michael@0: return aElement.middleClick(aElement, left, top, expectedEvent); michael@0: } michael@0: michael@0: MozMillController.prototype.rightClick = function (aElement, left, top, expectedEvent) { michael@0: logDeprecated("controller.rightClick", "Use the MozMillElement object."); michael@0: michael@0: return aElement.rightClick(left, top, expectedEvent); michael@0: } michael@0: michael@0: MozMillController.prototype.check = function (aElement, state) { michael@0: logDeprecated("controller.check", "Use the MozMillElement object."); michael@0: michael@0: return aElement.check(state); michael@0: } michael@0: michael@0: MozMillController.prototype.radio = function (aElement) { michael@0: logDeprecated("controller.radio", "Use the MozMillElement object."); michael@0: michael@0: return aElement.select(); michael@0: } michael@0: michael@0: MozMillController.prototype.waitThenClick = function (aElement, timeout, interval) { michael@0: logDeprecated("controller.waitThenClick", "Use the MozMillElement object."); michael@0: michael@0: return aElement.waitThenClick(timeout, interval); michael@0: } michael@0: michael@0: MozMillController.prototype.waitForElement = function (aElement, timeout, interval) { michael@0: logDeprecated("controller.waitForElement", "Use the MozMillElement object."); michael@0: michael@0: return aElement.waitForElement(timeout, interval); michael@0: } michael@0: michael@0: MozMillController.prototype.waitForElementNotPresent = function (aElement, timeout, interval) { michael@0: logDeprecated("controller.waitForElementNotPresent", "Use the MozMillElement object."); michael@0: michael@0: return aElement.waitForElementNotPresent(timeout, interval); michael@0: }