Nor*_*yer 5 automated-tests android-espresso
我正在我的 Android 应用程序上运行 Espresso 测试。如果一个失败,我将打印视图树。不幸的是,我不知道这个故障究竟发生在哪一行。
有没有办法告诉 Espresso 在抛出异常(例如 NoMatchingViewException)时打印堆栈跟踪,以便我可以看到我的测试在哪一行失败?
谢谢托马斯
编辑:
我得到的输出看起来像(删除了中间的部分):
android.support.test.espresso.AmbiguousViewMatcherException: 'with id: com.wombatl.mobility.charge.test:id/stopChargingButton' matches multiple views in the hierarchy.
Problem views are marked with '****MATCHES****' below.
View Hierarchy:
+>DecorView{id=-1, visibility=VISIBLE, width=1080, height=2160, has-focus=true, has-focusable=true, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, layout-params=WM.LayoutParams{(0,0)(fillxfill) ty=1 fl=#81810100 pfl=0x20000 wanim=0x10302f6 needsMenuKey=2 colorMode=0}, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=3}
|
+->LinearLayout{id=-1, visibility=VISIBLE, width=1080, height=2034, has-focus=true, has-focusable=true, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, layout-params=android.widget.FrameLayout$LayoutParams@48dbdb, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=2}
|
(...)
|
+--------------->AppCompatTextView{id=2131361990, res-name=hoursTextView, visibility=VISIBLE, width=68, height=53, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, layout-params=android.widget.Rela
Run Code Online (Sandbox Code Playgroud)
den*_*nys -1
明确指出您有多个与 id 的视图匹配的内容stopChargingButton。就是这样。
android.support.test.espresso.AmbiguousViewMatcherException: 'with id: com.wombatl.mobility.charge.test:id/stopChargingButton' matches multiple views in the hierarchy.
Run Code Online (Sandbox Code Playgroud)
视图匹配器应该只匹配一个元素,否则 Espresso 不知道要操作哪个视图并抛出异常。添加额外的匹配条件来识别屏幕上的特定视图。您可以使用allOf()hamcrest matcher 来实现此目的。
如果您想要操作的视图是 -use 的ListView一部分onData()。如果它在里面,RecyclerView请查看文档如何对作为RecyclerView.