Skip to content

Lightpanda: the headless browser designed for AI and automation

runner: run timers in text and image documents

Scott Taylor012d9ba48718
diff --git a/src/TestHTTPServer.zig b/src/TestHTTPServer.zig
index e005c085b..bbd23289a 100644
--- a/src/TestHTTPServer.zig
+++ b/src/TestHTTPServer.zig
@@ -158,6 +158,10 @@ fn getContentType(file_path: []const u8) []const u8 {
return "text/html";
}
+ if (std.mem.endsWith(u8, file_path, ".txt")) {
+ return "text/plain";
+ }
+
if (std.mem.endsWith(u8, file_path, ".xml")) {
// some wpt tests do this
return "text/xml";
diff --git a/src/browser/Runner.zig b/src/browser/Runner.zig
index 0fb715fb1..e2dbcee5c 100644
--- a/src/browser/Runner.zig
+++ b/src/browser/Runner.zig
@@ -479,7 +479,9 @@ fn firstConditionError(conditions: []const WaitCondition) !void {
fn hasRunnablePage(session: *Session) bool {
for (session.pages.items) |page| {
switch (page.frame._parse_state) {
- .html, .complete => return true,
+ // A text or image document has a JS context too: its timers and
+ // animation frames run like an HTML document's.
+ .html, .complete, .text, .raw_done => return true,
else => {},
}
}
@@ -519,6 +521,34 @@ test "Runner: waitForScript" {
try runner.waitForScript(page.frame_id, "document.querySelector('#sel1')", 10);
}
+fn expectTimersRun(url: [:0]const u8) !void {
+ const page = try testing.test_session.createPage();
+ defer page.close();
+ try page.navigate(url, .{});
+
+ var runner = page.session.runner(.{});
+ try runner.waitForFrame(page.frame_id, 2000, .{ .until = .done });
+ {
+ var ls: js.Local.Scope = undefined;
+ page.frame().?.js.localScope(&ls);
+ defer ls.deinit();
+ try ls.local.eval(
+ \\window.__fired = [];
+ \\setTimeout(() => __fired.push('timeout'), 0);
+ \\requestAnimationFrame(() => __fired.push('raf'));
+ , null);
+ }
+ try runner.waitForScript(page.frame_id, "window.__fired.length === 2", 500);
+}
+
+test "Runner: runs timers in a text document" {
+ try expectTimersRun("http://127.0.0.1:9582/src/browser/tests/runner/plain.txt");
+}
+
+test "Runner: runs timers in an image document" {
+ try expectTimersRun("http://127.0.0.1:9582/images/ok.png");
+}
+
test "Runner: networkidle notifies child frames" {
const page = try testing.pageTest("runner/iframe_idle.html", .{});
defer page.close();
diff --git a/src/browser/tests/runner/plain.txt b/src/browser/tests/runner/plain.txt
new file mode 100644
index 000000000..2bb5d0875
--- /dev/null
+++ b/src/browser/tests/runner/plain.txt
@@ -0,0 +1 @@
+plain text document