【问题标题】:How to set visibility of the textview using android espresso test如何使用 android espresso 测试设置 textview 的可见性
【发布时间】:2018-05-05 17:55:54
【问题描述】:

我想从测试用例中设置文本视图的可见性。我正在使用 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";
        }
    };
} 

【问题讨论】:

  • ((TextView) view).setVisibility(View.VISIBLE);((TextView) view).setVisibility(View.GONE);?
  • 如果我这样使用...我收到此错误... android.support.test.espresso.PerformException: Error perform 'replace text' on view 'Animations or transitions are enabled on the目标设备。

标签: android testing textview visibility android-espresso


【解决方案1】:

试试这个,

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";
            }
        };
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-10-21
    • 1970-01-01
    • 1970-01-01
    • 2011-12-16
    • 1970-01-01
    • 1970-01-01
    • 2020-12-06
    • 1970-01-01
    相关资源
    最近更新 更多