def test_callback(app: wda.Client):
    event = threading.Event()
    def _cb(client: wda.Client, url: str):
        if url.endswith("/sendkeys"):
            pass
        assert isinstance(client, wda.Client)
        assert url.endswith("/status")
        event.set()

    app.register_callback(wda.Callback.HTTP_REQUEST_BEFORE, _cb)
    # app.register_callback(wda.Callback.HTTP_REQUEST_AFTER, lambda url, response: print(url, response))
    app.status()
    assert event.is_set(), "callback is not called"

    # test remove_callback
    event.clear()
    app.unregister_callback(wda.Callback.HTTP_REQUEST_BEFORE, _cb)
    app.status()
    assert not event.is_set(), "callback is should not be called"

    event.clear()
    app.unregister_callback(wda.Callback.HTTP_REQUEST_BEFORE)
    app.status()
    assert not event.is_set(), "callback is should not be called"

    event.clear()
    app.unregister_callback()
    app.status()
    assert not event.is_set(), "callback is should not be called"
def test_error_callback(c: wda.Client):
    def err_handler(client: wda.Client, err):
        if isinstance(err, wda.WDARequestError):
            print("ERROR:", err)
            return wda.Callback.RET_ABORT # 直接退出
            return wda.Callback.RET_CONTINUE # 忽略错误继续执行
            return wda.Callback.RET_RETRY # 重试一下

    c.register_callback(wda.Callback.ERROR, err_handler)
    c.send_keys("hello callback")
def test_send_keys_callback(c: wda.Client):
    def _handle_alert_before_send_keys(client: wda.Client, urlpath: str):
        if not urlpath.endswith("/wda/keys"):
            return
        if client.alert.exists:
            client.alert.accept()
        print("callback called")

    c.register_callback(wda.Callback.HTTP_REQUEST_BEFORE, _handle_alert_before_send_keys)
    c.send_keys("hello callback")