Skip to content

Commit 36e3e1c

Browse files
committed
enh: support for usage in a REPL
1 parent 05e48f6 commit 36e3e1c

File tree

5 files changed

+41
-4
lines changed

5 files changed

+41
-4
lines changed

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,24 @@ def test_playwright_is_visible_on_google(page):
9090

9191
For more information on pytest-playwright, see [GitHub](https://github.com/microsoft/playwright-pytest#readme).
9292

93+
#### REPL support without context managers
94+
95+
For scripting purposes, it is also possible to start and stop Playwright manually without relying on the indentation of the REPL.
96+
97+
```py
98+
from playwright import sync_playwright
99+
100+
playwright = sync_playwright().get()
101+
for browser_type in [playwright.chromium, playwright.firefox, playwright.webkit]:
102+
browser = browser_type.launch()
103+
page = browser.newPage()
104+
page.goto("http://whatsmyuseragent.org/")
105+
page.screenshot(path=f"example-{browser_type.name}.png")
106+
browser.close()
107+
108+
playwright.stop()
109+
```
110+
93111
## More examples
94112

95113
#### Mobile and geolocation

playwright/async_api.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5924,5 +5924,8 @@ def selectors(self) -> "Selectors":
59245924
def devices(self) -> typing.Dict[str, DeviceDescriptor]:
59255925
return mapping.from_maybe_impl(self._impl_obj.devices)
59265926

5927+
def stop(self) -> NoneType:
5928+
return mapping.from_maybe_impl(self._impl_obj.stop())
5929+
59275930

59285931
mapping.register(PlaywrightImpl, Playwright)

playwright/main.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,14 @@ def callback_wrapper(playwright_impl: Playwright) -> None:
9393
self._connection.call_on_object_with_known_name("Playwright", callback_wrapper)
9494
set_dispatcher_fiber(greenlet(lambda: self._connection.run_sync()))
9595
dispatcher_fiber().switch()
96-
return self._playwright
96+
playwright = self._playwright
97+
playwright.stop = self.__exit__ # type: ignore
98+
return playwright
9799

98-
def __exit__(self, exc_type: Any, exc_val: Any, exc_tb: Any) -> None:
100+
def get(self) -> SyncPlaywright:
101+
return self.__enter__()
102+
103+
def __exit__(self, *args: Any) -> None:
99104
self._connection.stop_sync()
100105

101106

@@ -106,11 +111,16 @@ def __init__(self) -> None:
106111
async def __aenter__(self) -> AsyncPlaywright:
107112
self._connection = await run_driver_async()
108113
self._connection.run_async()
109-
return AsyncPlaywright(
114+
playwright = AsyncPlaywright(
110115
await self._connection.wait_for_object_with_known_name("Playwright")
111116
)
117+
playwright.stop = self.__aexit__ # type: ignore
118+
return playwright
119+
120+
async def get(self) -> AsyncPlaywright:
121+
return await self.__aenter__()
112122

113-
async def __aexit__(self, exc_type: Any, exc_val: Any, exc_tb: Any) -> None:
123+
async def __aexit__(self, *args: Any) -> None:
114124
self._connection.stop_async()
115125

116126

playwright/playwright.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,6 @@ def __init__(
3939
device["name"]: device["descriptor"]
4040
for device in initializer["deviceDescriptors"]
4141
}
42+
43+
def stop(self) -> None:
44+
pass

playwright/sync_api.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6164,5 +6164,8 @@ def selectors(self) -> "Selectors":
61646164
def devices(self) -> typing.Dict[str, DeviceDescriptor]:
61656165
return mapping.from_maybe_impl(self._impl_obj.devices)
61666166

6167+
def stop(self) -> NoneType:
6168+
return mapping.from_maybe_impl(self._impl_obj.stop())
6169+
61676170

61686171
mapping.register(PlaywrightImpl, Playwright)

0 commit comments

Comments
 (0)