lig*_*igi 4 android optionmenu android-espresso
目前点击溢出菜单中某些设备上的菜单项我正在执行以下操作:
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())
}
}
Run Code Online (Sandbox Code Playgroud)
但我正在寻找一种更好的方法 - 理想情况下,我必须知道菜单ID.你如何在浓咖啡测试中做到这一点?
不幸的是,你的理想情况无法完成.这是由于支持库的构建.
让我们从PopupMenu参考的开始,MenuPopupHelper参考MenuPopup.这是一个扩展的抽象类ie.通过StandardMenuPopup.它参考了MenuAdapter.如果你看看MenuAdapter你会看到的第92行,那么这一行:
itemView.initialize(getItem(position), 0);
Run Code Online (Sandbox Code Playgroud)
这是关键方法调用.它可以在ActionMenuItemView或中调用ListMenuItemView.他们的实现在这种情况下有所不同,id附加到ActionMenuItemView,并且没有附加到ListMenuItemView
而且,MenuAdapter.getItemId(int position)回报只是position.菜单项的ID在溢出菜单中丢失.
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);
}
}
Run Code Online (Sandbox Code Playgroud)
用法:
onView(withMenuIdOrText(R.id.menu_id, R.string.menu_text)).perform(click());
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
462 次 |
| 最近记录: |