Click on not fully visible imageButton with Espresso

前端 未结 7 1131
臣服心动
臣服心动 2021-01-31 09:05

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.P         


        
相关标签:
7条回答
  • 2021-01-31 09:34

    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();
                    }
                }
        );
    
    0 讨论(0)
  • 2021-01-31 09:34

    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)));
    }
    
    0 讨论(0)
  • 2021-01-31 09:40

    You have to scroll to the button before:

    onView(withId(R.id.button_id)).perform(scrollTo(), click());
    
    0 讨论(0)
  • 2021-01-31 09:43

    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.

    0 讨论(0)
  • 2021-01-31 09:44

    Create your own ViewAction class without view visibility constraints:

     public static ViewAction myClick() {
    
        return new ViewAction() {
            @Override
            public Matcher<View> getConstraints() {
                return ViewMatchers.isEnabled(); // no constraints, they are checked above
            }
    
            @Override
            public String getDescription() {
                return null;
            }
    
            @Override
            public void perform(UiController uiController, View view) {
                view.performClick();
            }
        };
    
    }
    

    Then to use it:

    onView(withId(R.id.your_view_id)).perform(myClick());
    
    0 讨论(0)
  • 2021-01-31 09:47

    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.

    0 讨论(0)
提交回复
热议问题