File tree Expand file tree Collapse file tree 2 files changed +14
-2
lines changed Expand file tree Collapse file tree 2 files changed +14
-2
lines changed Original file line number Diff line number Diff line change 2525
2626from playwright .async_api import Playwright as AsyncPlaywright
2727from playwright .connection import Connection
28- from playwright .helper import not_installed_error
28+ from playwright .helper import Error , not_installed_error
2929from playwright .object_factory import create_remote_object
3030from playwright .playwright import Playwright
3131from playwright .sync_api import Playwright as SyncPlaywright
@@ -72,7 +72,10 @@ async def run_driver_async() -> Connection:
7272
7373
7474def run_driver () -> Connection :
75- return asyncio .get_event_loop ().run_until_complete (run_driver_async ())
75+ loop = asyncio .get_event_loop ()
76+ if loop .is_running ():
77+ raise Error ("Can only run one Playwright at a time." )
78+ return loop .run_until_complete (run_driver_async ())
7679
7780
7881class SyncPlaywrightContextManager :
Original file line number Diff line number Diff line change @@ -205,3 +205,12 @@ def test_sync_workers_page_workers(page: Page, server):
205205
206206 page .goto (server .EMPTY_PAGE )
207207 assert len (page .workers ) == 0
208+
209+
210+ def test_sync_playwright_multiple_times ():
211+ with sync_playwright () as pw1 :
212+ assert pw1 .chromium
213+ with pytest .raises (Error ) as exc :
214+ with sync_playwright () as pw2 :
215+ assert pw1 .chromium == pw2 .chromium
216+ assert "Can only run one Playwright at a time." in exc .value .message
You can’t perform that action at this time.
0 commit comments