在Espresso中将View转换为Matcher <view>的简单方法

时间:2016-04-25 04:47:41

标签: android android-espresso

对于ins,如下面的示例,我希望首先设置监听器并使用espresso,因此如何实现toMatcher

@Test
    public void testSpinner2() throws Exception {
        r.launchActivity(null);
        Spinner sp = (Spinner) r.getActivity().findViewById(R.id.spinner);
        sp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               //do smth
            }
        });

        onView(toMatcher(sp));
    }

1 个答案:

答案 0 :(得分:0)

I guess following code can work:

public static Matcher<View> toMatcher(final View v) {
        return new TypeSafeMatcher<View>() {
            @Override
            protected boolean matchesSafely(View item) {
                return item == v;
            }

            @Override

            public void describeTo(Description description) {
                description.appendText(v.toString());
            }
        };
    }