Android Espresso检查选择的微调文本

Jez*_*spo 21 android android-espresso

我的Espresso测试中有这个代码

    onView(withId(R.id.src))
            .perform(click());

    onData(hasToString(startsWith("CCD")))
            .perform(click());

   onView(withId(R.id.src))
           .check(matches(withText(containsString("CCD"))));
Run Code Online (Sandbox Code Playgroud)

我要做的是点击中的项目,Spinner并检查它是否确实在选中Spinner.

但是我收到了这个错误:

android.support.test.espresso.base.DefaultFailureHandler $ AssertionFailedWithCauseError:'with text:包含"CCD"的字符串与所选视图不匹配.预期:带文本:包含"CCD"的字符串得到:"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}"

Jon*_*nas 62

替换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))));
Run Code Online (Sandbox Code Playgroud)

参考:https://code.google.com/p/android-test-kit/issues/detail?id = 85


Arp*_*wal 6

真正简单的解决方案,对我来说很有效...而不使用Matcher For 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"))));
Run Code Online (Sandbox Code Playgroud)