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(returnsbool) - a value to become falsy: :meth:
WhileNotor :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 |
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
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 |
False
|
ignore_exception
|
bool
|
Treat a |
False
|
timeout_message
|
Optional[str]
|
Message for the |
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 |
Source code in flaui/core/tools.py
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 |
False
|
ignore_exception
|
bool
|
Treat a |
False
|
timeout_message
|
Optional[str]
|
Message for the |
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 |
Source code in flaui/core/tools.py
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 |
False
|
ignore_exception
|
bool
|
On timeout, raise |
False
|
timeout_message
|
Optional[str]
|
Message for the |
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 |
Exception
|
On timeout otherwise: the last exception |
Source code in flaui/core/tools.py
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 |
False
|
ignore_exception
|
bool
|
Treat a |
False
|
timeout_message
|
Optional[str]
|
Message for the |
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 |
Source code in flaui/core/tools.py
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 |
False
|
ignore_exception
|
bool
|
Treat a |
False
|
timeout_message
|
Optional[str]
|
Message for the |
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 |
Source code in flaui/core/tools.py
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 |
False
|
ignore_exception
|
bool
|
Treat a |
False
|
timeout_message
|
Optional[str]
|
Message for the |
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 |
Source code in flaui/core/tools.py
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 |
False
|
ignore_exception
|
bool
|
Treat a |
False
|
timeout_message
|
Optional[str]
|
Message for the |
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 |
Source code in flaui/core/tools.py
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 |
False
|
ignore_exception
|
bool
|
Treat a |
False
|
timeout_message
|
Optional[str]
|
Message for the |
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 |