Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Espresso AutoCompleteTextView click

So I recently started messing around with Espresso in one of my existing Android projects.

Everything went pretty decently, until I came to find AutoCompleteTextView in my program. I don't seem to understand how to properly click the first thing in the autocomplete list. I'm actually not even sure which to use, onView() or onData() in this instance.

like image 702
user3050720 Avatar asked Jul 25 '16 07:07

user3050720


3 Answers

By some reasons which I don't know, AStupidNoob's solution doesn't work. So I found another one:

onView(withText("Spinner Item"))
            .inRoot(RootMatchers.isPlatformPopup())
            .perform(click());

The AutoCompleteTextView itself

<com.google.android.material.textfield.TextInputLayout
    android:id="@+id/textInputLayout2"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="12dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="8dp"
    app:layout_constraintEnd_toStartOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <AutoCompleteTextView
        android:id="@+id/product"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:completionThreshold="1"
        android:hint="@string/product"
        android:singleLine="true"
        android:textSize="16sp" />

</com.google.android.material.textfield.TextInputLayout>
like image 55
Akbolat SSS Avatar answered Oct 19 '22 13:10

Akbolat SSS


I think I found a bit of a cleaner method than the accepted answer!

onData(equalTo("ITEM")).inRoot(RootMatchers.isPlatformPopup()).perform(click());

The breakdown:

  • onData(x) This will find the view rendering the data object matching x in the drop down. The data is provided by the Adaptor given to the AutoCompleteTextView, so it can be an object of any type that Adaptor provides, it probably won't be a View. You'll want to use the standard hamcrest core matchers for this (equalTo, instanceOf, etc...) rather than (withText, withId, etc...). It might be a pain to try and find what object this is and how to match it, but there isn't a neater way: with a lot of items in your adapter some of the views won't even be in the hierarchy yet, so onView can't work! onData will make sure to load the views that match your data. Checkout here (this what onData returns) and here (this loads the matching data)
  • inRoot(RootMatchers.isPlatformPopup()) So it turns out the dropdown menu is on another window than the default window your activity runs in. So we have to specify that we want to search that window. The accepted answer uses RootMatchers.withDecorView(not(is(mActivityRule.getActivity().getWindow().getDecorView()))) which seems to match any window that is not the default one.

Anyways HTH someone else.

like image 13
AStupidNoob Avatar answered Oct 19 '22 14:10

AStupidNoob


So i finally figured it out, thanks to this previous question: Testing autocomplete textview using espresso tool

Ill just post my version of it for people who might use it in future.

    onData(instanceOf("Whatever your arrayadapter contains".class)).inRoot(RootMatchers.withDecorView(not(is(mActivityRule.getActivity().getWindow().getDecorView())))).perform(ViewActions.click());
like image 1
user3050720 Avatar answered Oct 19 '22 15:10

user3050720