14
14
15
15
import asyncio
16
16
from pathlib import Path
17
- from typing import Any , Awaitable , Callable , Dict , Generator , Optional
17
+ from typing import Any , AsyncGenerator , Awaitable , Callable , Dict , List , Optional
18
18
19
19
import pytest
20
20
24
24
25
25
26
26
@pytest .fixture ()
27
- def launch_persistent_context (
27
+ async def launch_persistent_context (
28
28
browser_type : BrowserType ,
29
29
browser_channel : Optional [str ],
30
30
tmp_path : Path ,
31
31
launch_arguments : Dict [str , Any ],
32
32
is_headless_shell : bool ,
33
- ) -> Generator [Callable [[ str ] , Awaitable [BrowserContext ]], None , None ]:
33
+ ) -> AsyncGenerator [Callable [... , Awaitable [BrowserContext ]], None ]:
34
34
if browser_channel and browser_channel .startswith ("chrome" ):
35
35
pytest .skip (
36
36
"--load-extension is not supported in Chrome anymore. https://groups.google.com/a/chromium.org/g/chromium-extensions/c/1-g8EFx2BBY/m/S0ET5wPjCAAJ"
37
37
)
38
38
if is_headless_shell :
39
39
pytest .skip ("Headless Shell has no support for extensions" )
40
40
41
+ contexts : List [BrowserContext ] = []
42
+
41
43
async def launch (extension_path : str , ** kwargs : Any ) -> BrowserContext :
42
44
context = await browser_type .launch_persistent_context (
43
45
str (tmp_path ),
@@ -48,23 +50,27 @@ async def launch(extension_path: str, **kwargs: Any) -> BrowserContext:
48
50
f"--load-extension={ extension_path } " ,
49
51
],
50
52
)
53
+ contexts .append (context )
51
54
return context
52
55
53
56
yield launch
54
57
58
+ for context in contexts :
59
+ await context .close ()
60
+
55
61
56
62
@pytest .mark .only_browser ("chromium" )
57
63
async def test_should_give_access_to_the_service_worker (
58
- launch_persistent_context : Any ,
64
+ launch_persistent_context : Callable [..., Awaitable [ BrowserContext ]] ,
59
65
assetdir : Path ,
60
66
) -> None :
61
67
extension_path = str (assetdir / "extension-mv3-simple" )
62
- context : BrowserContext = await launch_persistent_context (extension_path )
68
+ context = await launch_persistent_context (extension_path )
63
69
service_workers = context .service_workers
64
70
service_worker = (
65
71
service_workers [0 ]
66
72
if len (service_workers )
67
- else await context .wait_for_event ("backgroundpage " )
73
+ else await context .wait_for_event ("serviceworker " )
68
74
)
69
75
assert service_worker
70
76
assert service_worker in context .service_workers
@@ -76,19 +82,19 @@ async def test_should_give_access_to_the_service_worker(
76
82
77
83
@pytest .mark .only_browser ("chromium" )
78
84
async def test_should_give_access_to_the_service_worker_when_recording_video (
79
- launch_persistent_context : Any ,
85
+ launch_persistent_context : Callable [..., Awaitable [ BrowserContext ]] ,
80
86
tmp_path : Path ,
81
87
assetdir : Path ,
82
88
) -> None :
83
89
extension_path = str (assetdir / "extension-mv3-simple" )
84
- context : BrowserContext = await launch_persistent_context (
90
+ context = await launch_persistent_context (
85
91
extension_path , record_video_dir = (tmp_path / "videos" )
86
92
)
87
93
service_workers = context .service_workers
88
94
service_worker = (
89
95
service_workers [0 ]
90
96
if len (service_workers )
91
- else await context .wait_for_event ("backgroundpage " )
97
+ else await context .wait_for_event ("serviceworker " )
92
98
)
93
99
assert service_worker
94
100
assert service_worker in context .service_workers
@@ -101,12 +107,12 @@ async def test_should_give_access_to_the_service_worker_when_recording_video(
101
107
# https://github.com/microsoft/playwright/issues/32762
102
108
@pytest .mark .only_browser ("chromium" )
103
109
async def test_should_report_console_messages_from_content_script (
104
- launch_persistent_context : Any ,
110
+ launch_persistent_context : Callable [..., Awaitable [ BrowserContext ]] ,
105
111
assetdir : Path ,
106
112
server : Server ,
107
113
) -> None :
108
114
extension_path = str (assetdir / "extension-mv3-with-logging" )
109
- context : BrowserContext = await launch_persistent_context (extension_path )
115
+ context = await launch_persistent_context (extension_path )
110
116
page = await context .new_page ()
111
117
[message , _ ] = await asyncio .gather (
112
118
page .context .wait_for_event (
0 commit comments