android-studio - 在 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 中可以吗?如果没有,是否可以从命令行进行?
解决方案
由于测试是在 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)可能会使用该映射中的不同值。这解释了为什么从控制台运行时会看到完整消息。
推荐阅读
- node.js - 在重构一些 api 调用后使用 React 时,一些 express api 调用会出现 cors 错误
- c# - 在 EF Core 中设置预定义条件
- drake - 如何使用 MeshCat 和 pydake 将 drake 的可视化输出通过管道传输到端口?
- python-3.x - 在日期时间序列中查找事件的平均年龄
- c# - 序列化名称值对列表,不包括键和值部分
- java - 如何在多对多关系 [Spring Boot 2、JPA、Hibernate、PostgreSQL] 中的一个操作中保存多个实体
- scala - 从 csv 创建数据框并使用 inferSchema 转换列比创建数据框和使用 withColumn 转换要慢得多
- r - 使用传单包查找点之间的最短路径并在地图上绘图
- kubernetes - 始终将 pod 与 pvc 和 gce 永久磁盘搭配使用
- javascript - 如何在国家地图中添加悬停效果或鼠标悬停/鼠标悬停效果?