Class AbstractBrowser

All Implemented Interfaces:
ContextProvider, PropertyChangeSource, Rebindable
Direct Known Subclasses:
Chrome, Firefox

public abstract class AbstractBrowser extends AnahataToolkit implements Rebindable
The abstract base toolkit for all web automation implementations.

This class centralizes all pure Selenium WebDriver interactions (DOM querying, execution, navigation, and screenshots). Concrete implementations (like Chrome or Firefox) must handle the browser-specific launch protocols, DevTools connections, and process management.

Author:
anahata
  • Field Details

    • drones

      protected final Map<String, BrowserDrone> drones
      The centralized registry mapping drone IDs to their stateful objects.
  • Constructor Details

    • AbstractBrowser

      protected AbstractBrowser()
      Protected constructor for subclass instantiation.
  • Method Details

    • getDriver

      public abstract org.openqa.selenium.WebDriver getDriver(String droneId)
      Resolves the active WebDriver for the requested drone.

      Implementation details: This is an internal lifecycle method. If the connection is lost, concrete implementations should attempt to re-establish it using the cached port and profile data.

      Parameters:
      droneId - The unique ID of the drone.
      Returns:
      The active WebDriver, or null if it cannot be resolved.
    • getStatus

      public String getStatus(String droneId)
      Gets the current status of the browser driver for a specific drone.
      Parameters:
      droneId - The ID of the drone.
      Returns:
      A status report.
    • killAll

      public abstract String killAll()
      Closes all active browser processes.
      Returns:
      A status message describing the cleanup operations.
    • listTabs

      public abstract List<String> listTabs(String droneId)
      Lists all open tabs/windows in the specified drone.

      Implementations should ideally use native DevTools protocols to avoid forcing window focus and causing screen flickering.

      Parameters:
      droneId - The ID of the drone.
      Returns:
      A list of formatted tab titles and URLs.
    • getScreenshot

      public String getScreenshot(String droneId, String name) throws Exception
      Takes a screenshot of the current page and attaches it to the session.
      Parameters:
      droneId - The ID of the drone.
      name - The name of the screenshot file.
      Returns:
      A status message.
      Throws:
      Exception - if screenshot fails.
    • switchToTab

      public String switchToTab(String droneId, int index)
      Switches the active tab/window by its index.
      Parameters:
      droneId - The ID of the drone.
      index - The index of the tab.
      Returns:
      A status message.
    • openNewTab

      public String openNewTab(String droneId, String url)
    • goBack

      public String goBack(String droneId)
      Navigates back in the browser history.
      Parameters:
      droneId - The ID of the drone.
      Returns:
      A status message.
    • goForward

      public String goForward(String droneId)
      Navigates forward in the browser history.
      Parameters:
      droneId - The ID of the drone.
      Returns:
      A status message.
    • refresh

      public String refresh(String droneId)
      Refreshes the current page.
      Parameters:
      droneId - The ID of the drone.
      Returns:
      A status message.
    • getPageSource

      public String getPageSource(String droneId)
      Gets the full HTML source of the current page.
      Parameters:
      droneId - The ID of the drone.
      Returns:
      The page source.
    • getPageText

      public String getPageText(String droneId)
      Gets the visible text content of the current page.
      Parameters:
      droneId - The ID of the drone.
      Returns:
      The page text.
    • inspectForm

      public String inspectForm(String droneId)
      Inspects the current page for input fields and buttons.
      Parameters:
      droneId - The ID of the drone.
      Returns:
      A summary of found elements.
    • clickElement

      public String clickElement(String droneId, String identifier)
      Performs a stateful click by first attempting to locate the element using a multi-strategy search (ID, Name, Link Text, XPath text content).
      Parameters:
      droneId - The ID of the drone.
      identifier - The ID, Name, or visible text of the element.
      Returns:
      A status message indicating success or failure.
    • scrollToElement

      public String scrollToElement(String droneId, String identifier)
      Scrolls the specified element into view.
      Parameters:
      droneId - The ID of the drone.
      identifier - The element identifier.
      Returns:
      A status message.
    • waitForElement

      public String waitForElement(String droneId, String cssSelector, int timeoutSeconds)
      Safely executes a CSS-based visibility check using WebDriverWait.
      Parameters:
      droneId - The ID of the drone.
      cssSelector - The CSS selector of the element.
      timeoutSeconds - The maximum time to wait in seconds.
      Returns:
      A status message confirming visibility.
    • executeScript

      public Object executeScript(String droneId, String script)
      Executes arbitrary JavaScript in the current browser session.
      Parameters:
      droneId - The ID of the drone.
      script - The script.
      Returns:
      The result.
    • fillForm

      public String fillForm(String droneId, Map<String,String> data)
      Fills a web form with the provided data.
      Parameters:
      droneId - The ID of the drone.
      data - The form data.
      Returns:
      A status message.
    • close

      public String close(String droneId)
      Closes the browser session and removes it from the registry.
      Parameters:
      droneId - The ID of the drone.
      Returns:
      A confirmation message.
    • locateElementGracefully

      protected org.openqa.selenium.WebElement locateElementGracefully(org.openqa.selenium.WebDriver driver, String identifier)
      Helper method to safely locate elements using a cascading fallback strategy.

      Execution order: ID -> Name -> Link Text -> XPath (text contains). This avoids throwing raw NoSuchElementExceptions and allows the caller to handle missing elements gracefully.

      Parameters:
      driver - The active WebDriver instance.
      identifier - The ID, Name, or visible text to search for.
      Returns:
      The located WebElement, or null if not found.