1
1
import os
2
2
import time
3
3
from e2b_desktop import Sandbox
4
+ from PIL import ImageChops , Image
5
+ import io
4
6
5
- # def test_right_click(sandbox: Sandbox):
6
- # sandbox.right_click()
7
- # time.sleep(5)
8
- # pos = sandbox.locate_on_screen("Create Document")
9
- # print(pos)
10
- # assert pos is not None
7
+ def crop_around_point (image , cursor_pos ):
8
+ x , y = cursor_pos
9
+ box = (x - 50 , y - 50 , x + 50 , y + 50 ) # 100x100 box
10
+ return image .crop (box )
11
11
12
+ def images_are_equal (img1 , img2 ):
13
+ # Use ImageChops to find the difference
14
+ diff = ImageChops .difference (img1 , img2 )
15
+ # Check if there is any difference
16
+ return diff .getbbox () is None # Returns True if images are equal, False if different
12
17
13
- # def test_screenshot(sandbox: Sandbox):
14
- # screenshot_path = "test-screenshot.png"
15
- # sandbox.screenshot(screenshot_path)
16
- # assert os.path.exists(screenshot_path)
18
+ def test_right_click (sandbox : Sandbox ):
19
+ # Capture the initial screenshot
20
+ initial_screenshot_bytes = sandbox .take_screenshot ()
21
+ initial_image = Image .open (io .BytesIO (initial_screenshot_bytes ))
22
+
23
+ # Get cursor position and perform right click
24
+ cursor_pos = sandbox .get_cursor_position ()
25
+ sandbox .right_click ()
26
+ time .sleep (5 ) # Wait for UI to respond
27
+
28
+ # Capture and process the second screenshot
29
+ post_click_screenshot_bytes = sandbox .take_screenshot ()
30
+ post_click_image = Image .open (io .BytesIO (post_click_screenshot_bytes ))
31
+
32
+ # Crop both images around the cursor position
33
+ cropped_image_1 = crop_around_point (initial_image , cursor_pos )
34
+ cropped_image_2 = crop_around_point (post_click_image , cursor_pos )
35
+
36
+ # Compare the cropped images
37
+ assert not images_are_equal (cropped_image_1 , cropped_image_2 ), "The image around the cursor did not change after right-click."
17
38
18
- # def test_get_cursor_position(sandbox: Sandbox):
19
- # pos = sandbox.get_cursor_position()
20
- # assert pos == (512, 384) # In the middle of the screen
39
+ def test_screenshot (sandbox : Sandbox ):
40
+ image = sandbox .take_screenshot ()
41
+ assert image , "Screenshot was not taken successfully"
42
+
43
+ # Check if the image is a valid image format
44
+ try :
45
+ img = Image .open (io .BytesIO (image ))
46
+ img .verify () # Verify that it is an image
47
+ except Exception :
48
+ assert False , "The screenshot is not a valid image."
21
49
50
+ def test_get_cursor_position (sandbox : Sandbox ):
51
+ pos = sandbox .get_cursor_position ()
52
+ assert pos == (512 , 384 ), f"Expected cursor position to be (512, 384), but got { pos } "
22
53
23
- # def test_get_screen_size(sandbox: Sandbox):
24
- # size = sandbox.get_screen_size()
25
- # assert size == (1024, 768) # 1024x768 screen
26
54
27
- # def test_write(sandbox: Sandbox):
28
- # # Create a file and open it in a text editor
29
- # text_file_path = "/home/user/test.txt"
30
- # sandbox.write(text_file_path, "hello")
31
- # sandbox.open_file(text_file_path)
55
+ def test_get_screen_size (sandbox : Sandbox ):
56
+ size = sandbox .get_screen_size ()
57
+ assert size == (1024 , 768 ), f"Expected screen size to be (1024, 768), but got { size } "
58
+
59
+
60
+ def test_write (sandbox : Sandbox ):
61
+ # Create a file and open it in a text editor
62
+ text_file_path = "/home/user/test.txt"
63
+ sandbox .files .write (text_file_path , "hello" )
64
+ sandbox .open (text_file_path )
65
+ # Add an assertion here, perhaps check if the content is correct
66
+ content = sandbox .files .read (text_file_path )
67
+ assert content == "hello" , f"Expected content 'hello' in { text_file_path } , but got { content } "
0 commit comments