Click on a menu item that is sometimes found in the overflow menu

currently click on a menu item that is sometimes found on some devices in the overflow menu, I do the following:

fun invokeMenu(@IdRes menuId: Int, @StringRes menuStringRes: Int) {
 try {
  onView(withId(menuId)).perform(click())
 } catch (nmv: NoMatchingViewException) {
  openActionBarOverflowOrOptionsMenu(InstrumentationRegistry.getInstrumentation().targetContext)
  onView(withText(menuStringRes)).perform(click())
 }
}

But I'm looking for a better approach - ideally, where I just need to know the menu identifier. How do you do it in espresso tests?

+4
source share
1 answer

Unfortunately, your ideal case could not be fulfilled. This is due to the construction of support libraries.

PopupMenu, MenuPopupHelper, MenuPopup. , .. StandardMenuPopup. MenuAdapter. 92 MenuAdapter, :

itemView.initialize(getItem(position), 0);

. ActionMenuItemView, ListMenuItemView. , id ActionMenuItemView ListMenuItemView

, MenuAdapter.getItemId(int position) position. .


Hovewer, . :

public static Matcher<View> withMenuIdOrText(@IdRes int id, @StringRes int menuText) {
    Matcher<View> matcher = withId(id);
    try {
        onView(matcher).check(matches(isDisplayed()));
        return matcher;
    } catch (Exception NoMatchingViewException) {
        openActionBarOverflowOrOptionsMenu(InstrumentationRegistry.getInstrumentation().getTargetContext());
        return withText(menuText);
    }
}

:

onView(withMenuIdOrText(R.id.menu_id, R.string.menu_text)).perform(click());
+4

Source: https://habr.com/ru/post/1659537/


All Articles