You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When using the Playwright Python library, event callbacks registered with page.on() do not have the same contextvars as the parent code. This behavior can lead to issues when users expect shared context between the main code and event handlers.
It would be great if Playwright could propagate contextvars to event handlers by default to facilitate shared context.
Reproduction Code Sample:
This code demonstrates failure to get context var. Tested on Python 3.10, Playwright versions 1.31 and 1.29
importcontextvarsfromplaywright.sync_apiimportsync_playwrightshared_var=contextvars.ContextVar("shared_var")
defon_request(request):
try:
print("Shared value in callback:", shared_var.get())
exceptLookupError:
print("Failed to get shared value in callback")
defmain():
shared_var.set("some value")
withsync_playwright() asp:
browser=p.chromium.launch()
page=browser.new_page()
page.on("request", on_request)
# Perform some actions and trigger the eventpage.goto("https://example.com")
browser.close()
if__name__=="__main__":
main()
Context
This came up for me because I used context managers to annotate logs but my logging of network requests in Playwright were not getting annotated.
The text was updated successfully, but these errors were encountered:
+1 for this. I would like to implement similar feature to consolidate the logs under the same session/request through the same id stored within the contextvars.
When using the Playwright Python library, event callbacks registered with
page.on()
do not have the same contextvars as the parent code. This behavior can lead to issues when users expect shared context between the main code and event handlers.It would be great if Playwright could propagate contextvars to event handlers by default to facilitate shared context.
Reproduction Code Sample:
This code demonstrates failure to get context var. Tested on Python 3.10, Playwright versions 1.31 and 1.29
Context
This came up for me because I used context managers to annotate logs but my logging of network requests in Playwright were not getting annotated.
The text was updated successfully, but these errors were encountered: