Skip to content

Commit 78cb766

Browse files
committed
Log for unimplemented parameter
Wrap script_on_new_document execution in try/catch for better error reporting. Improve test for script_on_new_document
1 parent 886aa3a commit 78cb766

2 files changed

Lines changed: 56 additions & 22 deletions

File tree

src/cdp/domains/page.zig

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,10 @@ fn addScriptToEvaluateOnNewDocument(cmd: anytype) !void {
159159

160160
const bc = cmd.browser_context orelse return error.BrowserContextNotLoaded;
161161

162+
if (params.runImmediately) {
163+
log.warn(.not_implemented, "addScriptOnNewDocument", .{ .param = "runImmediately" });
164+
}
165+
162166
const script_id = bc.next_script_id;
163167
bc.next_script_id += 1;
164168

@@ -191,7 +195,6 @@ fn removeScriptToEvaluateOnNewDocument(cmd: anytype) !void {
191195
break;
192196
}
193197
}
194-
195198
return cmd.sendResult(null, .{});
196199
}
197200

@@ -524,8 +527,13 @@ pub fn pageNavigated(arena: Allocator, bc: anytype, event: *const Notification.P
524527
defer ls.deinit();
525528

526529
for (bc.scripts_on_new_document.items) |script| {
530+
var try_catch: lp.js.TryCatch = undefined;
531+
try_catch.init(&ls.local);
532+
defer try_catch.deinit();
533+
527534
ls.local.eval(script.source, null) catch |err| {
528-
log.warn(.cdp, "script on new doc failed", .{ .err = err });
535+
const caught = try_catch.caughtOrError(arena, err);
536+
log.warn(.cdp, "script on new doc", .{ .caught = caught });
529537
};
530538
}
531539
}
@@ -893,7 +901,7 @@ test "cdp.page: addScriptToEvaluateOnNewDocument" {
893901
var ctx = try testing.context();
894902
defer ctx.deinit();
895903

896-
_ = try ctx.loadBrowserContext(.{ .id = "BID-9", .url = "hi.html", .target_id = "FID-000000000X".* });
904+
var bc = try ctx.loadBrowserContext(.{ .id = "BID-9", .url = "hi.html", .target_id = "FID-000000000X".* });
897905

898906
{
899907
// Register a script — should return unique identifier "1"
@@ -922,4 +930,22 @@ test "cdp.page: addScriptToEvaluateOnNewDocument" {
922930
try ctx.processMessage(.{ .id = 23, .method = "Page.removeScriptToEvaluateOnNewDocument", .params = .{ .identifier = "999" } });
923931
try ctx.expectSentResult(null, .{ .id = 23 });
924932
}
933+
934+
{
935+
try ctx.processMessage(.{ .id = 34, .method = "Page.reload" });
936+
// wait for this event, which is sent after we've run the registered scripts
937+
try ctx.expectSentEvent("Page.frameNavigated", .{
938+
.frame = .{.loaderId = "LID-0000000002"},
939+
}, .{});
940+
941+
const page = bc.session.currentPage() orelse unreachable;
942+
943+
var ls: js.Local.Scope = undefined;
944+
page.js.localScope(&ls);
945+
defer ls.deinit();
946+
947+
const test_val = try ls.local.exec("window.__test2", null);
948+
try testing.expectEqual(2, try test_val.toI32());
949+
}
950+
925951
}

src/cdp/testing.zig

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -172,13 +172,26 @@ const TestContext = struct {
172172
index: ?usize = null,
173173
};
174174
pub fn expectSent(self: *TestContext, expected: anytype, opts: SentOpts) !void {
175-
const serialized = try json.Stringify.valueAlloc(base.arena_allocator, expected, .{
176-
.whitespace = .indent_2,
177-
.emit_null_optional_fields = false,
178-
});
175+
const expected_json = blk: {
176+
// Zig makes this hard. When sendJSON is called, we're sending an anytype.
177+
// We can't record that in an ArrayList(???), so we serialize it to JSON.
178+
// Now, ideally, we could just take our expected structure, serialize it to
179+
// json and check if the two are equal.
180+
// Except serializing to JSON isn't deterministic.
181+
// So we serialize the JSON then we deserialize to json.Value. And then we can
182+
// compare our anytype expectation with the json.Value that we captured
183+
184+
const serialized = try json.Stringify.valueAlloc(base.arena_allocator, expected, .{
185+
.whitespace = .indent_2,
186+
.emit_null_optional_fields = false,
187+
});
188+
189+
break :blk try std.json.parseFromSliceLeaky(json.Value, base.arena_allocator, serialized, .{});
190+
};
191+
179192
for (0..5) |_| {
180193
for (self.received.items, 0..) |received, i| {
181-
if (try compareExpectedToSent(serialized, received) == false) {
194+
if (try base.isEqualJson(expected_json, received) == false) {
182195
continue;
183196
}
184197

@@ -191,6 +204,15 @@ const TestContext = struct {
191204
}
192205
return;
193206
}
207+
208+
if (self.cdp_) |*cdp__| {
209+
if (cdp__.browser_context) |*bc| {
210+
if (bc.session.page != null) {
211+
var runner = try bc.session.runner(.{});
212+
_ = try runner.tick(.{.ms = 1000});
213+
}
214+
}
215+
}
194216
std.Thread.sleep(5 * std.time.ns_per_ms);
195217
try self.read();
196218
}
@@ -303,17 +325,3 @@ pub fn context() !TestContext {
303325
.socket = pair[0],
304326
};
305327
}
306-
307-
// Zig makes this hard. When sendJSON is called, we're sending an anytype.
308-
// We can't record that in an ArrayList(???), so we serialize it to JSON.
309-
// Now, ideally, we could just take our expected structure, serialize it to
310-
// json and check if the two are equal.
311-
// Except serializing to JSON isn't deterministic.
312-
// So we serialize the JSON then we deserialize to json.Value. And then we can
313-
// compare our anytype expectation with the json.Value that we captured
314-
315-
fn compareExpectedToSent(expected: []const u8, actual: json.Value) !bool {
316-
const expected_value = try std.json.parseFromSlice(json.Value, std.testing.allocator, expected, .{});
317-
defer expected_value.deinit();
318-
return base.isEqualJson(expected_value.value, actual);
319-
}

0 commit comments

Comments
 (0)