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 ?
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
From: Android Espresso check selected spinner text
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.
In my case, the simplest possible solution worked (Kotlin):
onView(withId(R.id.spinner)).perform(click())
onView(withText("Spinner Item 1")).perform(click());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With