Merge pull request #450 from lightpanda-io/cdp-playwright

cdp: improve playwright support
This commit is contained in:
Pierre Tachoire
2025-03-10 15:56:41 +01:00
committed by GitHub
3 changed files with 101 additions and 11 deletions

View File

@@ -262,3 +262,37 @@ fn navigate(cmd: anytype) !void {
.frameId = bc.frame_id,
}, .{ .session_id = session_id });
}
const testing = @import("testing.zig");
test "cdp.page: getFrameTree" {
var ctx = testing.context();
defer ctx.deinit();
{
try testing.expectError(error.BrowserContextNotLoaded, ctx.processMessage(.{ .id = 10, .method = "Page.getFrameTree", .params = .{ .targetId = "X" } }));
try ctx.expectSentError(-31998, "BrowserContextNotLoaded", .{ .id = 10 });
}
const bc = try ctx.loadBrowserContext(.{ .id = "BID-9" });
{
try ctx.processMessage(.{ .id = 11, .method = "Page.getFrameTree" });
try ctx.expectSentResult(.{
.frameTree = .{
.frame = .{
.id = bc.frame_id,
.loaderId = bc.loader_id,
.url = bc.url,
.domainAndRegistry = "",
.securityOrigin = bc.security_origin,
.mimeType = "text/html",
.adFrameStatus = .{
.adFrameType = "none",
},
.secureContextType = bc.secure_context_type,
.crossOriginIsolatedContextType = "NotIsolated",
.gatedAPIFeatures = [_][]const u8{},
},
},
}, .{ .id = 11 });
}
}

View File

@@ -56,6 +56,7 @@ fn sendInspector(cmd: anytype, action: anytype) !void {
}
}
// the result to return is handled directly by the inspector.
bc.session.callInspector(cmd.input.json);
}

View File

@@ -231,21 +231,25 @@ fn getTargetInfo(cmd: anytype) !void {
}
return cmd.sendResult(.{
.targetInfo = .{
.targetId = target_id,
.type = "page",
.title = "",
.url = "",
.attached = true,
.canAccessOpener = false,
},
}, .{ .include_session_id = false });
}
return cmd.sendResult(.{
.targetInfo = .{
.type = "browser",
.title = "",
.url = "",
.attached = true,
.canAccessOpener = false,
},
}, .{ .include_session_id = false });
}
@@ -548,3 +552,54 @@ test "cdp.target: attachToTarget" {
try ctx.expectSentEvent("Target.attachedToTarget", .{ .sessionId = session_id, .targetInfo = .{ .url = "chrome://newtab/", .title = "about:blank", .attached = true, .type = "page", .canAccessOpener = false, .browserContextId = "BID-9", .targetId = bc.target_id.? } }, .{});
}
}
test "cdp.target: getTargetInfo" {
var ctx = testing.context();
defer ctx.deinit();
{
try ctx.processMessage(.{ .id = 9, .method = "Target.getTargetInfo" });
try ctx.expectSentResult(.{
.targetInfo = .{
.type = "browser",
.title = "",
.url = "",
.attached = true,
.canAccessOpener = false,
},
}, .{ .id = 9 });
}
{
try testing.expectError(error.BrowserContextNotLoaded, ctx.processMessage(.{ .id = 10, .method = "Target.getTargetInfo", .params = .{ .targetId = "X" } }));
try ctx.expectSentError(-31998, "BrowserContextNotLoaded", .{ .id = 10 });
}
const bc = try ctx.loadBrowserContext(.{ .id = "BID-9" });
{
try testing.expectError(error.TargetNotLoaded, ctx.processMessage(.{ .id = 10, .method = "Target.getTargetInfo", .params = .{ .targetId = "TID-8" } }));
try ctx.expectSentError(-31998, "TargetNotLoaded", .{ .id = 10 });
}
// pretend we createdTarget first
_ = try bc.session.createPage();
bc.target_id = "TID-A";
{
try testing.expectError(error.UnknownTargetId, ctx.processMessage(.{ .id = 10, .method = "Target.getTargetInfo", .params = .{ .targetId = "TID-8" } }));
try ctx.expectSentError(-31998, "UnknownTargetId", .{ .id = 10 });
}
{
try ctx.processMessage(.{ .id = 11, .method = "Target.getTargetInfo", .params = .{ .targetId = "TID-A" } });
try ctx.expectSentResult(.{
.targetInfo = .{
.targetId = "TID-A",
.type = "page",
.title = "",
.url = "",
.attached = true,
.canAccessOpener = false,
},
}, .{ .id = 11 });
}
}