Created
April 21, 2015 06:27
Revisions
-
felthy renamed this gist
Apr 21, 2015 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
felthy created this gist
Apr 21, 2015 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,35 @@ /** * When investigating a selenium test failure on a remote headless browser that couldn't be reproduced * locally, I wanted to add some javascript to the site under test that would dump some state to the * page (so it could be captured by Selenium as a screenshot when the test failed). JSON.stringify() * didn't work because the object declared a toJSON() method, and JSON.stringify() just calls that * method if it's present. This was a Moment object, so toJSON() returned a string but I wanted to see * the internal state of the object instead. * * So, this is a rough and ready function that recursively dumps any old javascript object. */ function printObject(o, indent) { var out = ''; if (typeof indent === 'undefined') { indent = 0; } for (var p in o) { if (o.hasOwnProperty(p)) { var val = o[p]; out += new Array(4 * indent + 1).join(' ') + p + ': '; if (typeof val === 'object') { if (val instanceof Date) { out += 'Date "' + val.toISOString() + '"'; } else { out += '{\n' + printObject(val, indent + 1) + new Array(4 * indent + 1).join(' ') + '}'; } } else if (typeof val === 'function') { } else { out += '"' + val + '"'; } out += ',\n'; } } return out; }