Skip to content

Commit 3c7a9ba

Browse files
authored
fix: multiple Playwright instances (#106)
1 parent 02b6ea0 commit 3c7a9ba

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

playwright/main.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
from playwright.async_api import Playwright as AsyncPlaywright
2727
from playwright.connection import Connection
28-
from playwright.helper import not_installed_error
28+
from playwright.helper import Error, not_installed_error
2929
from playwright.object_factory import create_remote_object
3030
from playwright.playwright import Playwright
3131
from playwright.sync_api import Playwright as SyncPlaywright
@@ -72,7 +72,10 @@ async def run_driver_async() -> Connection:
7272

7373

7474
def 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

7881
class SyncPlaywrightContextManager:

tests/test_sync.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff 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

0 commit comments

Comments
 (0)