我如何使用 espresso 从基于项目文本的列表视图中单击项目?
How can i use espresso to click an item from a list view based on item' text?
我正在尝试使用浓缩咖啡单击列表视图项目但没有成功。
我尝试了 Whosebug 上的所有内容
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(class:java.lang.Integer)标记:0,数据:1(class: java.lang.Integer) 令牌: 1, 数据: 2 (class: java.lang.Integer) 令牌: 2, 数据: 3 (class: java.lang.Integer) 令牌: 3、数据:4(class:java.lang.Integer) 令牌:4]>
我找到了一个方法,我会post它来帮助别人。
我必须将列表视图更改为回收视图并使用此命令。
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'
}
我正在尝试使用浓缩咖啡单击列表视图项目但没有成功。
我尝试了 Whosebug 上的所有内容
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(class:java.lang.Integer)标记:0,数据:1(class: java.lang.Integer) 令牌: 1, 数据: 2 (class: java.lang.Integer) 令牌: 2, 数据: 3 (class: java.lang.Integer) 令牌: 3、数据:4(class:java.lang.Integer) 令牌:4]>
我找到了一个方法,我会post它来帮助别人。 我必须将列表视图更改为回收视图并使用此命令。
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'
}