Android. Espresso. How to click on Spinner item with text?
12,147
Solution 1
I've already found this answer:
Replace
withText()
withwithSpinnerText()
onView(withId(spinnerId)).perform(click()); onData(allOf(is(instanceOf(String.class)), is(selectionText))).perform(click()); onView(withId(spinnerId)).check(matches(withSpinnerText(containsString(selectionText))));
Reference: https://code.google.com/p/android-test-kit/issues/detail?id=85
So instead of using a bit complicated:
onData(anything())
.inAdapterView(withId(R.id.spn_trans_type))
.onChildView(allOf(withId(textViewIdToTest), withText(expectedText)))
.perform(click());
maybe you should use
onData(allOf(is(instanceOf(String.class)), is(selectionText)))
.perform(click());
onView(withId(spinnerId))
.check(matches(withSpinnerText(containsString(selectionText))));
where selectionText
would be your expected string value and spinnerId
an id of your Spinner
view.
Solution 2
In my case, the simplest possible solution worked (Kotlin):
onView(withId(R.id.spinner)).perform(click())
onView(withText("Spinner Item 1")).perform(click());
Solution 3
Just use this code:
ViewInteraction customTextView = onView(
allOf(withId(R.id.tv_spinner_desc), withText("hello"), isDisplayed()));
customTextView.perform(click());
Author by
IgorOK
Updated on July 29, 2022Comments
-
IgorOK almost 2 years
I am trying to write a test that performs a click on a
Spinner
item by text.My test contains these lines:
onView(withId(R.id.spn_trans_type)) .perform(click()); onData(anything()) .inAdapterView(withId(R.id.spn_trans_type)) .onChildView(allOf(withId(textViewIdToTest), withText(expectedText))) .perform(click());
But I got an exception:
NoMatchingViewException: No views in hierarchy found matching: with id: com.rirdev.aalf.demo:id/spn_trans_type
How to find spinner adapter view? In other words what should I put into
inAdapterView()
method ?