Skip to content

Lightpanda: the headless browser designed for AI and automation

webdriver: navigate back/forward

Karl Seguina59976fe54ab
diff --git a/src/browser/webapi/History.zig b/src/browser/webapi/History.zig
index ac1ec9f91..67717c23a 100644
--- a/src/browser/webapi/History.zig
+++ b/src/browser/webapi/History.zig
@@ -110,18 +110,17 @@ fn goInner(delta: i32, frame: *Frame) !void {
const index = @as(usize, @intCast(index_s));
const entry = frame._session.navigation._entries.items[index];
- if (entry._url) |url| {
- if (frame.isSameOrigin(url)) {
- const target = frame.window.asEventTarget();
- if (frame._event_manager.hasDirectListeners(target, "popstate", frame.window._on_popstate)) {
- const event = (try PopStateEvent.initTrusted(comptime .wrap("popstate"), .{ .state = entry._state.value }, frame)).asEvent();
- try frame._event_manager.dispatchDirect(target, event, frame.window._on_popstate, .{ .context = "Pop State" });
- }
- // hashchange is queued by navigateInner.
+ _ = try frame._session.navigation.navigateInner(entry._url, .{ .traverse = index }, frame);
+
+ const url = entry._url orelse return;
+ if (frame.isSameOrigin(url)) {
+ const target = frame.window.asEventTarget();
+ if (frame._event_manager.hasDirectListeners(target, "popstate", frame.window._on_popstate)) {
+ const event = (try PopStateEvent.initTrusted(comptime .wrap("popstate"), .{ .state = entry._state.value }, frame)).asEvent();
+ try frame._event_manager.dispatchDirect(target, event, frame.window._on_popstate, .{ .context = "Pop State" });
}
+ // hashchange is queued by navigateInner.
}
-
- _ = try frame._session.navigation.navigateInner(entry._url, .{ .traverse = index }, frame);
}
pub fn back(_: *History, frame: *Frame) !void {
diff --git a/src/browser/webapi/navigation/Navigation.zig b/src/browser/webapi/navigation/Navigation.zig
index 3dda744da..f9892674c 100644
--- a/src/browser/webapi/navigation/Navigation.zig
+++ b/src/browser/webapi/navigation/Navigation.zig
@@ -442,6 +442,9 @@ pub fn navigateInner(
if (is_same_document) {
frame.url = new_url;
+ try frame.window._location._url.setHref(new_url, &frame.js.execution);
+ // `:target` matches off the fragment, which might have just changed.
+ frame.styleChanged();
committed.resolve("navigation traverse", {});
// todo: Fire navigate event
diff --git a/src/server/Server.zig b/src/server/Server.zig
index 3a1003f39..559807cc8 100644
--- a/src/server/Server.zig
+++ b/src/server/Server.zig
@@ -2094,6 +2094,45 @@ test "server: HTTP page commands" {
try testing.expectEqual("{\"value\":\"" ++ url ++ "\"}", responseBody(try sessionCommand(&c, "GET", &session_id, "/url", "")));
}
+test "server: HTTP history commands" {
+ const session_id = try createHTTPSession("{\"capabilities\":{}}", false);
+ defer deleteHTTPSession(&session_id, true) catch |err| @panic(@errorName(err));
+
+ var c = try createTestClient();
+ defer c.deinit();
+
+ // nothing to go back to yet
+ try testing.expectEqual("{\"value\":null}", responseBody(try sessionCommand(&c, "POST", &session_id, "/back", "{}")));
+ try testing.expectEqual("{\"value\":\"about:blank\"}", responseBody(try sessionCommand(&c, "GET", &session_id, "/url", "")));
+
+ const first = "http://127.0.0.1:9582/src/browser/tests/webdriver/elements.html";
+ const second = "http://127.0.0.1:9582/src/browser/tests/webdriver/input.html";
+ try testing.expectEqual("{\"value\":null}", responseBody(try sessionCommand(&c, "POST", &session_id, "/url", "{\"url\":\"" ++ first ++ "\"}")));
+ try testing.expectEqual("{\"value\":null}", responseBody(try sessionCommand(&c, "POST", &session_id, "/url", "{\"url\":\"" ++ second ++ "\"}")));
+
+ // cross-document: answered once the other document is loaded
+ try testing.expectEqual("{\"value\":null}", responseBody(try sessionCommand(&c, "POST", &session_id, "/back", "{}")));
+ try testing.expectEqual("{\"value\":\"" ++ first ++ "\"}", responseBody(try sessionCommand(&c, "GET", &session_id, "/url", "")));
+ try testing.expectEqual("{\"value\":\"complete\"}", responseBody(try sessionCommand(&c, "POST", &session_id, "/execute/sync", "{\"script\":\"return document.readyState\",\"args\":[]}")));
+
+ try testing.expectEqual("{\"value\":null}", responseBody(try sessionCommand(&c, "POST", &session_id, "/forward", "{}")));
+ try testing.expectEqual("{\"value\":\"" ++ second ++ "\"}", responseBody(try sessionCommand(&c, "GET", &session_id, "/url", "")));
+
+ // nothing to go forward to
+ try testing.expectEqual("{\"value\":null}", responseBody(try sessionCommand(&c, "POST", &session_id, "/forward", "{}")));
+ try testing.expectEqual("{\"value\":\"" ++ second ++ "\"}", responseBody(try sessionCommand(&c, "GET", &session_id, "/url", "")));
+
+ // same-document: no load to wait for, the document stays
+ try testing.expectEqual("{\"value\":null}", responseBody(try sessionCommand(&c, "POST", &session_id, "/execute/sync",
+ \\{"script":"window.pops = []; window.onpopstate = function(e) { pops.push(location.hash) }; history.pushState(null, '', '#pushed')","args":[]}
+ )));
+ try testing.expectEqual("{\"value\":null}", responseBody(try sessionCommand(&c, "POST", &session_id, "/back", "{}")));
+ try testing.expectEqual("{\"value\":\"" ++ second ++ "\"}", responseBody(try sessionCommand(&c, "GET", &session_id, "/url", "")));
+ try testing.expectEqual("{\"value\":null}", responseBody(try sessionCommand(&c, "POST", &session_id, "/forward", "{}")));
+ try testing.expectEqual("{\"value\":\"" ++ second ++ "#pushed\"}", responseBody(try sessionCommand(&c, "GET", &session_id, "/url", "")));
+ try testing.expectEqual("{\"value\":\",#pushed\"}", responseBody(try sessionCommand(&c, "POST", &session_id, "/execute/sync", "{\"script\":\"return pops.join()\",\"args\":[]}")));
+}
+
test "server: HTTP element commands" {
const session_id = try createHTTPSession("{\"capabilities\":{}}", false);
defer deleteHTTPSession(&session_id, true) catch |err| @panic(@errorName(err));
diff --git a/src/server/bidi/browsing_context.zig b/src/server/bidi/browsing_context.zig
index 64d7062a1..969c1c71b 100644
--- a/src/server/bidi/browsing_context.zig
+++ b/src/server/bidi/browsing_context.zig
@@ -72,6 +72,8 @@ pub fn processMessage(cmd: *BiDi.Command, action: []const u8) !void {
getTree,
create,
navigate,
+ reload,
+ traverseHistory,
close,
locateNodes,
setViewport,
@@ -81,6 +83,8 @@ pub fn processMessage(cmd: *BiDi.Command, action: []const u8) !void {
.getTree => return getTree(cmd),
.create => return create(cmd),
.navigate => return bidiNavigate(cmd),
+ .reload => return bidiReload(cmd),
+ .traverseHistory => return bidiTraverseHistory(cmd),
.close => return close(cmd),
.locateNodes => return locateNodes(cmd),
.setViewport => return setViewport(cmd),
@@ -196,6 +200,36 @@ fn bidiNavigate(cmd: *BiDi.Command) !void {
return navigate(cmd, ctx, .{ .url = p.url, .wait = p.wait });
}
+fn bidiReload(cmd: *BiDi.Command) !void {
+ const p = try cmd.params(struct {
+ context: []const u8,
+ wait: NavigateOpts.Wait = .none,
+ });
+
+ const ctx = (try requireContext(cmd, p.context)) orelse return;
+ return reload(cmd, ctx, p.wait);
+}
+
+fn bidiTraverseHistory(cmd: *BiDi.Command) !void {
+ const p = try cmd.params(struct {
+ context: []const u8,
+ delta: i32,
+ });
+
+ const ctx = (try requireContext(cmd, p.context)) orelse return;
+ const frame = cmd.bidi.user_context.session.currentFrame() orelse {
+ return cmd.sendError("unknown error", "no frame");
+ };
+ if (canTraverse(frame, p.delta) == false) {
+ return cmd.sendError("no such history entry", "no history entry at that delta");
+ }
+ if (p.delta == 0) {
+ // the current entry, history.go(0) would reload it
+ return cmd.sendDone();
+ }
+ return traverse(cmd, ctx, frame, p.delta);
+}
+
pub const NavigateOpts = struct {
url: [:0]const u8,
wait: Wait,
@@ -271,6 +305,31 @@ fn startNavigation(cmd: *BiDi.Command, ctx: *Context, frame: *Frame, url: [:0]co
}
}
+// Whether the session history has an entry `delta` steps from the current one.
+pub fn canTraverse(frame: *const Frame, delta: i32) bool {
+ const navigation = frame._session.navigation;
+ const target = @as(i64, @intCast(navigation._index)) + delta;
+ return target >= 0 and target < navigation._entries.items.len;
+}
+
+// Traverses the session history like history.go(delta), popstate and
+// hashchange included. A same-document entry answers right away, any other
+// once the new document loads.
+pub fn traverse(cmd: *BiDi.Command, ctx: *Context, frame: *Frame, delta: i32) !void {
+ {
+ // History expects to be called from JS, with an active local.
+ const prev_local = frame.js.local;
+ defer frame.js.local = prev_local;
+ var ls: js.Local.Scope = undefined;
+ frame.js.localScope(&ls);
+ defer ls.deinit();
+ frame.js.local = &ls.local;
+
+ try frame._session.history.go(delta, frame);
+ }
+ return answerAfterNavigation(cmd, ctx, frame);
+}
+
// For commands that start a navigation, e.g. clicking a link.
pub fn answerAfterNavigation(cmd: *BiDi.Command, ctx: *Context, frame: *const Frame) !void {
if (frame._queued_navigation == null) {
@@ -956,3 +1015,44 @@ test "bidi.browsing_context: setViewport" {
});
try ctx.expectSentError("invalid argument", null, .{ .id = 7 });
}
+
+test "bidi.browsing_context: traverseHistory and reload" {
+ var ctx = try testing.context();
+ defer ctx.deinit();
+ const context_id = try ctx.createContext(.{ .url = "bidi/values.html" });
+
+ try ctx.processMessage(.{
+ .id = 1,
+ .method = "browsingContext.navigate",
+ .params = .{ .context = context_id, .url = testing.test_server ++ "bidi/locate.html", .wait = "complete" },
+ });
+ try ctx.wait();
+ try ctx.expectSentResult(.{ .url = testing.test_server ++ "bidi/locate.html" }, .{ .id = 1 });
+
+ // about:blank, values.html, locate.html
+ try ctx.processMessage(.{ .id = 2, .method = "browsingContext.traverseHistory", .params = .{ .context = context_id, .delta = 1 } });
+ try ctx.expectSentError("no such history entry", null, .{ .id = 2 });
+ try ctx.processMessage(.{ .id = 3, .method = "browsingContext.traverseHistory", .params = .{ .context = context_id, .delta = -3 } });
+ try ctx.expectSentError("no such history entry", null, .{ .id = 3 });
+ try ctx.processMessage(.{ .id = 4, .method = "browsingContext.traverseHistory", .params = .{ .context = "nope", .delta = -1 } });
+ try ctx.expectSentError("no such frame", null, .{ .id = 4 });
+
+ try ctx.processMessage(.{ .id = 5, .method = "browsingContext.traverseHistory", .params = .{ .context = context_id, .delta = -1 } });
+ try ctx.wait();
+ try ctx.expectSentResult(.{ .url = testing.test_server ++ "bidi/values.html" }, .{ .id = 5 });
+ try testing.expectEqual(testing.test_server ++ "bidi/values.html", (try ctx.frame()).url);
+
+ // a same-document entry answers without a load
+ try ctx.processMessage(.{
+ .id = 6,
+ .method = "script.evaluate",
+ .params = .{ .expression = "history.pushState(null, '', '#pushed')", .awaitPromise = false, .target = .{ .context = context_id } },
+ });
+ try ctx.processMessage(.{ .id = 7, .method = "browsingContext.traverseHistory", .params = .{ .context = context_id, .delta = -1 } });
+ try ctx.expectSentResult(null, .{ .id = 7 });
+ try testing.expectEqual(testing.test_server ++ "bidi/values.html", (try ctx.frame()).url);
+
+ try ctx.processMessage(.{ .id = 8, .method = "browsingContext.reload", .params = .{ .context = context_id, .wait = "complete" } });
+ try ctx.wait();
+ try ctx.expectSentResult(.{ .url = testing.test_server ++ "bidi/values.html" }, .{ .id = 8 });
+}
diff --git a/src/server/bidi/http_command.zig b/src/server/bidi/http_command.zig
index 201bf24cf..e1a846530 100644
--- a/src/server/bidi/http_command.zig
+++ b/src/server/bidi/http_command.zig
@@ -47,6 +47,8 @@ pub const element_key = "element-6066-11e4-a52e-4f735466cecf";
pub const Command = union(enum) {
navigate_to: NavigateTo,
get_current_url,
+ back,
+ forward,
refresh,
get_title,
get_window_handle,
@@ -222,6 +224,8 @@ const Route = struct {
const routes = [_]Route{
.init(.POST, "/url", .navigate_to),
.init(.GET, "/url", .get_current_url),
+ .init(.POST, "/back", .back),
+ .init(.POST, "/forward", .forward),
.init(.POST, "/refresh", .refresh),
.init(.GET, "/title", .get_title),
.init(.GET, "/window", .get_window_handle),
@@ -334,6 +338,8 @@ pub fn process(cmd: *BiDi.Command) !void {
switch (cmd.input.http) {
.navigate_to => |p| return navigateTo(cmd, p),
.get_current_url => return getCurrentUrl(cmd),
+ .back => return traverse(cmd, -1),
+ .forward => return traverse(cmd, 1),
.refresh => return refresh(cmd),
.get_title => return getTitle(cmd),
.get_window_handle => return getWindowHandle(cmd),
@@ -387,6 +393,17 @@ fn getCurrentUrl(cmd: *BiDi.Command) !void {
return cmd.sendResult(frame.url);
}
+// POST /session/{id}/back and /forward. With no entry to go to, there's
+// nothing to do.
+fn traverse(cmd: *BiDi.Command, delta: i32) !void {
+ const ctx = (try currentContext(cmd)) orelse return;
+ const frame = (try currentFrame(cmd)) orelse return;
+ if (browsing_context.canTraverse(frame, delta) == false) {
+ return cmd.sendDone();
+ }
+ return browsing_context.traverse(cmd, ctx, frame, delta);
+}
+
// POST /session/{id}/refresh.
fn refresh(cmd: *BiDi.Command) !void {
const ctx = (try currentContext(cmd)) orelse return;
@@ -1104,6 +1121,8 @@ test "bidi.http_command: parse" {
try testing.expect(try parse(arena, .GET, "/element/7/text", "") == .get_element_text);
try testing.expect(try parse(arena, .GET, "/url", "") == .get_current_url);
+ try testing.expect(try parse(arena, .POST, "/back", "{}") == .back);
+ try testing.expect(try parse(arena, .POST, "/forward", "{}") == .forward);
try testing.expect(try parse(arena, .GET, "/window/handles", "") == .get_window_handles);
try testing.expect(try parse(arena, .DELETE, "/actions", "") == .release_actions);