在 Espresso 中,当多个视图匹配时如何避免 AmbiguousViewMatcherException [英] In Espresso, how to avoid AmbiguousViewMatcherException when multiple views match

查看:27
本文介绍了在 Espresso 中,当多个视图匹配时如何避免 AmbiguousViewMatcherException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

拥有包含一些图像的 gridView.gridView 的单元格来自相同的预定义布局,具有相同的 id 和 desc.

Having gridView which has some images. The gridView's cell comes out from same predefined layout, which has same id and desc.

R.id.item_image == 2131493330

R.id.item_image == 2131493330

onView(withId(is(R.id.item_image))).perform(click());

由于网格中的所有单元格都具有相同的 id,因此它得到了 AmbiguousViewMatcherException.如何只拿起第一个或其中任何一个?谢谢!

Since all cells in the grid have same id, it got AmbiguousViewMatcherException. How to just pick up first one or any of one of them? Thanks!

android.support.test.espresso.AmbiguousViewMatcherException: 'with id: is <2131493330>' 匹配层次结构中的多个视图.问题视图在下方标有****MATCHES****".

android.support.test.espresso.AmbiguousViewMatcherException: 'with id: is <2131493330>' matches multiple views in the hierarchy. Problem views are marked with '****MATCHES****' below.

+------------->ImageView{id=2131493330, res-name=item_image, desc=Image,visibility=VISIBLE, width=262, height=262, 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, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0} ****MATCHES****

+------------->ImageView{id=2131493330, res-name=item_image, desc=Image, visibility=VISIBLE, width=262, height=262, 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, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0} ****MATCHES****

+------------->ImageView{id=2131493330, res-name=item_image, desc=Image,visibility=VISIBLE, width=262, height=262, 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, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0} ****MATCHES****|

+------------->ImageView{id=2131493330, res-name=item_image, desc=Image, visibility=VISIBLE, width=262, height=262, 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, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0} ****MATCHES**** |

推荐答案

你应该使用onData()GridView进行操作:

You should use onData() to operate on GridView:

onData(withId(R.id.item_image))
        .inAdapterView(withId(R.id.grid_adapter_id))
        .atPosition(0)
        .perform(click());

此代码将点击 GridView

这篇关于在 Espresso 中,当多个视图匹配时如何避免 AmbiguousViewMatcherException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆