如何使用 Espresso 测试操作栏上的主页按钮?

问题描述 投票:0回答:5

我已启用主页按钮以返回到上一个视图。简单地说,这样做:

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> ...

android android-espresso
5个回答
18
投票

当您使用资源中的描述时,可以单击按钮。

onView(withContentDescription(R.string.abc_action_bar_up_description)).perform(click());

与 appcompat-v7:23.1.1 配合使用效果良好


6
投票

我做了以下解决方法:

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。


0
投票

我自己回答。根据 click home icon with espresso 上的帖子,不可能为 ActionBar 中的 Home 按钮指定

Id
,至少在支持库的版本 7 中是不可能的,所以我们应该使用“Navigate up”。但为什么呢?

这就是 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
          }
|

0
投票

使用默认ID时

android:id="@android:id/home"
:

onView(withId(android.R.id.home)).perform(click());

-1
投票

我不知道使用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。对于主页按钮,请单击我使用 Robotiums

Solo#clickOnActionBarHomeButton
。它超过 1 行,如 click home icon with espresso 但您不必指定标题。 (在某些特殊情况下可能有用......)。无论如何,我根据 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();
。它可能对你有用。

© www.soinside.com 2019 - 2024. All rights reserved.