diff --git a/console/console-log-logged.html b/console/console-log-logged.html index 99fe1a336aaa58e..de527b2f611938b 100644 --- a/console/console-log-logged.html +++ b/console/console-log-logged.html @@ -5,19 +5,20 @@ diff --git a/resources/testdriver.js b/resources/testdriver.js index 4c2d27c8c030c24..89adc0803d4adeb 100644 --- a/resources/testdriver.js +++ b/resources/testdriver.js @@ -44,13 +44,70 @@ return pointerInteractablePaintTree.indexOf(element) !== -1; } - /** * @namespace {test_driver} */ window.test_driver = { - get event_target() { - return window.test_driver_internal.event_target; + /** + * @namespace {bidi} - Represents `WebDriver BiDi `_ protocol. + */ + bidi: { + /** + * `session `_ module. + */ + session: { + /** + * Subscribe to event. + * + * Events will be Matches the behaviour of the `session.subscribe + * `_ WebDriver BiDi command. + * + * @param {String} event - The event to subscribe to. + * @param {WindowProxy} context - Browsing context in which + * to run the call, or null for the current + * browsing context. + * @return {Promise} + */ + subscribe: function (event, context = null) { + return window.test_driver_bidi_internal.session.subscribe(event, context); + } + }, + /** + * `log `_ module. + */ + log: { + /** + * `log.entryAdded `_ event. + */ + entryAdded: { + /** + * Add an event listener for the `log.entryAdded + * `_ event. + * + * @param callback {function(event): void} - The callback to call when the event is fired. + * @returns {function(): void} - A function to call to remove the event listener. + */ + on: function (callback) { + return window.test_driver_bidi_internal.log.entryAdded.on(callback); + }, + /** + * Get a promise that resolves the next time the `log.entryAdded + * `_ event is fired. + * + * @returns {Promise} + */ + once: function () { + return new Promise((resolve) => { + const remove_handle = window.test_driver_bidi_internal.log.entryAdded.on( + (event) => { + remove_handle(); + resolve(event); + } + ); + }); + }, + } + } }, /** @@ -188,22 +245,6 @@ return window.test_driver_internal.delete_all_cookies(context); }, - /** - * Subscribe to event. - * - * Events will be Matches the behaviour of the `session.subscribe - * `_ WebDriver BiDi command. - * - * @param {String} event - The event to subscribe to. - * @param {WindowProxy} context - Browsing context in which - * to run the call, or null for the current - * browsing context. - * @return {Promise} - */ - subscribe: function(event, context=null) { - return window.test_driver_internal.subscribe(event, context); - }, - /** * Get details for all cookies in the current context. * See https://w3c.github.io/webdriver/#get-all-cookies @@ -1043,9 +1084,24 @@ */ get_virtual_sensor_information: function(sensor_type, context=null) { return window.test_driver_internal.get_virtual_sensor_information(sensor_type, context); - } + }, }; + window.test_driver_bidi_internal = { + session: { + subscribe(event, context=null) { + throw new Error("subscribe() is not implemented by testdriver-vendor.js"); + }, + }, + log: { + entryAdded: { + on: function () { + throw new Error("bidi.log.entryAdded.on is not implemented by testdriver-vendor.js"); + } + } + } + } + window.test_driver_internal = { /** * This flag should be set to `true` by any code which implements the @@ -1055,11 +1111,6 @@ */ in_automation: false, - get event_target() { - throw new Error("event_target is not implemented by testdriver-vendor.js"); - }, - - async click(element, coords) { if (this.in_automation) { throw new Error("click() is not implemented by testdriver-vendor.js"); @@ -1078,10 +1129,6 @@ throw new Error("get_all_cookies() is not implemented by testdriver-vendor.js"); }, - async subscribe(event, context=null) { - throw new Error("subscribe() is not implemented by testdriver-vendor.js"); - }, - async get_named_cookie(name, context=null) { throw new Error("get_named_cookie() is not implemented by testdriver-vendor.js"); }, diff --git a/tools/wptrunner/wptrunner/testdriver-extra.js b/tools/wptrunner/wptrunner/testdriver-extra.js index 0e342447244de2d..3b9c8de1614a3ef 100644 --- a/tools/wptrunner/wptrunner/testdriver-extra.js +++ b/tools/wptrunner/wptrunner/testdriver-extra.js @@ -2,6 +2,7 @@ (function() { const pending = new Map(); + const event_target = new EventTarget(); let result = null; let ctx_cmd_id = 0; @@ -41,10 +42,10 @@ } } else if (data.type === "testdriver-event") { const event_name = data.status; - const event_data = data.message; + const event_data = JSON.parse(data.message); const event =new Event(event_name); event.data = event_data; - window.test_driver_internal.event_target.dispatchEvent(event); + event_target.dispatchEvent(event); } }); @@ -168,12 +169,14 @@ window.test_driver_internal.in_automation = true; - const event_target = new EventTarget(); - Object.defineProperty(window.test_driver_internal, "event_target", { - get: function() { - return event_target; - } - }); + window.test_driver_bidi_internal.session.subscribe = function(event, context=null) { + return create_action("subscribe", {event, context}); + }; + + window.test_driver_bidi_internal.log.entryAdded.on = function (callback) { + event_target.addEventListener("log.entryAdded", callback); + return () => event_target.removeEventListener("log.entryAdded", callback); + }; window.test_driver_internal.set_test_context = function(context) { if (window.__wptrunner_message_queue) { @@ -192,10 +195,6 @@ return create_action("delete_all_cookies", {context}); }; - window.test_driver_internal.subscribe = function(event, context=null) { - return create_action("subscribe", {event, context}); - }; - window.test_driver_internal.get_all_cookies = function(context=null) { return create_action("get_all_cookies", {context}); };