Espresso如何点击长选项菜单中尚未显示的(选项)菜单项?
打开选项菜单很简单:
openActionBarOverflowOrOptionsMenu( getInstrumentation().getTargetContext());
我试过,例如scrollTo,但它没有成功:
onView( withText("Option menu item text")).perform( scrollTo(), click());
onView( withText( R.id.optionMenuId)).perform( scrollTo(), click());
onView( withId( is( R.id.appOptionMenu))).perform( swipeDown()); // where SwipeDown is a simple utility method on GeneralSwipeAction.
onData( anything()).inAdapterView( withId(R.id.wpeOptionMenu)).atPosition( 12).perform(click()); // I guess because it is not an adapter
你有一个很好的解决方案吗?
答案 0 :(得分:8)
ActionBar溢出菜单是PopUpWindow containing a ListView。
scrollTo() only works on descendants of ScrollView,所以不会在这里工作。
因为您想要的视图位于AdapterView中,所以您需要使用onData。
数据对象是AdapterView,类型为MenuItem,您希望匹配Menu项的标题。像这样:
onData(allOf(instanceOf(MenuItem.class), withTitle(title))).perform(click());
static MenuItemTitleMatcher withTitle(String title) {
return new MenuItemTitleMatcher(title);
}
class MenuItemTitleMatcher extends BaseMatcher<Object> {
private final String title;
public MenuItemTitleMatcher(String title) { this.title = title; }
@Override public boolean matches(Object o) {
if (o instanceof MenuItem) {
return ((MenuItem) o).getTitle().equals(title);
}
return false;
}
@Override public void describeTo(Description description) { }
}