Skip to content

Handling Browser Alerts

A browser alert is a native dialog box rendered by the browser (not the web page’s HTML or CSS). It is commonly used to display messages, request confirmation, or prompt the user for simple input.

When an alert is opened, it blocks JavaScript execution and prevents user interaction with the page until the alert is dismissed. This makes alerts useful for interrupting the user with important warnings or confirmations.

While an alert is open, page interaction commands such as I.see() and I.click() will fail with a "Command blocked by alert" error, because the alert prevents any interaction with the page.

Validating an alert

To assert that an alert is open, use I.seeAlert().

js
I.click("Next")
I.seeAlert()

You can provide a text, to validate the message displayed in the alert.

js
I.click("Next")
I.seeAlert("Please keep your browser open while the report is generating. Click OK to start.") // to simply validate that an alert is open

Accepting an alert

To accept or confirm an alert, use the I.acceptAlert() command.

js
I.acceptAlert()

This presses the "OK" button (or the equivalent in the browser’s language) to dismiss the alert.

Cancel an alert

To cancel or decline an alert, use the I.cancelAlert() command.

js
I.cancelAlert()

This presses the "Cancel" button (or the equivalent in other languages).

INFO

If the alert does not provide a Cancel option, the OK button will be pressed instead to dismiss it.

Filling an alert prompt

Some alerts (such as prompt() dialogs) contain a text input field.

To fill the text box in an alert, use the I.fillAlert() command.

js
I.seeAlert("Please enter your name")
I.fillAlert("John")
I.acceptAlert()

This fills the alert’s input field with "John", then accepts the alert.