Bug 1274274 - Convert ElementManager to a class; r?automatedtester draft
authorAndreas Tolfsen <ato@mozilla.com>
Fri, 20 May 2016 13:32:52 +0100
changeset 370716 78079075037133904c128a65d5481aee8d9ebeb5
parent 370715 9d19d9e82de19ad31cea2aa1f33e010126be1350
child 370717 701e18bb990227960df41d2f3ebf492b46f3aef6
push id19137
push userbmo:ato@mozilla.com
push dateWed, 25 May 2016 09:03:54 +0000
reviewersautomatedtester
bugs1274274
milestone49.0a1
Bug 1274274 - Convert ElementManager to a class; r?automatedtester MozReview-Commit-ID: EqcuWGxxlC
testing/marionette/element.js
--- a/testing/marionette/element.js
+++ b/testing/marionette/element.js
@@ -54,57 +54,57 @@ element.Strategy = {
   LinkText: "link text",
   PartialLinkText: "partial link text",
   TagName: "tag name",
   XPath: "xpath",
   Anon: "anon",
   AnonAttribute: "anon attribute",
 };
 
-this.ElementManager = function ElementManager() {
-  this.seenItems = {};
-  this.timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
-};
+this.ElementManager = class {
+  constructor() {
+    this.seenItems = {};
+    this.timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
+  }
 
-ElementManager.prototype = {
   /**
    * Reset values
    */
-  reset: function EM_clear() {
+  reset() {
     this.seenItems = {};
-  },
+  }
 
   /**
    * Make a collection of elements seen.
    *
    * The oder of the returned web element references is guaranteed to
    * match that of the collection passed in.
    *
    * @param {NodeList} els
    *     Sequence of elements to add to set of seen elements.
    *
    * @return {Array.<WebElement>}
    *     List of the web element references associated with each element
    *     from |els|.
    */
-  addAll: function(els) {
+  addAll(els) {
     let add = this.add.bind(this);
     return [...els].map(add);
-  },
+  }
 
   /**
-  * Make an element seen.
-  *
-  * @param {nsIDOMElement} el
-  *    Element to add to set of seen elements.
-  *
-  * @return {string}
-  *     Web element reference associated with element.
-  */
-  add: function(el) {
+   * Make an element seen.
+   *
+   * @param {nsIDOMElement} el
+   *    Element to add to set of seen elements.
+   *
+   * @return {string}
+   *     Web element reference associated with element.
+   */
+  add(el) {
     for (let i in this.seenItems) {
       let foundEl;
       try {
         foundEl = this.seenItems[i].get();
       } catch (e) {}
 
       if (foundEl) {
         if (XPCNativeWrapper(foundEl) == XPCNativeWrapper(el)) {
@@ -114,30 +114,30 @@ ElementManager.prototype = {
         // cleanup reference to GC'd element
         delete this.seenItems[i];
       }
     }
 
     let id = element.generateUUID();
     this.seenItems[id] = Cu.getWeakReference(el);
     return id;
-  },
+  }
 
   /**
    * Retrieve element from its unique ID
    *
    * @param String id
    *        The DOM reference ID
    * @param nsIDOMWindow, ShadowRoot container
    *        The window and an optional shadow root that contains the element
    *
    * @returns nsIDOMElement
    *        Returns the element or throws Exception if not found
    */
-  getKnownElement: function EM_getKnownElement(id, container) {
+  getKnownElement(id, container) {
     let el = this.seenItems[id];
     if (!el) {
       throw new JavaScriptError(`Element has not been seen before. Id given was ${id}`);
     }
     try {
       el = el.get();
     }
     catch(e) {
@@ -155,64 +155,64 @@ ElementManager.prototype = {
         !(XPCNativeWrapper(el).ownerDocument == wrappedFrame.document) ||
         this.isDisconnected(XPCNativeWrapper(el), wrappedShadowRoot,
           wrappedFrame)) {
       throw new StaleElementReferenceError(
           "The element reference is stale. Either the element " +
           "is no longer attached to the DOM or the page has been refreshed.");
     }
     return el;
-  },
+  }
 
   /**
    * Check if the element is detached from the current frame as well as the
    * optional shadow root (when inside a Shadow DOM context).
    * @param nsIDOMElement el
    *        element to be checked
    * @param ShadowRoot shadowRoot
    *        an optional shadow root containing an element
    * @param nsIDOMWindow frame
    *        window that contains the element or the current host of the shadow
    *        root.
    * @return {Boolean} a flag indicating that the element is disconnected
    */
-  isDisconnected: function EM_isDisconnected(el, shadowRoot, frame) {
+  isDisconnected(el, shadowRoot, frame) {
     if (shadowRoot && frame.ShadowRoot) {
       if (el.compareDocumentPosition(shadowRoot) &
         DOCUMENT_POSITION_DISCONNECTED) {
         return true;
       }
       // Looking for next possible ShadowRoot ancestor
       let parent = shadowRoot.host;
       while (parent && !(parent instanceof frame.ShadowRoot)) {
         parent = parent.parentNode;
       }
       return this.isDisconnected(shadowRoot.host, parent, frame);
     } else {
       return el.compareDocumentPosition(frame.document.documentElement) &
         DOCUMENT_POSITION_DISCONNECTED;
     }
-  },
+  }
 
   /**
    * Convert values to primitives that can be transported over the
    * Marionette protocol.
    *
    * This function implements the marshaling algorithm defined in the
    * WebDriver specification:
    *
    *     https://dvcs.w3.org/hg/webdriver/raw-file/tip/webdriver-spec.html#synchronous-javascript-execution
    *
    * @param object val
    *        object to be marshaled
    *
    * @return object
    *         Returns a JSON primitive or Object
    */
-  wrapValue: function EM_wrapValue(val) {
+  wrapValue(val) {
     let result = null;
 
     switch (typeof(val)) {
       case "undefined":
         result = null;
         break;
 
       case "string":
@@ -247,31 +247,31 @@ ElementManager.prototype = {
               logger.debug(`Skipping ${prop} due to: ${e.message}`);
             }
           }
         }
         break;
     }
 
     return result;
-  },
+  }
 
   /**
    * Convert any ELEMENT references in 'args' to the actual elements
    *
    * @param object args
    *        Arguments passed in by client
    * @param nsIDOMWindow, ShadowRoot container
    *        The window and an optional shadow root that contains the element
    *
    * @returns object
    *        Returns the objects passed in by the client, with the
    *        reference IDs replaced by the actual elements.
    */
-  convertWrappedArguments: function EM_convertWrappedArguments(args, container) {
+  convertWrappedArguments(args, container) {
     let converted;
     switch (typeof(args)) {
       case 'number':
       case 'string':
       case 'boolean':
         converted = args;
         break;
       case 'object':