Winton Hou
Winton Hou

Reputation: 901

PerformException: Error performing 'single click'

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.

Upvotes: 88

Views: 75296

Answers (14)

Rohit Singla
Rohit Singla

Reputation: 122

Sharing the adb commands(to be run after attached the device) for the solution provided by @appoll which has worked for me:-

adb shell settings put global window_animation_scale 0
adb shell settings put global transition_animation_scale 0
adb shell settings put global animator_duration_scale 0

You should also try out closeSoftKeyboard()

Upvotes: 0

Marawan Mamdouh
Marawan Mamdouh

Reputation: 1015

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

Upvotes: 0

harley hu
harley hu

Reputation: 81

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

Upvotes: 0

Mahmoud Mabrok
Mahmoud Mabrok

Reputation: 1482

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().

Upvotes: 4

L&#237;dia Redondo
L&#237;dia Redondo

Reputation: 11

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

Upvotes: 0

Alexander Pacha
Alexander Pacha

Reputation: 9710

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.

Upvotes: 142

Pinak Gauswami
Pinak Gauswami

Reputation: 817

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.

Upvotes: 1

Vilen
Vilen

Reputation: 5061

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.

Upvotes: 0

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

Upvotes: 15

jyo cool
jyo cool

Reputation: 11

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

Upvotes: 1

Chen Lixing
Chen Lixing

Reputation: 1

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.

Upvotes: -5

Prabin Timsina
Prabin Timsina

Reputation: 2182

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

Upvotes: 39

Krishna
Krishna

Reputation: 1634

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

Upvotes: 6

appoll
appoll

Reputation: 2950

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.

Upvotes: 44

Related Questions