【发布时间】:2017-06-20 00:42:23
【问题描述】:
我有一个列表,其中每一行都包含一个名称和一个调用上下文菜单选项的按钮。我想编写一个测试来验证以下内容
- 上下文菜单包含正确数量的项目
- 上下文菜单包含正确的值
- 上下文菜单不包含任何不必要的选项(上面的检查 1 和 2 将测试这种情况)
我也想测试一下长选时actionBar和actionBar溢出菜单的内容。
对于这两个测试,我可以编写一个检查以确保有一个显示正确“标签”的视图元素(即使用 onView(withText(this.elementText)) 查找视图。但是我有 2 个操作具有标签相同但 ID 不同,我需要确保上下文菜单/长按菜单中的操作正确。
我不能将我在 XML 中指定的 ID 用于上下文菜单的菜单,因为 Android 的上下文菜单视图没有这些 ID,而是包含一个内部 Android ID(参见下面的屏幕截图)。
当我使用 Robotium 编写测试时,我必须获取某种类型的所有当前视图并通过它们进行解析,检查它们是否是 actionBar 项,请参见下面的示例代码。
public static List<MenuItemImpl> getLongClickMenuItems(String itemName) {
List<MenuItemImpl> menuItems = new ArrayList<>();
// long select the item
solo.clickLongOnText(itemName);
// get the children of the of the long click action bar
ArrayList<ActionMenuView> outViews = solo.getCurrentViews(ActionMenuView.class, solo.getView(R.id.action_mode_bar));
if (!outViews.isEmpty()) {
// get the first child which contains the action bar actions
ActionMenuView actionMenuView = outViews.get(0);
// loop over the children of the ActionMenuView which is the individual ActionMenuItemViews
// only a few fit will fit on the actionBar, others will be in the overflow menu
int count = actionMenuView.getChildCount();
for (int i = 0; i < count; i++) {
View child = actionMenuView.getChildAt(i);
if (child instanceof ActionMenuItemView) {
menuItems.add(((ActionMenuItemView) child).getItemData());
} else {
// this is the more button, click on it and wait for the popup window
// which will contain a list of ListMenuItemView
// As we are using the AppCompat the actionBar's menu items are the
// the AppCompat's ListMenuItemView (android.support.v7.view.menu.ListMenuItemView)
// In the context menu, the menu items are Android's native ListMenuItemView
// (com.android.internal.view.menu.ListMenuItemView)
solo.clickOnView(child);
solo.waitForView(ListMenuItemView.class);
ArrayList<ListMenuItemView> popupItems = solo.getCurrentViews(ListMenuItemView.class);
for (ListMenuItemView lvItem : popupItems) {
menuItems.add(lvItem.getItemData());
}
// close the more button actions menu
solo.goBack();
}
}
}
// get out of long click mode
solo.goBack();
return menuItems;
}
有谁知道我如何使用 Expresso 获取 Context Row 菜单项列表。
有谁知道我如何使用 Expresso 获取 actionBar 项目(包括溢出菜单中的所有项目)?
【问题讨论】: