File tree Expand file tree Collapse file tree 8 files changed +110
-67
lines changed
Expand file tree Collapse file tree 8 files changed +110
-67
lines changed Original file line number Diff line number Diff line change 1515codegen.log
1616Brewfile.lock.json
1717screenshot.png
18+ openapi.v1.yaml
Load Diff This file was deleted.
Original file line number Diff line number Diff line change 1+ import os
2+
3+ from dotenv import load_dotenv
4+
5+ from browserbase import Browserbase
6+
7+ # Load our environment variables
8+ load_dotenv (override = True )
9+
10+ # Make sure we have the required environment variables
11+ BROWSERBASE_CONNECT_URL = os .environ .get (
12+ "BROWSERBASE_CONNECT_URL" , "wss://connect.browserbase.com"
13+ )
14+ _BROWSERBASE_API_KEY = os .environ .get ("BROWSERBASE_API_KEY" )
15+ if not _BROWSERBASE_API_KEY :
16+ raise ValueError ("BROWSERBASE_API_KEY is not set in environment" )
17+ BROWSERBASE_API_KEY : str = _BROWSERBASE_API_KEY
18+ _BROWSERBASE_PROJECT_ID = os .environ .get ("BROWSERBASE_PROJECT_ID" )
19+ if not _BROWSERBASE_PROJECT_ID :
20+ raise ValueError ("BROWSERBASE_PROJECT_ID is not set in environment" )
21+ BROWSERBASE_PROJECT_ID = _BROWSERBASE_PROJECT_ID or ""
22+
23+ # Instantiate our Browserbase client
24+ bb = Browserbase (api_key = BROWSERBASE_API_KEY )
Original file line number Diff line number Diff line change 1+ # End-to-end tests
2+
3+ This directory contains end-to-end tests that run against a real Browserbase instance.
4+
5+ ## Running the tests
6+
7+ To run the tests, you will need to set the following environment variables:
8+
9+ - ` BROWSERBASE_API_KEY ` : Your Browserbase API key
10+ - ` BROWSERBASE_PROJECT_ID ` : The ID of the project you want to use for the tests
11+
12+ You can set these variables in a ` .env ` file in the root of this directory.
13+
14+ Then, run the tests with:
15+
16+ ``` sh
17+ $ rye run test:e2e
18+ ```
19+
20+ ## Writing tests
21+
22+ The tests are written using pytest and the [ pytest-playwright] ( https://playwright.dev/python/docs/pytest ) plugin.
23+
24+ You can find more information about writing tests in the [ pytest documentation] ( https://docs.pytest.org/en/7.1.x/ ) .
25+
26+ To submit a test, create a new file in the ` e2e ` directory with a name that describes the test and starts with ` test_ ` .
Original file line number Diff line number Diff line change 1+
Original file line number Diff line number Diff line change 1+ import pytest
2+ from playwright .sync_api import Playwright , sync_playwright
3+
4+ from browserbase import Browserbase
5+
6+ from .. import (
7+ BROWSERBASE_API_KEY ,
8+ playwright_basic ,
9+ )
10+
11+ bb = Browserbase (api_key = BROWSERBASE_API_KEY )
12+
13+
14+ @pytest .fixture (scope = "session" )
15+ def playwright ():
16+ with sync_playwright () as p :
17+ yield p
18+
19+
20+ def test_create_session (playwright : Playwright ):
21+ page = playwright_basic .run (playwright )
22+ assert page ["title" ] == "Hacker News"
Original file line number Diff line number Diff line change 1+ from playwright .sync_api import Playwright , sync_playwright
2+
3+ from examples import (BROWSERBASE_API_KEY , BROWSERBASE_CONNECT_URL ,
4+ BROWSERBASE_PROJECT_ID , bb )
5+
6+
7+ def run (playwright : Playwright ):
8+ # Create a session on Browserbase
9+ session = bb .sessions .create (project_id = BROWSERBASE_PROJECT_ID )
10+
11+ # Connect to the remote session
12+ connect_url = (
13+ f"{ BROWSERBASE_CONNECT_URL } ?sessionId={ session .id } &apiKey={ BROWSERBASE_API_KEY } "
14+ )
15+ chromium = playwright .chromium
16+ browser = chromium .connect_over_cdp (connect_url )
17+ context = browser .contexts [0 ]
18+ page = context .pages [0 ]
19+
20+ # Execute Playwright actions on the remote browser tab
21+ page .goto ("https://news.ycombinator.com/" )
22+ assert page .title == "Hacker News"
23+ page .screenshot (path = "screenshot.png" )
24+
25+ updated_session = bb .sessions .retrieve (session .id )
26+ print (updated_session .)
27+
28+ page .close ()
29+ browser .close ()
30+
31+
32+ if __name__ == "__main__" :
33+ with sync_playwright () as playwright :
34+ run (playwright )
Original file line number Diff line number Diff line change @@ -82,9 +82,9 @@ format = { chain = [
8282
8383"check:importable" = " python -c 'import browserbase'"
8484
85- "example" = " python -c 'import sys; from pathlib import Path; example = Path(\" examples\" ) / sys.argv[1]; exec(open(example).read())'"
85+ "example" = " python -c 'import sys; from pathlib import Path; example = Path(\" examples\" ) / ( sys.argv[1] + \" .py \" ) ; exec(open(example).read())'"
8686
87- "test:e2e" = " python -m pytest e2e"
87+ "test:e2e" = " python -m pytest examples/ e2e"
8888
8989typecheck = { chain = [
9090 " typecheck:pyright" ,
You can’t perform that action at this time.
0 commit comments