Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,24 @@ def test_playwright_is_visible_on_google(page):

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

#### REPL support without context managers

For scripting purposes, it is also possible to start and stop Playwright manually without relying on the indentation of the REPL.

```py
from playwright import sync_playwright

playwright = sync_playwright().start()
for browser_type in [playwright.chromium, playwright.firefox, playwright.webkit]:
browser = browser_type.launch()
page = browser.newPage()
page.goto("http://whatsmyuseragent.org/")
page.screenshot(path=f"example-{browser_type.name}.png")
browser.close()

playwright.stop()
```

## More examples

#### Mobile and geolocation
Expand Down
3 changes: 3 additions & 0 deletions playwright/async_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -5942,5 +5942,8 @@ def selectors(self) -> "Selectors":
def devices(self) -> typing.Dict[str, DeviceDescriptor]:
return mapping.from_maybe_impl(self._impl_obj.devices)

def stop(self) -> NoneType:
return mapping.from_maybe_impl(self._impl_obj.stop())


mapping.register(PlaywrightImpl, Playwright)
18 changes: 14 additions & 4 deletions playwright/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,14 @@ def callback_wrapper(playwright_impl: Playwright) -> None:
self._connection.call_on_object_with_known_name("Playwright", callback_wrapper)
set_dispatcher_fiber(greenlet(lambda: self._connection.run_sync()))
dispatcher_fiber().switch()
return self._playwright
playwright = self._playwright
playwright.stop = self.__exit__ # type: ignore
return playwright

def __exit__(self, exc_type: Any, exc_val: Any, exc_tb: Any) -> None:
def start(self) -> SyncPlaywright:
return self.__enter__()

def __exit__(self, *args: Any) -> None:
self._connection.stop_sync()


Expand All @@ -106,11 +111,16 @@ def __init__(self) -> None:
async def __aenter__(self) -> AsyncPlaywright:
self._connection = await run_driver_async()
self._connection.run_async()
return AsyncPlaywright(
playwright = AsyncPlaywright(
await self._connection.wait_for_object_with_known_name("Playwright")
)
playwright.stop = self.__aexit__ # type: ignore
return playwright

async def start(self) -> AsyncPlaywright:
return await self.__aenter__()

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


Expand Down
3 changes: 3 additions & 0 deletions playwright/playwright.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,6 @@ def __init__(
device["name"]: device["descriptor"]
for device in initializer["deviceDescriptors"]
}

def stop(self) -> None:
pass
3 changes: 3 additions & 0 deletions playwright/sync_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -6182,5 +6182,8 @@ def selectors(self) -> "Selectors":
def devices(self) -> typing.Dict[str, DeviceDescriptor]:
return mapping.from_maybe_impl(self._impl_obj.devices)

def stop(self) -> NoneType:
return mapping.from_maybe_impl(self._impl_obj.stop())


mapping.register(PlaywrightImpl, Playwright)