A Javascript pop-up creates a dialog that captures focus. This prevents any actions from being taken until the dialog is acknowledged. On Chrome-based monitoring points, Selenium can be used to acknowledge Javascript pop-ups.

There are three different types of Javascript pop-ups: Alerts, Confirmations, and Prompts.

Alerts - are dialogs containing a single button (for example, “OK”) that must be pressed in order to continue.
Confirmations - are dialogs containing more than one button (for example, “OK” and “Cancel”). One of these must be pressed in order to continue.
Prompts - are dialogs that prompt the user for input and contain more than one button (for example, “OK” and “Cancel”). One of these must be pressed in order to continue.

Each pop-up type is handled with different Selenium commands.

Handling alerts

There are two ways of acknowledging an alert with Selenium commands: using assertAlert or using assertAlertPresent.

  • assertAlert(pattern) - This requires a string to match the alert text. If it matches, script execution continues. Otherwise, a milestone error event will occur.
  • assertAlertPresent() - This will dismiss the alert and continue. It does not need a string to match.

Examples

In this example, a sample web page has a “Try it” button:

alert-try-it.png

When pressed, the following Javascript alert is displayed:

alert-popup.png

Using assertAlert

To use assertAlert to acknowledge the alert, the Selenium script looks as follows (where the click action opens the alert):

alert-assertalert.png

Notice that the target field associated with assertAlert is used to match the text in the alert.

Using asserAlertPresent

To use assertAlertPresent to acknowledge the alert, the Selenium script looks as follows (where the click action opens the alert):

alert-assertalertpresent.png

Notice that the target field associated with assertAlertPresent is empty.

Handling confirmations

There are two ways of acknowledging a confirmation with Selenium commands: using assertConfirmation or using assertConfirmationPresent. Another command, chooseCancelOnNextConfirmation, is required in order to select “Cancel” instead of “OK”.

  • assertConfirmation(pattern) - This requires a string to match the confirmation text. If it matches, script execution continues. Otherwise, a milestone error event will occur.
  • assertConfirmationPresent() - This will dismiss the confirmation and continue. It does not need a string to match.
  • chooseCancelOnNextConfirmation - This instructs Selenium to click the “Cancel” button the next time a Javascript confirmation appears. The command chooseOkOnNextConfirmation also exists, but is not typically necessary as Selenium accepts confirmations by default.

Examples

In this example, a sample web page has a “Try it” button:

confirmation-try-it.png

When pressed, the following Javascript confirmation is displayed:

confirmation-popup.png

Using assertConfirmation

To use assertConfirmation to acknowledge the confirmation, the Selenium script looks as follows (where the click action opens the confirmation):

confirmation-assertconfirmation.png

Notice that the target field associated with assertConfirmation is used to match the text in the confirmation.

Using assertConfirmationPresent

To use assertConfirmationPresent to acknowledge the confirmation, the Selenium script looks as follows (where the click action opens the confirmation):

confirmation-assertconfirmationpresent.png

Notice that the target field associated with assertConfirmationPresent is empty.

Using chooseCancelOnNextConfirmation

To use chooseCancelOnNextConfirmation in conjunction with assertConfirmationPresent to cancel the confirmation, the Selenium script looks as follows (where the click action opens the confirmation):

confirmation-choosecancel.png

Notice that chooseCancelOnNextConfirmation is executed before the click action. Also, note that chooseCancelOnNextConfirmation could be used in conjunction with assertConfirmation.

Handling prompts

Similar to confirmations, there are two ways of acknowledging a prompt with Selenium commands: using assertPrompt or using assertPromptPresent. Another command, answerOnNextPrompt is used to fill in the prompt. The chooseCancelOnNextConfirmation command is used in order to select “Cancel” instead of “OK” (note that this is the same command as is used with confirmations).

  • answerOnNextPrompt(answer) - This is used to fill in the next prompt that appears with the “answer” text. If this command is not used when the next prompt is acknowledged, then the default text for the prompt will be accepted.
  • assertPrompt(pattern) - This will input any text provided by answerOnNextPrompt then acknowledge the prompt if the prompt text matches the string provided. If it matches, script execution continues. Otherwise, a milestone error event will occur.
  • assertPromptPresent() - This will input any text provided by answerOnNextPrompt, then accept the prompt without text matching.

Examples

In this example, a sample web page has a “Try it” button:

prompt-try-it.png

When pressed, the following Javascript prompt is displayed:

prompt-popup.png

Using assertPrompt

To use assertPrompt to acknowledge the prompt, the Selenium script looks as follows (where the click action opens the prompt):

prompt-assertprompt.png

Notice that the target field associated with assertPrompt is used to match the text in the prompt.

Using assertPromptPresent

To use assertPromptPresent to acknowledge the prompt, the Selenium script looks as follows (where the click action opens the prompt):

prompt-assertpromptpresent.png

Notice that the target field associated with assertPromptPresent is empty.

Using answerOnNextPrompt

To use answerOnNextPrompt in conjunction with assertPrompt to answer the prompt, the Selenium script looks as follows (where the click action opens the prompt):

prompt-answeronnext.png

Notice that answerOnNextPrompt is executed before the click action. Also, note that answerOnNextPrompt could be used in conjunction with assertPromptPresent.

Unacknowledged pop-ups

If there is a Javascript pop-up present during script execution, and it is not acknowledged by commands in your Selenium script, the web job will fail due to the unhandled alert. Since a user interacting with your site would have to acknowledge the pop-up, the script needs to do the same.