|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Quick test of local server mode with the embedded binary.""" |
| 3 | + |
| 4 | +import os |
| 5 | +import sys |
| 6 | +import traceback |
| 7 | +from pathlib import Path |
| 8 | + |
| 9 | +sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "src")) |
| 10 | + |
| 11 | +from stagehand import Stagehand |
| 12 | + |
| 13 | + |
| 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) |
| 20 | + |
| 21 | + os.environ["BROWSERBASE_FLOW_LOGS"] = "1" |
| 22 | + |
| 23 | + print("🚀 Testing local server mode...") # noqa: T201 |
| 24 | + client = None |
| 25 | + |
| 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 | + ) |
| 37 | + |
| 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 |
| 47 | + |
| 48 | + print(f"✅ Session started: {session_id}") # noqa: T201 |
| 49 | + print(f"🌐 Server running at: {client.base_url}") # noqa: T201 |
| 50 | + |
| 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 |
| 54 | + |
| 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 |
| 61 | + |
| 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 |
| 75 | + |
| 76 | + |
| 77 | +if __name__ == "__main__": |
| 78 | + main() |
0 commit comments