playwright-python icon indicating copy to clipboard operation
playwright-python copied to clipboard

[Feature] Custom timeout for assertions

Open mxschmitt opened this issue 3 years ago • 1 comments

Upstream issue: https://github.com/microsoft/playwright/issues/12824

Please upvote this issue for the Python support.

mxschmitt avatar Jun 14 '22 09:06 mxschmitt

please add this, I think this should be the configurable

snackattas avatar Aug 22 '22 22:08 snackattas

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))

paunovic avatar Oct 08 '22 11:10 paunovic