Skip to content

Commit 8cea075

Browse files
authored
enh: support for usage in a REPL (#161)
1 parent fc5485c commit 8cea075

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
@@ -89,6 +89,24 @@ def test_playwright_is_visible_on_google(page):
8989

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

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

94112
#### Mobile and geolocation

playwright/async_api.py

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

5945+
def stop(self) -> NoneType:
5946+
return mapping.from_maybe_impl(self._impl_obj.stop())
5947+
59455948

59465949
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 start(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 start(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
@@ -6182,5 +6182,8 @@ def selectors(self) -> "Selectors":
61826182
def devices(self) -> typing.Dict[str, DeviceDescriptor]:
61836183
return mapping.from_maybe_impl(self._impl_obj.devices)
61846184

6185+
def stop(self) -> NoneType:
6186+
return mapping.from_maybe_impl(self._impl_obj.stop())
6187+
61856188

61866189
mapping.register(PlaywrightImpl, Playwright)

0 commit comments

Comments
 (0)