Skip to content

Commit

Permalink
Treat exceptions during DefaultFailureHandler screenshots as suppress…
Browse files Browse the repository at this point in the history
…ed 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
  • Loading branch information
hoisie authored and copybara-androidxtest committed Jan 24, 2024
1 parent c16d70f commit 7e6c095
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
1 change: 1 addition & 0 deletions espresso/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ The following artifacts were released:
* Remove Kotlin StringKt calls from Java code
* Remove all support for Android SDKs < 19. Minimum is API 19 (Android Kit Kat 4.4)
* Stop posting empty tasks to background threads when running in non-remote mode
* Better handle exceptions that may occur in DefaultFailureHandler's hierarchy capture and screenshot process.

**New Features**

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,14 @@ static Truncater<AmbiguousViewMatcherException> getAmbiguousViewMatcherException
@Override
public void handle(Throwable error, Matcher<View> viewMatcher) {
int count = failureCount.incrementAndGet();
TestOutputEmitter.captureWindowHierarchy("explore-window-hierarchy-" + count + ".xml");
takeScreenshot("view-op-error-" + count);
try {
TestOutputEmitter.captureWindowHierarchy("explore-window-hierarchy-" + count + ".xml");
takeScreenshot("view-op-error-" + count);
} catch (RuntimeException screenshotException) {
// Ensure that the root cause exception is surfaced, not an auxiliary exception that may occur
// during the capture/screenshot process.
error.addSuppressed(screenshotException);
}

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

0 comments on commit 7e6c095

Please sign in to comment.