Skip to content

capture-page.csx fails with an opaque JSON error when a page script returns anything but an object #429

Description

@DutchJaFO

Description

scripts/testing/capture-page.csx evaluates a page script, prints its return value, and then inspects that value for width/height so a document can crop the capture to an element by returning its getBoundingClientRect().

That inspection indexes the value as a JSON object. When the script returns a number, string, boolean or array, it throws InvalidOperationException: The node must be of type 'JsonObject' — after the value has already been printed correctly. The caller gets the right answer on stdout, a non-zero exit, and a stack trace naming System.Text.Json rather than the snippet.

Reproduction steps

'return document.querySelectorAll("div").length' |
  dotnet script scripts/testing/capture-page.csx -- --url "http://localhost:PORT/" --out out.png --script-stdin

Measured 2026-09-29 during #348's verification pass.

Actual behaviour

The count prints, then:

System.InvalidOperationException: The node must be of type 'JsonObject'.
   at System.Text.Json.Nodes.JsonNode.AsObject()
   at Submission#0...capture-page.csx:line 224

Expected behaviour

A non-object return is a normal thing for a snippet to do. The crop is optional, so its absence should simply mean "no crop": skip the inspection unless the value is an object carrying width and height, keep the printed value, and exit 0. If a shape genuinely cannot be used, say so in terms of the snippet rather than of JSON node types.

Why it matters beyond one mistake

Several documents state their reads as browser-console js blocks returning arrays — notifications-and-changelog/13 steps 3, 6 and 7, notifications-and-changelog/14 step 2. Any of those piped through the harness fails this way, and the failure looks like the page rather than the tooling.

Definition of done

  • Failing test(s) listed above are red before the fix is written
  • Fix implemented
  • All listed tests pass (green)
  • No regression in related tests
  • Findings summarised in a closing comment

Activity

  1. added this to the v1.9.0 milestone on Sep 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions