Possible to get the form currently alerted?

Is it possible to get the form that is currently alerted (or I guess the “top most” form in the case where multiple forms may be alerted)?

For example, if I wanted to debug the form using the Client REPL at run-time, how can I “get” the alerted form? get_open_form() will give me the underlying template but I don’t believe it has any parent/child relationship to an alerted form.

The reason I’m trying to do this is for testing and not debugging so the debugger doesn’t meet my needs.

Alternatively, if anvil.alert doesn’t support that, and there’s no JS magic available, might anvil_extras.routing.alert? I suspect it must hang on to a reference to the form somewhere…

Are you passing an instance of a Form as the alert’s content?

If it’s just for tests I would consider patching anvil.alert

# import this only in test environment in a startup module
import anvil

current_alert_forms = []

old_alert = anvil.alert

def show_alert(sender, **event_args):
    current_alert_forms.append(sender)

def hide_alert(sender, **event_args):
    current_alert_forms.remove(sender)

def alert(content, **kws):
    if isinstance(content, str):
        content = anvil.Label(text=content)

    content.add_event_handler("show", show_alert)
    content.add_event_handler("hide", hide_alert)

    return old_alert(content, **kws)


anvil.alert = alert

and then in your tests you can inspect the current_alert_forms as you need

But it also depends what you’re trying to do in the tests.

If you just want to make sure an alert is there with some content.
you can just inspect the page and use the document to query select elements you’d expect to see.

This is perfect for what we’re trying to do. Thanks!