Extract Session.wait into a Runner

This is done for a couple reasons. The first is just to have things a little
more self-contained for eventually supporting more advanced "wait" logic, e.g.
waiting for a selector.

The other is to provide callers with more fine-grained controlled. Specifically
the ability to manually "tick", so that they can [presumably] do something
after every tick. This is needed by the test runner to support more advanced
cases (cases that need to test beyond 'load') and it also improves (and fixes
potential use-after-free, the lp.waitForSelector)
This commit is contained in:
Karl Seguin
2026-03-23 12:15:53 +08:00
parent a69a22ccd7
commit c9bc370d6a
15 changed files with 315 additions and 241 deletions

View File

@@ -241,12 +241,12 @@ fn waitForSelector(cmd: anytype) !void {
const params = (try cmd.params(Params)) orelse return error.InvalidParam;
const bc = cmd.browser_context orelse return error.NoBrowserContext;
const page = bc.session.currentPage() orelse return error.PageNotLoaded;
_ = bc.session.currentPage() orelse return error.PageNotLoaded;
const timeout_ms = params.timeout orelse 5000;
const selector_z = try cmd.arena.dupeZ(u8, params.selector);
const node = lp.actions.waitForSelector(selector_z, timeout_ms, page) catch |err| {
const node = lp.actions.waitForSelector(selector_z, timeout_ms, bc.session) catch |err| {
if (err == error.InvalidSelector) return error.InvalidParam;
if (err == error.Timeout) return error.InternalError;
return error.InternalError;
@@ -316,7 +316,8 @@ test "cdp.lp: action tools" {
const page = try bc.session.createPage();
const url = "http://localhost:9582/src/browser/tests/mcp_actions.html";
try page.navigate(url, .{ .reason = .address_bar, .kind = .{ .push = null } });
_ = bc.session.wait(.{});
var runner = try bc.session.runner(.{});
try runner.wait(.{ .ms = 2000 });
// Test Click
const btn = page.document.getElementById("btn", page).?.asNode();
@@ -376,7 +377,8 @@ test "cdp.lp: waitForSelector" {
const page = try bc.session.createPage();
const url = "http://localhost:9582/src/browser/tests/mcp_wait_for_selector.html";
try page.navigate(url, .{ .reason = .address_bar, .kind = .{ .push = null } });
_ = bc.session.wait(.{});
var runner = try bc.session.runner(.{});
try runner.wait(.{.ms = 2000});
// 1. Existing element
try ctx.processMessage(.{