Click home icon with Espresso

匿名 (未验证) 提交于 2019-12-03 01:59:02

问题:

I am trying to click the home icon in some Espresso tests via:

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?

回答1:

To not depend on the app locale, you can use the code from Matt Logan by replacing "Navigate up" with 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.



回答2:

Use the withContentDescription() Matcher:

onView(withContentDescription("Navigate up")).perform(click());


回答3:

I had trouble navigating back from one Activity to another, but then I found top-level actions:

Espresso.pressBack();


回答4:

I found a real solution to this issue. By using the hierarchyviewer I found that the toolbar looks like this:

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:

public static Matcher 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!

EDIT: The support lib version 24.2.0 uses AppCompatImageButton instead of ImageButton, so I added it, too.

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;


回答5:

I was having problems with "Navigate up" in an emulator, this worked for me:

onView(isRoot()).perform(ViewActions.pressMenuKey());


回答6:

Espresso.pressBack();

Or

onView(withContentDescription("Navigate up")).perform(click());


回答7:

public static Matcher navigationIconMatcher() {     return allOf(             isAssignableFrom(ImageButton.class),             withParent(isAssignableFrom(Toolbar.class))); }  @Test public void clickHamburgerIcon() throws Exception { onView(navigationIconMatcher()).perform(click()); // ... }

this works always!



回答8:

To press back View:

onView(isRoot()).perform(pressBack());


回答9:

Add onbackpress in your activity, and use:

onView(withContentDescription("Navigate up")).perform(click());


回答10:

Maybe you can call:

pressKey(KeyEvent.KEYCODE_HOME);


转载请标明出处:Click home icon with Espresso
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!