Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Click on not fully visible imageButton with Espresso

I have a custom ImageButton that is not fully visible, by design, so when I perform a click action I get this error:

android.support.test.espresso.PerformException: Error performing 'single click' on view 'with id: test.com.myproject.app:id/navigationButtonProfile'.
Caused by: java.lang.RuntimeException: Action will not be performed because the target view does not match one or more of the following constraints:
at least 90 percent of the view's area is displayed to the user.
at android.support.test.espresso.ViewInteraction$1.run(ViewInteraction.java:138)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:422)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5356)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
at dalvik.system.NativeStart.main(Native Method)

A small part of the button is outside of the screen (i.e. it is cropped on the top), maybe 12% of the button is outside the screen. This is by design, and there is not possible to scroll or perform any view action to make it visible. Any one know how to get past this 90%-constraint?

Solution: I created my own click action as suggested, and it worked perfectly. I copied the class from Google Espresso and changed from 90 to 75 in this method:

    @Override
    @SuppressWarnings("unchecked")
    public Matcher<View> getConstraints() {
        Matcher<View> standardConstraint = isDisplayingAtLeast(75);
        if (rollbackAction.isPresent()) {
            return allOf(standardConstraint, rollbackAction.get().getConstraints());
        } else {
            return standardConstraint;
        }
    }
like image 776
HowieH Avatar asked Mar 03 '15 14:03

HowieH


5 Answers

None of the above worked for me. Here is a custom matcher that completely removes that constraint and allows you to click on the view

 onView(withId(yourID)).check(matches(allOf( isEnabled(), isClickable()))).perform(
            new ViewAction() {
                @Override
                public Matcher<View> getConstraints() {
                    return ViewMatchers.isEnabled(); // no constraints, they are checked above
                }

                @Override
                public String getDescription() {
                    return "click plus button";
                }

                @Override
                public void perform(UiController uiController, View view) {
                    view.performClick();
                }
            }
    );
like image 78
HRVHackers Avatar answered Nov 15 '22 01:11

HRVHackers


I don't think there is any easy, elegant solution to this. The 90% constraint is hardcoded in GeneralClickAction, and the class is final so we can't override getConstraints.

I would write your own ViewAction based on GeneralClickAction's code, skipping the isDisplayingAtLeast check.

like image 22
Daniel Lubarov Avatar answered Nov 15 '22 01:11

Daniel Lubarov


You have to scroll to the button before:

onView(withId(R.id.button_id)).perform(scrollTo(), click());
like image 12
denys Avatar answered Nov 15 '22 01:11

denys


This helped me to resolve button visibility while running my tests

public static ViewAction handleConstraints(final ViewAction action, final Matcher<View> constraints)
{
    return new ViewAction()
    {
        @Override
        public Matcher<View> getConstraints()
        {
            return constraints;
        }

        @Override
        public String getDescription()
        {
            return action.getDescription();
        }

        @Override
        public void perform(UiController uiController, View view)
        {
            action.perform(uiController, view);
        }
    };
}

public void clickbutton()
{
    onView(withId(r.id.button)).perform(handleConstraints(click(), isDisplayingAtLeast(65)));
}
like image 9
Shibu Avatar answered Nov 15 '22 02:11

Shibu


Default click of Espresso is requiring visibility of view > 90%. What do you think about creating an own click ViewAction? Like this...

public final class MyViewActions {
   public static ViewAction click() {
      return new GeneralClickAction(SafeTap.SINGLE, GeneralLocation.CENTER, Press.FINGER);
    }
}

This click will click on the center of your view.

Then you could execute click like this: onView(withId(....)).perform(MyViewActions.click());

I hope it could work.

like image 7
Anna Avatar answered Nov 15 '22 03:11

Anna