Android咖啡检查选定的微调文本

在我的Espressotesting中有这个代码

onView(withId(R.id.src)) .perform(click()); onData(hasToString(startsWith("CCD"))) .perform(click()); onView(withId(R.id.src)) .check(matches(withText(containsString("CCD")))); 

我想要做的是点击Spinner项目,并检查它是否确实在Spinnerselect。

但是我得到这个错误:

android.support.test.espresso.base.DefaultFailureHandler $ AssertionFailedWithCauseError:'with text:一个包含“CCD”的string不匹配所选的视图。 预期:使用文本:包含“CCD”的string得到:“AppCompatSpinner {id = 2131558533,res-name = src,visibility = VISIBLE,width = 528,height = 163,has-focus = false,has-focusable = true, has-window-focus = true,is-clickable = true,is-enabled = true,is-focused = false,is-focusable = true,is-layout-requested = false,is-selected = false,root-is- layout-requested = false,has-input-connection = false,x = 0.0,y = 0.0,child-count = 1}“

Solutions Collecting From Web of "Android咖啡检查选定的微调文本"

withText()replacewithSpinnerText()

 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

对于自定义适配器,我有哟创build一个自定义匹配器:

  onView(withId(R.id.spinner)).perform(click()); onData(allOf(is(instanceOf(YourCustomClass.class)), withMyValue("Open"))).perform(click()); public static <T> Matcher<T> withMyValue(final String name) { return new BaseMatcher<T>() { @Override public boolean matches(Object item) { return item.toString().equals(name); } @Override public void describeTo(Description description) { } }; } 

然后,您必须重写您的自定义类的toString()方法。

真正简单的解决scheme,为我解决…..不使用匹配器CustomSpinner

  onView(withId(R.id.custom_spinner)).perform(click()); onData(anything()).atPosition(1).perform(click()); onView(withId(R.id.custom_spinner)).check(matches(withSpinnerText(containsString("yourstring"))));