Skip to content

Commit

Permalink
test: Print objects in console API calls
Browse files Browse the repository at this point in the history
These were previously just shown as an empty string with Chromium, or as
"[Object object]" with Firefox. Implement proper stringification of
objects for both browsers, so that we can see console messages in our
test logs in a similar way as browsers format them in their console.
  • Loading branch information
martinpitt committed Oct 20, 2023
1 parent 918bca9 commit bfe16d2
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
15 changes: 14 additions & 1 deletion test/common/chromium-cdp-driver.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,24 @@ function clearExceptions() {
return Promise.resolve();
}

function stringifyConsoleArg(arg) {
if (arg.type === 'string')
return arg.value;
if (arg.type === 'object') {
const obj = {};
arg.preview.properties.forEach(prop => {
obj[prop.name] = prop.value.toString();
});
return JSON.stringify(obj);
}
return JSON.stringify(arg);
}

function setupLogging(client) {
client.Runtime.enable();

client.Runtime.consoleAPICalled(info => {
const msg = info.args.map(v => (v.value || "").toString()).join(" ");
const msg = info.args.map(stringifyConsoleArg).join(" ");
messages.push([info.type, msg]);
process.stderr.write("> " + info.type + ": " + msg + "\n");

Expand Down
10 changes: 9 additions & 1 deletion test/common/firefox-cdp-driver.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,19 @@ function clearExceptions() {
return Promise.resolve();
}

function stringifyConsoleArg(arg) {
if (arg.type === 'string')
return arg.value;
if (arg.type === 'object')
return JSON.stringify(arg.value);
return JSON.stringify(arg);
}

function setupLogging(client) {
client.Runtime.enable();

client.Runtime.consoleAPICalled(info => {
const msg = info.args.map(v => (v.value || "").toString()).join(" ");
const msg = info.args.map(stringifyConsoleArg).join(" ");
messages.push([info.type, msg]);
process.stderr.write("> " + info.type + ": " + msg + "\n");

Expand Down

0 comments on commit bfe16d2

Please sign in to comment.