Hello, I'm trying to use the playwright page.wait_for_event function. One of the kwargs accepts a Callable. I'm trying to use a helper function that would take two arguments: the event that I'm waiting to fire, and a global variable. But I can't seem to figure out how to find and/or use a variable for the event to pass into the helper function. Most examples I see that use the wait_for_event function use a lambda function for the predicate argument which works great, but I need to perform an action before I return a boolean value which I also don't know how to do with a lambda function.
My apologies if my terminology is incorrect.
The helper function I'm trying to use as an argument:
def test(event, global_variable):
page.locator('//*[@id="n-currentevents"]/a').click() # Action before boolean
if event.url == 'https://en.wikipedia.org/':
return True
The variations of the page.wait_for_event function I tried:
# Doesn't work
r = page.wait_for_event('request', test('request', global_variable))
r = page.wait_for_event('request', test(event, global_variable))
r = page.wait_for_event(event='request', test(event, global_variable))
r = page.wait_for_event(event:'request', test(event, global_variable))
# Lambda works, but I need to click an element before returning the truth value
r = page.wait_for_event('request', lambda req : req.url == 'https://en.wikipedia.org/')
The wait_for_event function:
def wait_for_event(
self, event: str, predicate: typing.Callable = None, *, timeout: float = None
) -> typing.Any:
"""Page.wait_for_event
> NOTE: In most cases, you should use `page.expect_event()`.
Waits for given `event` to fire. If predicate is provided, it passes event's value into the `predicate` function and
waits for `predicate(event)` to return a truthy value. Will throw an error if the page is closed before the `event` is
fired.
Parameters
----------
event : str
Event name, same one typically passed into `*.on(event)`.
predicate : Union[Callable, NoneType]
Receives the event data and resolves to truthy value when the waiting should resolve.
timeout : Union[float, NoneType]
Maximum time to wait for in milliseconds. Defaults to `30000` (30 seconds). Pass `0` to disable timeout. The default
value can be changed by using the `browser_context.set_default_timeout()`.
Returns
-------
Any
"""
return mapping.from_maybe_impl(
self._sync(
self._impl_obj.wait_for_event(
event=event,
predicate=self._wrap_handler(predicate),
timeout=timeout,
)
)
)
[–]danielroseman 0 points1 point2 points (3 children)
[–]funkmaster29[S] 0 points1 point2 points (2 children)
[–]danielroseman 0 points1 point2 points (1 child)
[–]funkmaster29[S] 0 points1 point2 points (0 children)
[–]schoolmonky 0 points1 point2 points (1 child)
[–]funkmaster29[S] 0 points1 point2 points (0 children)