[feat]: add `context.setExtraHTTPHeaders()`

Merged
SE
9mo ago
+224-0

why

  • to enable users to set headers to be included in every request created by every page within the context

what changed

  • added a new function context.setExtraHTTPHeaders() which calls the Network.setExtraHTTPHeaders CDP command on all existing sessions
  • we also call Network.setExtraHTTPHeaders with the user defined headers whenever we attach to a new target (via onAttachedToTarget)
  • if there is a rejected promise when headers are being set, a StagehandSetExtraHTTPHeadersError will be thrown

test plan

  • added an e2e test to confirm that headers are being correctly set by listening to Network.requestWillBeSent and inspecting headers
  • also added unit tests to confirm that the custom error is being thrown correctly when promises reject

Summary by cubic

Adds context.setExtraHTTPHeaders() to set default HTTP headers on every request from all pages in a context. Implements STG-1411; applies to current sessions and newly attached targets after Network.enable.

  • New Features
    • New API: context.setExtraHTTPHeaders(headers) sends Network.enable then Network.setExtraHTTPHeaders to each active session; repeats on attach for new targets.
    • Public error: StagehandSetExtraHTTPHeadersError with per-session failure details.
    • Tests: e2e verifies navigation headers; unit tests cover multi-session send and error reporting; public error type is exported.