|
| 1 | +"""Run the Stagehand wrapper without installing Stagehand. |
| 2 | +
|
| 3 | +This uses a fake Stagehand-shaped page so the example is deterministic: |
| 4 | +
|
| 5 | + python examples/stagehand_wrapper_example.py |
| 6 | +
|
| 7 | +Then open the BrowserTrace UI: |
| 8 | +
|
| 9 | + browsertrace |
| 10 | +""" |
| 11 | + |
| 12 | +from __future__ import annotations |
| 13 | + |
| 14 | +import asyncio |
| 15 | +import os |
| 16 | + |
| 17 | +from browsertrace import Tracer |
| 18 | +from browsertrace.integrations.stagehand import wrap_stagehand |
| 19 | + |
| 20 | + |
| 21 | +class DemoStagehandPage: |
| 22 | + url = "https://shop.example.test/cart" |
| 23 | + |
| 24 | + async def screenshot(self) -> bytes: |
| 25 | + return b"\x89PNG\r\n\x1a\n" + b"\x00" * 16 |
| 26 | + |
| 27 | + async def act(self, instruction: str) -> dict[str, str]: |
| 28 | + await asyncio.sleep(0) |
| 29 | + return {"status": "completed", "instruction": instruction} |
| 30 | + |
| 31 | + async def extract(self, instruction: str) -> dict[str, str]: |
| 32 | + await asyncio.sleep(0) |
| 33 | + return { |
| 34 | + "status": "completed", |
| 35 | + "instruction": instruction, |
| 36 | + "order_total": "$42.00", |
| 37 | + } |
| 38 | + |
| 39 | + |
| 40 | +async def main() -> None: |
| 41 | + tracer = Tracer(home=os.environ.get("BROWSERTRACE_HOME")) |
| 42 | + page = wrap_stagehand( |
| 43 | + DemoStagehandPage(), |
| 44 | + tracer, |
| 45 | + name="demo: stagehand checkout flow", |
| 46 | + ) |
| 47 | + |
| 48 | + await page.act("click the checkout button") |
| 49 | + await page.extract("extract the order total") |
| 50 | + page.bt_run.close() |
| 51 | + |
| 52 | + print(f"BrowserTrace run id: {page.bt_run.id}") |
| 53 | + print("Recorded Stagehand calls: act, extract") |
| 54 | + print("Open the local UI with: browsertrace") |
| 55 | + |
| 56 | + |
| 57 | +if __name__ == "__main__": |
| 58 | + asyncio.run(main()) |
0 commit comments