Clicking a button in Espresso test got problem. Let's say I have two activities "Activity1" and "Activity2". Click a dialog OK button in Activity1 starts Activity2 where the button in Activity2 can't be clicked.
// The current activity in testing
// .....
onView(withText(R.string.dialog_btn_ok)).perform(click()); // go to the second activity
// The button on the second activity
onView(withId(R.id.btnMP)).check(matches(isDisplayed())); // this is ok
onView(withId(R.id.btnMP)).perform(click()); // got error here
android.support.test.espresso.PerformException: Error performing 'single click' on view 'with id: ..........
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. Target view: "Button{id=2131296390, res-name=btnMP, visibility=VISIBLE, width=652, height=160, has-focus=false, has-focusable=true, has-window-focus=true, is-clickable=true, is-enabled=true, is-focused=false, is-focusable=true, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=20.0, y=-16.0, text=Modify Parameter, input-type=0, ime-target=false, has-links=false}"
When I change this with perform(scrollTo())
, a different error is shown.
// The button on the second activity
onView(withId(R.id.btnMP)).check(matches(isDisplayed())); // this is ok
onView(withId(R.id.btnMP)).perform(scrollTo(), click()); // got error here
android.support.test.espresso.PerformException: Error performing 'scroll to' on view 'with id ....
Caused by: java.lang.RuntimeException: Action will not be performed because the target view does not match one or more of the following constraints: (view has effective visibility=VISIBLE and is descendant of a: (is assignable from class: class android.widget.ScrollView or is assignable from class: class android.widget.HorizontalScrollView)) Target view: "Button{id=2131296390, res-name=btnMP, visibility=VISIBLE, width=652, height=160, has-focus=false, has-focusable=true, has-window-focus=true, is-clickable=true, is-enabled=true, is-focused=false, is-focusable=true, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=20.0, y=-16.0, text=Modify Parameter, input-type=0, ime-target=false, has-links=false}" at
The solution is to create your custom GeneralClickAction with view visibility that you want to click less then Espresso's GeneralClickAction requires. Currently the minimum visibility value is 90% - see code here line 60. Set it to be 80% or less. Just copy the whole class rename it and change the value provided to this method to 80
isDisplayingAtLeast(80)
. Then create your click action that uses your custom GeneralClickAction:But I'd rather fix the layout of the activity if possible to avoid creation of workaround for button visibility.
Seems your view with id
R.id.btnMP
is not visible at the screen so you are receiving the first error. You are trying to resolve this byscrollTo()
but your view in not inside ScrollView. So how your activity is organized? If you are usingRecyclerView
(for example) you should use special version of scrollTo - http://developer.android.com/reference/android/support/test/espresso/contrib/RecyclerViewActions.html and so on. So first take a look where your view is hosted and then it will be clear how to scroll to it.