karatelabs/karate · error · DriverException

dialog dismiss failed

Error message

dialog dismiss failed: {error}

What it means

CdpDialog.dismiss() sends Page.handleJavaScriptDialog with accept=false and throws DriverException 'dialog dismiss failed: <error>' if CDP returns an error. As with accept, the usual cause is the dialog no longer existing when the command arrives.

Solutions

  1. Check isHandled() before dismissing to avoid double-handling
  2. Catch DriverException and treat it as success when the dialog already disappeared
  3. Ensure only one code path (event handler or test step) manages the dialog
  4. Add a small synchronization so handlers don't race the test's explicit dismiss

Example fix

// before
dialog.dismiss(); // may fail if already handled by event handler
// after
if (!dialog.isHandled()) {
    dialog.dismiss();
}
Defensive patterns

Strategy: validation

Validate before calling

if (dialog == null || dialog.isHandled()) {
    return; // already dismissed or gone
}

Type guard

boolean canDismiss(CdpDialog d) { return d != null && !d.isHandled(); }

Try / catch

try { dialog.dismiss(); }
catch (DriverException e) {
    if (e.getMessage().startsWith("dialog dismiss failed")) {
        logger.info("dialog already handled elsewhere");
    } else throw e;
}

Prevention

When it happens

Trigger: Calling dismiss() after the dialog was already handled or auto-closed; page navigated away, destroying the dialog; a prior accept()/dismiss() on the same dialog object.

Common situations: Event handler (setupEventHandlers) dismissing a dialog the test already handled; double dismissal in chained scenario steps; timing races in CI where the browser closed the dialog first.

Understand the failure class

Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.

Related errors


AI-assisted analysis of karatelabs/karate@a22eb90246 (2026-09-12). Data as JSON: /api/errors/6f1878c10cdcda6d. Report an issue: GitHub.

Appendix: source

Thrown at karate-core/src/main/java/io/karatelabs/driver/cdp/CdpDialog.java:109

        // to prevent retry attempts that will also fail
        if (response.isError()) {
            // Log but don't throw - the dialog is effectively handled (gone)
            throw new DriverException("dialog accept failed: " + response.getError());
        }
    }

    @Override
    public void dismiss() {
        if (handled) {
            // Already handled - silently return to avoid race condition errors
            return;
        }
        handled = true;
        CdpResponse response = cdp.method("Page.handleJavaScriptDialog")
                .param("accept", false)
                .send();
        if (response.isError()) {
            throw new DriverException("dialog dismiss failed: " + response.getError());
        }
    }

    @Override
    public boolean isHandled() {
        return handled;
    }

    @Override
    public String toString() {
        return "Dialog{type='" + type + "', message='" + message + "'}";
    }

}

View on GitHub (pinned to a22eb90246)