1
1
from examples import bb , BROWSERBASE_PROJECT_ID , BROWSERBASE_API_KEY
2
- from browserbase .types import SessionCreateResponse
3
2
from selenium import webdriver
4
- from selenium .webdriver .remote .webdriver import WebDriver
5
3
from selenium .webdriver .remote .remote_connection import RemoteConnection
4
+ from typing import Dict
6
5
7
6
8
7
class BrowserbaseConnection (RemoteConnection ):
9
8
"""
10
9
Manage a single session with Browserbase.
11
10
"""
12
11
13
- browserbase_session : SessionCreateResponse
14
12
session_id : str
15
13
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
18
16
self .session_id = session_id
19
17
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
22
22
23
23
# Update headers to include the Browserbase required information
24
24
headers ["x-bb-api-key" ] = BROWSERBASE_API_KEY
@@ -27,31 +27,32 @@ def get_remote_connection_headers(self, parsed_url, keep_alive=False):
27
27
return headers
28
28
29
29
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
+ )
33
37
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
+ )
36
43
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"
38
50
51
+ finally :
52
+ # Make sure to quit the driver so your session is ended!
53
+ print ("Quitting driver" )
54
+ # driver.quit()
39
55
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 ())
44
56
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