如何使用Espresso测试操作栏上的主页按钮? [英] How do I test the home button on the Action Bar with Espresso?

查看:65
本文介绍了如何使用Espresso测试操作栏上的主页按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已启用主页按钮以返回上一个视图.只需执行以下操作即可:

I have enabled the home button to return to the previous view. Simply, doing this:

getActionBar().setDisplayHomeAsUpEnabled(true);

我正在使用 com.android.support:appcompat-v7:21.0.2 的最新版本.但是,当我使用以下代码时,抛出异常将不起作用.

I'm using the last version of com.android.support:appcompat-v7:21.0.2. However, when I use the below code it doesn't work throwing a Exception.

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:找不到层次结构中符合ID为:的视图是< 2131296261>...

推荐答案

我做了以下解决方法:

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.

Basically I use the content description attribute instead of the view id.

这篇关于如何使用Espresso测试操作栏上的主页按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆