playwright-python
playwright-python copied to clipboard
[Feature] Custom timeout for assertions
Upstream issue: https://github.com/microsoft/playwright/issues/12824
Please upvote this issue for the Python support.
please add this, I think this should be the configurable
For anyone stumbling upon this thread and looking for workaround until this is implemented, following code has worked for me:
def patch_playwright_assertions(config: _pytest.config.Config) -> None:
"""
Patch Playwright assertion timeout to match our configuration.
This is temporary solution until Playwright supports setting custom timeout for assertions.
Github issue: https://github.com/microsoft/playwright-python/issues/1358
:param config: pytest config fixture
"""
def patch_timeout(_member_obj: FunctionType) -> Callable:
def patch_timeout_inner(*args, **kwargs) -> Any:
parameters = inspect.signature(_member_obj).parameters
timeout_arg_index = list(parameters.keys()).index("timeout")
if timeout_arg_index >= 0:
if len(args) > timeout_arg_index:
args = list(args) # type: ignore
args[timeout_arg_index] = int(get_config(config, "timeout") or 30000) # type: ignore
elif "timeout" in kwargs:
kwargs["timeout"] = int(get_config(config, "timeout") or 30000)
return _member_obj(*args, **kwargs)
return patch_timeout_inner
for assertion_cls in [PageAssertions, LocatorAssertions, APIResponseAssertions]:
for member_name, member_obj in inspect.getmembers(assertion_cls):
if isinstance(member_obj, FunctionType):
if "timeout" in inspect.signature(member_obj).parameters:
setattr(assertion_cls, member_name, patch_timeout(member_obj))