Class AlertActions


public class AlertActions extends FluentWebDriverAction
Provides actions for interacting with browser alert, confirm, and prompt dialogs.

AlertActions extends FluentWebDriverAction and exposes a fluent API for accepting or dismissing alerts, reading alert text, and typing into prompt dialogs. Every constructor automatically waits for an alert to be present before returning, so any subsequent action is guaranteed to operate on a live dialog.

Example usage:

driver.alert().acceptAlert();
driver.alert().typeIntoPromptAlert("my input").acceptAlert();
See Also:
  • Constructor Details

    • AlertActions

      public AlertActions()
      Creates a new AlertActions instance using the currently active SHAFT-managed driver. Waits for an alert to be present before returning.

      Example:

      AlertActions alert = new AlertActions();
      alert.acceptAlert();
      
    • AlertActions

      public AlertActions(org.openqa.selenium.WebDriver driver)
      Creates a new AlertActions instance wrapping the provided WebDriver. Waits for an alert to be present before returning.

      Example:

      AlertActions alert = new AlertActions(webDriver);
      alert.dismissAlert();
      
      Parameters:
      driver - the WebDriver instance whose current window will be used to interact with the alert
    • AlertActions

      public AlertActions(DriverFactoryHelper helper)
      Creates a new AlertActions instance using an existing DriverFactoryHelper. Waits for an alert to be present before returning.

      Example:

      AlertActions alert = new AlertActions(driverFactoryHelper);
      alert.acceptAlert();
      
      Parameters:
      helper - the DriverFactoryHelper that provides the underlying driver and session configuration
  • Method Details

    • isAlertPresent

      public boolean isAlertPresent()
      Checks whether a browser alert dialog is currently present.

      Example:

      if (driver.alert().isAlertPresent()) {
          driver.alert().acceptAlert();
      }
      
      Returns:
      true if an alert is present and reachable; false otherwise
    • acceptAlert

      public AlertActions acceptAlert()
      Accepts (clicks "OK" on) the currently displayed alert, confirm, or prompt dialog. If no alert is present, the action is recorded as a failure.

      Example:

      driver.alert().acceptAlert();
      
      Returns:
      this AlertActions instance for fluent method chaining
    • dismissAlert

      public AlertActions dismissAlert()
      Dismisses (clicks "Cancel" on) the currently displayed alert or confirm dialog. If no alert is present, the action is recorded as a failure.

      Example:

      driver.alert().dismissAlert();
      
      Returns:
      this AlertActions instance for fluent method chaining
    • getAlertText

      public String getAlertText()
      Retrieves the text message displayed in the currently active browser alert dialog.

      Example:

      String message = driver.alert().getAlertText();
      
      Returns:
      the alert message text, or null if the alert is not present or an error occurs while retrieving the text
    • typeIntoPromptAlert

      public AlertActions typeIntoPromptAlert(String text)
      Types the specified text into a JavaScript prompt dialog's input field. The prompt must already be present; call acceptAlert() afterwards to submit it.

      Example:

      driver.alert().typeIntoPromptAlert("myValue").acceptAlert();
      
      Parameters:
      text - the text to send to the prompt dialog's input field; must not be null
      Returns:
      this AlertActions instance for fluent method chaining