Skip to content

Add expected_type= to Wait For Alert and types= to Wait For Alerts to verify the dialog type #5118

Description

@Snooz82

Use case

Wait For Alert and Wait For Alerts can already verify a dialog's text, but they cannot verify which kind of dialog fired. A page can show either a confirm() or a prompt() depending on state, and a regression that swaps one for the other (or that suddenly shows a plain alert() instead of a confirmation) currently passes silently as long as the text happens to match — and if the texts are identical or empty, there is no way at all to tell the dialogs apart. Playwright knows the type of every dialog (alert, confirm, prompt, beforeunload), but the library never surfaces it, and there is no workaround because dialogs cannot be inspected by any other keyword.

Asserting the type alongside the text is a natural extension of the same keyword that already returns the message text.

Proposed keyword / arguments

New named-only argument on Wait For Alert:

  • expected_type: DialogType | None = None — one of alert, confirm, prompt, beforeunload. When given, the keyword fails if the handled dialog's type differs. Default None skips the check (current behavior).

Matching new argument on Wait For Alerts:

  • types: list[DialogType | None] | None = None — one entry per expected dialog, None entries skip the check for that dialog. Default None disables type checking entirely (current behavior). When given, the list length must match actions/prompt_inputs/texts, consistent with the existing validation.
${promise} =    Promise To    Wait For Alert    action=accept    text=Delete this item?    expected_type=confirm
Click           id=delete
${text} =       Wait For    ${promise}

# Two dialogs: first must be a confirm, second a prompt
${promise} =    Promise To    Wait For Alerts
...    ["accept", "accept"]
...    [None, "my input"]
...    [None, None]
...    types=["confirm", "prompt"]
Click       id=confirmAndPrompt
${texts} =    Wait For    ${promise}

Playwright API

Maps to dialog.type(), which returns one of alert, beforeunload, confirm or prompt. The dialog object is already handled inside the wrapper's waitForAlerts implementation; its type just needs to be read and checked.

Implementation notes

  • protobuf/playwright.proto: extend the AlertAction message with an optional expected type field.
  • node/playwright-wrapper: in the dialog handler, compare dialog.type() against the expected type (when set) and reject with a clear error message on mismatch.
  • Browser/keywords/interaction.py: add expected_type to wait_for_alert and types to wait_for_alerts (with the same length validation as the existing lists), plus a DialogType enum in Browser/utils/data_types.py if none fits.
  • Docs and atest: the test app already triggers alert/confirm/prompt dialogs.

Backwards compatibility

Purely additive: both arguments default to None, which skips type verification entirely, so all existing suites behave exactly as before.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions