mirror of
https://github.com/lightpanda-io/browser.git
synced 2026-03-29 16:10:04 +00:00
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:
@@ -129,9 +129,10 @@ pub fn CDPT(comptime TypeProvider: type) type {
|
||||
// A bit hacky right now. The main server loop doesn't unblock for
|
||||
// scheduled task. So we run this directly in order to process any
|
||||
// timeouts (or http events) which are ready to be processed.
|
||||
pub fn pageWait(self: *Self, ms: u32) Session.WaitResult {
|
||||
const session = &(self.browser.session orelse return .no_page);
|
||||
return session.wait(.{ .timeout_ms = ms });
|
||||
pub fn pageWait(self: *Self, ms: u32) !Session.Runner.CDPWaitResult {
|
||||
const session = &(self.browser.session orelse return error.NoPage);
|
||||
var runner = try session.runner(.{});
|
||||
return runner.waitCDP(.{ .ms = ms });
|
||||
}
|
||||
|
||||
// Called from above, in processMessage which handles client messages
|
||||
|
||||
@@ -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(.{
|
||||
|
||||
@@ -136,7 +136,8 @@ const TestContext = struct {
|
||||
0,
|
||||
);
|
||||
try page.navigate(full_url, .{});
|
||||
_ = bc.session.wait(.{});
|
||||
var runner = try bc.session.runner(.{});
|
||||
try runner.wait(.{ .ms = 2000 });
|
||||
}
|
||||
return bc;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user