Espresso UI testing and... open drawer image button click

Espresso UI testing and... open drawer image button click

A few days ago I experienced an interesting problem... I had to help a colleague of mine to create some Espresso UI tests and she got a really strange problem... The default Espresso actions' recording tool failed to record the action of performing a click on the open Drawer's ImageButton in the Toolbar!

For unknown reasons, the recorder created these lines of code:

ViewInteraction appCompatImageButton = onView(
    allOf(childAtPosition(
        childAtPosition(withClassName(
            is("android.widget.FrameLayout")), 1), 1), isDisplayed()));
appCompatImageButton.perform(click());

Which is really strange... After quite some time thinking and researching I tried to find the AppCompatImageView this way and it worked:

onView(allOf(isAssignableFrom(AppCompatImageButton.class),
  withParent(isAssignableFrom(Toolbar.class))))
.perform(click());

So, if you have the same issues with the Espresso UI tests recording tool and the toolbar's open drawer button, try to find and click the widget this way. ☺️