如何使用android espresso测试设置textview的可见性

我想从测试用例中设置文本视图的可见性.我正在使用espresso来测试UI.我使用viewAction将文本设置为文本视图.但我想为文本视图设置可见性.请任何人帮我解决这个问题.
这是我将文本设置为文本视图的代码.

public ViewAction setTextInTextView(final String value){
    return new ViewAction() {
        @SuppressWarnings("unchecked")
        @Override
        public void perform(UiController uiController, View view) {
            ((TextView) view).setText(value);
        }

        @Override
        public Matcher<View> getConstraints() {
            return allOf(isDisplayed(), isAssignableFrom(TextView.class));
        }

        @Override
        public String getDescription() {
            return "replace text";
        }
    };
} 

解决方法:

试试这个,

public class MainActivityInstrumentationTest {

    @Rule
    public ActivityTestRule<MainActivity> activityTestRule = new ActivityTestRule<>(MainActivity.class);

    @Test
    public void validateEditText() {

        onView(withId(R.id.out)).perform(setTextViewVisibitity(true));

        // Just for viewing the results. Remove after use.
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        onView(withId(R.id.out)).perform(setTextViewVisibitity(false));

        // Just for viewing the results. Remove after use.
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    private static ViewAction setTextViewVisibitity(final boolean value) {
        return new ViewAction() {

            @Override
            public Matcher<View> getConstraints() {
                return isAssignableFrom(TextView.class);
            }

            @Override
            public void perform(UiController uiController, View view) {
                view.setVisibility(value ? View.VISIBLE : View.GONE);
            }

            @Override
            public String getDescription() {
                return "Show / Hide View";
            }
        };
    }
}
上一篇:苹果Mac轻量级网页代码编辑器:​​​​​​​​​​​​Espresso


下一篇:android – Espresso在自定义视图类中找不到View