Skip to content

Lightpanda: the headless browser designed for AI and automation

cdp: report console.log/console.warn with their own consoleAPICalled types

Navid EMADb136b3a84d3a
diff --git a/src/Notification.zig b/src/Notification.zig
index 1dff474fe..c8064f12f 100644
--- a/src/Notification.zig
+++ b/src/Notification.zig
@@ -265,9 +265,10 @@ pub const DialogResponse = struct {
};
pub const ConsoleMessageType = enum {
+ log,
debug,
info,
- warn,
+ warning,
@"error",
fatal,
trace,
diff --git a/src/browser/webapi/Console.zig b/src/browser/webapi/Console.zig
index 3d97325a5..a7f1661e1 100644
--- a/src/browser/webapi/Console.zig
+++ b/src/browser/webapi/Console.zig
@@ -71,12 +71,12 @@ pub fn info(_: *const Console, values: []js.Value, exec: *js.Execution) void {
pub fn log(_: *const Console, values: []js.Value, exec: *js.Execution) void {
logger.info(.js, "console.log", .{ValueWriter{ .values = values }});
- dispatchConsoleMessage(values, .info, exec);
+ dispatchConsoleMessage(values, .log, exec);
}
pub fn warn(_: *const Console, values: []js.Value, exec: *js.Execution) void {
logger.warn(.js, "console.warn", .{ValueWriter{ .values = values }});
- dispatchConsoleMessage(values, .info, exec);
+ dispatchConsoleMessage(values, .warning, exec);
}
pub fn clear(_: *const Console) void {}
diff --git a/src/cdp/domains/runtime.zig b/src/cdp/domains/runtime.zig
index 0ca9c6c15..6228ee738 100644
--- a/src/cdp/domains/runtime.zig
+++ b/src/cdp/domains/runtime.zig
@@ -163,3 +163,28 @@ pub fn consoleMessage(arena: Allocator, bc: *CDP.BrowserContext, event: *const N
.args = args.items,
}, .{ .session_id = session_id });
}
+
+const testing = @import("../testing.zig");
+
+test "cdp.runtime: consoleAPICalled type matches the console method" {
+ // Wire types per the CDP protocol: console.log -> "log",
+ // console.warn -> "warning" (not "warn"), console.info -> "info",
+ // console.error -> "error", console.debug -> "debug".
+ var ctx = try testing.context();
+ defer ctx.deinit();
+
+ var bc = try ctx.loadBrowserContext(.{ .id = "BID-CONS", .url = "hi.html", .target_id = "FID-0000000CON".* });
+ try ctx.processMessage(.{ .id = 60, .method = "Runtime.enable" });
+
+ const frame = bc.session.currentFrame() orelse unreachable;
+ var ls: js.Local.Scope = undefined;
+ frame.js.localScope(&ls);
+ defer ls.deinit();
+ _ = try ls.local.exec("console.log('l'); console.warn('w'); console.info('i'); console.error('e'); console.debug('d');", null);
+
+ try ctx.expectSentEvent("Runtime.consoleAPICalled", .{ .type = "log" }, .{});
+ try ctx.expectSentEvent("Runtime.consoleAPICalled", .{ .type = "warning" }, .{});
+ try ctx.expectSentEvent("Runtime.consoleAPICalled", .{ .type = "info" }, .{});
+ try ctx.expectSentEvent("Runtime.consoleAPICalled", .{ .type = "error" }, .{});
+ try ctx.expectSentEvent("Runtime.consoleAPICalled", .{ .type = "debug" }, .{});
+}