Skip to content

Commit 835cdce

Browse files
authored
test: add test for extension testing (microsoft#695)
1 parent 9534c38 commit 835cdce

File tree

5 files changed

+59
-6
lines changed

5 files changed

+59
-6
lines changed

playwright/_impl/_browser_context.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ def __init__(
6969
self._timeout_settings = TimeoutSettings(None)
7070
self._browser: Optional["Browser"] = None
7171
self._owner_page: Optional[Page] = None
72-
self._is_closed_or_closing = False
7372
self._options: Dict[str, Any] = {}
7473
self._background_pages: Set[Page] = set()
7574
self._service_workers: Set[Worker] = set()
@@ -129,6 +128,8 @@ def __init__(
129128
from_nullable_channel(params.get("page")),
130129
),
131130
)
131+
self._closed_future: asyncio.Future = asyncio.Future()
132+
self.once(self.Events.Close, lambda: self._closed_future.set_result(True))
132133

133134
def __repr__(self) -> str:
134135
return f"<BrowserContext browser={self.browser}>"
@@ -278,18 +279,15 @@ def expect_event(
278279
return EventContextManagerImpl(wait_helper.result())
279280

280281
def _on_close(self) -> None:
281-
self._is_closed_or_closing = True
282282
if self._browser:
283283
self._browser._contexts.remove(self)
284284

285285
self.emit(BrowserContext.Events.Close)
286286

287287
async def close(self) -> None:
288-
if self._is_closed_or_closing:
289-
return
290-
self._is_closed_or_closing = True
291288
try:
292289
await self._channel.send("close")
290+
await self._closed_future
293291
except Exception as e:
294292
if not is_safe_close_error(e):
295293
raise e
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
console.log('hey from the content-script');
2+
self.thisIsTheContentScript = true;
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// Mock script for background extension
2+
window.MAGIC = 42;
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "Simple extension",
3+
"version": "0.1",
4+
"background": {
5+
"scripts": ["index.js"]
6+
},
7+
"content_scripts": [{
8+
"matches": ["<all_urls>"],
9+
"css": [],
10+
"js": ["content-script.js"]
11+
}],
12+
"permissions": ["background", "activeTab"],
13+
"manifest_version": 2
14+
}

tests/async/test_launcher.py

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ async def test_browser_type_launch_should_reject_if_launched_browser_fails_immed
4747
with pytest.raises(Error):
4848
await browser_type.launch(
4949
**launch_arguments,
50-
executable_path=assetdir / "dummy_bad_browser_executable.js"
50+
executable_path=assetdir / "dummy_bad_browser_executable.js",
5151
)
5252

5353

@@ -109,3 +109,40 @@ async def test_browser_launch_non_existing_executable_path_shows_install_msg(
109109
with pytest.raises(Error) as exc_info:
110110
await browser_type.launch(executable_path=tmpdir.join("executable"))
111111
assert "python -m playwright install" in exc_info.value.message
112+
113+
114+
@pytest.mark.only_browser("chromium")
115+
async def test_browser_launch_should_return_background_pages(
116+
browser_type: BrowserType,
117+
tmpdir,
118+
browser_channel,
119+
assetdir,
120+
launch_arguments,
121+
):
122+
if browser_channel:
123+
pytest.skip()
124+
125+
extension_path = str(assetdir / "simple-extension")
126+
context = await browser_type.launch_persistent_context(
127+
str(tmpdir),
128+
**{
129+
**launch_arguments,
130+
"headless": False,
131+
"args": [
132+
f"--disable-extensions-except={extension_path}",
133+
f"--load-extension={extension_path}",
134+
],
135+
}, # type: ignore
136+
)
137+
background_page = None
138+
if len(context.background_pages):
139+
background_page = context.background_pages[0]
140+
else:
141+
background_page = await context.wait_for_event("backgroundpage")
142+
assert background_page
143+
assert background_page in context.background_pages
144+
assert background_page not in context.pages
145+
assert await background_page.evaluate("window.MAGIC") == 42
146+
await context.close()
147+
assert len(context.background_pages) == 0
148+
assert len(context.pages) == 0

0 commit comments

Comments
 (0)