Skip to content

Commit 7e6c095

Browse files
hoisiecopybara-androidxtest
authored andcommitted
Treat exceptions during DefaultFailureHandler screenshots as suppressed exceptions
If an Espresso failure occurs, DefaultFailureHandler will capture a window snapshot and take a screenshot. However, if a RuntimeException occurs during this process, it will replace the original exception. An example is if there is an exception that occurs during a View's rendering logic. The original exception will trigger DefaultFailureHandle's screenshot, which may trigger a different exception, hiding the root cause. PiperOrigin-RevId: 601202589
1 parent c16d70f commit 7e6c095

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

espresso/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ The following artifacts were released:
2222
* Remove Kotlin StringKt calls from Java code
2323
* Remove all support for Android SDKs < 19. Minimum is API 19 (Android Kit Kat 4.4)
2424
* Stop posting empty tasks to background threads when running in non-remote mode
25+
* Better handle exceptions that may occur in DefaultFailureHandler's hierarchy capture and screenshot process.
2526

2627
**New Features**
2728

espresso/core/java/androidx/test/espresso/base/DefaultFailureHandler.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,14 @@ static Truncater<AmbiguousViewMatcherException> getAmbiguousViewMatcherException
119119
@Override
120120
public void handle(Throwable error, Matcher<View> viewMatcher) {
121121
int count = failureCount.incrementAndGet();
122-
TestOutputEmitter.captureWindowHierarchy("explore-window-hierarchy-" + count + ".xml");
123-
takeScreenshot("view-op-error-" + count);
122+
try {
123+
TestOutputEmitter.captureWindowHierarchy("explore-window-hierarchy-" + count + ".xml");
124+
takeScreenshot("view-op-error-" + count);
125+
} catch (RuntimeException screenshotException) {
126+
// Ensure that the root cause exception is surfaced, not an auxiliary exception that may occur
127+
// during the capture/screenshot process.
128+
error.addSuppressed(screenshotException);
129+
}
124130

125131
// Iterates through the list of handlers to handle the exception, but at most one handler will
126132
// update the exception and throw at the end of the handling.

0 commit comments

Comments
 (0)