84

I got a error when i run android espresso test:

com.google.android.apps.common.testing.ui.espresso.PerformException: Error performing 'single click' on view 'with id: is <2131034173>'.

My code is easy:

onView(withId(R.id.btn)).perform(click());

But there is no error with this code:

onView(withId(R.id.btn)).check(matches(isDisplayed()));

I can not find the cause why it happen.

appoll
  • 2,910
  • 28
  • 40
Winton Hou
  • 861
  • 1
  • 6
  • 4
  • 4
    Please post the complete stack trace. The first bit of the error message is not enough. With PerformExceptions, the interesting bits are in the "Caused by" section, it shows the Exception which originally made the click fail. – haffax Apr 24 '15 at 19:11
  • 1
    @Winton did you find any solution of this problem? i am running through the same issue – Gaurav Dec 18 '15 at 16:15
  • This can happen when you run a batch of tests. Try running the same test again (that test alone), and see if you are still facing the same issue. – Henry Aug 29 '16 at 08:29

13 Answers13

140

The trick is to read the full stack-trace of the error. In the middle, there is some crucial piece of information like this:

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: "ImageView{id=2131492903, res-name=button_hamburger, desc=opens the side drawer, visibility=VISIBLE, width=64, height=64, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=true, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=6.0, y=6.0}"

which explains the error in detail.

Alexander Pacha
  • 9,187
  • 3
  • 68
  • 108
  • 1
    Thanks! In my case its because of NPE. Its strange since I expect my variables to be instantiated. It seems I have to run the entire test so it initializes the Activity/Fragment before running it. I am running a single test method(which describe a single user story). I highly suggest to google to remove the play button beside the test mode as in this case it is useless, I think it is also useless mostly on other cases. :/ – Neon Warge Oct 15 '16 at 13:30
  • Thanks! In my case I failed to add the activity that is supposed to be started in the test, to the manifest file. – Saulo Aguiar Nov 03 '16 at 00:04
  • It was exactly my case, Thanks! – Mo Zaatar Apr 24 '17 at 02:09
  • Very useful advice to read the whole stacktrace - I had NPE in another thread causing the issue with click, which can be barely told from the main message. – Ivan Apr 24 '17 at 20:29
  • for me it was because I was mucking about with whatsapp during the tests, not surprising really! – hmac Feb 26 '20 at 12:53
43

Try to make sure that the soft keyboard is not showing. It can easily be closed with the closeSoftKeyboard ViewAction.

Moreover, make sure that system animations are disabled. Under Settings -> Developing Options turn off the following:

  • Window animation scale
  • Transition animation scale
  • Animator duration scale

Also, this might be caused by ANR dialogs from other apps.

There's been an issue reported here as well.

appoll
  • 2,910
  • 28
  • 40
  • 2
    Thanks. I had changed my code and turn all scale off, but error is stil there. There is no ANR dialogs while run test. – Winton Hou Apr 24 '15 at 01:41
  • Great answer. Espresso.closeSoftKeyboard() save my day... as my test second test fail (regardless of the order) when both first and second test has a button to click.. – Elye Aug 09 '16 at 07:24
36

I had the same problem because the soft keyboard was overlapping the element. I used scrollTo() followed by click() to resolve the issue.

onView(withId(R.id.btn))
     .perform(scrollTo())
     .perform(click());

If above does not work, try adding the following first:

onView(withId(R.id.myEditText)).perform(closeSoftKeyboard());
Maher Abuthraa
  • 17,493
  • 11
  • 81
  • 103
Prabin Timsina
  • 2,131
  • 3
  • 16
  • 27
14

I had that problem even using

onView(withId(R.id.myEditText)).perform(closeSoftKeyboard());

What I found out, was that in my case, on some devices, Each Time I used

onView(withId(R.id.myEditText)).perform(TypeTextAction());

It was like the system stacked a new keyboard on top of another, so what solved my problem was to ALWAYS use closeSoftKeyboard() EVERY-TIME I used TypeTextAction Like this.

onView(withId(R.id.myEditText)).perform(typeTextAction(), closeSoftKeyboard());

So if I needed to edit a form it would be like:

onView(withId(R.id.myEditText1)).perform(typeTextAction(), closeSoftKeyboard());
onView(withId(R.id.myEditText2)).perform(typeTextAction(), closeSoftKeyboard());
onView(withId(R.id.myEditText3)).perform(typeTextAction(), closeSoftKeyboard());
onView(withId(R.id.myEditText4)).perform(typeTextAction(), closeSoftKeyboard());
Eric Aya
  • 69,473
  • 35
  • 181
  • 253
6

If the view is not visible during the testing...use perform(scrollTo())...It will scroll and click action will perfrom.

Example :-

 onView(withId(R.id.btn)).perform(scrollTo()).perform(click());
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
Krishna
  • 1,556
  • 13
  • 21
4

after you perform typeText with edit text, close the soft keyboard as it may cover your view

by closeSoftKeyboard()

so full code be:

    onView(withId(R.id.fab)).perform(click())
    onView(withId(R.id.edReminder)).perform(typeText("TestAA"))
    closeSoftKeyboard()
    onView(withId(R.id.btnAdd)).perform(click())

NOTE: using closeSoftKeyboard() with perform() not permitted as perform() accept ViewAction and closeSoftKeyboard() return Unit use ViewActions.closeSoftKeyboard() with perform().

Mahmoud Mabrok
  • 1,362
  • 16
  • 24
1

I had the same issue, And solved it by changing the position of the element.

There is no element on the position where I was trying to click. Trying to click on position 3 but the element is at 2nd position(Completely forgot that index starts from 0) So, I changed the position of the element and its working perfectly now

jyo cool
  • 11
  • 1
  • Also solved this way. For Kaspresso/Kakao it will be `val button = KButton { withIndex(1) { withId(R.id.button) } }`. – CoolMind Jan 16 '23 at 01:43
1

One Possibility in your TestCase is that if you perform database operations using LiveData so you should avoid use below Rule.

@Rule public InstantTaskExecutorRule instantTaskExecutorRule = new InstantTaskExecutorRule();

After Removing these Line TestCase work Perfectly.

Pinak Gauswami
  • 789
  • 6
  • 10
0

there are few reasons this can happen. in my case it was because after clicking a button there was a progress bar that continued spinning forever, so make sure if there is a network call or some waiting process, you stop progress bar after callback is received. also performing a click waits for some action to happen so make sure you are not just clicking but also performing action when click is performed.

Vilen
  • 5,061
  • 3
  • 28
  • 39
0

For me it was like Eric Aya said. But I didn't close de keyboard everytime, just before and after I needed to "change the keyboard" because I was typing a number Edittext.

    //Type the user data
    onView(withId(R.id.edit_name)).perform(typeText(name));
    onView(withId(R.id.edit_lastname)).perform(typeText(lastname));
    onView(withId(R.id.edit_email2)).perform(typeText(email));
    onView(withId(R.id.edit_password2)).perform(typeText(password), closeSoftKeyboard());

    //if its a number edittext we have to use String.valueOf
    //also we need to closesoftkeyboard before and after, so it changes from text 
    //to number
    onView(withId(R.id.edit_age)).perform(typeText(String.valueOf(age)), 
    closeSoftKeyboard());
    onView(withId(R.id.edit_is_admin)).perform(typeText(admin), closeSoftKeyboard());
0

becase my layout is not scrollview, so I can not use perform(scrollTo()). I solve it by add annotation @Config(qualifiers = "h750dp").

harley hu
  • 81
  • 1
  • 4
0

This may happen because you forget to close your keyboard,

So when you take an input and then click a button, make sure you close your keyboard first.

For clarification, see this example.

onView(withId(R.id.reminderTitle)).perform(replaceText("test string"), closeSoftKeyboard())
onView(withId(R.id.saveReminder)).perform(click())
Marawan Mamdouh
  • 584
  • 1
  • 6
  • 15
-5

The error was caused by UI thread block. Please check your target Activity code, especially the setUp or init function.

I have met the same error, there is a wrong listener in UI thread which always be called. When I remove the listener, the error could be fixed.

Amar Pratap
  • 1,000
  • 7
  • 20