-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_visible_browser.py
More file actions
176 lines (135 loc) · 6.1 KB
/
test_visible_browser.py
File metadata and controls
176 lines (135 loc) · 6.1 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
#!/usr/bin/env python3
"""
Visible Browser Test - Shows browser window and demonstrates screenshot location
This test runs with headless=False so you can see the browser in action.
"""
import asyncio
import os
import sys
from pathlib import Path
# Add src to path
sys.path.insert(0, str(Path(__file__).parent / "src"))
from src.core.browser_controller import BrowserController
from src.config.browser_config import BrowserConfig
from src.types.browser_types import BrowserType
async def visible_browser_test():
"""Test with visible browser window"""
print("🌐 Testing with VISIBLE browser...")
print("📍 Current working directory:", os.getcwd())
print()
# Create configuration for VISIBLE browser
config = BrowserConfig(
browser_type=BrowserType.CHROME,
headless=False, # Show the browser window!
window_size=(1280, 720),
page_load_timeout=30,
implicit_wait=10
)
async with BrowserController(config) as controller:
# Create a session
session = await controller.create_session()
try:
print("✓ Browser window should be visible now!")
# Navigate to a test page
print("📂 Navigating to test page...")
await session.navigate_to("https://httpbin.org/html")
# Wait a bit so you can see the page
print("⏳ Waiting 3 seconds so you can see the page...")
await asyncio.sleep(3)
# Try to find an element
title_element = await session.find_element("h1")
if title_element:
print("✓ Found title element on page")
# Take a screenshot and show exactly where it's saved
screenshot_filename = "visible_test_screenshot.png"
screenshot_path = os.path.abspath(screenshot_filename)
print(f"📸 Taking screenshot...")
print(f"📍 Screenshot will be saved to: {screenshot_path}")
success = await session.take_screenshot(screenshot_filename)
if success and os.path.exists(screenshot_filename):
file_size = os.path.getsize(screenshot_filename)
print(f"✅ Screenshot saved successfully!")
print(f"📁 File location: {screenshot_path}")
print(f"📏 File size: {file_size:,} bytes")
# Show directory contents to confirm
print(f"📂 Current directory contents:")
for item in os.listdir("."):
if item.endswith(('.png', '.jpg', '.jpeg')):
print(f" 🖼️ {item}")
else:
print("❌ Screenshot failed")
print("⏳ Waiting 5 more seconds before closing...")
await asyncio.sleep(5)
except Exception as e:
print(f"❌ Error: {e}")
finally:
print("🔒 Closing browser session...")
await controller.close_session(session.session_id)
print("✅ Test completed!")
async def screenshot_location_test():
"""Test different screenshot locations"""
print("\n" + "="*60)
print("📸 Screenshot Location Test")
print("="*60)
config = BrowserConfig(
browser_type=BrowserType.CHROME,
headless=True, # Keep headless for this test
window_size=(1280, 720)
)
async with BrowserController(config) as controller:
session = await controller.create_session()
try:
await session.navigate_to("https://httpbin.org/html")
# Test different screenshot paths
test_cases = [
"screenshot1.png", # Current directory
"screenshots/screenshot2.png", # Subdirectory
os.path.join(os.getcwd(), "screenshot3.png"), # Absolute path
]
for i, screenshot_path in enumerate(test_cases, 1):
print(f"\n📸 Test {i}: {screenshot_path}")
# Create directory if needed
screenshot_dir = os.path.dirname(screenshot_path)
if screenshot_dir and not os.path.exists(screenshot_dir):
os.makedirs(screenshot_dir)
print(f"📁 Created directory: {screenshot_dir}")
# Take screenshot
success = await session.take_screenshot(screenshot_path)
if success and os.path.exists(screenshot_path):
abs_path = os.path.abspath(screenshot_path)
file_size = os.path.getsize(screenshot_path)
print(f"✅ Screenshot saved!")
print(f"📍 Absolute path: {abs_path}")
print(f"📏 File size: {file_size:,} bytes")
else:
print(f"❌ Screenshot failed: {screenshot_path}")
finally:
await controller.close_session(session.session_id)
async def main():
"""Run both tests"""
print("🚀 Browser Visibility and Screenshot Location Tests")
print("="*60)
try:
# Test 1: Visible browser
await visible_browser_test()
# Test 2: Screenshot locations
await screenshot_location_test()
except Exception as e:
print(f"❌ Test failed: {e}")
return False
return True
if __name__ == "__main__":
# Run the tests
success = asyncio.run(main())
print("\n" + "="*60)
if success:
print("🎉 All tests completed successfully!")
print("\nKey Points:")
print("• Set headless=False to see the browser window")
print("• Screenshots are saved to the current working directory by default")
print("• You can specify absolute or relative paths for screenshots")
print("• Check the file paths printed above to find your screenshots")
else:
print("❌ Some tests failed")
print("="*60)
sys.exit(0 if success else 1)