diff --git a/testing/marionette/evaluate.js b/testing/marionette/evaluate.js index ac781530e723..20fada29e58f 100644 --- a/testing/marionette/evaluate.js +++ b/testing/marionette/evaluate.js @@ -247,7 +247,7 @@ evaluate.fromJSON = function (obj, seenEls, win, shadowRoot = undefined) { * web elements. */ evaluate.toJSON = function (obj, seenEls) { - let t = Object.prototype.toString.call(obj); + const t = Object.prototype.toString.call(obj); // null if (t == "[object Undefined]" || t == "[object Null]") { @@ -270,6 +270,12 @@ evaluate.toJSON = function (obj, seenEls) { return element.makeWebElement(uuid); } + // custom JSON representation + else if (typeof obj["toJSON"] == "function") { + let unsafeJSON = obj.toJSON(); + return evaluate.toJSON(unsafeJSON, seenEls); + } + // arbitrary objects + files else { let rv = {}; diff --git a/testing/marionette/harness/marionette_harness/tests/unit/test_execute_script.py b/testing/marionette/harness/marionette_harness/tests/unit/test_execute_script.py index 66054376d622..1a7c208c5566 100644 --- a/testing/marionette/harness/marionette_harness/tests/unit/test_execute_script.py +++ b/testing/marionette/harness/marionette_harness/tests/unit/test_execute_script.py @@ -49,6 +49,9 @@ class TestExecuteContent(MarionetteTestCase): "return typeof arguments[0] != 'undefined'", [property], sandbox=sandbox), "property {} is undefined".format(property)) + def assert_is_web_element(self, element): + self.assertIsInstance(element, HTMLElement) + def test_return_number(self): self.assertEqual(1, self.marionette.execute_script("return 1")) self.assertEqual(1.5, self.marionette.execute_script("return 1.5")) @@ -330,6 +333,25 @@ class TestExecuteContent(MarionetteTestCase): # test inspection of arguments self.marionette.execute_script("__webDriverArguments.toString()") + def test_toJSON(self): + foo = self.marionette.execute_script(""" + return { + toJSON () { + return "foo"; + } + }""", + sandbox=None) + self.assertEqual("foo", foo) + + def test_unsafe_toJSON(self): + el = self.marionette.execute_script(""" + return { + toJSON () { + return document.documentElement; + } + }""", + sandbox=None) + self.assert_is_web_element(el) class TestExecuteChrome(WindowManagerMixin, TestExecuteContent):