Click on not fully visible imageButton with Espresso

江枫思渺然 提交于 2019-12-03 03:37:54

问题


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;
        }
    }

回答1:


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.




回答2:


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();
                }
            }
    );



回答3:


You have to scroll to the button before:

onView(withId(R.id.button_id)).perform(scrollTo(), click());



回答4:


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)));
}



回答5:


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.




回答6:


For the same error on a text view I ended up doing as below, the answers posted above and other SO questions were a lot of help. I did try scrollTo method, but for some reason the error persisted. Hope this helps someone.

        onView(allOf(withId(R.id.recycler_view), isDisplayed())).perform(RecyclerViewActions
                .actionOnItem(hasDescendant(withText("my text")), click()));


来源:https://stackoverflow.com/questions/28834579/click-on-not-fully-visible-imagebutton-with-espresso

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