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.
There are two ways of acknowledging an alert with Selenium commands: using assertAlert or using assertAlertPresent.
In this example, a sample web page has a “Try it” button:
When pressed, the following Javascript alert is displayed:
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.
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.
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”.
In this example, a sample web page has a “Try it” button:
When pressed, the following Javascript confirmation is displayed:
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.
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.
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.
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).
In this example, a sample web page has a “Try it” button:
When pressed, the following Javascript prompt is displayed:
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.
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.
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.
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.