Skip to content

Commit e00201a

Browse files
testing.py: fix failing test by finding all widgets
1 parent cf0537b commit e00201a

File tree

1 file changed

+71
-40
lines changed

1 file changed

+71
-40
lines changed

internal_filesystem/lib/mpos/ui/testing.py

Lines changed: 71 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -125,101 +125,131 @@ def capture_screenshot(filepath, width=320, height=240, color_format=lv.COLOR_FO
125125
return buffer
126126

127127

128-
def get_all_labels(obj, labels=None):
128+
def get_all_widgets_with_text(obj, widgets=None):
129129
"""
130-
Recursively find all label widgets in the object hierarchy.
130+
Recursively find all widgets that have text in the object hierarchy.
131131
132132
This traverses the entire widget tree starting from obj and
133-
collects all LVGL label objects. Useful for comprehensive
134-
text verification or debugging.
133+
collects all widgets that have a get_text() method and return
134+
non-empty text. This includes labels, checkboxes, buttons with
135+
text, etc.
135136
136137
Args:
137138
obj: LVGL object to search (typically lv.screen_active())
138-
labels: Internal accumulator list (leave as None)
139+
widgets: Internal accumulator list (leave as None)
139140
140141
Returns:
141-
list: List of all label objects found in the hierarchy
142+
list: List of all widgets with text found in the hierarchy
142143
143144
Example:
144-
labels = get_all_labels(lv.screen_active())
145-
print(f"Found {len(labels)} labels")
145+
widgets = get_all_widgets_with_text(lv.screen_active())
146+
print(f"Found {len(widgets)} widgets with text")
146147
"""
147-
if labels is None:
148-
labels = []
148+
if widgets is None:
149+
widgets = []
149150

150-
# Check if this object is a label
151+
# Check if this object has text
151152
try:
152-
if obj.get_class() == lv.label_class:
153-
labels.append(obj)
153+
if hasattr(obj, 'get_text'):
154+
text = obj.get_text()
155+
if text: # Only add if text is non-empty
156+
widgets.append(obj)
154157
except:
155-
pass # Not a label or no get_class method
158+
pass # Error getting text or no get_text method
156159

157160
# Recursively check children
158161
try:
159162
child_count = obj.get_child_count()
160163
for i in range(child_count):
161164
child = obj.get_child(i)
162-
get_all_labels(child, labels)
165+
get_all_widgets_with_text(child, widgets)
163166
except:
164167
pass # No children or error accessing them
165168

166-
return labels
169+
return widgets
170+
171+
172+
def get_all_labels(obj, labels=None):
173+
"""
174+
Recursively find all label widgets in the object hierarchy.
175+
176+
DEPRECATED: Use get_all_widgets_with_text() instead for better
177+
compatibility with all text-containing widgets (labels, checkboxes, etc.)
178+
179+
This traverses the entire widget tree starting from obj and
180+
collects all LVGL label objects. Useful for comprehensive
181+
text verification or debugging.
182+
183+
Args:
184+
obj: LVGL object to search (typically lv.screen_active())
185+
labels: Internal accumulator list (leave as None)
186+
187+
Returns:
188+
list: List of all label objects found in the hierarchy
189+
190+
Example:
191+
labels = get_all_labels(lv.screen_active())
192+
print(f"Found {len(labels)} labels")
193+
"""
194+
# For backwards compatibility, use the new function
195+
return get_all_widgets_with_text(obj, labels)
167196

168197

169198
def find_label_with_text(obj, search_text):
170199
"""
171-
Find a label widget containing specific text.
200+
Find a widget containing specific text.
172201
173-
Searches the entire widget hierarchy for a label whose text
174-
contains the search string (substring match). Returns the
175-
first match found.
202+
Searches the entire widget hierarchy for any widget (label, checkbox,
203+
button, etc.) whose text contains the search string (substring match).
204+
Returns the first match found.
176205
177206
Args:
178207
obj: LVGL object to search (typically lv.screen_active())
179208
search_text: Text to search for (can be substring)
180209
181210
Returns:
182-
LVGL label object if found, None otherwise
211+
LVGL widget object if found, None otherwise
183212
184213
Example:
185-
label = find_label_with_text(lv.screen_active(), "Settings")
186-
if label:
187-
print(f"Found Settings label at {label.get_coords()}")
214+
widget = find_label_with_text(lv.screen_active(), "Settings")
215+
if widget:
216+
print(f"Found Settings widget at {widget.get_coords()}")
188217
"""
189-
labels = get_all_labels(obj)
190-
for label in labels:
218+
widgets = get_all_widgets_with_text(obj)
219+
for widget in widgets:
191220
try:
192-
text = label.get_text()
221+
text = widget.get_text()
193222
if search_text in text:
194-
return label
223+
return widget
195224
except:
196-
pass # Error getting text from this label
225+
pass # Error getting text from this widget
197226
return None
198227

199228

200229
def get_screen_text_content(obj):
201230
"""
202-
Extract all text content from all labels on screen.
231+
Extract all text content from all widgets on screen.
203232
204233
Useful for debugging or comprehensive text verification.
205-
Returns a list of all text strings found in label widgets.
234+
Returns a list of all text strings found in any widgets with text
235+
(labels, checkboxes, buttons, etc.).
206236
207237
Args:
208238
obj: LVGL object to search (typically lv.screen_active())
209239
210240
Returns:
211-
list: List of all text strings found in labels
241+
list: List of all text strings found in widgets
212242
213243
Example:
214244
texts = get_screen_text_content(lv.screen_active())
215245
assert "Welcome" in texts
216246
assert "Version 1.0" in texts
217247
"""
218-
labels = get_all_labels(obj)
248+
widgets = get_all_widgets_with_text(obj)
219249
texts = []
220-
for label in labels:
250+
for widget in widgets:
221251
try:
222-
text = label.get_text()
252+
text = widget.get_text()
223253
if text:
224254
texts.append(text)
225255
except:
@@ -250,10 +280,11 @@ def verify_text_present(obj, expected_text):
250280

251281
def print_screen_labels(obj):
252282
"""
253-
Debug helper: Print all label text found on screen.
283+
Debug helper: Print all text found on screen from any widget.
254284
255285
Useful for debugging tests to see what text is actually present.
256-
Prints to stdout with numbered list.
286+
Prints to stdout with numbered list. Includes text from labels,
287+
checkboxes, buttons, and any other widgets with text.
257288
258289
Args:
259290
obj: LVGL object to search (typically lv.screen_active())
@@ -262,15 +293,15 @@ def print_screen_labels(obj):
262293
# When a test fails, use this to see what's on screen
263294
print_screen_labels(lv.screen_active())
264295
# Output:
265-
# Found 5 labels on screen:
296+
# Found 5 text widgets on screen:
266297
# 0: MicroPythonOS
267298
# 1: Version 0.3.3
268299
# 2: Settings
269-
# 3: About
300+
# 3: Force Update (checkbox)
270301
# 4: WiFi
271302
"""
272303
texts = get_screen_text_content(obj)
273-
print(f"Found {len(texts)} labels on screen:")
304+
print(f"Found {len(texts)} text widgets on screen:")
274305
for i, text in enumerate(texts):
275306
print(f" {i}: {text}")
276307

0 commit comments

Comments
 (0)