The website uses cookies. By using this site, you agree to our use of cookies as described in the Privacy Policy.
I Agree

Selenium Pop-up Handling

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:

Screen shot of web page with 'JavaScript Alert' text and 'Try it' button

When pressed, the following Javascript alert is displayed:

Screen shot of a popup with text: 'Hello! I am an alert box!' and an 'OK' button

Using assertAlert

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

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):

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:

When pressed, the following Javascript confirmation is displayed:

Using assertConfirmation

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

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):

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):

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:

When pressed, the following Javascript prompt is displayed:

Using assertPrompt

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

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):

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):

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.

Measure
Measure
Summary | 13 Annotations
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.
2021/03/05 22:46
There are three different types of Javascript pop-ups: Alerts, Confirmations, and Prompts.
2021/03/05 22:48
Alerts
2021/03/05 22:48
dialogs containing a single button
2021/03/05 22:48
that must be pressed in order to continue.
2021/03/05 22:48
Confirmations
2021/03/05 22:48
dialogs containing more than one button
2021/03/05 22:48
One of these must be pressed in order to continue.
2021/03/05 22:48
Prompts
2021/03/05 22:48
dialogs that prompt the user for input and contain more than one button
2021/03/05 22:48
There are two ways of acknowledging an alert with Selenium commands: using assertAlert or using assertAlertPresent.
2021/03/05 22:48
assertAlert(pattern) - This requires a string to match the alert text. If it matches, script execution continues. Otherwise, a milestone error event will occur.
2021/03/05 22:48
assertAlertPresent() - This will dismiss the alert and continue. It does not need a string to match.
2021/03/05 22:48