如何使用Espresso测试操作栏上的主页按钮? [英] How do I test the home button on the Action Bar with 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屋!