Android Espresso check selected spinner text

前端 未结 3 1627
忘了有多久
忘了有多久 2020-12-09 03:32

I have this code in my Espresso test

onView(withId(R.id.src))
    .perform(click());
onData(hasToString(startsWith(\"CCD\")))
    .perform(click());
onView(w         


        
相关标签:
3条回答
  • 2020-12-09 03:43

    Replace withText() with withSpinnerText()

    onView(withId(spinnerId)).perform(click());
    onData(allOf(is(instanceOf(String.class)), is(selectionText))).perform(click());
    onView(withId(spinnerId)).check(matches(withSpinnerText(containsString(selectionText))));
    

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

    0 讨论(0)
  • 2020-12-09 03:46

    For custom adapter i had yo create a custom matcher:

     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) {
    
            }
        };
    }
    

    Then you must override toString() method on your custom class.

    0 讨论(0)
  • 2020-12-09 03:55

    Really simple solution that worked out for me .....without using 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"))));
    
    0 讨论(0)
提交回复
热议问题