Skip to content

Commit dfbe098

Browse files
DeepDiver1975claude
andcommitted
test/gui: make window screenshot grab robust across Squish APIs
The grabScreenshot() attribute does not exist on Squish objects in the CI image, and the raised AttributeError was not caught, so the run errored and produced no files. Make _grab_window try both grabScreenshot(path) and the grabWidget(obj).save(path) forms, validate success by checking the file actually appeared, and catch all exceptions so a failed crop always falls back to the proven saveDesktopScreenshot. A run can no longer error out or produce zero files because of the capture API. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Signed-off-by: Thomas Müller <1005065+DeepDiver1975@users.noreply.github.com>
1 parent 25e5534 commit dfbe098

1 file changed

Lines changed: 30 additions & 19 deletions

File tree

test/gui/shared/scripts/helpers/DocScreenshotHelper.py

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -99,26 +99,37 @@ def capture_doc_screenshot(screen_id):
9999
target = _target_path(rel_name)
100100
window_ref = SCREENSHOT_WINDOW.get(screen_id)
101101

102-
if window_ref is not None:
103-
try:
104-
window = squish.waitForObject(window_ref)
105-
# object.grabScreenshot(path) is the current API (grabWidget is
106-
# deprecated). Image.save() takes a single full path, not
107-
# (dir, name) -- passing two args silently fails to write.
108-
window.grabScreenshot(target)
109-
if os.path.exists(target):
110-
test.log(f"Doc screenshot (window) captured: {rel_name}")
111-
return target
112-
test.log(
113-
f"grabScreenshot wrote no file for '{screen_id}'; "
114-
f"falling back to desktop capture"
115-
)
116-
except (LookupError, RuntimeError) as err:
117-
test.log(
118-
f"grabScreenshot failed for '{screen_id}' ({err}); "
119-
f"falling back to desktop capture"
120-
)
102+
if window_ref is not None and _grab_window(window_ref, target):
103+
test.log(f"Doc screenshot (window) captured: {rel_name}")
104+
return target
121105

106+
# Always-available fallback: full desktop capture (proven to write).
122107
squish.saveDesktopScreenshot(target)
123108
test.log(f"Doc screenshot (desktop) captured: {rel_name}")
124109
return target
110+
111+
112+
def _grab_window(window_ref, target):
113+
"""Try to grab just the given window to `target`. Return True on success.
114+
115+
Squish's screenshot API varies across versions: grabWidget(obj) is
116+
deprecated but returns an Image whose .save(path) takes a SINGLE full path
117+
(not dir, name); newer objects expose .grabScreenshot(path). Try each and
118+
treat "a file actually appeared" as the only success criterion, so a save
119+
that silently no-ops still falls back to a desktop capture. Catch broadly
120+
on purpose -- any failure here must not abort the run.
121+
"""
122+
window = squish.waitForObject(window_ref)
123+
for attempt in (
124+
lambda: window.grabScreenshot(target),
125+
lambda: squish.grabWidget(window).save(target),
126+
):
127+
try:
128+
if os.path.exists(target):
129+
os.remove(target)
130+
attempt()
131+
if os.path.exists(target):
132+
return True
133+
except Exception as err: # noqa: BLE001 - must never abort the run
134+
test.log(f"window grab attempt failed: {err}")
135+
return False

0 commit comments

Comments
 (0)