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

On ICSTestingBrowser (C:\CSTesting-DotNet), register the next dialog before the action that opens it: AcceptNextDialog(), AcceptNextDialog(promptText) for prompts, DismissNextDialog(), or SetDialogHandler(accept, promptText).

Method Description
AcceptNextDialog()Accept the next dialog (OK). For alert / confirm.
AcceptNextDialog(promptText)Accept the next prompt with text.
DismissNextDialog()Dismiss the next confirm / prompt (Cancel).
SetDialogHandler(accept, promptText)Lower-level; sets accept/dismiss and optional prompt text.

Example: accept then click

browser.AcceptNextDialog();
await browser.Locator("#btnAlert").ClickAsync();   // alert("Hello")

Example: dismiss confirm

browser.DismissNextDialog();
await browser.Locator("#btnConfirm").ClickAsync();

Example: accept prompt with text

browser.AcceptNextDialog("my input");
await browser.Locator("#btnPrompt").ClickAsync();

Important: Set the handler before the action that opens the dialog.

Wrong — the click can hang:

await browser.Locator("#btnAlert").ClickAsync();
browser.AcceptNextDialog();

Correct:

browser.AcceptNextDialog();
await browser.Locator("#btnAlert").ClickAsync();

If no handler is set, the implementation defaults to accepting with an empty prompt string (see CSTestingBrowser). For predictable tests, set the next response explicitly before triggering the dialog.

beforeunload dialog

When the page is closed or navigated away, the browser may show a beforeunload confirmation. Use AcceptNextDialog() / DismissNextDialog() before await browser.CloseAsync() or the action that triggers unload, same as other dialogs.

browser.DismissNextDialog();
await browser.CloseAsync();

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.print in the page (e.g. via browser.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:

await browser.GotoAsync("https://example.com/page-with-print");
await browser.EvaluateAsync<object>(
    @"() => { window.printRequested = false; window.print = function() { window.printRequested = true; }; }");
await browser.Locator("//button[contains(.,'Print')]").First().ClickAsync();
var requested = await browser.EvaluateAsync<bool>("window.printRequested");
Assert.That(requested, Is.True);

(Adjust the selector to match your page; use XPath if needed, e.g. //button[contains(.,'Print')].)

Summary

Dialog type CSTesting API Notes
alertAcceptNextDialog() before clickNo built-in "last message" API; assert on page state if needed.
confirmAcceptNextDialog() or DismissNextDialog() before clickSet before the action that triggers confirm.
promptAcceptNextDialog(text) or DismissNextDialog() before clickSet before the action that triggers prompt.
beforeunloadAcceptNextDialog() or DismissNextDialog() before close/navigateUse when your flow triggers "Leave site?".
PrintNo built-in APIStub window.print and assert it was called if needed.