Development issue/problem:

I’m trying to click on the house icon in some of the espresso tests:

onView(withId(android.R.id.home)).perform(click()) ;

This works well for Android > 3.0 – but not for older versions, because appcompat does not seem to use this identifier for this item. What’s a good approach to doing what I want to do?

How can I solve this problem?

Solution 1:

To avoid depending on the application locale, you can use Matt Logan’s code 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 application in more than 5 languages and I had to act on it.

Solution 2:

Use the comparator withContentDescription() :

onView(withContentDescription(Navigate to top)).perform(click()) ;

Solution 3:

I had trouble moving from one activity to another, but then I found activities at a higher level:

Espresso.pressBack() ;

Solution 4:

I’ve found a real solution to this problem. While using the hierarchical viewer I noticed that the toolbar looks like this:

This means that we can assign a hamburger symbol (no return key) as follows:

onView(withContentDescription(open navigation)).perform(click()) ;

But the best solution for me was to understand that the hamburger icon is a simple image button and a direct children’s view of the v7 toolbar. So I’ve written the corresponding tool:

public static Matcher androidHomeMatcher() {rendement allOf(withParent(withClassName(is(Toolbar.class.getName()))), withClassName(is(ImageButton.class.getName())), is(AppCompatImageButton.class.getName())))));});}))

@Test
public invalid clickHamburgerIcon() throws an exception {
onView(androidHomeMatcher()).perform(click());
//.
}

This solution is better because it has to match the appearance regardless of the location used in your test. Click home icon with Espresso

EDIT: Note that the toolbar can be android.support.v7.widget.Toolbar or android.widget.Toolbar – depending on your manual!

EDIT: The Lib version 24.2.0 support uses AppCompatImageButton instead of ImageButton, so I added that as well.

EDIT: In order for this to work, you need to import the correct methods. Here’s the used import:

static import android.support.espresso matcher.ViewMatchers.metClassName;
static import android.support.espresso matcher.ViewMatchers.metParent;
static import org.hamcrest.matchers.allOf;
static import org.hamcrest.matchers.is

Solution No 5:

I had trouble navigating the emulator, it worked for me:

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

Solution No 6:

Espresso.pressBack() ;

Or

onView(withContentDescription(Navigate to top)).perform(click()) ;

Solution No 7:

public static Matcher navigationIconMatcher() {
return allOf(
isAssignableFrom(ImageButton.class),
withParent(isAssignableFrom(Toolbar.class));
}.

@Test
public invalid clickHamburgerIcon() throws Exception {
onView(navigationIconMatcher()).perform(click());
// .
}

It always works!

Solution No 8:

If you want to open/close a drawer, I recommend that you use the Espresso Library:

onView(withId(R.id.drawer_layout)).perform(DrawerActions.open()) ;

Solution No 9:

To click on the Preview button:

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

Solution No 10:

// Click the top navigation button to return to the
onView(withContentDescription(getToolbarNavigationContentDescription()).perform(click()) list;

The methods:

private String getToolbarNavigationContentDescription() {
return TestUtils.getToolbarNavigationContentDescription(
activityTestRule.getActivity(), R.id.toolbar);
}

public static String getToolbarNavigationContentDescription(
@NonNull Activity, @IdRes int toolbarId) {
Toolbar toolbar = activity.findViewById(toolbarId);
if (toolbar!= null) {
return (String) toolbar.getNavigationContentDescription();
} else {
throw new RuntimeException(Toolbar not found.);
}
}.

Good luck!

The message Click on the house icon with the espresso appeared for the first time on – Sharing and storing technological knowledge.

espresso allof,espresso check if view exists,allof android,espresso get text from view,espresso cheat sheet,espresso withclassname,android espresso custom matcher,openactionbaroverfloworoptionsmenu,espresso inroot,espresso ondata,childatposition espresso,espresso select spinner item,check spinner text espresso,espresso check if activity launched,android espresso toast,espresso start activity with intent,espresso android example github,espresso vs appium,espresso onview not found,android espresso tutorial,espresso click button,espresso click menu item,bottomnavigationview espresso click,withid espresso,android-espresso toolbar back button

You May Also Like

Steps To Start And Join Live Voice Chat On Telegram

To attract more users to its cloud-based instant messaging solution, Telegram announced…

4 Browsers that are Super Awesome

Facebook Twitter Pinterest Reddit Messenger WhatsApp Xbox One supports a wide range…

Eve Spectrum 4K 144Hz Gaming Monitor – Review and First Look

The Spectrum 4K 144Hz monitor is increasingly becoming a good option for…

Something went wrong while downloading your template [Fix]

How to correct an error when loading a model? 1. Retrieve a…