2121
2222import pytest
2323from e2e_utils import APP_CONFIGS , StreamlitRunner
24- from playwright .sync_api import Browser , BrowserContext , Page
24+ from playwright .sync_api import Page
2525
2626
27- @pytest .fixture (scope = "function" )
28- def context (browser : Browser ) -> Generator [BrowserContext , None , None ]:
29- """Create a new browser context with custom settings."""
30- context_options : dict [str , Any ] = {
27+ @pytest .fixture (scope = "session" )
28+ def browser_context_args (
29+ browser_context_args : dict [str , Any ],
30+ browser_name : str ,
31+ ) -> dict [str , Any ]:
32+ """Extend pytest-playwright context args without replacing its context fixture."""
33+ permissions = list (browser_context_args .get ("permissions" , []))
34+ if browser_name == "chromium" :
35+ permissions .extend (["clipboard-read" , "clipboard-write" ])
36+
37+ context_args = {
38+ ** browser_context_args ,
3139 "accept_downloads" : True ,
32- "service_workers" : "block"
33- if browser .browser_type .name == "chromium"
34- else "allow" ,
40+ "service_workers" : "block" if browser_name == "chromium" else "allow" ,
3541 }
36- if browser .browser_type .name == "chromium" :
37- context_options ["permissions" ] = ["clipboard-read" , "clipboard-write" ]
38-
39- context = browser .new_context (** context_options )
40- context .set_default_timeout (30000 )
41-
42- yield context
43- context .close ()
42+ if permissions :
43+ context_args ["permissions" ] = sorted (set (permissions ))
44+ return context_args
4445
4546
46- @pytest .fixture (scope = "function" )
47- def page ( context : BrowserContext ) -> Generator [Page , None , None ]:
48- """Create a new page with console/error logging for debugging ."""
49- page = context .new_page ( )
47+ @pytest .fixture (scope = "function" , autouse = True )
48+ def instrument_page ( page : Page ) -> Generator [None , None , None ]:
49+ """Add console/error logging while keeping plugin-managed page lifecycle ."""
50+ page . context .set_default_timeout ( 30000 )
5051
5152 def handle_console (msg ):
5253 if msg .type == "error" :
@@ -59,8 +60,7 @@ def handle_console(msg):
5960 page .on ("console" , handle_console )
6061 page .on ("pageerror" , lambda err : print (f"[PAGE ERROR] { err } " ))
6162
62- yield page
63- page .close ()
63+ yield
6464
6565
6666@pytest .fixture (scope = "module" )
@@ -77,7 +77,7 @@ def app(request):
7777@pytest .fixture
7878def page_at_app (app , page : Page ):
7979 """Navigate to the app and wait for it to be ready."""
80- page . _pivot_keys = app .pivot_keys
80+ setattr ( page , " _pivot_keys" , app .pivot_keys )
8181 page .goto (app .server_url )
8282 page .wait_for_selector ("text=Pivot Table E2E Test App" , timeout = 30000 )
8383 page .add_style_tag (
0 commit comments