Lightpanda: the headless browser designed for AI and automation
webdriver: allow switching to the existing top-level window
Robert McConnell3bb524b5c6b1
diff --git a/src/server/Server.zig b/src/server/Server.zigindex 9fa302cec..d24df22d3 100644--- a/src/server/Server.zig+++ b/src/server/Server.zig@@ -2113,6 +2113,15 @@ test "server: HTTP page commands" {try testing.expectEqual(try testing.arena_allocator.print("{{\"value\":[\"{s}\"]}}", .{handle}), body);}+ // The W3C switch-to-window command accepts only the existing handle.+ const select_current = try testing.arena_allocator.print("{{\"handle\":\"{s}\"}}", .{handle});+ try testing.expectEqual("{\"value\":null}", responseBody(try sessionCommand(&c, "POST", &session_id, "/window", select_current)));+ // A wrong handle must not create or switch to a new context.+ const bad = try sessionCommand(&c, "POST", &session_id, "/window", "{\"handle\":\"not-a-window\"}");+ try testing.expect(std.mem.startsWith(u8, bad, "HTTP/1.1 404 Not Found\r\n"));+ try testing.expect(std.mem.find(u8, responseBody(bad), "\"no such window\"") != null);+ try testing.expectEqual(try testing.arena_allocator.print("{{\"value\":\"{s}\"}}", .{handle}), responseBody(try sessionCommand(&c, "GET", &session_id, "/window", "")));+const url = "http://127.0.0.1:9582/src/browser/tests/bidi/input.html";try testing.expectEqual("{\"value\":null}", responseBody(try sessionCommand(&c, "POST", &session_id, "/url", "{\"url\":\"" ++ url ++ "\"}")));try testing.expectEqual("{\"value\":\"" ++ url ++ "\"}", responseBody(try sessionCommand(&c, "GET", &session_id, "/url", "")));diff --git a/src/server/bidi/http_command.zig b/src/server/bidi/http_command.zigindex c2e0a5248..fefb0d8ed 100644--- a/src/server/bidi/http_command.zig+++ b/src/server/bidi/http_command.zig@@ -53,6 +53,7 @@ pub const Command = union(enum) {get_title,get_window_handle,get_window_handles,+ switch_to_window: SwitchToWindow,close_window,get_window_rect,set_window_rect: SetWindowRect,@@ -92,6 +93,10 @@ pub const Command = union(enum) {// A command's path parameters are its leading fields (see `parse`); the rest// come from the body.+pub const SwitchToWindow = struct {+ handle: []const u8,+};+pub const ElementId = struct {id: []const u8,};@@ -231,6 +236,7 @@ const routes = [_]Route{.init(.GET, "/title", .get_title),.init(.GET, "/window", .get_window_handle),.init(.GET, "/window/handles", .get_window_handles),+ .init(.POST, "/window", .switch_to_window),.init(.DELETE, "/window", .close_window),.init(.GET, "/window/rect", .get_window_rect),.init(.POST, "/window/rect", .set_window_rect),@@ -338,6 +344,7 @@ pub fn process(cmd: *BiDi.Command) !void {.get_title => return getTitle(cmd),.get_window_handle => return getWindowHandle(cmd),.get_window_handles => return getWindowHandles(cmd),+ .switch_to_window => |p| return switchToWindow(cmd, p),.close_window => return closeWindow(cmd),.get_window_rect, .maximize_window, .minimize_window, .fullscreen_window => return getWindowRect(cmd),.set_window_rect => |p| return setWindowRect(cmd, p),@@ -423,6 +430,17 @@ fn getWindowHandles(cmd: *BiDi.Command) !void {return cmd.sendResult(&[_][]const u8{&ctx.id});}+// POST /session/{id}/window. Only the current top-level context is+// supported; selecting it is a no-op, other handles are not valid.+fn switchToWindow(cmd: *BiDi.Command, p: SwitchToWindow) !void {+ const ctx = &(cmd.bidi.browsing_context orelse+ return cmd.sendError("no such window", "unknown window handle"));+ if (!std.mem.eql(u8, &ctx.id, p.handle)) {+ return cmd.sendError("no such window", "unknown window handle");+ }+ return cmd.sendDone();+}+// DELETE /session/{id}/windowfn closeWindow(cmd: *BiDi.Command) !void {if (cmd.bidi.browsing_context) |*ctx| {@@ -1128,6 +1146,9 @@ test "bidi.http_command: parse" {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.expectEqual("abc", (try parse(arena, .POST, "/window", "{\"handle\":\"abc\"}")).switch_to_window.handle);+ try testing.expectError(error.InvalidArgument, parse(arena, .POST, "/window", "{}"));+ try testing.expectError(error.InvalidArgument, parse(arena, .POST, "/window", "{\"handle\":17}"));try testing.expect(try parse(arena, .DELETE, "/window", "") == .close_window);try testing.expect(try parse(arena, .DELETE, "/actions", "") == .release_actions);