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