我正在尝试编写一个测试,通过文本执行单击Spiner
项目。
我的测试包含以下行:
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());
但是我有一个例外:NoMatchingViewException:在层次结构中没有找到匹配的视图:id: com.rirdev.aalf.demo:id/spn_trans_type
如何找到微调适配器视图?换句话说,我应该在inAdapterView()
方法中放入什么?
我已经找到了这个答案:
用SpinnerText()替换
onView(withId(spinnerId)).perform(click());
onData(allOf(is(instanceOf(String.class)), is(selectionText))).perform(click());
onView(withId(spinnerId)).check(matches(withSpinnerText(containsString(selectionText))));
参考:https://code.google.com/p/android-test-kit/issues/detail?id=85
来自:Android浓缩咖啡检查选定的微调文本
所以与其使用有点复杂:
onData(anything())
.inAdapterView(withId(R.id.spn_trans_type))
.onChildView(allOf(withId(textViewIdToTest), withText(expectedText)))
.perform(click());
也许你应该用
onData(allOf(is(instanceOf(String.class)), is(selectionText)))
.perform(click());
onView(withId(spinnerId))
.check(matches(withSpinnerText(containsString(selectionText))));
其中selationText
将是您期望的字符串值,spinnerId
是您的Spiner
视图的id。
在我的例子中,最简单的解决方案有效(静态编程语言):
onView(withId(R.id.spinner)).perform(click())
onView(withText("Spinner Item 1")).perform(click());
只需使用此代码:
ViewInteraction customTextView = onView(
allOf(withId(R.id.tv_spinner_desc), withText("hello"), isDisplayed()));
customTextView.perform(click());