Android 如何选择浓缩咖啡溢出菜单中的复选框?
我正在编写一个带有复选框的溢出菜单的UI测试,如下图所示。我想检查每个复选框的状态,并切换它们,但我很难找到匹配它们的方法。复选框及其标签是不同的视图,因此我无法将复选框与Android 如何选择浓缩咖啡溢出菜单中的复选框?,android,checkbox,android-espresso,overflow-menu,Android,Checkbox,Android Espresso,Overflow Menu,我正在编写一个带有复选框的溢出菜单的UI测试,如下图所示。我想检查每个复选框的状态,并切换它们,但我很难找到匹配它们的方法。复选框及其标签是不同的视图,因此我无法将复选框与withText()匹配,而hasSibling()也不起作用。因为布局是由菜单框架生成的,所以我无法控制复选框的id(它们都有id“checkbox”) 这是菜单的外观: 这是我在匹配和切换复选框方面的尝试。它失败时出现NoMatchingViewException private static void toggleMe
withText()
匹配,而hasSibling()
也不起作用。因为布局是由菜单框架生成的,所以我无法控制复选框的id(它们都有id“checkbox”)
这是菜单的外观:
这是我在匹配和切换复选框方面的尝试。它失败时出现NoMatchingViewException
private static void toggleMenu(String label, boolean initial) {
openActionBarOverflowOrOptionsMenu(getInstrumentation().getTargetContext());
ViewInteraction v = onView(allOf(instanceOf(CheckBox.class), hasSibling(withText(label)), isCompletelyDisplayed()));
if(initial)
v.check(matches(isChecked()));
else
v.check(matches(not(isChecked())));
v.perform(click());
openActionBarOverflowOrOptionsMenu(getInstrumentation().getTargetContext());
v = onView(allOf(instanceOf(CheckBox.class), hasSibling(withText(label)), isCompletelyDisplayed()));
if(!initial)
v.check(matches(isChecked()));
else
v.check(matches(not(isChecked())));
pressBack();
}
因此,事实证明,标签在层次结构中比复选框低一级,复选框同级的子级也是如此。因此,查找复选框并将其切换的代码如下:
private static void toggleMenu(String label, boolean initial) {
openActionBarOverflowOrOptionsMenu(getInstrumentation().getTargetContext());
ViewInteraction v = onView(allOf(instanceOf(CheckBox.class), hasSibling(withChild(withText(label))), isCompletelyDisplayed()));
if(initial)
v.check(matches(isChecked()));
else
v.check(matches(not(isChecked())));
v.perform(click());
openActionBarOverflowOrOptionsMenu(getInstrumentation().getTargetContext());
v = onView(allOf(instanceOf(CheckBox.class), hasSibling(withChild(withText(label))), isCompletelyDisplayed()));
if(!initial)
v.check(matches(isChecked()));
else
v.check(matches(not(isChecked())));
pressBack();
}