RecyclerView espresso 测试 click() 不工作
RecyclerView espresso test click() not working
我遇到了奇怪的浓缩咖啡仪器测试行为。单击回收站视图的项目无效。
此处未发生点击:
onView(withId(R.id.recyclerView)).perform(actionOnItemAtPosition(0, click()));
但这个解决方法有效:
onView(withId(R.id.recyclerView)).perform(actionOnItemAtPosition(0, recyclerClick()));
// ...
public static ViewAction recyclerClick() {
return new ViewAction() {
@Override
public Matcher<View> getConstraints() {
return any(View.class);
}
@Override
public String getDescription() {
return "performing click() on recycler view item";
}
@Override
public void perform(UiController uiController, View view) {
view.performClick();
}
};
}
这是 Espresso 还是 RecyclerView 的问题?
您可以使用 RecyclerViewActions
只需使用:
onView(withId(R.id.recyclerView)).perform(
RecyclerViewActions.actionOnItemAtPosition(0, click()));
例如:
@RunWith(AndroidJUnit4.class)
public class MainActivityTest {
@Rule
public ActivityTestRule<MainActivity> activityRule = new ActivityTestRule<>(MainActivity.class);
@Test
public void clickItem() {
onView(withId(R.id.recycler_view))
.perform(RecyclerViewActions.actionOnItemAtPosition(o, click()));
onView(withId(R.id.text))
.check(matches(withText("XX")))
.check(matches(isDisplayed()));
}
}
应该与RecyclerView
具体无关。 Espresso 对其 ViewActions.click()
实现所做的是将 MotionEvent
发送到目标视图的中心。确保没有 child 拦截它。
我遇到了奇怪的浓缩咖啡仪器测试行为。单击回收站视图的项目无效。
此处未发生点击:
onView(withId(R.id.recyclerView)).perform(actionOnItemAtPosition(0, click()));
但这个解决方法有效:
onView(withId(R.id.recyclerView)).perform(actionOnItemAtPosition(0, recyclerClick()));
// ...
public static ViewAction recyclerClick() {
return new ViewAction() {
@Override
public Matcher<View> getConstraints() {
return any(View.class);
}
@Override
public String getDescription() {
return "performing click() on recycler view item";
}
@Override
public void perform(UiController uiController, View view) {
view.performClick();
}
};
}
这是 Espresso 还是 RecyclerView 的问题?
您可以使用 RecyclerViewActions
只需使用:
onView(withId(R.id.recyclerView)).perform(
RecyclerViewActions.actionOnItemAtPosition(0, click()));
例如:
@RunWith(AndroidJUnit4.class)
public class MainActivityTest {
@Rule
public ActivityTestRule<MainActivity> activityRule = new ActivityTestRule<>(MainActivity.class);
@Test
public void clickItem() {
onView(withId(R.id.recycler_view))
.perform(RecyclerViewActions.actionOnItemAtPosition(o, click()));
onView(withId(R.id.text))
.check(matches(withText("XX")))
.check(matches(isDisplayed()));
}
}
应该与RecyclerView
具体无关。 Espresso 对其 ViewActions.click()
实现所做的是将 MotionEvent
发送到目标视图的中心。确保没有 child 拦截它。