Skip to content

Commit 19cf427

Browse files
authored
chore: print pretty exception after playwright.stop() (microsoft#1899)
1 parent 4d79dfc commit 19cf427

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

playwright/_impl/_sync_base.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
import greenlet
3434

35+
from playwright._impl._helper import Error
3536
from playwright._impl._impl_to_api_mapping import ImplToApiMapping, ImplWrapper
3637

3738
mapping = ImplToApiMapping()
@@ -92,6 +93,10 @@ def _sync(
9293
coro: Union[Coroutine[Any, Any, Any], Generator[Any, Any, Any]],
9394
) -> Any:
9495
__tracebackhide__ = True
96+
if self._loop.is_closed():
97+
coro.close()
98+
raise Error("Event loop is closed! Is Playwright already stopped?")
99+
95100
g_self = greenlet.getcurrent()
96101
task: asyncio.tasks.Task[Any] = self._loop.create_task(coro)
97102
setattr(task, "__pw_stack__", inspect.stack())

tests/sync/test_sync.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
import multiprocessing
1616
import os
17+
from typing import Any, Dict
1718

1819
import pytest
1920

@@ -304,3 +305,31 @@ def test_sync_playwright_stop_multiple_times() -> None:
304305
p.start()
305306
p.join()
306307
assert p.exitcode == 0
308+
309+
310+
def _test_call_sync_method_after_playwright_close_with_own_loop(
311+
browser_name: str,
312+
launch_arguments: Dict[str, Any],
313+
empty_page: str,
314+
) -> None:
315+
playwright = sync_playwright().start()
316+
browser = playwright[browser_name].launch(**launch_arguments)
317+
context = browser.new_context()
318+
page = context.new_page()
319+
page.goto(empty_page)
320+
playwright.stop()
321+
with pytest.raises(Error) as exc:
322+
page.evaluate("1+1")
323+
assert "Event loop is closed! Is Playwright already stopped?" in str(exc.value)
324+
325+
326+
def test_call_sync_method_after_playwright_close_with_own_loop(
327+
server: Server, browser_name: str, launch_arguments: Dict[str, Any]
328+
) -> None:
329+
p = multiprocessing.Process(
330+
target=_test_call_sync_method_after_playwright_close_with_own_loop,
331+
args=[browser_name, launch_arguments, server.EMPTY_PAGE],
332+
)
333+
p.start()
334+
p.join()
335+
assert p.exitcode == 0

0 commit comments

Comments
 (0)