-
Notifications
You must be signed in to change notification settings - Fork 70
Expand file tree
/
Copy pathtest_graphical_screenshot.py
More file actions
85 lines (69 loc) · 2.71 KB
/
test_graphical_screenshot.py
File metadata and controls
85 lines (69 loc) · 2.71 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
"""
Graphical test for screenshot capture.
This test focuses on screenshot capture for visual regression testing.
Usage:
Desktop: ./tests/unittest.sh tests/test_screenshot.py
Device: ./tests/unittest.sh tests/test_screenshot.py --ondevice
"""
import os
import sys
import unittest
import mpos.ui
from mpos import AppManager, DeviceInfo, DisplayMetrics, capture_screenshot, wait_for_text
class TestScreenshotCapture(unittest.TestCase):
"""Test suite for screenshot capture."""
def setUp(self):
"""Set up test fixtures before each test method."""
if sys.platform == "esp32":
self.screenshot_dir = "screenshots"
else:
self.screenshot_dir = "../tests/screenshots"
try:
os.mkdir(self.screenshot_dir)
except OSError:
pass
self.hardware_id = DeviceInfo.hardware_id
print(f"Testing with hardware ID: {self.hardware_id}")
def tearDown(self):
"""Clean up after each test method."""
try:
mpos.ui.back_screen()
except Exception:
pass
def test_capture_about_app_screenshot(self):
"""Capture screenshot of the About app for regression testing."""
print("\n=== Starting About app screenshot test ===")
result = AppManager.start_app("com.micropythonos.about")
self.assertTrue(result, "Failed to start About app")
self.assertTrue(
wait_for_text("Hardware ID:", timeout=10),
"About app did not load within timeout",
)
screenshot_path = f"{self.screenshot_dir}/about_app_{self.hardware_id}.raw"
print(f"\nCapturing screenshot to: {screenshot_path}")
try:
width = DisplayMetrics.width()
height = DisplayMetrics.height()
buffer = capture_screenshot(screenshot_path, width=width, height=height)
print(f"Screenshot captured: {len(buffer)} bytes")
stat = os.stat(screenshot_path)
self.assertTrue(
stat[6] > 0,
"Screenshot file is empty",
)
expected_size = width * height * 2
self.assertEqual(
stat[6],
expected_size,
f"Screenshot file size {stat[6]} does not match expected {expected_size}",
)
print(f"Screenshot file size: {stat[6]} bytes")
except Exception as exc:
self.fail(f"Failed to capture screenshot: {exc}")
finally:
try:
print(f"Removing screenshot {screenshot_path}")
os.remove(screenshot_path)
except OSError:
pass
print("\n=== About app screenshot test completed successfully ===")