Handle Alert task | Zoho QEngine Help

Handle Alert task

In a nutshell

The Handle Alert task accepts or dismisses a JavaScript alert, confirmation, or prompt dialog that appears on a page during test execution. This resolves a dialog that would otherwise block the test from continuing to its next step. The task can also validate the dialog's displayed message before responding to it.

1. Overview

The Handle Alert task interacts with a JavaScript alert, confirmation, or prompt dialog that appears during test execution. Based on the test requirement, it accepts or dismisses the dialog.
It can be used in two ways:
  1. Without validating the message: Accepts or dismisses the dialog immediately. Use this when only the action matters, such as closing a low-stock alert without verifying its exact wording.
  2. After validating the message: Verifies that the dialog text matches an expected value before accepting or dismissing it. If the text does not match, the task fails. Use this when the dialog's wording is important, such as confirming that a delete warning names the correct item before cancelling it.
Unlike page elements, JavaScript alert, confirmation, or prompt dialog exists outside the page's DOM, so no locator can target it, and Handle Alert is the only way to interact with it. A custom pop-up built from the page's own HTML, such as a modal window, is a regular DOM element instead. A locator-based task such as Click or Assert Visible works on it directly.
NotesNote: Handle Alert has no parameter for entering text into a prompt dialog. Accepting a prompt dialog submits whatever value is already present in its input field.

2. Syntax

handleAlert(<decision>, <message>);

ParameterData TypeDescription
decisionBOOLEAN

Determines how the task responds to the dialog.

Accepted values:

  1. True - Accepts or confirms the dialog.
  2. False - Dismisses the dialog.
message (Optional)STRINGThe expected text displayed in the dialog. When provided, the task verifies the dialog message before responding and fails if the messages don't match.

3. Examples

Example 1: Confirming a payment confirmation dialog's message before accepting it

Accept a payment confirmation dialog after confirming it shows the correct order number.

// Click the Make Payment button using the Click task
click("#zs-make-payment-button");
// Wait for the payment to process
wait(5);
// Confirm the payment confirmation dialog's message and accept it using the Handle Alert task
handleAlert(True, "Payment for Order #12345 was successful.");
// Confirm the order confirmation page loads using the Assert Visible task
assertVisible(ui.zwatch.order-confirmation-heading, errorhandling.STOP_ON_ERROR);

where:


#zs-make-payment-buttonLocator for the Make Payment button.
5Duration to wait for the payment to process, in seconds.
TrueAccepts the dialog.
"Payment for Order #12345 was successful."Expected message displayed in the payment confirmation dialog.
ui.zwatch.order-confirmation-headingLocator for the order confirmation heading.
errorhandling.STOP_ON_ERRORStops the test case if the order confirmation heading isn't visible.

Example 2: Accepting a cart-clearing confirmation without checking its message

Accept the cart-clearing confirmation dialog and confirm the cart is empty afterward.

// Click the Clear Cart button using the Click task
click("#zs-clear-cart-button");
// Accept the confirmation dialog using the Handle Alert task
handleAlert(True);
// Confirm the cart is empty using the Assert Visible task
assertVisible(ui.zwatch.empty-cart-message, errorhandling.STOP_ON_ERROR);

where:


#zs-clear-cart-buttonLocator for the Clear Cart button.
TrueAccepts the confirmation dialog.
ui.zwatch.empty-cart-messageLocator for the empty cart message.
errorhandling.STOP_ON_ERRORStops the test case if the empty cart message isn't visible.

Example 3: Dismissing a confirmation dialog instead of accepting it

Dismiss a delete confirmation dialog and confirm the item still exists afterward.

// Click the Delete button using the Click task
click(ui.<page_name>.<delete_button>);
// Dismiss the confirmation dialog using the Handle Alert task
handleAlert(False);
// Confirm the item still exists using the Assert Visible task
assertVisible(ui.<page_name>.<item>, errorhandling.STOP_ON_ERROR);

where:


ui.<page_name>.<delete_button>Locator for the Delete button.
FalseDismisses the confirmation dialog.
ui.<page_name>.<item>Locator for the item that should still exist after dismissing the dialog.
errorhandling.STOP_ON_ERRORStops the test case if the item isn't visible.

4. Use cases

Scenario 1: Confirming a bulk delete completes after accepting the confirmation dialog

Administrators performing a bulk delete need to confirm that only the intended records are removed, making the confirmation dialog an important safeguard against accidental data loss.
A tester uses Zoho QEngine to validate that the confirmation dialog displays the expected message before accepting it, then verifies that the selected records are deleted successfully.
Steps
  1. Open the records page in the same window.
  2. Select the records to delete.
  3. Click the bulk delete button.
  4. Confirm the dialog message and accept it.
  5. Wait for the success message to appear.
  6. Confirm the success message text.

Implementation

// Step 1: Open the records page using the Open URL task
openURL("<webpage_url>/records", "same window");
// Step 2: Select the records to delete using the Check task
check(ui.<records_page>.<select_all_checkbox>);
// Step 3: Click the bulk delete button using the Click task
click(ui.<records_page>.<bulk_delete_button>);
// Step 4: Confirm the dialog message and accept it to complete the deletion
handleAlert(True, "Delete 12 selected records?");
// Step 5: Wait until the success message becomes visible
wait(10, ui.<records_page>.<delete_success_message>, event.SHOW);
// Step 6: Confirm the success message text using the Assert Text task
assertText(ui.<records_page>.<delete_success_message>, "12 records deleted", errorhandling.STOP_ON_ERROR);
Scenario 2: Canceling an unsaved changes warning to remain on an edit page

Users editing a form field need a way to avoid losing unsaved changes when they accidentally try to leave the page. A confirmation dialog that warns before navigation gives them the chance to stay and finish their edits.
A tester uses Zoho QEngine to confirm that dismissing this dialog cancels the navigation and keeps the entered field value intact on the page.
Steps
  1. Open the profile edit form in the same window.
  2. Update a field on the form.
  3. Click a navigation link away from the page.
  4. Confirm the warning message and dismiss it to stay on the page.
  5. Confirm the updated field still holds the entered value.

Implementation

// Step 1: Open the profile edit form using the Open URL task
openURL("<webpage_url>/profile/edit", "same window");
// Step 2: Update the display name field using the Set Value task
setValue(ui.<profile_edit_page>.<display_name_field>, "Jordan Lee");
// Step 3: Click the Home navigation link using the Click task
click(ui.<profile_edit_page>.<home_nav_link>);
// Step 4: Confirm the unsaved changes warning and dismiss it to stay on the page
handleAlert(False, "You have unsaved changes. Leave this page?");
// Step 5: Confirm the display name field still holds the entered value using the Assert Value task
assertValue(ui.<profile_edit_page>.<display_name_field>, "Jordan Lee", errorhandling.STOP_ON_ERROR);
Scenario 3: Confirming a file rename completes with its pre-filled name after accepting a prompt dialog

Users renaming a file in a file manager need the dialog's pre-filled name preserved unless they intentionally change it, so an accidental accept doesn't silently corrupt the file name.

A tester uses Zoho QEngine to confirm that accepting the dialog without changing the pre-filled text keeps the file's name correct in the file list.

Steps

  1. Open the file manager page in the same window.
  2. Click the rename button for a file.
  3. Confirm the prompt message and accept it.
  4. Wait for the file list to refresh.
  5. Confirm the file name remains unchanged in the file list.

Implementation

// Step 1: Open the file manager page using the Open URL task
openURL("<webpage_url>/files", "same window");
// Step 2: Click the rename button for the target file using the Click task
click(ui.<file_manager_page>.<rename_button>);
// Step 3: Confirm the prompt message and accept it, submitting the pre-filled file name unchanged
handleAlert(True, "Rename this file?");
// Step 4: Wait until the file name label is present again after the rename action completes
wait(10, ui.<file_manager_page>.<file_name_label>, event.PRESENT);
// Step 5: Confirm the file name remains unchanged in the file list using the Assert Text task
assertText(ui.<file_manager_page>.<file_name_label>, "Q3_Report.pdf", errorhandling.STOP_ON_ERROR);

5. Points to note

  1. Handle Alert does not apply to a custom in-page pop-up, such as a modal window built into a page's design. Use a locator-based task such as Click or Assert Visible for that kind of element instead.
  2. Handle Alert must run immediately after the action that triggers the dialog. A JavaScript alert, confirmation, or prompt dialog blocks all other page interaction until it's resolved, so no other task can run before it. If accepting or dismissing the dialog leads to an asynchronous update, such as a success message or a page change, pair Handle Alert with a Wait task afterward.
  3. If the action that triggers the dialog occurs inside an iframe, use the Switch To task to switch to that iframe before triggering the dialog. The dialog appears outside the iframe, so the Handle Alert task works without a frame switch.

6. Related topics

  1. Click
  2. Wait
  3. Throw Error

7. What's next

Next step
Previous step
Next step

Now that you've learned all the Miscellaneous tasks, learn how to perform pointer-based interactions using the tasks in the Keyboard and Mouse Actions group, starting with the <Double Click> task.

Previous step

Before using the Handle Alert task, ensure that you've learned how to display a debugging message using the Info task.