Class CaptureNetworkRecorder

java.lang.Object
com.shaft.capture.network.CaptureNetworkRecorder
All Implemented Interfaces:
AutoCloseable

public final class CaptureNetworkRecorder extends Object implements AutoCloseable
Per-session network transaction recorder. Registers a single Selenium DevTools Filter (the same NetworkInterceptor-backed mechanism BrowserNetworkInterceptor uses), classifies each transaction, applies asset/first-party/glob filters and a transaction cap, scrubs secret headers via SecretHeaderReplacer, persists bodies through NetworkBodyStore, and emits one CaptureNetworkRecorder.RecordedTransaction per recorded transaction to the session sink, which is responsible for wrapping it into a first-class CaptureEvent.NetworkEvent with a properly sequenced EventContext.

This recorder is the sole owner of the DevTools network filter for its session. Selenium's NetworkInterceptor registration is a single-slot replace, not a compose/stack: whichever interceptor registers last on a given driver silently wins, and the other's filter goes dead with no warning. com.shaft.driver.internal.DriverFactory.DriverFactoryHelper may already have started a BrowserNetworkInterceptor-owned NetworkInterceptor for passive trace/HAR observation on the same driver whenever shaft.trace.enabled and shaft.trace.includeNetwork are true -- a global property independent of API capture. Before installing its own filter, start() calls DriverFactoryHelper.releaseBrowserNetworkObservationForHandoff(WebDriver) so that prior passive observer relinquishes the DevTools filter first; this recorder's own filter composes BrowserObservabilityRecorder.startNetwork(HttpRequest) and BrowserObservabilityRecorder.finishNetwork(BrowserObservabilityRecorder.NetworkExchange, HttpResponse, String) so trace/HAR capture keeps working without a second interceptor. When the driver's BrowserNetworkInterceptor instead has mock/validate rules registered, handoff is refused (replacing that filter would silently break those rules); in that case this recorder logs a one-time warning and does not register its own filter, avoiding the double-registration entirely.

  • Constructor Details

    • CaptureNetworkRecorder

      public CaptureNetworkRecorder(org.openqa.selenium.WebDriver driver, Path bodiesDirectory, NetworkCaptureOptions options, String sessionId, Supplier<String> currentPageUrlSupplier, Consumer<CaptureNetworkRecorder.RecordedTransaction> sink, Consumer<String> warn)
      Creates a network recorder for one browser session.
      Parameters:
      driver - active WebDriver session
      bodiesDirectory - directory used to persist request/response bodies
      options - network capture filtering and body-capture options
      sessionId - owning capture session identifier, used for deterministic secret-ref derivation
      currentPageUrlSupplier - supplies the last-known top-level page URL for first-party classification and initiatorPageUrl tagging; must be a non-blocking read (see createFilter() for why it must never call back into the WebDriver session)
      sink - destination for each recorded, redacted network transaction
      warn - destination for safe one-time warnings
  • Method Details

    • start

      public boolean start()
      Starts network capture when the driver supports DevTools. Gates on HasDevTools; when unsupported, records a warning and leaves UI-only capture unaffected.

      Before installing its own DevTools network filter, this method asks DriverFactoryHelper to release any passive trace/HAR observer it already registered on this driver, so this recorder becomes the sole interceptor owner instead of silently racing with and replacing it (or being replaced by it). When the driver's BrowserNetworkInterceptor instead has mock/validate rules registered, the handoff is refused; this method then logs a one-time warning and does not register a filter, so API capture is skipped rather than double-registering and breaking those rules.

      Returns:
      true when the network filter was registered
    • transactionCount

      public int transactionCount()
      Returns the number of network transactions recorded so far.
      Returns:
      recorded transaction count
    • lastEndpoints

      public List<String> lastEndpoints()
      Returns the most recently observed endpoints, most-recent-first, bounded to a small preview.
      Returns:
      immutable, most-recent-first endpoint list
    • close

      public void close()
      Stops network capture and releases the DevTools filter exactly once.
      Specified by:
      close in interface AutoCloseable
    • matchesGlob

      public static boolean matchesGlob(String url, String glob)
      Matches a URL against a Playwright-style glob (used for both live network include/exclude filters here and HAR-export filtering in ManagedCaptureRecorder).
      Parameters:
      url - URL to test
      glob - glob pattern; a blank glob never matches
      Returns:
      true when the URL matches the glob