Skip to content

Lightpanda: the headless browser designed for AI and automation

fetch: default --wait-until to load

Adrià Arrufatd79fb2fa6c98
diff --git a/src/help.zon b/src/help.zon
index e88ede3b0..f01175831 100644
--- a/src/help.zon
+++ b/src/help.zon
@@ -114,10 +114,13 @@
\\ --wait-until condition is met.
\\ --wait-until <UNTIL>
\\ Wait until the specified event. Checked before other --wait-* options.
- \\ Defaults to 'done'. If --wait-selector, --wait-script or
+ \\ Defaults to 'load'. If --wait-selector, --wait-script or
\\ --wait-script-file specified, defaults to none.
\\ Allowed values: "load", "domcontentloaded", "networkalmostidle",
\\ "networkidle", "done".
+ \\ 'done' waits for full quiescence (no pending scripts or network);
+ \\ pages with constant background activity never reach it and run
+ \\ to --wait-ms.
\\ --with-base
\\ Add a <base> tag in dump.
\\ Defaults to false.
diff --git a/src/lightpanda.zig b/src/lightpanda.zig
index 67f6ed37e..52bd67208 100644
--- a/src/lightpanda.zig
+++ b/src/lightpanda.zig
@@ -189,10 +189,22 @@ pub const FetchOpts = struct {
writer: ?*std.Io.Writer = null,
json: bool = false,
};
+
+/// `.load`, not `.done`: pages with constant background activity never go
+/// quiescent, so `.done` just rides the `wait_ms` cap. A lone
+/// `wait_selector`/`wait_script` is itself the wait, so no level applies
+/// unless given explicitly.
+fn resolveWaitUntil(opts: FetchOpts) ?Config.WaitUntil {
+ if (opts.wait_until) |wu| return wu;
+ if (opts.wait_selector == null and opts.wait_script == null) return .load;
+ return null;
+}
/// Loads each url in `urls` in a fresh session and waits per `opts`.
///
/// Errors:
-/// - `error.Timeout` if the wait deadline (`opts.wait_ms`) expires.
+/// - `error.Timeout` if the deadline expires while a `wait_selector` or
+/// `wait_script` is still unmet. The `wait_until` phase never raises it:
+/// when the budget runs out the page is dumped as-is.
/// - `error.Cancelled` if the embedder installed a `Session.cancel_hook`
/// that returned true during the wait. The hook is opt-in via
/// `session.cancel_hook = .{...}`; without it, this error never fires.
@@ -239,13 +251,8 @@ pub fn fetch(app: *App, browser: *Browser, urls: []const [:0]const u8, opts: Fet
var timer: std.Io.Timestamp = .now(io, .boot);
- if (opts.wait_until) |wu| {
+ if (resolveWaitUntil(opts)) |wu| {
try runner.waitForAll(opts.wait_ms, .{ .until = wu });
- } else if (opts.wait_selector == null and opts.wait_script == null) {
- // We default to .done if both wait_selector and wait_script are null
- // This allows the caller to ONLY --wait-selector or ONLY --wait-script
- // or combine --wait-until WITH --wait-selector/script
- try runner.waitForAll(opts.wait_ms, .{ .until = .done });
}
if (opts.wait_selector) |selector| {
@@ -508,6 +515,17 @@ test "writeJsonEnvelope: null frame with dump mode and content" {
}, aw.written());
}
+test "fetch: resolveWaitUntil" {
+ try testing.expectEqual(.load, resolveWaitUntil(.{ .dump = .{} }));
+ try testing.expectEqual(.done, resolveWaitUntil(.{ .dump = .{}, .wait_until = .done }));
+ try testing.expectEqual(null, resolveWaitUntil(.{ .dump = .{}, .wait_selector = "#main" }));
+ try testing.expectEqual(null, resolveWaitUntil(.{ .dump = .{}, .wait_script = "true" }));
+ try testing.expectEqual(
+ .networkidle,
+ resolveWaitUntil(.{ .dump = .{}, .wait_until = .networkidle, .wait_selector = "#main" }),
+ );
+}
+
test {
std.testing.refAllDecls(@This());
}