问题
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.
回答1:
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 matchingx
in the drop down. The data is provided by theAdaptor
given to theAutoCompleteTextView
, so it can be an object of any type thatAdaptor
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, soonView
can't work!onData
will make sure to load the views that match your data. Checkout here (this whatonData
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 usesRootMatchers.withDecorView(not(is(mActivityRule.getActivity().getWindow().getDecorView())))
which seems to match any window that is not the default one.
Anyways HTH someone else.
回答2:
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>
回答3:
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());
回答4:
You can verify this library example: Library
来源:https://stackoverflow.com/questions/38562341/espresso-autocompletetextview-click