Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set visibility of the textview using android espresso test

I want to set visibility for the text view from test cases.I am using espresso for testing the UI. I used the viewAction to set the text to the text view. But I want to set visibility for the text view. Please, any one helps me to resolve this issue. Here is my code for setting the text to the text view.

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";
        }
    };
} 
like image 907
Lassie Avatar asked Nov 22 '17 05:11

Lassie


People also ask

How do you check espresso visibility?

One simple way to check for a View or its subclass like a Button is to use method getVisibility from View class. I must caution that visibility attribute is not clearly defined in the GUI world. A view may be considered visible but may be overlapped with another view, for one example, making it hidden.

How do you assert espresso?

The most used assertion is the matches() assertion. It uses a ViewMatcher object to assert the state of the currently selected view. onView(...). check(matches(withText("Hello!")))


1 Answers

Try this,

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";
            }
        };
    }
}
like image 151
K Neeraj Lal Avatar answered Nov 15 '22 05:11

K Neeraj Lal