-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_browser_use.py
More file actions
235 lines (200 loc) · 7.95 KB
/
test_browser_use.py
File metadata and controls
235 lines (200 loc) · 7.95 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#!/usr/bin/env python3
"""
Test the server with browser_use library
"""
import asyncio
from browser_use import Agent
import requests
def check_server_health():
"""Check if our server is running"""
try:
response = requests.get("http://localhost:8000/health", timeout=5)
if response.status_code == 200:
print("✅ Server is running and healthy")
return True
else:
print(f"⚠️ Server returned status {response.status_code}")
return False
except Exception as e:
print(f"❌ Server is not running: {e}")
print("💡 Start the server with: uv run python openrouter_server.py")
return False
async def test_browser_use_integration():
"""Test browser_use with our Google AI Studio server"""
print("🌐 Testing browser_use Integration")
print("=" * 50)
# Check server first
if not check_server_health():
return False
try:
# Check if browser_use is installed
try:
import browser_use
print(f"✅ browser_use version: {browser_use.__version__}")
except ImportError:
print("❌ browser_use not installed")
print("💡 Install with: pip install browser-use")
return False
except AttributeError:
print("✅ browser_use installed (version not available)")
# Import required components
from browser_use import Agent
from openai import OpenAI
import inspect
# Check the Agent constructor signature
sig = inspect.signature(Agent.__init__)
params = list(sig.parameters.keys())
print(f"🔍 Agent constructor parameters: {params}")
# Create OpenAI client pointing to our server
client = OpenAI(
api_key="dummy-key",
base_url="http://localhost:8000/v1"
)
# Test the client first
print("🧪 Testing OpenAI client connection...")
try:
models = client.models.list()
print(f"✅ Client connected, available models: {[m.id for m in models.data]}")
except Exception as e:
print(f"⚠️ Client connection issue: {e}")
# Create agent with the correct parameters
if 'llm' in params:
print("🔧 Using 'llm' parameter (newer API)")
agent = Agent(
task="Go to Google and search for 'Python programming'",
llm=client
)
elif 'llm_config' in params:
print("🔧 Using 'llm_config' parameter (older API)")
agent = Agent(
task="Go to Google and search for 'Python programming'",
llm_config={
"provider": "openai",
"model": "google/gemini-pro",
"api_key": "dummy-key",
"base_url": "http://localhost:8000/v1"
}
)
else:
print(f"❌ Unknown Agent API - parameters: {params}")
return False
print("✅ Created browser_use agent with our server")
print("📋 Task: Go to Google and search for 'Python programming'")
print("⏳ Running browser automation...")
# Run the agent (with timeout)
try:
result = await asyncio.wait_for(agent.run(), timeout=60)
print(f"\n🎉 SUCCESS!")
print(f"📝 Result: {result}")
return True
except asyncio.TimeoutError:
print("⏰ Task timed out after 60 seconds")
return False
except Exception as e:
print(f"❌ browser_use integration failed: {e}")
print(f"🔍 Error type: {type(e).__name__}")
print("\n💡 Possible issues:")
print(" - browser_use not installed: pip install browser-use")
print(" - Wrong browser_use version")
print(" - Server not compatible with browser_use")
print(" - Configuration error")
return False
async def test_simple_browser_task():
"""Test a simple browser task"""
print("\n🧪 Testing Simple Browser Task")
print("=" * 40)
try:
from browser_use import Agent
from openai import OpenAI
import inspect
# Create OpenAI client pointing to our server
client = OpenAI(
api_key="dummy-key",
base_url="http://localhost:8000/v1"
)
# Check Agent parameters again
sig = inspect.signature(Agent.__init__)
params = list(sig.parameters.keys())
# Create agent with correct parameters
if 'llm' in params:
agent = Agent(
task="Navigate to https://www.python.org",
llm=client
)
elif 'llm_config' in params:
agent = Agent(
task="Navigate to https://www.python.org",
llm_config={
"provider": "openai",
"model": "google/gemini-pro",
"api_key": "dummy-key",
"base_url": "http://localhost:8000/v1"
}
)
else:
print(f"❌ Unknown Agent API - parameters: {params}")
return False
print("📋 Task: Navigate to python.org")
try:
result = await asyncio.wait_for(agent.run(), timeout=30)
print(f"✅ Simple task completed: {result}")
return True
except asyncio.TimeoutError:
print("⏰ Simple task timed out after 30 seconds")
return False
except Exception as e:
print(f"❌ Simple task failed: {e}")
print(f"🔍 Error type: {type(e).__name__}")
return False
def show_browser_use_setup():
"""Show how to set up browser_use with our server"""
print("\n📋 browser_use Setup Instructions")
print("=" * 50)
print("1️⃣ Install browser_use:")
print(" pip install browser-use")
print(" # or")
print(" uv add browser-use")
print("\n2️⃣ Start your server:")
print(" uv run python openrouter_server.py")
print("\n3️⃣ Use browser_use with your server:")
print("""
from browser_use import Agent
from openai import OpenAI
# Create OpenAI client pointing to your server
client = OpenAI(
api_key="dummy-key",
base_url="http://localhost:8000/v1"
)
agent = Agent(
task="Your automation task here",
llm=client # Pass the OpenAI client
)
result = await agent.run()
""")
print("\n4️⃣ Example tasks:")
print(" - 'Search for Python tutorials on Google'")
print(" - 'Go to GitHub and find trending Python repos'")
print(" - 'Navigate to Stack Overflow and search for Flask'")
print(" - 'Open YouTube and search for coding tutorials'")
async def main():
print("🌐 browser_use Integration Test")
print("This tests using your Google AI Studio server with browser_use")
print("=" * 70)
# Test basic integration
success1 = await test_browser_use_integration()
if success1:
# Test simple task
success2 = await test_simple_browser_task()
if success1 and success2:
print("\n🎉 BROWSER_USE INTEGRATION SUCCESSFUL!")
print("✅ Your Google AI Studio server works with browser_use")
print("✅ You can now automate browsers using Gemini Pro")
print("\n🚀 Ready for browser automation!")
else:
print("\n⚠️ Partial success - basic integration works")
else:
print("\n❌ Integration failed")
# Always show setup instructions
show_browser_use_setup()
if __name__ == "__main__":
asyncio.run(main())