首页 > 解决方案 > 在 Android Studio 中,当 Espresso 测试失败时,如何在日志窗口中查看完整的层次结构视图

问题描述

我编写了一个失败的 Espresso 测试(未找到匹配的视图)。

例子: onView((allOf(withText("OK"), hasSibling(withText("Text"))))).perform(click());

我得到的错误是:

androidx.test.espresso.NoMatchingViewException: No views in hierarchy found
matching: (with text: is "OK" and has sibling: with text: is "Text")
If the target view is not part of the view hierarchy, you may need to use 
Espresso.onData to load it from one of the following
AdapterViews:androidx.appcompat.widget.AppCompatSpinner{da0caf9 VFED..CL. 
........ 273,0-505,48 #7f0901c1 app:id/structure_formation_spinner}
View Hierarchy:
....

然后显示视图层次结构。问题是在 Android Studio 中,层次结构被截断为 32K 个字符。

我知道如何修复返回的错误,这不是我的问题。

我的问题是:如何查看完整的视图层次结构?在 Android Studio 中可以吗?如果没有,是否可以从命令行进行?

标签: android-studioandroid-espresso

解决方案


由于测试是在 Device/Emulator 上执行的,因此可以从 Logcat(即 Android Studio 中的 Logcat 窗口)获取带有完整消息的异常(视图层次结构只是一条消息,附加到NoMatchingViewException)。过滤器E/TestRunner简化了搜索。

不过,Logcat 本身并不是无限的。默认大小为 1024KB。它可以通过改变Settings | Editor | General | Console


TL;博士; Android Studio 和 Android Device/Emulator 通过 protobuf 消息进行通信。com.android.ddmlib.testrunner.InstrumentationProtoResultParser#updateState来自设备的数据以键值映射(的集合)的形式到达InstrumentationData.ResultsBundleEntry

Android Studio 中的 Instrumented Test Results 视图仅显示stackTrace. println("hello")您可以通过在仪器测试中的任何位置添加来说服自己。该行只会出现在 Logcat 中,而不会出现在 Test Result 视图中。

stackTrace是通过键从映射中获得的StatusKeys.STACK(见ln 239)。不幸的是,此时数据已经被截断。有STREAM包含全文的密钥,但未使用。所以问题出在设备端,它未能发送完整的堆栈跟踪,IMO。

其他运行程序(例如,从控制台运行时的 gradle)可能会使用该映射中的不同值。这解释了为什么从控制台运行时会看到完整消息。


推荐阅读