我正在尝试编写测试,按文本执行单击旋转器项目。
我的测试包含以下几行:
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()方法中添加什么内容?
答案 0 :(得分:13)
我已经找到了这个答案:
将
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
所以不要使用有点复杂的东西:
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
是您Spinner
视图的ID。
答案 1 :(得分:1)
如果微调框在对话框中,请使用它单击所选内容:
onData(allOf(is(instanceOf(String.class)),
is(selectionText))).inRoot(isPlatformPopup()).perform(click());
答案 2 :(得分:1)
就我而言,最简单的解决方案可行(科特琳):
onView(withId(R.id.spinner)).perform(click())
onView(withText("Spinner Item 1")).perform(click());
答案 3 :(得分:0)
只需使用此代码:
ViewInteraction customTextView = onView(
allOf(withId(R.id.tv_spinner_desc), withText("hello"), isDisplayed()));
customTextView.perform(click());