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
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();
}
}
);
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)));
}
You have to scroll to the button before:
onView(withId(R.id.button_id)).perform(scrollTo(), click());
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.
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());
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.