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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@ dist
.envrc
codegen.log
Brewfile.lock.json
screenshot.png
openapi.v1.yaml
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,18 @@ so that your API Key is not stored in source control.

See the [examples](examples) directory for more usage examples.

> [!NOTE]
> Running the examples requires [Rye](https://rye.astral.sh/) to be installed.

To run the examples, clone this repository and run the following commands from the project root (this directory):

```bash
python3 -m venv .venv
source .venv/bin/activate
python3 -m pip install .
python3 -m examples/02_create_session.py # replace with the example you want to run
rye sync
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.
> [!NOTE]
> Make sure you have a `.env` file that matches the [.env.example](.env.example) file in the root of this repository.

## Async usage

Expand Down
28 changes: 0 additions & 28 deletions examples/01_quickstart.py

This file was deleted.

44 changes: 0 additions & 44 deletions examples/02_create_session.py

This file was deleted.

24 changes: 24 additions & 0 deletions examples/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import os

from dotenv import load_dotenv

from browserbase import Browserbase

# Load our environment variables
load_dotenv(override=True)

# Make sure we have the required environment variables
BROWSERBASE_CONNECT_URL = os.environ.get(
"BROWSERBASE_CONNECT_URL", "wss://connect.browserbase.com"
)
_BROWSERBASE_API_KEY = os.environ.get("BROWSERBASE_API_KEY")
if not _BROWSERBASE_API_KEY:
raise ValueError("BROWSERBASE_API_KEY is not set in environment")
BROWSERBASE_API_KEY: str = _BROWSERBASE_API_KEY
_BROWSERBASE_PROJECT_ID = os.environ.get("BROWSERBASE_PROJECT_ID")
if not _BROWSERBASE_PROJECT_ID:
raise ValueError("BROWSERBASE_PROJECT_ID is not set in environment")
BROWSERBASE_PROJECT_ID = _BROWSERBASE_PROJECT_ID or ""

# Instantiate our Browserbase client
bb = Browserbase(api_key=BROWSERBASE_API_KEY)
26 changes: 26 additions & 0 deletions examples/e2e/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# End-to-end tests

This directory contains end-to-end tests that run against a real Browserbase instance.

## Running the tests

To run the tests, you will need to set the following environment variables:

- `BROWSERBASE_API_KEY`: Your Browserbase API key
- `BROWSERBASE_PROJECT_ID`: The ID of the project you want to use for the tests

You can set these variables in a `.env` file in the root of this directory.

Then, run the tests with:

```sh
$ rye run test:e2e
```

## Writing tests

The tests are written using pytest and the [pytest-playwright](https://playwright.dev/python/docs/pytest) plugin.

You can find more information about writing tests in the [pytest documentation](https://docs.pytest.org/en/7.1.x/).

To submit a test, create a new file in the `e2e` directory with a name that describes the test and starts with `test_`.
1 change: 1 addition & 0 deletions examples/e2e/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

21 changes: 21 additions & 0 deletions examples/e2e/test_playwright_basic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import pytest
from playwright.sync_api import Playwright, sync_playwright

from browserbase import Browserbase

from .. import (
BROWSERBASE_API_KEY,
playwright_basic,
)

bb = Browserbase(api_key=BROWSERBASE_API_KEY)


@pytest.fixture(scope="session")
def playwright():
with sync_playwright() as p:
yield p


def test_playwright_basic(playwright: Playwright):
playwright_basic.run(playwright)
41 changes: 41 additions & 0 deletions examples/playwright_basic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
from playwright.sync_api import Playwright, sync_playwright

from examples import (
BROWSERBASE_API_KEY,
BROWSERBASE_PROJECT_ID,
BROWSERBASE_CONNECT_URL,
bb,
)


def run(playwright: Playwright):
# 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
connect_url = (
f"{BROWSERBASE_CONNECT_URL}?sessionId={session.id}&apiKey={BROWSERBASE_API_KEY}"
)
chromium = playwright.chromium
browser = chromium.connect_over_cdp(connect_url)
context = browser.contexts[0]
page = context.pages[0]

# 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)
8 changes: 8 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,15 @@ dev-dependencies = [
"respx",
"pytest",
"pytest-asyncio",
"pytest-playwright",
"ruff",
"time-machine",
"nox",
"dirty-equals>=0.6.0",
"importlib-metadata>=6.7.0",
"rich>=13.7.1",
"python-dotenv",
"playwright",
]

[tool.rye.scripts]
Expand All @@ -79,10 +82,15 @@ format = { chain = [

"check:importable" = "python -c 'import browserbase'"

"example" = "python -c 'import sys; from pathlib import Path; example = Path(\"examples\") / (sys.argv[1] + \".py\"); exec(open(example).read())'"

"test:e2e" = "python -m pytest examples/e2e"

typecheck = { chain = [
"typecheck:pyright",
"typecheck:mypy"
]}

"typecheck:pyright" = "pyright"
"typecheck:verify-types" = "pyright --verifytypes browserbase --ignoreexternal"
"typecheck:mypy" = "mypy ."
Expand Down
26 changes: 26 additions & 0 deletions requirements-dev.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
# all-features: true
# with-sources: false
# generate-hashes: false
# universal: false

-e file:.
annotated-types==0.6.0
Expand All @@ -21,6 +22,9 @@ attrs==23.1.0
certifi==2023.7.22
# via httpcore
# via httpx
# via requests
charset-normalizer==3.4.0
# via requests
colorlog==6.7.0
# via nox
dirty-equals==0.6.0
Expand All @@ -32,6 +36,8 @@ exceptiongroup==1.1.3
# via anyio
filelock==3.12.4
# via virtualenv
greenlet==3.1.1
# via playwright
h11==0.14.0
# via httpcore
httpcore==1.0.2
Expand All @@ -42,6 +48,7 @@ httpx==0.25.2
idna==3.4
# via anyio
# via httpx
# via requests
importlib-metadata==7.0.0
iniconfig==2.0.0
# via pytest
Expand All @@ -60,6 +67,8 @@ packaging==23.2
# via pytest
platformdirs==3.11.0
# via virtualenv
playwright==1.48.0
# via pytest-playwright
pluggy==1.3.0
# via pytest
py==1.11.0
Expand All @@ -68,16 +77,28 @@ pydantic==2.7.1
# via browserbase
pydantic-core==2.18.2
# via pydantic
pyee==12.0.0
# via playwright
pygments==2.18.0
# via rich
pyright==1.1.380
pytest==7.1.1
# via pytest-asyncio
# via pytest-base-url
# via pytest-playwright
pytest-asyncio==0.21.1
pytest-base-url==2.1.0
# via pytest-playwright
pytest-playwright==0.5.2
python-dateutil==2.8.2
# via time-machine
python-dotenv==1.0.1
python-slugify==8.0.4
# via pytest-playwright
pytz==2023.3.post1
# via dirty-equals
requests==2.32.3
# via pytest-base-url
respx==0.20.2
rich==13.7.1
ruff==0.6.9
Expand All @@ -89,6 +110,8 @@ sniffio==1.3.0
# via anyio
# via browserbase
# via httpx
text-unidecode==1.3
# via python-slugify
time-machine==2.9.0
tomli==2.0.1
# via mypy
Expand All @@ -99,6 +122,9 @@ typing-extensions==4.8.0
# via mypy
# via pydantic
# via pydantic-core
# via pyee
urllib3==2.2.3
# via requests
virtualenv==20.24.5
# via nox
zipp==3.17.0
Expand Down
1 change: 1 addition & 0 deletions requirements.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
# all-features: true
# with-sources: false
# generate-hashes: false
# universal: false

-e file:.
annotated-types==0.6.0
Expand Down
Loading