Dialogs
Introduction
CSTesting can interact with JavaScript dialogs: alert, confirm, and prompt, as well as the beforeunload confirmation when closing the page. You set how the next dialog will be handled before the action that triggers it (e.g. before click()). If you do not set a response, the action that opens the dialog can block until the dialog is handled (behavior depends on the browser/backend).
alert(), confirm(), prompt() dialogs
You register how to handle the next dialog by calling acceptNextAlert(), acceptNextAlert(String promptText), or dismissNextAlert() before the action that opens the dialog.
| Method | Description |
|---|---|
browser.acceptNextAlert() | Next dialog will be accepted (OK). Use for alert or confirm. |
browser.acceptNextAlert(String promptText) | Next prompt will be accepted with the given text. |
browser.dismissNextAlert() | Next confirm or prompt will be dismissed (Cancel). |
browser.getLastAlertMessage() | Message of the last dialog that opened (for assertions). |
Example: accept then click
browser.acceptNextAlert();
browser.locator("#btnAlert").click(); // triggers alert("Hello")
String msg = browser.getLastAlertMessage(); // "Hello"
assertEquals("Hello", msg);
Example: dismiss confirm
browser.dismissNextAlert();
browser.locator("#btnConfirm").click(); // confirm("Sure?") -> Cancel
Example: accept prompt with text
browser.acceptNextAlert("my input");
browser.locator("#btnPrompt").click(); // prompt("Name?") accepted with "my input"
Important: You must set the dialog response before the action that triggers the dialog. Otherwise the action can stall, because JavaScript dialogs are modal and block further page execution until they are handled.
Wrong — the click can hang:
browser.locator("#btnAlert").click(); // Dialog opens; execution blocks here
browser.acceptNextAlert(); // Never runs until dialog is handled
Correct — set response first:
browser.acceptNextAlert();
browser.locator("#btnAlert").click(); // Dialog opens and is accepted
String msg = browser.getLastAlertMessage();
If you do not call acceptNextAlert or dismissNextAlert before a dialog appears, behavior is implementation-dependent (e.g. some backends may auto-dismiss). For reliable tests, always set the next dialog response before triggering it.
beforeunload dialog
When the page is closed or navigated away, the browser may show a beforeunload confirmation ("Leave site?"). CSTesting's browser.close() closes the page; whether beforeunload runs and how it is handled depends on the browser and backend.
To handle the beforeunload dialog explicitly, set the next dialog response before the action that would trigger it (e.g. closing the window or navigating away):
browser.dismissNextAlert(); // or acceptNextAlert() to leave
browser.close(); // or an action that triggers beforeunload
If your test triggers navigation or close in a way that shows beforeunload, call acceptNextAlert() or dismissNextAlert() before that action so the dialog does not block the test.
Print dialogs
CSTesting does not provide a dedicated API for print dialogs (window.print()). The native print dialog is controlled by the OS/browser and is not a JavaScript alert/confirm/prompt dialog.
To test that a print dialog would be triggered, you can:
- Stub
window.printin the page (e.g. viabrowser.evaluate(...)) and assert that it was called, or - Rely on your app's own logic and assert on the state of the page before/after the "Print" action.
Example of stubbing and asserting that print was requested:
browser.gotoUrl("https://example.com/page-with-print");
browser.evaluate("window.printRequested = false; window.print = function() { window.printRequested = true; };");
browser.locator("//button[contains(.,'Print')]").first().click(); // or your print button selector
Object requested = browser.evaluate("window.printRequested");
assertTrue(Boolean.TRUE.equals(requested));
(Adjust the selector to match your page; use XPath if needed, e.g. //button[contains(.,'Print')].)
Summary
| Dialog type | CSTesting API | Notes |
|---|---|---|
| alert | acceptNextAlert() before click | Use getLastAlertMessage() to assert text. |
| confirm | acceptNextAlert() or dismissNextAlert() before click | Set before the action that triggers confirm. |
| prompt | acceptNextAlert(text) or dismissNextAlert() before click | Set before the action that triggers prompt. |
| beforeunload | acceptNextAlert() or dismissNextAlert() before close/navigate | Use when your flow triggers "Leave site?". |
| No built-in API | Stub window.print and assert it was called if needed. |