Skip to content

Commit

Permalink
Update test_driver.js API
Browse files Browse the repository at this point in the history
  • Loading branch information
sadym-chromium committed Feb 16, 2024
1 parent 887104b commit 465c289
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 54 deletions.
27 changes: 14 additions & 13 deletions console/console-log-logged.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,20 @@
<script src="/resources/testharnessreport.js"></script>
<script src="/resources/testdriver.js"></script>
<script>
promise_test(async t => {
promise_test(async () => {
const some_message = "SOME MESSAGE";
await test_driver.subscribe("log.entryAdded");
// Subscribe to log.entryAdded BiDi events.
await test_driver.bidi.session.subscribe("log.entryAdded");
// Add a listener for the log.entryAdded event.
const log_entry_promise = test_driver.bidi.log.entryAdded.once();
// Emit a console.log message.
console.log(some_message)
let resolve;
const promise = new Promise(r => resolve = r);

window.test_driver_internal.event_target.addEventListener("log.entryAdded", event => {
resolve(event)
});

const event = await promise;
const data = JSON.parse(event.data);
assert_equals(data.args[0].value, some_message)
}, "Subscribe to log events");
// Wait for the log.entryAdded event to be received.
const event = await log_entry_promise;
// Assert the log.entryAdded event has the expected message.
const data = event.data;
assert_equals(data.args.length, 1);
const event_message = data.args[0];
assert_equals(event_message.value, some_message);
}, "Assert log event are logged");
</script>
105 changes: 76 additions & 29 deletions resources/testdriver.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 <https://w3c.github.io/webdriver-bidi>`_ protocol.
*/
bidi: {
/**
* `session <https://w3c.github.io/webdriver-bidi/#module-session>`_ module.
*/
session: {
/**
* Subscribe to event.
*
* Events will be Matches the behaviour of the `session.subscribe
* <https://w3c.github.io/webdriver-bidi/#command-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<never>}
*/
subscribe: function (event, context = null) {
return window.test_driver_bidi_internal.session.subscribe(event, context);
}
},
/**
* `log <https://w3c.github.io/webdriver-bidi/#module-log>`_ module.
*/
log: {
/**
* `log.entryAdded <https://w3c.github.io/webdriver-bidi/#event-log-entryAdded>`_ event.
*/
entryAdded: {
/**
* Add an event listener for the `log.entryAdded
* <https://w3c.github.io/webdriver-bidi/#event-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
* <https://w3c.github.io/webdriver-bidi/#event-log-entryAdded>`_ event is fired.
*
* @returns {Promise<Object>}
*/
once: function () {
return new Promise((resolve) => {
const remove_handle = window.test_driver_bidi_internal.log.entryAdded.on(
(event) => {
remove_handle();
resolve(event);
}
);
});
},
}
}
},

/**
Expand Down Expand Up @@ -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
* <https://w3c.github.io/webdriver-bidi/#command-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<never>}
*/
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
Expand Down Expand Up @@ -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
Expand All @@ -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");
Expand All @@ -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");
},
Expand Down
23 changes: 11 additions & 12 deletions tools/wptrunner/wptrunner/testdriver-extra.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

(function() {
const pending = new Map();
const event_target = new EventTarget();

let result = null;
let ctx_cmd_id = 0;
Expand Down Expand Up @@ -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);
}
});

Expand Down Expand Up @@ -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) {
Expand All @@ -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});
};
Expand Down

0 comments on commit 465c289

Please sign in to comment.