Skip to content

Retry

flaui.core.tools.Retry

Polling helpers modelled on C# FlaUI.Core.Tools.Retry.

Every method calls retry_method repeatedly until its condition holds or timeout runs out. Unlike C#, timeout and interval are plain integers in milliseconds (not TimeSpan), and results are returned directly (not wrapped in a RetryResult).

Pick the method by what you are waiting for:

  • a value to become truthy: :meth:While (returns it) or :meth:WhileFalse (returns bool)
  • a value to become falsy: :meth:WhileNot or :meth:WhileTrue
  • a non-None value: :meth:WhileNull; a value to go away (become None): :meth:WhileNotNull
  • a non-empty collection: :meth:WhileEmpty
  • a call to stop raising (e.g. lookups raising ElementNotFound): :meth:WhileException

IsTimeOutReached(start_time, timeout) staticmethod

Return whether timeout milliseconds have passed since start_time.

Parameters:

Name Type Description Default
start_time float

Start time from time.monotonic() (seconds).

required
timeout int

Timeout in milliseconds.

required

Returns:

Type Description
bool

True if the timeout has been reached, False otherwise.

Source code in flaui/core/tools.py
@staticmethod
def IsTimeOutReached(start_time: float, timeout: int) -> bool:
    """Return whether ``timeout`` milliseconds have passed since ``start_time``.

    :param start_time: Start time from ``time.monotonic()`` (seconds).
    :param timeout: Timeout in milliseconds.
    :return: True if the timeout has been reached, False otherwise.
    """
    return (time.monotonic() - start_time) * 1000.0 > timeout

While(retry_method, timeout=1000, interval=100, throw_on_timeout=False, ignore_exception=False, timeout_message=None, last_value_on_timeout=False, default_on_timeout=None) staticmethod

Retry until retry_method returns a truthy value, and return that value.

Mirrors the polling of C# Retry.While. Unlike C#, there is no separate check function: the result itself is tested for truthiness.

Only ValueError and AssertionError are handled (see ignore_exception). Any other exception, including :class:~flaui.lib.exceptions.ElementNotFound, propagates straight away; use :meth:WhileException to retry a lookup that raises until it succeeds.

Example::

value = Retry.While(lambda: label.text, timeout=5000, interval=100)  # 5 s

Parameters:

Name Type Description Default
retry_method Callable[[], T]

Callable taking no arguments; it is called repeatedly.

required
timeout int

How long to keep retrying, in milliseconds, defaults to 1000.

1000
interval int

Pause between attempts, in milliseconds, defaults to 100.

100
throw_on_timeout bool

Raise TimeoutError when the timeout is reached, defaults to False.

False
ignore_exception bool

Treat a ValueError / AssertionError from retry_method as a failed attempt and keep retrying, instead of re-raising it, defaults to False.

False
timeout_message Optional[str]

Message for the TimeoutError, defaults to None.

None
last_value_on_timeout bool

On timeout, return the last (falsy) result instead of raising, defaults to False.

False
default_on_timeout Optional[T]

On timeout, return this value instead of raising, defaults to None.

None

Returns:

Type Description
T

The first truthy result.

Raises:

Type Description
TimeoutError

On timeout, unless last_value_on_timeout or default_on_timeout provides a value to return.

Source code in flaui/core/tools.py
@staticmethod
def While(
    retry_method: Callable[[], T],
    timeout: int = 1000,
    interval: int = 100,
    throw_on_timeout: bool = False,
    ignore_exception: bool = False,
    timeout_message: Optional[str] = None,
    last_value_on_timeout: bool = False,
    default_on_timeout: Optional[T] = None,
) -> T:
    """Retry until ``retry_method`` returns a truthy value, and return that value.

    Mirrors the polling of C# ``Retry.While``. Unlike C#, there is no separate check function:
    the result itself is tested for truthiness.

    Only ``ValueError`` and ``AssertionError`` are handled (see ``ignore_exception``). Any other
    exception, including :class:`~flaui.lib.exceptions.ElementNotFound`, propagates straight away;
    use :meth:`WhileException` to retry a lookup that raises until it succeeds.

    Example::

        value = Retry.While(lambda: label.text, timeout=5000, interval=100)  # 5 s

    :param retry_method: Callable taking no arguments; it is called repeatedly.
    :param timeout: How long to keep retrying, in **milliseconds**, defaults to 1000.
    :param interval: Pause between attempts, in **milliseconds**, defaults to 100.
    :param throw_on_timeout: Raise ``TimeoutError`` when the timeout is reached, defaults to False.
    :param ignore_exception: Treat a ``ValueError`` / ``AssertionError`` from ``retry_method`` as a
        failed attempt and keep retrying, instead of re-raising it, defaults to False.
    :param timeout_message: Message for the ``TimeoutError``, defaults to None.
    :param last_value_on_timeout: On timeout, return the last (falsy) result instead of raising,
        defaults to False.
    :param default_on_timeout: On timeout, return this value instead of raising, defaults to None.
    :raises TimeoutError: On timeout, unless ``last_value_on_timeout`` or ``default_on_timeout``
        provides a value to return.
    :return: The first truthy result.
    """
    start = Retry._now_ms()
    last_value: Optional[T] = None
    while True:
        try:
            val = retry_method()
            last_value = val
            if val:
                return val
        except (ValueError, AssertionError) as e:
            if not ignore_exception:
                raise
            last_value = e  # type: ignore
        if Retry._now_ms() - start >= timeout:
            if throw_on_timeout:
                raise TimeoutError(timeout_message or f"Timeout of {timeout} ms exceeded.")
            if last_value_on_timeout:
                return last_value  # type: ignore
            if default_on_timeout is not None:
                return default_on_timeout
            raise TimeoutError(timeout_message or f"Timeout of {timeout} ms exceeded.")
        Retry._sleep_ms(interval)

WhileEmpty(retry_method, timeout=1000, interval=100, throw_on_timeout=False, ignore_exception=False, timeout_message=None, last_value_on_timeout=False, default_on_timeout=None) staticmethod

Retry while retry_method returns an empty iterable, and return the first non-empty result.

A non-iterable result counts as "not empty" and is returned straight away.

Only ValueError and AssertionError are handled (see ignore_exception). Any other exception, including :class:~flaui.lib.exceptions.ElementNotFound, propagates straight away; use :meth:WhileException to retry a lookup that raises until it succeeds.

Parameters:

Name Type Description Default
retry_method Callable[[], T]

Callable taking no arguments; it is called repeatedly.

required
timeout int

How long to keep retrying, in milliseconds, defaults to 1000.

1000
interval int

Pause between attempts, in milliseconds, defaults to 100.

100
throw_on_timeout bool

Raise TimeoutError when the timeout is reached, defaults to False.

False
ignore_exception bool

Treat a ValueError / AssertionError from retry_method as a failed attempt and keep retrying, instead of re-raising it, defaults to False.

False
timeout_message Optional[str]

Message for the TimeoutError, defaults to None.

None
last_value_on_timeout bool

On timeout, return the last (empty) result instead of raising, defaults to False.

False
default_on_timeout Optional[T]

On timeout, return this value instead of raising, defaults to None.

None

Returns:

Type Description
T

The first non-empty result.

Raises:

Type Description
TimeoutError

On timeout, unless last_value_on_timeout or default_on_timeout provides a value to return.

Source code in flaui/core/tools.py
@staticmethod
def WhileEmpty(
    retry_method: Callable[[], T],
    timeout: int = 1000,
    interval: int = 100,
    throw_on_timeout: bool = False,
    ignore_exception: bool = False,
    timeout_message: Optional[str] = None,
    last_value_on_timeout: bool = False,
    default_on_timeout: Optional[T] = None,
) -> T:
    """Retry while ``retry_method`` returns an empty iterable, and return the first non-empty result.

    A non-iterable result counts as "not empty" and is returned straight away.

    Only ``ValueError`` and ``AssertionError`` are handled (see ``ignore_exception``). Any other
    exception, including :class:`~flaui.lib.exceptions.ElementNotFound`, propagates straight away;
    use :meth:`WhileException` to retry a lookup that raises until it succeeds.

    :param retry_method: Callable taking no arguments; it is called repeatedly.
    :param timeout: How long to keep retrying, in **milliseconds**, defaults to 1000.
    :param interval: Pause between attempts, in **milliseconds**, defaults to 100.
    :param throw_on_timeout: Raise ``TimeoutError`` when the timeout is reached, defaults to False.
    :param ignore_exception: Treat a ``ValueError`` / ``AssertionError`` from ``retry_method`` as a
        failed attempt and keep retrying, instead of re-raising it, defaults to False.
    :param timeout_message: Message for the ``TimeoutError``, defaults to None.
    :param last_value_on_timeout: On timeout, return the last (empty) result instead of raising,
        defaults to False.
    :param default_on_timeout: On timeout, return this value instead of raising, defaults to None.
    :raises TimeoutError: On timeout, unless ``last_value_on_timeout`` or ``default_on_timeout``
        provides a value to return.
    :return: The first non-empty result.
    """
    start = Retry._now_ms()
    last_value: Optional[T] = None
    while True:
        try:
            val = retry_method()
            last_value = val
            if not (isinstance(val, Iterable) and not val):
                return val
        except (ValueError, AssertionError) as e:
            if not ignore_exception:
                raise
            last_value = e  # type: ignore
        if Retry._now_ms() - start >= timeout:
            if throw_on_timeout:
                raise TimeoutError(timeout_message or f"Timeout of {timeout} ms exceeded.")
            if last_value_on_timeout:
                return last_value  # type: ignore
            if default_on_timeout is not None:
                return default_on_timeout
            raise TimeoutError(timeout_message or f"Timeout of {timeout} ms exceeded.")
        Retry._sleep_ms(interval)

WhileException(retry_method, timeout=1000, interval=100, throw_on_timeout=False, ignore_exception=False, timeout_message=None, last_value_on_timeout=False, default_on_timeout=None) staticmethod

Retry while retry_method raises any exception, and return its first successful result.

This is the helper for element lookups, which raise :class:~flaui.lib.exceptions.ElementNotFound until the element exists::

button = Retry.WhileException(
    lambda: window.find_first_descendant(cf.by_automation_id("OK")),
    timeout=5000,
    throw_on_timeout=True,
)

Parameters:

Name Type Description Default
retry_method Callable[[], T]

Callable taking no arguments; it is called repeatedly.

required
timeout int

How long to keep retrying, in milliseconds, defaults to 1000.

1000
interval int

Pause between attempts, in milliseconds, defaults to 100.

100
throw_on_timeout bool

On timeout, raise TimeoutError instead of re-raising the last exception, defaults to False.

False
ignore_exception bool

On timeout, raise TimeoutError instead of re-raising the last exception, defaults to False.

False
timeout_message Optional[str]

Message for the TimeoutError, defaults to None.

None
last_value_on_timeout bool

Accepted for signature parity; not used.

False
default_on_timeout Optional[T]

Accepted for signature parity; not used.

None

Returns:

Type Description
T

The first result returned without raising.

Raises:

Type Description
TimeoutError

On timeout, when throw_on_timeout or ignore_exception is True.

Exception

On timeout otherwise: the last exception retry_method raised.

Source code in flaui/core/tools.py
@staticmethod
def WhileException(  # pylint: disable=unused-argument  # last_value/default_on_timeout: C# signature parity
    retry_method: Callable[[], T],
    timeout: int = 1000,
    interval: int = 100,
    throw_on_timeout: bool = False,
    ignore_exception: bool = False,
    timeout_message: Optional[str] = None,
    last_value_on_timeout: bool = False,
    default_on_timeout: Optional[T] = None,
) -> T:
    """Retry while ``retry_method`` raises any exception, and return its first successful result.

    This is the helper for element lookups, which raise
    :class:`~flaui.lib.exceptions.ElementNotFound` until the element exists::

        button = Retry.WhileException(
            lambda: window.find_first_descendant(cf.by_automation_id("OK")),
            timeout=5000,
            throw_on_timeout=True,
        )

    :param retry_method: Callable taking no arguments; it is called repeatedly.
    :param timeout: How long to keep retrying, in **milliseconds**, defaults to 1000.
    :param interval: Pause between attempts, in **milliseconds**, defaults to 100.
    :param throw_on_timeout: On timeout, raise ``TimeoutError`` instead of re-raising the last
        exception, defaults to False.
    :param ignore_exception: On timeout, raise ``TimeoutError`` instead of re-raising the last
        exception, defaults to False.
    :param timeout_message: Message for the ``TimeoutError``, defaults to None.
    :param last_value_on_timeout: Accepted for signature parity; not used.
    :param default_on_timeout: Accepted for signature parity; not used.
    :raises TimeoutError: On timeout, when ``throw_on_timeout`` or ``ignore_exception`` is True.
    :raises Exception: On timeout otherwise: the last exception ``retry_method`` raised.
    :return: The first result returned without raising.
    """
    start = Retry._now_ms()
    last_exc: Optional[Exception] = None
    while True:
        try:
            return retry_method()
        except Exception as e:  # Always ignore and retry until success
            last_exc = e
        if Retry._now_ms() - start >= timeout:
            if throw_on_timeout:
                raise TimeoutError(timeout_message or f"Timeout of {timeout} ms exceeded.")
            if last_exc is not None and not ignore_exception:
                # If not configured to ignore, propagate last exception
                raise last_exc
            raise TimeoutError(timeout_message or f"Timeout of {timeout} ms exceeded.")
        Retry._sleep_ms(interval)

WhileFalse(retry_method, timeout=1000, interval=100, throw_on_timeout=False, ignore_exception=False, timeout_message=None, last_value_on_timeout=False, default_on_timeout=None) staticmethod

Retry while retry_method returns a falsy value; succeed once it becomes truthy.

This is the usual "wait until" helper::

Retry.WhileFalse(lambda: button.is_enabled, timeout=2000)

Python equivalent of C# Retry.WhileFalse, which returns a RetryResult<bool>; this returns the boolean directly.

Only ValueError and AssertionError are handled (see ignore_exception). Any other exception, including :class:~flaui.lib.exceptions.ElementNotFound, propagates straight away; use :meth:WhileException to retry a lookup that raises until it succeeds.

Parameters:

Name Type Description Default
retry_method Callable[[], T]

Callable taking no arguments; it is called repeatedly.

required
timeout int

How long to keep retrying, in milliseconds, defaults to 1000.

1000
interval int

Pause between attempts, in milliseconds, defaults to 100.

100
throw_on_timeout bool

Raise TimeoutError when the timeout is reached, defaults to False.

False
ignore_exception bool

Treat a ValueError / AssertionError from retry_method as a failed attempt and keep retrying, instead of re-raising it, defaults to False.

False
timeout_message Optional[str]

Message for the TimeoutError, defaults to None.

None
last_value_on_timeout bool

Accepted for signature parity; not used.

False
default_on_timeout Optional[T]

Accepted for signature parity; not used.

None

Returns:

Type Description
bool

True if the predicate became truthy in time, False if the timeout was reached.

Raises:

Type Description
TimeoutError

On timeout, only when throw_on_timeout is True.

Source code in flaui/core/tools.py
@staticmethod
def WhileFalse(  # pylint: disable=unused-argument  # last_value/default_on_timeout: C# signature parity
    retry_method: Callable[[], T],
    timeout: int = 1000,
    interval: int = 100,
    throw_on_timeout: bool = False,
    ignore_exception: bool = False,
    timeout_message: Optional[str] = None,
    last_value_on_timeout: bool = False,
    default_on_timeout: Optional[T] = None,
) -> bool:
    """Retry while ``retry_method`` returns a falsy value; succeed once it becomes truthy.

    This is the usual "wait until" helper::

        Retry.WhileFalse(lambda: button.is_enabled, timeout=2000)

    Python equivalent of C# ``Retry.WhileFalse``, which returns a ``RetryResult<bool>``; this
    returns the boolean directly.

    Only ``ValueError`` and ``AssertionError`` are handled (see ``ignore_exception``). Any other
    exception, including :class:`~flaui.lib.exceptions.ElementNotFound`, propagates straight away;
    use :meth:`WhileException` to retry a lookup that raises until it succeeds.

    :param retry_method: Callable taking no arguments; it is called repeatedly.
    :param timeout: How long to keep retrying, in **milliseconds**, defaults to 1000.
    :param interval: Pause between attempts, in **milliseconds**, defaults to 100.
    :param throw_on_timeout: Raise ``TimeoutError`` when the timeout is reached, defaults to False.
    :param ignore_exception: Treat a ``ValueError`` / ``AssertionError`` from ``retry_method`` as a
        failed attempt and keep retrying, instead of re-raising it, defaults to False.
    :param timeout_message: Message for the ``TimeoutError``, defaults to None.
    :param last_value_on_timeout: Accepted for signature parity; not used.
    :param default_on_timeout: Accepted for signature parity; not used.
    :raises TimeoutError: On timeout, only when ``throw_on_timeout`` is True.
    :return: True if the predicate became truthy in time, False if the timeout was reached.
    """
    start = Retry._now_ms()
    while True:
        try:
            if bool(retry_method()):
                return True
        except (ValueError, AssertionError):
            if not ignore_exception:
                raise
        if Retry._now_ms() - start >= timeout:
            if throw_on_timeout:
                raise TimeoutError(timeout_message or f"Timeout of {timeout} ms exceeded.")
            return False
        Retry._sleep_ms(interval)

WhileNot(retry_method, timeout=1000, interval=100, throw_on_timeout=False, ignore_exception=False, timeout_message=None, last_value_on_timeout=False, default_on_timeout=None) staticmethod

Retry until retry_method returns a falsy value, and return that value.

Only ValueError and AssertionError are handled (see ignore_exception). Any other exception, including :class:~flaui.lib.exceptions.ElementNotFound, propagates straight away; use :meth:WhileException to retry a lookup that raises until it succeeds.

Parameters:

Name Type Description Default
retry_method Callable[[], T]

Callable taking no arguments; it is called repeatedly.

required
timeout int

How long to keep retrying, in milliseconds, defaults to 1000.

1000
interval int

Pause between attempts, in milliseconds, defaults to 100.

100
throw_on_timeout bool

Raise TimeoutError when the timeout is reached, defaults to False.

False
ignore_exception bool

Treat a ValueError / AssertionError from retry_method as a failed attempt and keep retrying, instead of re-raising it, defaults to False.

False
timeout_message Optional[str]

Message for the TimeoutError, defaults to None.

None
last_value_on_timeout bool

On timeout, return the last (truthy) result instead of raising, defaults to False.

False
default_on_timeout Optional[T]

On timeout, return this value instead of raising, defaults to None.

None

Returns:

Type Description
T

The first falsy result.

Raises:

Type Description
TimeoutError

On timeout, unless last_value_on_timeout or default_on_timeout provides a value to return.

Source code in flaui/core/tools.py
@staticmethod
def WhileNot(
    retry_method: Callable[[], T],
    timeout: int = 1000,
    interval: int = 100,
    throw_on_timeout: bool = False,
    ignore_exception: bool = False,
    timeout_message: Optional[str] = None,
    last_value_on_timeout: bool = False,
    default_on_timeout: Optional[T] = None,
) -> T:
    """Retry until ``retry_method`` returns a falsy value, and return that value.

    Only ``ValueError`` and ``AssertionError`` are handled (see ``ignore_exception``). Any other
    exception, including :class:`~flaui.lib.exceptions.ElementNotFound`, propagates straight away;
    use :meth:`WhileException` to retry a lookup that raises until it succeeds.

    :param retry_method: Callable taking no arguments; it is called repeatedly.
    :param timeout: How long to keep retrying, in **milliseconds**, defaults to 1000.
    :param interval: Pause between attempts, in **milliseconds**, defaults to 100.
    :param throw_on_timeout: Raise ``TimeoutError`` when the timeout is reached, defaults to False.
    :param ignore_exception: Treat a ``ValueError`` / ``AssertionError`` from ``retry_method`` as a
        failed attempt and keep retrying, instead of re-raising it, defaults to False.
    :param timeout_message: Message for the ``TimeoutError``, defaults to None.
    :param last_value_on_timeout: On timeout, return the last (truthy) result instead of raising,
        defaults to False.
    :param default_on_timeout: On timeout, return this value instead of raising, defaults to None.
    :raises TimeoutError: On timeout, unless ``last_value_on_timeout`` or ``default_on_timeout``
        provides a value to return.
    :return: The first falsy result.
    """
    start = Retry._now_ms()
    last_value: Optional[T] = None
    while True:
        try:
            val = retry_method()
            last_value = val
            if not val:
                return val
        except (ValueError, AssertionError) as e:
            if not ignore_exception:
                raise
            last_value = e  # type: ignore
        if Retry._now_ms() - start >= timeout:
            if throw_on_timeout:
                raise TimeoutError(timeout_message or f"Timeout of {timeout} ms exceeded.")
            if last_value_on_timeout:
                return last_value  # type: ignore
            if default_on_timeout is not None:
                return default_on_timeout
            raise TimeoutError(timeout_message or f"Timeout of {timeout} ms exceeded.")
        Retry._sleep_ms(interval)

WhileNotNull(retry_method, timeout=1000, interval=100, throw_on_timeout=False, ignore_exception=False, timeout_message=None, last_value_on_timeout=False, default_on_timeout=None) staticmethod

Retry while retry_method returns a non-None value; succeed once it returns None.

Useful for waiting until something disappears.

Only ValueError and AssertionError are handled (see ignore_exception). Any other exception, including :class:~flaui.lib.exceptions.ElementNotFound, propagates straight away; use :meth:WhileException to retry a lookup that raises until it succeeds.

Parameters:

Name Type Description Default
retry_method Callable[[], T]

Callable taking no arguments; it is called repeatedly.

required
timeout int

How long to keep retrying, in milliseconds, defaults to 1000.

1000
interval int

Pause between attempts, in milliseconds, defaults to 100.

100
throw_on_timeout bool

Raise TimeoutError when the timeout is reached, defaults to False.

False
ignore_exception bool

Treat a ValueError / AssertionError from retry_method as a failed attempt and keep retrying, instead of re-raising it, defaults to False.

False
timeout_message Optional[str]

Message for the TimeoutError, defaults to None.

None
last_value_on_timeout bool

Accepted for signature parity; the last value is always returned on timeout.

False
default_on_timeout Optional[T]

Accepted for signature parity; not used.

None

Returns:

Type Description
T

None once the result became None, or the last result if the timeout was reached.

Raises:

Type Description
TimeoutError

On timeout, only when throw_on_timeout is True.

Source code in flaui/core/tools.py
@staticmethod
def WhileNotNull(  # pylint: disable=unused-argument  # last_value/default_on_timeout: C# signature parity
    retry_method: Callable[[], T],
    timeout: int = 1000,
    interval: int = 100,
    throw_on_timeout: bool = False,
    ignore_exception: bool = False,
    timeout_message: Optional[str] = None,
    last_value_on_timeout: bool = False,
    default_on_timeout: Optional[T] = None,
) -> T:
    """Retry while ``retry_method`` returns a non-None value; succeed once it returns None.

    Useful for waiting until something disappears.

    Only ``ValueError`` and ``AssertionError`` are handled (see ``ignore_exception``). Any other
    exception, including :class:`~flaui.lib.exceptions.ElementNotFound`, propagates straight away;
    use :meth:`WhileException` to retry a lookup that raises until it succeeds.

    :param retry_method: Callable taking no arguments; it is called repeatedly.
    :param timeout: How long to keep retrying, in **milliseconds**, defaults to 1000.
    :param interval: Pause between attempts, in **milliseconds**, defaults to 100.
    :param throw_on_timeout: Raise ``TimeoutError`` when the timeout is reached, defaults to False.
    :param ignore_exception: Treat a ``ValueError`` / ``AssertionError`` from ``retry_method`` as a
        failed attempt and keep retrying, instead of re-raising it, defaults to False.
    :param timeout_message: Message for the ``TimeoutError``, defaults to None.
    :param last_value_on_timeout: Accepted for signature parity; the last value is always returned
        on timeout.
    :param default_on_timeout: Accepted for signature parity; not used.
    :raises TimeoutError: On timeout, only when ``throw_on_timeout`` is True.
    :return: None once the result became None, or the last result if the timeout was reached.
    """
    start = Retry._now_ms()
    last_value: Optional[T] = None
    while True:
        try:
            val = retry_method()
            last_value = val
            if val is None:
                return None  # type: ignore
        except (ValueError, AssertionError) as e:
            if not ignore_exception:
                raise
            last_value = e  # type: ignore
        if Retry._now_ms() - start >= timeout:
            if throw_on_timeout:
                raise TimeoutError(timeout_message or f"Timeout of {timeout} ms exceeded.")
            return last_value  # type: ignore
        Retry._sleep_ms(interval)

WhileNull(retry_method, timeout=1000, interval=100, throw_on_timeout=False, ignore_exception=False, timeout_message=None, last_value_on_timeout=False, default_on_timeout=None) staticmethod

Retry while retry_method returns None, and return the first non-None result.

Only ValueError and AssertionError are handled (see ignore_exception). Any other exception, including :class:~flaui.lib.exceptions.ElementNotFound, propagates straight away; use :meth:WhileException to retry a lookup that raises until it succeeds.

Parameters:

Name Type Description Default
retry_method Callable[[], T]

Callable taking no arguments; it is called repeatedly.

required
timeout int

How long to keep retrying, in milliseconds, defaults to 1000.

1000
interval int

Pause between attempts, in milliseconds, defaults to 100.

100
throw_on_timeout bool

Raise TimeoutError when the timeout is reached, defaults to False.

False
ignore_exception bool

Treat a ValueError / AssertionError from retry_method as a failed attempt and keep retrying, instead of re-raising it, defaults to False.

False
timeout_message Optional[str]

Message for the TimeoutError, defaults to None.

None
last_value_on_timeout bool

Accepted for signature parity; not used.

False
default_on_timeout Optional[T]

Accepted for signature parity; not used.

None

Returns:

Type Description
T

The first non-None result, or None if the timeout was reached.

Raises:

Type Description
TimeoutError

On timeout, only when throw_on_timeout is True.

Source code in flaui/core/tools.py
@staticmethod
def WhileNull(  # pylint: disable=unused-argument  # last_value/default_on_timeout: C# signature parity
    retry_method: Callable[[], T],
    timeout: int = 1000,
    interval: int = 100,
    throw_on_timeout: bool = False,
    ignore_exception: bool = False,
    timeout_message: Optional[str] = None,
    last_value_on_timeout: bool = False,
    default_on_timeout: Optional[T] = None,
) -> T:
    """Retry while ``retry_method`` returns None, and return the first non-None result.

    Only ``ValueError`` and ``AssertionError`` are handled (see ``ignore_exception``). Any other
    exception, including :class:`~flaui.lib.exceptions.ElementNotFound`, propagates straight away;
    use :meth:`WhileException` to retry a lookup that raises until it succeeds.

    :param retry_method: Callable taking no arguments; it is called repeatedly.
    :param timeout: How long to keep retrying, in **milliseconds**, defaults to 1000.
    :param interval: Pause between attempts, in **milliseconds**, defaults to 100.
    :param throw_on_timeout: Raise ``TimeoutError`` when the timeout is reached, defaults to False.
    :param ignore_exception: Treat a ``ValueError`` / ``AssertionError`` from ``retry_method`` as a
        failed attempt and keep retrying, instead of re-raising it, defaults to False.
    :param timeout_message: Message for the ``TimeoutError``, defaults to None.
    :param last_value_on_timeout: Accepted for signature parity; not used.
    :param default_on_timeout: Accepted for signature parity; not used.
    :raises TimeoutError: On timeout, only when ``throw_on_timeout`` is True.
    :return: The first non-None result, or None if the timeout was reached.
    """
    start = Retry._now_ms()
    while True:
        try:
            val = retry_method()
            if val is not None:
                return val
        except (ValueError, AssertionError):
            if not ignore_exception:
                raise
        if Retry._now_ms() - start >= timeout:
            if throw_on_timeout:
                raise TimeoutError(timeout_message or f"Timeout of {timeout} ms exceeded.")
            return None  # type: ignore
        Retry._sleep_ms(interval)

WhileTrue(retry_method, timeout=1000, interval=100, throw_on_timeout=False, ignore_exception=False, timeout_message=None, last_value_on_timeout=False, default_on_timeout=None) staticmethod

Retry while retry_method returns a truthy value; succeed once it becomes falsy.

Python equivalent of C# Retry.WhileTrue, which returns a RetryResult<bool>; this returns the boolean directly.

Only ValueError and AssertionError are handled (see ignore_exception). Any other exception, including :class:~flaui.lib.exceptions.ElementNotFound, propagates straight away; use :meth:WhileException to retry a lookup that raises until it succeeds.

Parameters:

Name Type Description Default
retry_method Callable[[], T]

Callable taking no arguments; it is called repeatedly.

required
timeout int

How long to keep retrying, in milliseconds, defaults to 1000.

1000
interval int

Pause between attempts, in milliseconds, defaults to 100.

100
throw_on_timeout bool

Raise TimeoutError when the timeout is reached, defaults to False.

False
ignore_exception bool

Treat a ValueError / AssertionError from retry_method as a failed attempt and keep retrying, instead of re-raising it, defaults to False.

False
timeout_message Optional[str]

Message for the TimeoutError, defaults to None.

None
last_value_on_timeout bool

Accepted for signature parity; not used.

False
default_on_timeout Optional[T]

Accepted for signature parity; not used.

None

Returns:

Type Description
bool

True if the predicate became falsy in time, False if the timeout was reached.

Raises:

Type Description
TimeoutError

On timeout, only when throw_on_timeout is True.

Source code in flaui/core/tools.py
@staticmethod
def WhileTrue(  # pylint: disable=unused-argument  # last_value/default_on_timeout: C# signature parity
    retry_method: Callable[[], T],
    timeout: int = 1000,
    interval: int = 100,
    throw_on_timeout: bool = False,
    ignore_exception: bool = False,
    timeout_message: Optional[str] = None,
    last_value_on_timeout: bool = False,
    default_on_timeout: Optional[T] = None,
) -> bool:
    """Retry while ``retry_method`` returns a truthy value; succeed once it becomes falsy.

    Python equivalent of C# ``Retry.WhileTrue``, which returns a ``RetryResult<bool>``; this
    returns the boolean directly.

    Only ``ValueError`` and ``AssertionError`` are handled (see ``ignore_exception``). Any other
    exception, including :class:`~flaui.lib.exceptions.ElementNotFound`, propagates straight away;
    use :meth:`WhileException` to retry a lookup that raises until it succeeds.

    :param retry_method: Callable taking no arguments; it is called repeatedly.
    :param timeout: How long to keep retrying, in **milliseconds**, defaults to 1000.
    :param interval: Pause between attempts, in **milliseconds**, defaults to 100.
    :param throw_on_timeout: Raise ``TimeoutError`` when the timeout is reached, defaults to False.
    :param ignore_exception: Treat a ``ValueError`` / ``AssertionError`` from ``retry_method`` as a
        failed attempt and keep retrying, instead of re-raising it, defaults to False.
    :param timeout_message: Message for the ``TimeoutError``, defaults to None.
    :param last_value_on_timeout: Accepted for signature parity; not used.
    :param default_on_timeout: Accepted for signature parity; not used.
    :raises TimeoutError: On timeout, only when ``throw_on_timeout`` is True.
    :return: True if the predicate became falsy in time, False if the timeout was reached.
    """
    start = Retry._now_ms()
    while True:
        try:
            if not bool(retry_method()):
                return True
        except (ValueError, AssertionError):
            if not ignore_exception:
                raise
        if Retry._now_ms() - start >= timeout:
            if throw_on_timeout:
                raise TimeoutError(timeout_message or f"Timeout of {timeout} ms exceeded.")
            return False
        Retry._sleep_ms(interval)