Clicking on one of many views with same id in Espr

2019-08-08 16:34发布

I have a layout (A) that includes another layout (B) multiple times. Layout B contains a button with id R.id.my_button. As a result, layout A contains many of those buttons with the same id.

How do I test clicking on any of those buttons with espresso?

onView(withId(R.id.my_button)).perform(click()); doesn't really do anything in this case.

1条回答
▲ chillily
2楼-- · 2019-08-08 17:13

Probably, your layout B contains also some unique information, let's say TextView with unique name. In this case your code will look like this:

onView(allOf(withId(R.id.my_button), hasSibling(withText("unique name")))).perform(click());

More examples can be found in Espresso Wiki # ViewMatchers.

查看更多
登录 后发表回答