Android 单击带有 Espresso 的主页图标
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23985181/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Click home icon with Espresso
提问by ligi
I am trying to click the home icon in some Espresso tests via:
我试图通过以下方式单击某些 Espresso 测试中的主页图标:
onView(withId(android.R.id.home)).perform(click());
This works fine for Android > 3.0 - but fails for older versions as appcompat
does not seem to use this id for this element then. What is a good approach to do what I want to do?
这适用于 Android > 3.0 - 但对于旧版本失败,因为当时appcompat
似乎没有将此 id 用于此元素。做我想做的事情的好方法是什么?
回答by sunlover3
To not depend on the app locale, you can use the code from Matt Loganby replacing "Navigate up" with R.string.abc_action_bar_up_description
:
为了不依赖于应用程序语言环境,您可以使用Matt Logan的代码,将“导航”替换为R.string.abc_action_bar_up_description
:
onView(withContentDescription(R.string.abc_action_bar_up_description)).perform(click());
This helped me a lot because I have an app in more than 5 languages and I had to act like this.
这对我帮助很大,因为我有一个超过 5 种语言的应用程序,我不得不这样做。
回答by Matt Logan
Use the withContentDescription()
Matcher
:
使用withContentDescription()
Matcher
:
onView(withContentDescription("Navigate up")).perform(click());
回答by Igor Filippov
I had trouble navigating back from one Activity to another, but then I found top-level actions:
我在从一个 Activity 导航回另一个 Activity 时遇到了麻烦,但后来我发现了顶级操作:
Espresso.pressBack();
回答by mreichelt
I found a real solution to this issue. By using the hierarchyviewer I found that the toolbar looks like this:
我找到了这个问题的真正解决方案。通过使用hierarchyviewer,我发现工具栏看起来像这样:
This means we could match the hamburger icon (not back button) like this:
这意味着我们可以像这样匹配汉堡包图标(不是后退按钮):
onView(withContentDescription("Open navigation")).perform(click());
But a better solution to me was to find out that the hamburger icon is the only ImageButton and a direct child view of the v7 Toolbar. So I wrote a helper method to match it:
但对我来说更好的解决方案是发现汉堡包图标是唯一的 ImageButton 和 v7 工具栏的直接子视图。所以我写了一个辅助方法来匹配它:
public static Matcher<View> androidHomeMatcher() {
return allOf(
withParent(withClassName(is(Toolbar.class.getName()))),
withClassName(anyOf(
is(ImageButton.class.getName()),
is(AppCompatImageButton.class.getName())
)));
}
@Test
public void clickHamburgerIcon() throws Exception {
onView(androidHomeMatcher()).perform(click());
// ...
}
This solution is better because it should match the view no matter which locale you use in your test. :-)
此解决方案更好,因为无论您在测试中使用哪种语言环境,它都应该与视图匹配。:-)
EDIT: Note that Toolbar might be android.support.v7.widget.Toolbar or android.widget.Toolbar - depending on your use case!
编辑:请注意,工具栏可能是 android.support.v7.widget.Toolbar 或 android.widget.Toolbar - 取决于您的用例!
EDIT: The support lib version 24.2.0 uses AppCompatImageButton instead of ImageButton, so I added it, too.
编辑:支持库版本 24.2.0 使用 AppCompatImageButton 而不是 ImageButton,所以我也添加了它。
EDIT: You have to import the correct methods to get this to work. Here are the imports used:
编辑:您必须导入正确的方法才能使其正常工作。以下是使用的导入:
import static android.support.test.espresso.matcher.ViewMatchers.withClassName;
import static android.support.test.espresso.matcher.ViewMatchers.withParent;
import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.is;
回答by Robin Sch?nborn
I was having problems with "Navigate up" in an emulator, this worked for me:
我在模拟器中“向上导航”时遇到问题,这对我有用:
onView(isRoot()).perform(ViewActions.pressMenuKey());
回答by Dong Thang
Espresso.pressBack();
Or
或者
onView(withContentDescription("Navigate up")).perform(click());
回答by user2210676
public static Matcher<View> navigationIconMatcher() {
return allOf(
isAssignableFrom(ImageButton.class),
withParent(isAssignableFrom(Toolbar.class)));
}
@Test
public void clickHamburgerIcon() throws Exception {
onView(navigationIconMatcher()).perform(click());
// ...
}
this works always!
这总是有效!
回答by testsingh
To press back View:
按下返回查看:
onView(isRoot()).perform(pressBack());
回答by Milo? ?ernilovsky
If your intention is opening / closing the drawer, I recommend using the Espresso contrib library:
如果您打算打开/关闭抽屉,我建议使用 Espresso contrib 库:
onView(withId(R.id.drawer_layout)).perform(DrawerActions.open());
回答by user12375116
onView(withContentDescription("Open navigation drawer")).perform(click())
onView(withContentDescription("打开导航抽屉")).perform(click())
this helped me
这对我有帮助