diff --git a/README.md b/README.md index 41f865c..62726e0 100644 --- a/README.md +++ b/README.md @@ -28,23 +28,45 @@ The full API of this library can be found in [api.md](api.md). ```python import os +from playwright.sync_api import Playwright, sync_playwright from browserbase import Browserbase -client = Browserbase( +BROWSERBASE_API_KEY = os.environ.get("BROWSERBASE_API_KEY") +BROWSERBASE_PROJECT_ID = os.environ.get("BROWSERBASE_PROJECT_ID") + +bb = Browserbase( # This is the default and can be omitted - api_key=os.environ.get("BROWSERBASE_API_KEY"), + api_key=BROWSERBASE_API_KEY, ) -context = client.contexts.create( - project_id="projectId", -) -print(context.id) -``` +def run(playwright: Playwright) -> None: + # Create a session on Browserbase + session = bb.sessions.create(project_id=BROWSERBASE_PROJECT_ID) + + # Connect to the remote session + chromium = playwright.chromium + browser = chromium.connect_over_cdp(session.connect_url) + context = browser.contexts[0] + page = context.pages[0] -While you can provide an `api_key` keyword argument, -we recommend using [python-dotenv](https://pypi.org/project/python-dotenv/) -to add `BROWSERBASE_API_KEY="My API Key"` to your `.env` file -so that your API Key is not stored in source control. + # Execute Playwright actions on the remote browser tab + page.goto("https://news.ycombinator.com/") + page_title = page.title() + assert ( + page_title == "Hacker News" + ), f"Page title is not 'Hacker News', it is '{page_title}'" + page.screenshot(path="screenshot.png") + + page.close() + browser.close() + print("Done!") + + +if __name__ == "__main__": + with sync_playwright() as playwright: + run(playwright) + +``` ## Examples @@ -63,33 +85,6 @@ rye run example playwright_basic # replace with the example you want to run > [!NOTE] > Make sure you have a `.env` file that matches the [.env.example](.env.example) file in the root of this repository. -## Async usage - -Simply import `AsyncBrowserbase` instead of `Browserbase` and use `await` with each API call: - -```python -import os -import asyncio -from browserbase import AsyncBrowserbase - -client = AsyncBrowserbase( - # This is the default and can be omitted - api_key=os.environ.get("BROWSERBASE_API_KEY"), -) - - -async def main() -> None: - context = await client.contexts.create( - project_id="projectId", - ) - print(context.id) - - -asyncio.run(main()) -``` - -Functionality between the synchronous and asynchronous clients is otherwise identical. - ## Using types Nested request parameters are [TypedDicts](https://docs.python.org/3/library/typing.html#typing.TypedDict). Responses are [Pydantic models](https://docs.pydantic.dev) which also provide helper methods for things like: diff --git a/examples/playwright_basic.py b/examples/playwright_basic.py index ac05917..1ba9753 100644 --- a/examples/playwright_basic.py +++ b/examples/playwright_basic.py @@ -1,16 +1,25 @@ +import os + from playwright.sync_api import Playwright, sync_playwright -from examples import ( - BROWSERBASE_PROJECT_ID, - bb, +from browserbase import Browserbase + +BROWSERBASE_API_KEY = os.environ.get("BROWSERBASE_API_KEY", "") +if not BROWSERBASE_API_KEY: + raise ValueError("BROWSERBASE_API_KEY is not set") +BROWSERBASE_PROJECT_ID = os.environ.get("BROWSERBASE_PROJECT_ID", "") +if not BROWSERBASE_PROJECT_ID: + raise ValueError("BROWSERBASE_PROJECT_ID is not set") + +bb = Browserbase( + # This is the default and can be omitted + api_key=BROWSERBASE_API_KEY, ) def run(playwright: Playwright) -> None: # Create a session on Browserbase session = bb.sessions.create(project_id=BROWSERBASE_PROJECT_ID) - assert session.id is not None - assert session.status == "RUNNING", f"Session status is {session.status}" # Connect to the remote session chromium = playwright.chromium