Implicitly create BrowserContext on createTarget if one doesn't exist

This commit is contained in:
Karl Seguin
2025-03-17 20:45:57 +08:00
parent 671dbcfd55
commit 430779979e

View File

@@ -100,7 +100,11 @@ fn createTarget(cmd: anytype) !void {
// forTab: ?bool = null, // forTab: ?bool = null,
})) orelse return error.InvalidParams; })) orelse return error.InvalidParams;
const bc = cmd.browser_context orelse return error.BrowserContextNotLoaded; const bc = cmd.browser_context orelse cmd.createBrowserContext() catch |err| switch (err) {
error.AlreadyExists => unreachable,
else => return err,
};
if (bc.target_id != null) { if (bc.target_id != null) {
return error.TargetAlreadyLoaded; return error.TargetAlreadyLoaded;
} }
@@ -459,18 +463,19 @@ test "cdp.target: disposeBrowserContext" {
} }
test "cdp.target: createTarget" { test "cdp.target: createTarget" {
var ctx = testing.context();
defer ctx.deinit();
{ {
try testing.expectError(error.BrowserContextNotLoaded, ctx.processMessage(.{ var ctx = testing.context();
.id = 10, defer ctx.deinit();
.method = "Target.createTarget", try ctx.processMessage(.{ .id = 10, .method = "Target.createTarget", .params = .{ .url = "about/blank" } });
.params = struct {}{},
})); // should create a browser context
try ctx.expectSentError(-31998, "BrowserContextNotLoaded", .{ .id = 10 }); const bc = ctx.cdp().browser_context.?;
try ctx.expectSentEvent("Target.targetCreated", .{ .targetInfo = .{ .url = "about:blank", .title = "about:blank", .attached = false, .type = "page", .canAccessOpener = false, .browserContextId = bc.id, .targetId = bc.target_id.? } }, .{});
try ctx.expectSentEvent("Target.attachedToTarget", .{ .sessionId = bc.session_id.?, .targetInfo = .{ .url = "chrome://newtab/", .title = "about:blank", .attached = true, .type = "page", .canAccessOpener = false, .browserContextId = bc.id, .targetId = bc.target_id.? } }, .{});
} }
var ctx = testing.context();
defer ctx.deinit();
const bc = try ctx.loadBrowserContext(.{ .id = "BID-9" }); const bc = try ctx.loadBrowserContext(.{ .id = "BID-9" });
{ {
try testing.expectError(error.UnknownBrowserContextId, ctx.processMessage(.{ .id = 10, .method = "Target.createTarget", .params = .{ .browserContextId = "BID-8" } })); try testing.expectError(error.UnknownBrowserContextId, ctx.processMessage(.{ .id = 10, .method = "Target.createTarget", .params = .{ .browserContextId = "BID-8" } }));