首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何用Espresso测试Action上的home按钮?

如何用Espresso测试Action上的home按钮?
EN

Stack Overflow用户
提问于 2014-12-17 14:36:20
回答 4查看 9.7K关注 0票数 12

我已经启用了home按钮返回到前面的视图。简单地说,这样做:

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

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2015-03-24 13:53:49

我做了以下解决办法:

代码语言:javascript
运行
复制
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。

票数 6
EN

Stack Overflow用户

发布于 2015-12-28 14:31:24

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

代码语言:javascript
运行
复制
onView(withContentDescription(R.string.abc_action_bar_up_description)).perform(click());

适用于appcompat-v7:23.1.1

票数 18
EN

Stack Overflow用户

发布于 2014-12-17 15:17:31

我自己回答。根据click home icon with espresso上的帖子,不可能为ActionBar中的Home按钮指定一个Id,至少在支持库的with 7中是这样的,所以我们应该使用“导航向上”。但是为什么呢?

这是由于Espresso错误跟踪的原因:

代码语言:javascript
运行
复制
--------> 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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27527988

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档