Class DriverFactoryHelper

java.lang.Object
com.shaft.driver.internal.DriverFactory.DriverFactoryHelper

public class DriverFactoryHelper extends Object
Internal helper responsible for initializing, managing, and closing WebDriver sessions.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates a helper instance without an attached WebDriver.
    DriverFactoryHelper(org.openqa.selenium.WebDriver driver)
    Creates a helper instance attached to an existing WebDriver session.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Clears browser network interception rules for the active WebDriver session.
    void
    Closes the currently attached driver session and clears the local driver reference.
    void
    closeDriver(org.openqa.selenium.WebDriver driver)
    Closes the given WebDriver session and performs all associated teardown tasks: attaches video recording (if scope is DriverSession), collects WebDriver logs, handles dockerized driver cleanup, and removes the WebDriverManager reference.
    static org.openqa.selenium.WebDriver
    Returns the WebDriver currently owned by this thread, when SHAFT created or attached one.
    static boolean
    hasBlockingBrowserNetworkInterceptionRules(org.openqa.selenium.WebDriver driver)
    Reports whether driver already has a BrowserNetworkInterceptor with active mock/validate rules registered, meaning a caller must not install its own competing DevTools network filter on this driver: doing so would silently replace Selenium's single-slot network filter and break those rules with no warning.
    void
    Initializes a driver using the currently configured target browser/mobile settings.
    void
    Initializes a driver using the supplied driver type and default options.
    void
    initializeDriver(@NonNull DriverFactory.DriverType driverType, org.openqa.selenium.MutableCapabilities customDriverOptions)
    Initializes a driver using an explicit driver type and optional custom capabilities.
    void
    initializeDriver(@NonNull org.openqa.selenium.WebDriver driver)
    Attaches the helper to an already initialized native WebDriver session.
    void
    initializeDriver(org.openqa.selenium.MutableCapabilities customDriverOptions)
    Initializes a driver using configured browser name and custom capabilities.
    static void
    Initializes and normalizes system-level execution properties before driver creation.
    static boolean
    Checks to see if the execution is a mobile-native execution
    static boolean
    Checks to see if the execution is a mobile-web execution
    static boolean
    Checks to see if the execution is a web-based execution
    static boolean
    Checks to see if the execution is a Windows desktop Appium execution.
    static void
    Performs configured Selenium Grid preflight during framework suite startup.
    void
    Registers a browser network interception rule for the active WebDriver session.
    static boolean
    releaseBrowserNetworkObservationForHandoff(org.openqa.selenium.WebDriver driver)
    Hands off sole ownership of driver's DevTools network filter away from any BrowserNetworkInterceptor this helper started for trace/HAR observation, so a caller that is about to install its own NetworkInterceptor on the same driver (for example a dedicated API-capture recorder) does not silently race with and replace it.
    void
    setDriver(org.openqa.selenium.WebDriver driver)
    Stores the helper driver and exposes it to thread-local lifecycle reporters.
    boolean
    Starts passive browser network observation for trace or contract capture.

    Methods inherited from class Object

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • DriverFactoryHelper

      public DriverFactoryHelper()
      Creates a helper instance without an attached WebDriver.
    • DriverFactoryHelper

      public DriverFactoryHelper(org.openqa.selenium.WebDriver driver)
      Creates a helper instance attached to an existing WebDriver session.
      Parameters:
      driver - the active WebDriver session
  • Method Details

    • getActiveDriver

      public static org.openqa.selenium.WebDriver getActiveDriver()
      Returns the WebDriver currently owned by this thread, when SHAFT created or attached one.
      Returns:
      the active WebDriver for the current thread, or null
    • setDriver

      public void setDriver(org.openqa.selenium.WebDriver driver)
      Stores the helper driver and exposes it to thread-local lifecycle reporters.
      Parameters:
      driver - the current WebDriver, or null to clear the thread state
    • registerBrowserNetworkInterceptionRule

      public void registerBrowserNetworkInterceptionRule(BrowserNetworkInterceptionRule rule)
      Registers a browser network interception rule for the active WebDriver session.
      Parameters:
      rule - the rule to activate
    • clearBrowserNetworkInterceptors

      public void clearBrowserNetworkInterceptors()
      Clears browser network interception rules for the active WebDriver session.
    • startBrowserNetworkObservation

      public boolean startBrowserNetworkObservation()
      Starts passive browser network observation for trace or contract capture.
      Returns:
      true when observation started
    • releaseBrowserNetworkObservationForHandoff

      public static boolean releaseBrowserNetworkObservationForHandoff(org.openqa.selenium.WebDriver driver)
      Hands off sole ownership of driver's DevTools network filter away from any BrowserNetworkInterceptor this helper started for trace/HAR observation, so a caller that is about to install its own NetworkInterceptor on the same driver (for example a dedicated API-capture recorder) does not silently race with and replace it.

      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. Callers that are about to become the sole DevTools-network-filter owner for driver should call this first.

      Does nothing (and returns false) when this helper's interceptor has active mock/validate rules registered: replacing that filter would silently drop the caller's expected mocking/validation behavior, so ownership is not released in that case and the caller must not double-register either.

      Parameters:
      driver - the WebDriver session the caller is about to attach its own network filter to
      Returns:
      true when passive trace/HAR observation was active and has been released so the caller is safe to become the sole interceptor owner; false when there was nothing to hand off, or when active interception rules block a safe handoff
    • hasBlockingBrowserNetworkInterceptionRules

      public static boolean hasBlockingBrowserNetworkInterceptionRules(org.openqa.selenium.WebDriver driver)
      Reports whether driver already has a BrowserNetworkInterceptor with active mock/validate rules registered, meaning a caller must not install its own competing DevTools network filter on this driver: doing so would silently replace Selenium's single-slot network filter and break those rules with no warning.
      Parameters:
      driver - the WebDriver session a caller is considering attaching its own network filter to
      Returns:
      true when this helper's interceptor for driver has active rules that block another owner from safely taking over the DevTools network filter
    • isMobileNativeExecution

      public static boolean isMobileNativeExecution()
      Checks to see if the execution is a mobile-native execution
      Returns:
      true if it's a mobile mobile-native execution
    • isMobileWebExecution

      public static boolean isMobileWebExecution()
      Checks to see if the execution is a mobile-web execution
      Returns:
      true if it's a mobile mobile-web execution
    • isNotMobileExecution

      public static boolean isNotMobileExecution()
      Checks to see if the execution is a web-based execution
      Returns:
      true if it's a web-based execution
    • isWindowsAppiumExecution

      public static boolean isWindowsAppiumExecution()
      Checks to see if the execution is a Windows desktop Appium execution.
      Returns:
      true if it's a Windows desktop Appium execution
    • initializeSystemProperties

      public static void initializeSystemProperties()
      Initializes and normalizes system-level execution properties before driver creation.
    • preflightRemoteGridIfConfigured

      public static void preflightRemoteGridIfConfigured()
      Performs configured Selenium Grid preflight during framework suite startup.
    • closeDriver

      public void closeDriver()
      Closes the currently attached driver session and clears the local driver reference.
    • closeDriver

      @Step("Close Driver Session") public void closeDriver(org.openqa.selenium.WebDriver driver)
      Closes the given WebDriver session and performs all associated teardown tasks: attaches video recording (if scope is DriverSession), collects WebDriver logs, handles dockerized driver cleanup, and removes the WebDriverManager reference.

      The method handles the following edge cases gracefully:

      • Driver already closed — logs at DEBUG level and continues
      • null driver — logs an informational message and returns
      • Exceptions during close() or quit() — caught and logged so the remaining teardown (log attachment, state cleanup) still executes
      Parameters:
      driver - the WebDriver instance to close; if null, the method is a no-op
    • initializeDriver

      public void initializeDriver()
      Initializes a driver using the currently configured target browser/mobile settings.
    • initializeDriver

      public void initializeDriver(@NonNull @NonNull DriverFactory.DriverType driverType)
      Initializes a driver using the supplied driver type and default options.
      Parameters:
      driverType - the desired driver type
    • initializeDriver

      public void initializeDriver(org.openqa.selenium.MutableCapabilities customDriverOptions)
      Initializes a driver using configured browser name and custom capabilities.
      Parameters:
      customDriverOptions - custom capabilities to merge into default options
    • initializeDriver

      public void initializeDriver(@NonNull @NonNull DriverFactory.DriverType driverType, org.openqa.selenium.MutableCapabilities customDriverOptions)
      Initializes a driver using an explicit driver type and optional custom capabilities.
      Parameters:
      driverType - the desired driver type
      customDriverOptions - custom capabilities to merge into default options
    • initializeDriver

      public void initializeDriver(@NonNull @NonNull org.openqa.selenium.WebDriver driver)
      Attaches the helper to an already initialized native WebDriver session.
      Parameters:
      driver - existing WebDriver instance