找出Espresso到底在哪一行失败

时间:2019-02-21 14:20:03

标签: automated-tests android-espresso

我正在我的Android应用上运行Espresso测试。如果失败了,我将打印视图树。不幸的是,我不知道这种故障发生在哪一行。

当抛出异常(例如NoMatchingViewException)时,是否可以告诉Espresso打印堆栈跟踪信息,这样我可以看到我的测试在哪一行失败?

谢谢  托马斯

编辑:

我得到的输出看起来像(中间部分已移开):

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

2 个答案:

答案 0 :(得分:0)

很明显,您对ID为stopChargingButton的视图有多个匹配项。就是这样。

android.support.test.espresso.AmbiguousViewMatcherException: 'with id: com.wombatl.mobility.charge.test:id/stopChargingButton' matches multiple views in the hierarchy.

视图匹配器应仅匹配一个元素,否则Espresso不知道要在哪个视图上进行操作并引发异常。添加其他匹配条件以标识屏幕上的特定视图。为此,您可以使用allOf() hamcrest匹配器。

如果要操作的视图是ListView的一部分,请使用onData()。如果它位于RecyclerView内,请查看文档中如何操作属于RecyclerView的项目。

答案 1 :(得分:0)

否,行号将不会显示在失败消息中。您将不得不自己推断。