我正在尝试使用espresso单击列表视图项,但没有成功。
我尝试了stackOverflow上的所有内容
onData(anything()).inAdapterView(withId(R.id.more_menu_list)).atPosition(0).
onChildView(withId(R.id.mm_item_text)).
check(matches(withText("Log in")))
.perform(click());
onView(allOf(is(instanceOf(MenuListAdapter.class)), hasSibling(withText("Log in")))).perform(click());
onData(allOf(is(instanceOf(MenuListAdapter.class)), hasEntry(equalTo("STR"), is("Log in"))))
.perform(click());
onData(anything()).inAdapterView(withContentDescription("Log in")).atPosition(0).perform(click());
onData(hasToString(startsWith("Promotions")))
.inAdapterView(withId(R.id.more_menu_list)).atPosition(0)
.perform(click());
onData(hasEntry(equalTo(MenuListAdapter.class),is("Log out")))
.onChildView(withId(R.id.more_menu_list));
onView(withId(R.id.mm_item_text)).check(matches(withText("Log in")));
I also tried to create a custom matcher
public static Matcher<Object> withItemValue(final String value) {
return new BoundedMatcher<Object, ExtraMenu>(ExtraMenu.class) {
@Override
protected boolean matchesSafely(ExtraMenu item) {
return item.getText().toUpperCase().equals(String.valueOf(value));
}
@Override
public void describeTo(Description description) {
description.appendText("has value " + value);
}
};
}
用作:
onData(withItemValue("Promotions")).inAdapterView(withId(R.id.more_menu_list)).perform(click());
大多数时候我都会收到此错误。有5种菜单 原因:java.lang.RuntimeException:未找到匹配的数据:具有值促销包含的值:<[数据:0(类:java.lang.Integer)令牌:0,数据:1(类:java.lang.Integer)令牌:1,数据:2(类:java.lang.Integer)令牌:2,数据:3(类:java.lang.Integer)令牌:3,数据:4(类:java.lang.Integer)令牌: 4]>
答案 0 :(得分:0)
我找到了一种方法,我将其发布以帮助他人。 我不得不将列表视图更改为回收视图,并使用了此命令。
onView(withId(R.id.list))
.perform(RecyclerViewActions.actionOnItem(
hasDescendant(withText("Your string")), click()));
如果在此代码行之前有多个操作,则可能要使用Thread.sleep(2000)。 另外,您在build.gradle中也需要这种依赖关系:
androidTestImplementation("com.android.support.test.espresso:espresso-contrib:2.2.2") {
exclude group: 'com.android.support', module: 'appcompat'
exclude group: 'com.android.support', module: 'support-v4'
exclude group: 'com.android.support', module: 'support-v7'
exclude group: 'com.android.support', module: 'design'
exclude module: 'support-annotations'
exclude module: 'recyclerview-v7'
}