Bug 1405474 - Add more attributes for elements in pprint output. draft
authorHenrik Skupin <mail@hskupin.info>
Wed, 18 Oct 2017 20:54:13 +0200
changeset 682837 6ce57dcf150102d129be73f3ffeea9779fc32763
parent 682167 4efa95ae9141a1973b2cdc026f315691f4e3d7ea
child 736456 f8f93139d293c4078218613763df3f530ca8f83b
push id85173
push userbmo:hskupin@gmail.com
push dateWed, 18 Oct 2017 20:23:47 +0000
bugs1405474
milestone58.0a1
Bug 1405474 - Add more attributes for elements in pprint output. Currently pprint only checks for 'id' and 'class', and adds those to the output. Given that lots of elements might not have those attributes a couple more should be added, which can help to uniquly identify those. MozReview-Commit-ID: 3thefe4oLN3
testing/marionette/format.js
testing/marionette/test_error.js
testing/marionette/test_format.js
--- a/testing/marionette/format.js
+++ b/testing/marionette/format.js
@@ -16,17 +16,17 @@ const MAX_STRING_LENGTH = 250;
  * <pre><code>
  *     const {pprint} = Cu.import("chrome://marionette/content/error.js", {});
  *     let bool = {value: true};
  *     pprint`Expected boolean, got ${bool}`;
  *     => 'Expected boolean, got [object Object] {"value": true}'
  *
  *     let htmlElement = document.querySelector("input#foo");
  *     pprint`Expected element ${htmlElement}`;
- *     => 'Expected element <input id="foo" class="bar baz">'
+ *     => 'Expected element <input id="foo" class="bar baz" type="input">'
  * </code></pre>
  */
 function pprint(ss, ...values) {
   function prettyObject(obj) {
     let proto = Object.prototype.toString.call(obj);
     let s = "";
     try {
       s = JSON.stringify(obj);
@@ -36,27 +36,23 @@ function pprint(ss, ...values) {
       } else {
         throw e;
       }
     }
     return proto + " " + s;
   }
 
   function prettyElement(el) {
-    let ident = [];
-    if (el.id) {
-      ident.push(`id="${el.id}"`);
-    }
-    if (el.classList.length > 0) {
-      ident.push(`class="${el.className}"`);
-    }
+    let attrs = ["id", "class", "href", "name", "src", "type"];
 
     let idents = "";
-    if (ident.length > 0) {
-      idents = " " + ident.join(" ");
+    for (let attr of attrs) {
+      if (el.hasAttribute(attr)) {
+        idents += ` ${attr}="${el.getAttribute(attr)}"`;
+      }
     }
 
     return `<${el.localName}${idents}>`;
   }
 
   let res = [];
   for (let i = 0; i < ss.length; i++) {
     res.push(ss[i]);
--- a/testing/marionette/test_error.js
+++ b/testing/marionette/test_error.js
@@ -204,24 +204,26 @@ add_test(function test_WebDriverError() 
   equal("webdriver error", err.status);
   ok(err instanceof WebDriverError);
 
   run_next_test();
 });
 
 add_test(function test_ElementClickInterceptedError() {
   let otherEl = {
+    hasAttribute: attr => attr in otherEl,
+    getAttribute: attr => attr in otherEl ? otherEl[attr] : null,
     nodeType: 1,
     localName: "a",
-    classList: [],
   };
   let obscuredEl = {
+    hasAttribute: attr => attr in obscuredEl,
+    getAttribute: attr => attr in obscuredEl ? obscuredEl[attr] : null,
     nodeType: 1,
     localName: "b",
-    classList: [],
     ownerDocument: {
       elementFromPoint: function (x, y) {
         return otherEl;
       },
     },
     style: {
       pointerEvents: "auto",
     }
--- a/testing/marionette/test_format.js
+++ b/testing/marionette/test_format.js
@@ -20,23 +20,29 @@ add_test(function test_pprint() {
   let complexObj = {toJSON: () => "foo"};
   equal('[object Object] "foo"', pprint`${complexObj}`);
 
   let cyclic = {};
   cyclic.me = cyclic;
   equal("[object Object] <cyclic object value>", pprint`${cyclic}`);
 
   let el = {
+    hasAttribute: attr => attr in el,
+    getAttribute: attr => attr in el ? el[attr] : null,
     nodeType: 1,
     localName: "input",
     id: "foo",
-    classList: {length: 1},
-    className: "bar baz",
+    class: "a b",
+    href: "#",
+    name: "bar",
+    src: "s",
+    type: "t",
   };
-  equal('<input id="foo" class="bar baz">', pprint`${el}`);
+  equal('<input id="foo" class="a b" href="#" name="bar" src="s" type="t">',
+        pprint`${el}`);
 
   run_next_test();
 });
 
 add_test(function test_truncate_empty() {
   equal(truncate``, "");
   run_next_test();
 });