-
Notifications
You must be signed in to change notification settings - Fork 175
Expand file tree
/
Copy pathautomate_desktop.py
More file actions
executable file
·152 lines (117 loc) · 4.46 KB
/
Copy pathautomate_desktop.py
File metadata and controls
executable file
·152 lines (117 loc) · 4.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
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
#!/usr/bin/env python3
"""
ComputerBox Example - Desktop Automation
Tests all ComputerBox functions comprehensively.
"""
import asyncio
import base64
import logging
import os
import sys
import boxlite
try:
sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))
from _helpers import setup_logging
except ImportError:
def setup_logging():
logging.basicConfig(level=logging.ERROR)
logger = logging.getLogger("computerbox_example")
async def test_all_functions():
"""Test all ComputerBox functions."""
print("=== ComputerBox - Testing All Functions ===\n")
async with boxlite.ComputerBox(cpu=2, memory=2048) as desktop:
# async with boxlite.ComputerBox(cpu=2, memory=2048) as desktop:
print("✓ Desktop started\n")
# 1. wait_until_ready()
print("1. wait_until_ready()")
await desktop.wait_until_ready(timeout=60)
print(" ✓ Desktop initialized\n")
# 2. get_screen_size()
print("2. get_screen_size()")
width, height = await desktop.get_screen_size()
print(f" ✓ Screen: {width}x{height}\n")
# 3. screenshot()
print("3. screenshot()")
result = await desktop.screenshot()
print(f" ✓ Captured: {result['width']}x{result['height']} {result['format']}")
print(f" ✓ Data size: {len(result['data'])} bytes\n")
# 4. mouse_move()
print("4. mouse_move(x, y)")
await desktop.mouse_move(512, 384)
print(" ✓ Moved to (512, 384)\n")
# 5. cursor_position()
print("5. cursor_position()")
x, y = await desktop.cursor_position()
print(f" ✓ Cursor at ({x}, {y})\n")
# 6. left_click()
print("6. left_click()")
await desktop.left_click()
print(" ✓ Clicked\n")
# 7. right_click()
print("7. right_click()")
await desktop.right_click()
print(" ✓ Right clicked\n")
# 8. middle_click()
print("8. middle_click()")
await desktop.middle_click()
print(" ✓ Middle clicked\n")
# 9. double_click()
print("9. double_click()")
await desktop.double_click()
print(" ✓ Double clicked\n")
# 10. triple_click()
print("10. triple_click()")
await desktop.triple_click()
print(" ✓ Triple clicked\n")
# 11. left_click_drag()
print("11. left_click_drag(start_x, start_y, end_x, end_y)")
await desktop.left_click_drag(100, 100, 200, 200)
print(" ✓ Dragged from (100,100) to (200,200)\n")
# 12. type()
print("12. type(text)")
await desktop.type("Hello BoxLite!")
print(" ✓ Typed: 'Hello BoxLite!'\n")
# 13. key()
print("13. key(keyname)")
await desktop.key("Return")
print(" ✓ Pressed: Return\n")
await desktop.key("ctrl+a")
print(" ✓ Pressed: Ctrl+A\n")
# 14. scroll()
print("14. scroll(x, y, direction, amount)")
await desktop.scroll(512, 384, "down", amount=3)
print(" ✓ Scrolled down 3 units\n")
print("=" * 50)
print("✓ All 14 functions tested successfully!")
async def example_workflow():
"""Example workflow: Take screenshots and interact."""
print("\n\n=== Example Workflow ===\n")
async with boxlite.ComputerBox(cpu=2, memory=2048) as desktop:
print("Desktop started\n")
# Wait for desktop
await desktop.wait_until_ready()
# Take initial screenshot
print("📸 Initial screenshot...")
img1 = await desktop.screenshot()
with open("screenshot_1.png", 'wb') as f:
f.write(base64.b64decode(img1['data']))
print(" ✓ Saved: screenshot_1.png\n")
# Interact with desktop
print("🖱️ Clicking application menu...")
await desktop.mouse_move(50, 20)
await desktop.left_click()
# Take final screenshot
print("\n📸 Final screenshot...")
img2 = await desktop.screenshot()
with open("screenshot_2.png", 'wb') as f:
f.write(base64.b64decode(img2['data']))
print(" ✓ Saved: screenshot_2.png\n")
print("✓ Workflow completed!")
async def main():
"""Run all examples."""
await test_all_functions()
await example_workflow()
if __name__ == "__main__":
setup_logging()
logger.info("Python logging configured; runtime logs will emit to stdout.")
asyncio.run(main())