我启用了主页按钮以返回到上一个视图。简单地说,这样做:
getActionBar().setDisplayHomeAsUpEnabled(true);
我使用的是com.android.support:appcompat-v7:21.0.2
的最新版本。但是,当我使用以下代码时,它无法引发异常。
Espresso.onView(ViewMatchers.withId(android.R.id.home)).perform(ViewActions.click());
Espresso.onView(ViewMatchers.withId(R.id.home)).perform(ViewActions.click());
例外:
com.google.android.apps.common.testing.ui.espresso.NoMatchingViewException: No views in hierarchy found matching: with id: is <2131296261> ...
当您使用资源中的描述时,可以单击该按钮。
onView(withContentDescription(R.string.abc_action_bar_up_description)).perform(click());
与 appcompat-v7:23.1.1 配合使用正常
我做了以下解决方法:
private String getString(int resId){
return getInstrumentation().getTargetContext().getString(resId);
}
public void testUI() {
onView(withContentDescription(getString(R.string.navigation_drawer_open))).perform(click());
onView(withContentDescription(getString(R.string.navigation_drawer_close))).perform(click());
}
基本上,我使用内容描述属性而不是视图ID。
我回答自己。根据带有浓缩咖啡的单击主页图标的帖子,无法为ActionBar中的主页按钮指定Id
,至少在支持库的第7版中,因此我们应该使用"向上导航"。但是为什么?
这就是Espresso错误跟踪的原因:
--------> ActionMenuView
{
id=-1, visibility=VISIBLE, width=144, height=168, has-focus=false,
has-focusable=true, has-window-focus=true, is-clickable=false,
is-enabled=true, is-focused=false, is-focusable=false,
is-layout-requested=false, is-selected=false, root-is-layout-requested=false,
has-input-connection=false, x=936.0, y=0.0, child-count=1
}
|
--------> ActionMenuItemView
{
id=2131296554, res-name=general, desc=, visibility=VISIBLE,
width=144, height=144, has-focus=false, has-focusable=true,
has-window-focus=true, is-clickable=true, is-enabled=true,
is-focused=false, is-focusable=true, is-layout-requested=false,
is-selected=false, root-is-layout-requested=false,
has-input-connection=false, x=0.0, y=12.0, text=, input-type=0, ime-target=false
}
|
--------> ImageButton
{
id=-1, desc=Navigate up, visibility=VISIBLE, width=168, height=168,
has-focus=false, has-focusable=true, has-window-focus=true, is-clickable=true,
is-enabled=true, is-focused=false, is-focusable=true, is-layout-requested=false,
is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0
}
|
使用appCompat的操作栏有这么大的区别。正如你们在评论中所写的那样,"导航"确实有效。对于那些像我一样无法计算如何使用浓缩咖啡的人来说,这是方法:
import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.action.ViewActions.click;
import static android.support.test.espresso.matcher.ViewMatchers.withContentDescription;
onView(withContentDescription("Navigate up")).perform(click());
已为 AppCompat v7 而降级
在我的项目中,我一起使用Robotium和Espresso。对于主页按钮,单击我使用机器人Solo#clickOnActionBarHomeButton
。它超过1行,就像在点击主页图标和浓缩咖啡但您不必指定标题。(在某些特殊情况下可能很有用...无论如何,我根据SDK版本决定使用哪一个:
public static void actionBarBack(Instrumentation instrumentation, final Solo solo, String actionBarText) {
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT) {
onView(allOf(withText(actionBarText), isDisplayed())).perform(click());
}
else {
instrumentation.runOnMainSync(new Runnable() {
@Override
public void run() {
solo.clickOnActionBarHomeButton();
}
});
}
}
这是用法:
Compatibility.actionBarBack(getInstrumentation(), solo, R.string.any);
它必须包装在runOnMainSync
中,因为 Robotium 测试框架有点懒惰,如果您有断言,例如操作栏标题,在语句之后它仍然不会回来。但只能尝试solo.clickOnActionBarHomeButton();
.它可能对你有用。