Skip to content

Commit 74e6d51

Browse files
committed
basic selenium
1 parent 5fe1b29 commit 74e6d51

File tree

4 files changed

+42
-33
lines changed

4 files changed

+42
-33
lines changed

examples/e2e/test_playwright.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@
2020
load_dotenv()
2121

2222
CI = os.getenv("CI", "false").lower() == "true"
23+
MAX_RETRIES = 3
2324

2425

25-
@pytest.fixture(scope="session")
26+
@pytest.fixture(scope="function") # Changed from "session" to "function"
2627
def playwright() -> Generator[Playwright, None, None]:
2728
with sync_playwright() as p:
2829
yield p

examples/e2e/test_selenium.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from .. import selenium_basic
2+
3+
4+
def test_selenium_basic() -> None:
5+
selenium_basic.run()

examples/playwright_contexts.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import time
22
from typing import Optional
3+
from pydantic import TypeAdapter
34

45
from playwright.sync_api import Cookie, Browser, Playwright, sync_playwright
56

@@ -47,10 +48,11 @@ def run(playwright: Playwright) -> None:
4748
# Step 2: Creates a session with the context
4849
session = bb.sessions.create(
4950
project_id=BROWSERBASE_PROJECT_ID,
50-
browser_settings=BrowserSettings(
51-
context=BrowserSettingsContext(id=context_id, persist=True),
51+
browser_settings=TypeAdapter(BrowserSettings).validate_python(
52+
{"context": {"id": context_id, "persist": True}}
5253
),
5354
)
55+
print(session)
5456

5557
assert (
5658
session.context_id == context_id

examples/selenium_basic.py

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
from examples import bb, BROWSERBASE_PROJECT_ID, BROWSERBASE_API_KEY
2-
from browserbase.types import SessionCreateResponse
32
from selenium import webdriver
4-
from selenium.webdriver.remote.webdriver import WebDriver
53
from selenium.webdriver.remote.remote_connection import RemoteConnection
4+
from typing import Dict
65

76

87
class BrowserbaseConnection(RemoteConnection):
98
"""
109
Manage a single session with Browserbase.
1110
"""
1211

13-
browserbase_session: SessionCreateResponse
1412
session_id: str
1513

16-
def __init__(self, session_id: str, *args, **kwargs):
17-
super().__init__(*args, **kwargs)
14+
def __init__(self, session_id: str, *args, **kwargs): # type: ignore
15+
super().__init__(*args, **kwargs) # type: ignore
1816
self.session_id = session_id
1917

20-
def get_remote_connection_headers(self, parsed_url, keep_alive=False):
21-
headers = super().get_remote_connection_headers(parsed_url, keep_alive)
18+
def get_remote_connection_headers( # type: ignore
19+
self, parsed_url: str, keep_alive: bool = False
20+
) -> Dict[str, str]:
21+
headers = super().get_remote_connection_headers(parsed_url, keep_alive) # type: ignore
2222

2323
# Update headers to include the Browserbase required information
2424
headers["x-bb-api-key"] = BROWSERBASE_API_KEY
@@ -27,31 +27,32 @@ def get_remote_connection_headers(self, parsed_url, keep_alive=False):
2727
return headers
2828

2929

30-
def run(driver: WebDriver):
31-
# Instruct the browser to go to the SF MOMA page
32-
driver.get("https://www.sfmoma.org")
30+
def run():
31+
# Use the custom class to create and connect to a new browser session
32+
session = bb.sessions.create(project_id=BROWSERBASE_PROJECT_ID)
33+
connection = BrowserbaseConnection(session.id, session.selenium_remote_url)
34+
driver = webdriver.Remote(
35+
command_executor=connection, options=webdriver.ChromeOptions() # type: ignore
36+
)
3337

34-
# Print out a bit of info about the page it landed on
35-
print(f"{driver.current_url=} | {driver.title=}")
38+
# Print a bit of info about the browser we've connected to
39+
print(
40+
"Connected to Browserbase",
41+
f"{driver.name} version {driver.caps['browserVersion']}", # type: ignore
42+
)
3643

37-
...
44+
try:
45+
# Perform our browser commands
46+
driver.get("https://www.sfmoma.org")
47+
print(f"At URL: {driver.current_url} | Title: {driver.title}")
48+
assert driver.current_url == "https://www.sfmoma.org/"
49+
assert driver.title == "SFMOMA"
3850

51+
finally:
52+
# Make sure to quit the driver so your session is ended!
53+
print("Quitting driver")
54+
# driver.quit()
3955

40-
# Use the custom class to create and connect to a new browser session
41-
session = bb.sessions.create(project_id=BROWSERBASE_PROJECT_ID)
42-
connection = BrowserbaseConnection(session.id, session.selenium_remote_url)
43-
driver = webdriver.Remote(connection, options=webdriver.ChromeOptions())
4456

45-
# Print a bit of info about the browser we've connected to
46-
print(
47-
"Connected to Browserbase",
48-
f"{driver.name} version {driver.caps['browserVersion']}",
49-
)
50-
51-
try:
52-
# Perform our browser commands
53-
run(driver)
54-
55-
finally:
56-
# Make sure to quit the driver so your session is ended!
57-
driver.quit()
57+
if __name__ == "__main__":
58+
run()

0 commit comments

Comments
 (0)