我正在尝试编写测试,按文本执行单击旋转器项目.
我的测试包含以下几行:
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: No views in hierarchy found matching: with id: com.rirdev.aalf.demo:id/spn_trans_type
我的问题是:如何找到微调器适配器视图?换句话说,我应该在adapterView()方法中添加什么?
我已经找到了这个答案:
替换
withText()
为withSpinnerText()
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 Espresso检查选定的微调文本
所以不要使用有点复杂:
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))));
selectionText
您的预期字符串值和视图spinnerId
的ID 将在何处Spinner
.