|
3 | 3 |
|
4 | 4 | import os |
5 | 5 | import sys |
| 6 | +import traceback |
| 7 | +from pathlib import Path |
6 | 8 |
|
7 | | -# Add src to path for local testing |
8 | | -sys.path.insert(0, "src") |
| 9 | +sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "src")) |
9 | 10 |
|
10 | 11 | from stagehand import Stagehand |
11 | 12 |
|
12 | | -# Set required API key for LLM operations |
13 | | -if not os.environ.get("MODEL_API_KEY") and not os.environ.get("OPENAI_API_KEY"): |
14 | | - print("❌ Error: MODEL_API_KEY or OPENAI_API_KEY environment variable not set") # noqa: T201 |
15 | | - print(" Set it with: export MODEL_API_KEY='sk-proj-...'") # noqa: T201 |
16 | | - sys.exit(1) |
17 | 13 |
|
18 | | -print("🚀 Testing local server mode...") # noqa: T201 |
| 14 | +def main() -> None: |
| 15 | + model_api_key = os.environ.get("MODEL_API_KEY") or os.environ.get("OPENAI_API_KEY") |
| 16 | + if not model_api_key: |
| 17 | + print("❌ Error: MODEL_API_KEY or OPENAI_API_KEY environment variable not set") # noqa: T201 |
| 18 | + print(" Set it with: export MODEL_API_KEY='sk-proj-...'") # noqa: T201 |
| 19 | + sys.exit(1) |
19 | 20 |
|
20 | | -try: |
21 | | - # Create client in local mode - will use bundled binary |
22 | | - print("📦 Creating Stagehand client in local mode...") # noqa: T201 |
23 | | - client = Stagehand( |
24 | | - server="local", |
25 | | - browserbase_api_key="local", # Dummy value - not used in local mode |
26 | | - browserbase_project_id="local", # Dummy value - not used in local mode |
27 | | - model_api_key=os.environ.get("MODEL_API_KEY") or os.environ["OPENAI_API_KEY"], |
28 | | - local_headless=True, |
29 | | - local_port=0, # Auto-pick free port |
30 | | - local_ready_timeout_s=15.0, # Give it time to start |
31 | | - ) |
| 21 | + os.environ["BROWSERBASE_FLOW_LOGS"] = "1" |
32 | 22 |
|
33 | | - print("🔧 Starting session (this will start the local server)...") # noqa: T201 |
34 | | - session = client.sessions.start( |
35 | | - model_name="openai/gpt-5-nano", |
36 | | - browser={ # type: ignore[arg-type] |
37 | | - "type": "local", |
38 | | - "launchOptions": {}, # Launch local Playwright browser with defaults |
39 | | - }, |
40 | | - ) |
41 | | - session_id = session.data.session_id |
| 23 | + print("🚀 Testing local server mode...") # noqa: T201 |
| 24 | + client = None |
42 | 25 |
|
43 | | - print(f"✅ Session started: {session_id}") # noqa: T201 |
44 | | - print(f"🌐 Server running at: {client.base_url}") # noqa: T201 |
| 26 | + try: |
| 27 | + print("📦 Creating Stagehand client in local mode...") # noqa: T201 |
| 28 | + client = Stagehand( |
| 29 | + server="local", |
| 30 | + browserbase_api_key="local", |
| 31 | + browserbase_project_id="local", |
| 32 | + model_api_key=model_api_key, |
| 33 | + local_headless=True, |
| 34 | + local_port=0, |
| 35 | + local_ready_timeout_s=15.0, |
| 36 | + ) |
45 | 37 |
|
46 | | - print("\n📍 Navigating to example.com...") # noqa: T201 |
47 | | - client.sessions.navigate( |
48 | | - id=session_id, |
49 | | - url="https://example.com", |
50 | | - ) |
51 | | - print("✅ Navigation complete") # noqa: T201 |
| 38 | + print("🔧 Starting session (this will start the local server)...") # noqa: T201 |
| 39 | + session = client.sessions.start( |
| 40 | + model_name="openai/gpt-5-nano", |
| 41 | + browser={ # type: ignore[arg-type] |
| 42 | + "type": "local", |
| 43 | + "launchOptions": {}, |
| 44 | + }, |
| 45 | + ) |
| 46 | + session_id = session.data.session_id |
52 | 47 |
|
53 | | - print("\n🔍 Extracting page heading...") # noqa: T201 |
54 | | - result = client.sessions.extract( |
55 | | - id=session_id, |
56 | | - instruction="Extract the main heading text from the page", |
57 | | - ) |
58 | | - print(f"📄 Extracted: {result.data.result}") # noqa: T201 |
| 48 | + print(f"✅ Session started: {session_id}") # noqa: T201 |
| 49 | + print(f"🌐 Server running at: {client.base_url}") # noqa: T201 |
59 | 50 |
|
60 | | - print("\n🛑 Ending session...") # noqa: T201 |
61 | | - client.sessions.end(id=session_id) |
62 | | - print("✅ Session ended") # noqa: T201 |
| 51 | + print("\n📍 Navigating to example.com...") # noqa: T201 |
| 52 | + client.sessions.navigate(id=session_id, url="https://example.com") |
| 53 | + print("✅ Navigation complete") # noqa: T201 |
63 | 54 |
|
64 | | - print("\n🔌 Closing client (will shut down server)...") # noqa: T201 |
65 | | - client.close() |
66 | | - print("✅ Server shut down successfully!") # noqa: T201 |
| 55 | + print("\n🔍 Extracting page heading...") # noqa: T201 |
| 56 | + result = client.sessions.extract( |
| 57 | + id=session_id, |
| 58 | + instruction="Extract the main heading text from the page", |
| 59 | + ) |
| 60 | + print(f"📄 Extracted: {result.data.result}") # noqa: T201 |
67 | 61 |
|
68 | | - print("\n🎉 All tests passed!") # noqa: T201 |
| 62 | + print("\n🛑 Ending session...") # noqa: T201 |
| 63 | + client.sessions.end(id=session_id) |
| 64 | + print("✅ Session ended") # noqa: T201 |
| 65 | + print("\n🎉 All tests passed!") # noqa: T201 |
| 66 | + except Exception as exc: |
| 67 | + print(f"\n❌ Error: {exc}") # noqa: T201 |
| 68 | + traceback.print_exc() |
| 69 | + sys.exit(1) |
| 70 | + finally: |
| 71 | + if client is not None: |
| 72 | + print("\n🔌 Closing client (will shut down server)...") # noqa: T201 |
| 73 | + client.close() |
| 74 | + print("✅ Server shut down successfully!") # noqa: T201 |
69 | 75 |
|
70 | | -except Exception as e: |
71 | | - print(f"\n❌ Error: {e}") # noqa: T201 |
72 | | - import traceback |
73 | | - traceback.print_exc() |
74 | | - sys.exit(1) |
| 76 | + |
| 77 | +if __name__ == "__main__": |
| 78 | + main() |
0 commit comments