Class PlaywrightService

java.lang.Object
com.shaft.mcp.PlaywrightService

@Service public class PlaywrightService extends Object
MCP-safe Playwright tools backed by SHAFT.GUI.Playwright.
  • Constructor Details

    • PlaywrightService

      public PlaywrightService()
      Creates the default Playwright MCP service.
  • Method Details

    • initialize

      public void initialize(String browser, boolean headless)
      Initializes a SHAFT Playwright browser session.
      Parameters:
      browser - chromium, chrome, firefox, webkit, or safari; blank uses SHAFT defaults
      headless - whether to launch headlessly
    • quit

      public void quit()
      Quits the active SHAFT Playwright session.
    • recordStart

      public McpMobileRecordingStatus recordStart(String outputPath, String mode, boolean includeSensitiveValues)
      Starts recording Playwright actions performed through MCP Playwright tools.
      Parameters:
      outputPath - workspace-contained recording JSON output path
      mode - recording label
      includeSensitiveValues - whether typed values may be persisted
      Returns:
      recorder status
    • recordStatus

      public McpMobileRecordingStatus recordStatus()
      Returns Playwright recording status.
      Returns:
      recorder status
    • recordStop

      public McpMobileRecordingStatus recordStop(boolean discard)
      Stops Playwright recording.
      Parameters:
      discard - whether to delete the recording output
      Returns:
      final recorder status
    • stepDelete

      public McpMobileRecordingStatus stepDelete(String stepId)
      Deletes a recorded Playwright step by its stable stepId, as surfaced in recorder status.
      Parameters:
      stepId - stable step id (e.g. "m2") from McpMobileRecordingStatus.steps()
      Returns:
      updated recorder status
    • stepReorder

      public McpMobileRecordingStatus stepReorder(String stepId, String direction)
      Moves a recorded Playwright step up or down by its stable stepId, as surfaced in recorder status.
      Parameters:
      stepId - stable step id (e.g. "m2") from McpMobileRecordingStatus.steps()
      direction - "up" or "down"
      Returns:
      updated recorder status
    • recordingCodeBlocks

      public McpMobileReplayResult recordingCodeBlocks(String recordingPath, String driverVariableName)
      Generates copy-paste replay code from a Playwright recording.
      Parameters:
      recordingPath - workspace-contained recording path
      driverVariableName - driver variable name to use in generated snippets
      Returns:
      replay code blocks
    • recordAtTargetCodeBlocks

      public McpMobileReplayResult recordAtTargetCodeBlocks(String recordingPath, String driverVariableName, String targetSourcePath, String insertAfter)
      Generates focused Playwright recording snippets for insertion into an existing Page Object.
      Parameters:
      recordingPath - workspace-contained recording path
      driverVariableName - driver variable name to use in generated snippets
      targetSourcePath - workspace-contained Java Page Object source path
      insertAfter - method name or textual anchor to insert after
      Returns:
      replay code blocks plus target insertion snippets
    • replayRecording

      public McpMobileReplayResult replayRecording(String recordingPath, String driverVariableName)
      Replays a Playwright recording against the active Playwright session.
      Parameters:
      recordingPath - workspace-contained recording path
      driverVariableName - driver variable name to use in generated snippets
      Returns:
      replay result
    • refresh

      public McpMobileActionResult refresh()
      Refreshes the Playwright page.
      Returns:
      recorded action metadata
    • setWindowSize

      public McpMobileActionResult setWindowSize(int width, int height)
      Sets the Playwright viewport size.
      Parameters:
      width - viewport width
      height - viewport height
      Returns:
      recorded action metadata
    • newWindow

      public McpMobileActionResult newWindow(String targetUrl, String windowType)
      Opens a new Playwright tab or window.
      Parameters:
      targetUrl - optional URL; blank opens about:blank
      windowType - TAB or WINDOW
      Returns:
      recorded action metadata
    • getCurrentUrl

      public String getCurrentUrl()
      Gets current Playwright page URL.
      Returns:
      current URL
    • getTitle

      public String getTitle()
      Gets current Playwright page title.
      Returns:
      page title
    • getPageDom

      public McpPageDomSnapshot getPageDom(int maxCharacters)
      Captures the current Playwright page DOM.
      Parameters:
      maxCharacters - maximum DOM characters to return
      Returns:
      page DOM snapshot
    • takeScreenshot

      public McpScreenshotResult takeScreenshot(String outputPath, boolean includeBase64)
      Takes a PNG screenshot of the current Playwright page.
      Parameters:
      outputPath - optional workspace-contained output file path
      includeBase64 - whether to include base64 bytes in the response
      Returns:
      screenshot metadata
    • saveStorageState

      public String saveStorageState(String filePath)
      Saves the active Playwright session's cookies, localStorage, and sessionStorage to a JSON file.

      Produces the same JSON schema as browser_storage_state_save, so files are interchangeable between the WebDriver and Playwright backends.

      Parameters:
      filePath - workspace-relative or workspace-contained output file path
      Returns:
      the absolute path the storage state was written to
    • loadStorageState

      public String loadStorageState(String filePath)
      Loads cookies, localStorage, and sessionStorage from a JSON file into the active Playwright session.

      Navigate to the target origin before loading storage state so browser cookie domain rules can apply.

      Parameters:
      filePath - workspace-contained source JSON file path
      Returns:
      a short confirmation including the resolved path and restored cookie count
    • click

      public McpMobileActionResult click(locatorStrategy locatorStrategy, String locatorValue)
      Clicks a Playwright element.
    • clickUsingJavaScript

      public McpMobileActionResult clickUsingJavaScript(locatorStrategy locatorStrategy, String locatorValue)
      Clicks a Playwright element using JavaScript.
    • doubleClick

      public McpMobileActionResult doubleClick(locatorStrategy locatorStrategy, String locatorValue)
      Double-clicks a Playwright element.
    • hover

      public McpMobileActionResult hover(locatorStrategy locatorStrategy, String locatorValue)
      Hovers over a Playwright element.
    • type

      public McpMobileActionResult type(locatorStrategy locatorStrategy, String locatorValue, String textValue)
      Types into a Playwright element.
    • appendText

      public McpMobileActionResult appendText(locatorStrategy locatorStrategy, String locatorValue, String textValue)
      Appends text to a Playwright element.
    • setValueUsingJavaScript

      public McpMobileActionResult setValueUsingJavaScript(locatorStrategy locatorStrategy, String locatorValue, String textValue)
      Sets an element value using JavaScript.
    • clear

      public McpMobileActionResult clear(locatorStrategy locatorStrategy, String locatorValue)
      Clears a Playwright element.
    • uploadFile

      public McpMobileActionResult uploadFile(locatorStrategy locatorStrategy, String locatorValue, String filePath)
      Uploads a file through a Playwright file input.
    • dragAndDrop

      public McpMobileActionResult dragAndDrop(locatorStrategy sourceLocatorStrategy, String sourceLocatorValue, locatorStrategy targetLocatorStrategy, String targetLocatorValue)
      Drags one Playwright element to another.
    • isDisplayed

      public boolean isDisplayed(locatorStrategy locatorStrategy, String locatorValue)
      Checks Playwright element visibility.
    • isEnabled

      public boolean isEnabled(locatorStrategy locatorStrategy, String locatorValue)
      Checks Playwright element enabled state.